计算数据域

时间:2015-02-06 20:36:47

标签: javascript d3.js

我正在阅读文件列表filestoplot.txt并将它们加载到数组(datasets[fileno])。这些2-D阵列的结构相似,我想计算每列(所有组合阵列)的最大值和最小值,以便我可以正确地建立全局d3轴。但是,我的代码(如下所示)无法正确返回gloablmaxgloablmin

var files=[];
var datasets=[],totalfiles;
var i,j,dset=1,olddset=0,maxscale=0;
var maxnecr=0;
var cols=8;
var maxvalues=[];
var globalmin=[];gloablmax=[];

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function loadfilenames(){

d3.csv("./filestoplot.txt", 
function(file){
    files = file.map(function(d) { for (key in d) { fn=d[key]; } return fn; })
    totalfiles=files.length;
    for (i=0;i<totalfiles;i++){ 
        datasets[i]=[]; 
        loaddataset(i);
        maxvalues[i]=[];
    }
    if (filesloaded==(totalfiles-1)) maxmin();
}
);
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function loaddataset(fileno){
d3.csv(files[fileno],function(a){
    console.log("loading file "+filesloaded);filesloaded++;
    datasets[fileno]=a.map(function(d1) {
    return [
    +d1["f1"] ,
    +d1["f2"] ,
    +d1["f3"] ,
    +d1["f4"] ,
    +d1["f5"] ,
    +d1["f6"] ,
    +d1["f3"]/(+d1["f3"] + +d1["f5"]),
    +d1["f7"]
    ];
}
);
}
);
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function maxmin(){
for (j=0; j <cols; j++) {
    globalmin[j]=Math.Min.apply(null,d3.extent(maxvalues,function(d){ return d[j][0]; }))
    gloablmax[j]=Math.Max.apply(null,d3.extent(maxvalues,function(d){ return d[j][1]; }))
    //d3.extent(maxvalues,function(d){ console.log(d[j]);});
}
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

</script>

<body style="max-width:90%" onload="loadfilenames();">
<script>

function changedataset(el){
    console.log(el.checked)
    maxmin();
}
</script>
</body>

在chrome控制台上,我可以看到maxvalues有正确的数据,但是maxmin显示了这个错误:

&#34;未捕获的TypeError:无法读取属性&#39; apply&#39;未定义&#34;

我会很高兴任何指针。感谢。

1 个答案:

答案 0 :(得分:2)

仅使用两次,

Math.Min.apply
Math.Max.apply

如果apply是'undefined属性',则表示Math.Min和/或Math.Max未定义。

看起来您只是有大写问题,请尝试Math.minMath.max(当然也可能存在其他问题,但这应该是TypeError的来源)。