English 中文(简体)
在 D3 中获取一个阵列的最大实际数组
原标题:Get the real max of an array in D3

我正用jQuery 带来一组逗号限制的数字, 用于在 D3 中绘制绘图目的 。 我试图获取此阵列的最大数量, 但情况不妙 。

var x_data = $( .CostImport ).text().split( , );
var y_data = $( .PriceImport ).text().split( , );
var r_data = $( .SalesImport ).text().split( , );
var c_data = $( .ZoneImport ).text().split( , );


 for (i = 0; i < x_data.length; i++) {
   data.push({ "x": x_data[i], "y": y_data[i], "c": c_data[i], "r": r_data[i] });
   }

alert(d3.max(r_data));
alert(d3.max(data, function (d) { return d.r }));

这两种警示都给我同样的号码,这是错误的。 实际最高号码是928870,但我正在找回9975。 计划一切运作顺利(散射图,用r决定大小和 c 颜色),所以我对为什么我无法拉动最大号码有点过敏。

有什么想法吗?

最佳回答

听起来这些值被当作字符串处理。 您需要使用 < code> parseInt 或 < code>parseFloat (取决于数值的精确度), 才能将其转换为您期待的 < code> max 函数的整数 。

在此快速函数将字符串数组转换为 int。 这将更好地进行原型, 但对于快速和肮脏的示例, 这将有效 :

function toIntArray(arr) {
    for (var i = 0; i < arr.length; i++) { 
        arr[i] = +arr[i]; 
    } 
    return arr;
}

保持原样的代码, 但修正这些线条 :

r_data = toIntArray(r_data);
data = toIntArray(data);

alert(d3.max(r_data));
alert(d3.max(data, function (d) { return d.r }));
问题回答

处理同样的问题, 但我处理的是数据对象而不是数组。 在将字符串转换为数字时, 发现 d3 中的过滤功能非常有用 。 下面是一个例子 。

data.filter(function(d,i) {
    d.x = +d.x;
d.y = +d.y;
});

xmax = d3.max(data, function (d) { return d.x });
console.log(xmax);




相关问题
WordPress Data Storage Efficiency

I ve been asked to review a WordPress plugin of sorts and try to find ways of making it faster. The premise of this plugin is basically to store a bunch of users and shifts and appointments and ...

Convert a 2D array index into a 1D index

I have two arrays for a chess variant I am coding in java...I have a console version so far which represents the board as a 1D array (size is 32) but I am working on making a GUI for it and I want it ...

Convert an array of integers for use in a SQL "IN" clause

Surely there is a framework method that given an array of integers, strings etc converts them into a list that can be used in a SQL "IN" clause? e.g. int[] values = {1,2,3}; would go to "(1,2,3)"

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

C++ Array Sort Me

Stuck on an array sorter. Have to sort numbers from largest to smallest. I m trying two loops (one nested in the other). Here s the code: int counter=0; // inner counter int counter2=0; // outer ...

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

Best practice of big javascript objects

sry for this imprecise topic name. I am querying a dataset a lot of times so using ajax request would end up in tons of http requests. For this reason I decided to use the json encode method to ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

热门标签