Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

d3.js - max and min value from json data which has array of values

Tags:

d3.js

How do i find the max and min values from JSON data. I want the max of checkins and Checkintimes of data1,data2 ...data(n). My code is :

 var max = d3.max(data, function(d) { return d.Checkintimes;} );
 var min = d3.min(data, function(d) { return d.Checkintimes;} );

I tried this option too

d3.json("Values.json", function(data) {



var maxmbt = d3.max(d3.values(data) );
var maxcheckins = d3.max(d3.values(data));

console.log(maxmbt);
console.log(maxcheckins);

xScale.domain([0,maxcheckins]);
xScale.domain([0,maxmbt]);

});

I have a json in this format:

[
{
   "name":"data1",
    "checkins":[[1,0],[2,0],[3,0],[4,3],[5,0],[6,0],[7,0],[8,3],[9,0],[10,0],[11,0],[12,0]],
    "teamsize":[[1,0],[2,0],[3,0],[4,3],[5,0],[6,0],[7,0],[8,1],[9,0],[10,0],[11,0],[12,0]],
    "Checkintimes":[[1,0],[2,0],[3,0],[4,184],[5,0],[6,0],[7,0],[8,0],[9,0],[10,0],[11,0],[12,0]]
},


{"name":"data2",


    "checkins":[[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],[7,0],[8,0],[9,0],[10,0],[11,27],[12,12]],
    "teamsize":[[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],[7,0],[8,0],[9,0],[10,0],[11,11],[12,11]],
    "Checkintimes":[[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],[7,0],[8,0],[9,0],[10,0],[11,10],[12,12]]
}
]

currently when i use the returning value is an array. not an unitary value. For e.g checkins max value here is 27 and Checkintimes - 184 in the aforesaid example.

like image 991
krishna_v Avatar asked Jan 09 '14 10:01

krishna_v


1 Answers

The d3.max function needs to have an accessor that gets individual values. You would need nested calls as you have nested data:

var max = d3.max(data, function(d) {
  return d3.max(d.Checkintimes, function(e) { return d3.max(e); });
});
like image 96
Lars Kotthoff Avatar answered Nov 15 '22 10:11

Lars Kotthoff