Reputation: 155
I am reading a list of files filestoplot.txt
and load them to an array (datasets[fileno]
). These 2-D arrays are similar in structure, and I wanted to calculate the max and min of each column (of all combined arrays) so that I can correctly establish the global d3 axis. However, my code (shown below ) does not correctly return gloablmax
and 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>
On the chrome console, I can see that maxvalues has the right data, however maxmin shows this error:
"Uncaught TypeError: Cannot read property 'apply' of undefined"
I would be greatful for any pointers. Thanks.
Upvotes: 0
Views: 38
Reputation: 2006
Apply is only used twice,
Math.Min.apply
Math.Max.apply
If apply is a 'property of undefined', it means Math.Min and/or Math.Max is undefined.
It looks like you just have a capitalization problem, try Math.min
and Math.max
(of course there may be other problems as well, but that should be the source of the TypeError).
Upvotes: 2