计算数据域
Calculate the domain of data
我正在读取文件列表 filestoplot.txt
并将它们加载到数组 (datasets[fileno]
)。这些二维数组在结构上相似,我想计算每列(所有组合数组的)的最大值和最小值,以便我可以正确地建立全局 d3 轴。但是,我的代码(如下所示)没有正确地 return gloablmax
和 gloablmin
。
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 显示此错误:
"Uncaught TypeError: Cannot read property 'apply' of undefined"
如有任何指点,我将不胜感激。谢谢。
Apply只用了两次,
Math.Min.apply
Math.Max.apply
如果 apply 是 'property of undefined',则表示 Math.Min and/or Math.Max 未定义。
看起来你只是大小写问题,试试Math.min
和Math.max
(当然也可能有其他问题,但这应该是TypeError的根源)。
我正在读取文件列表 filestoplot.txt
并将它们加载到数组 (datasets[fileno]
)。这些二维数组在结构上相似,我想计算每列(所有组合数组的)的最大值和最小值,以便我可以正确地建立全局 d3 轴。但是,我的代码(如下所示)没有正确地 return gloablmax
和 gloablmin
。
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 显示此错误:
"Uncaught TypeError: Cannot read property 'apply' of undefined"
如有任何指点,我将不胜感激。谢谢。
Apply只用了两次,
Math.Min.apply
Math.Max.apply
如果 apply 是 'property of undefined',则表示 Math.Min and/or Math.Max 未定义。
看起来你只是大小写问题,试试Math.min
和Math.max
(当然也可能有其他问题,但这应该是TypeError的根源)。