airtruk
airtruk

Reputation: 331

simple multi line graph with d3js

I have the following array

var data = [ 
             [{"time": 1, "value": 2.1}, {"time": 2, "value": 1.1}],{"time": 3, "value": 3.1}],
             [{"time": 1, "value": 5.3}, {"time": 2, "value": 0.1}, {"time": 3, "value": 6.1}]
           ];

and I need to find the maximum time and value out of the entire array. the code that doesn't quite work is

var x = d3.scale.linear()
    .domain([1, d3.max(data, function(d,i){ return d[i].time;})])
    .range([0, width]);

for some reason I get a maximum time of 2, and not 3. even if I use a larger dataset with more point I still don't get the actual maximum value.

Any help is appreciated.

Upvotes: 0

Views: 224

Answers (1)

meetamit
meetamit

Reputation: 25157

Your data is an array or arrays. If you want the "max of the maxes", you'll need to account for the nesting. One way to do it:

.domain([1, d3.max(data, function(arrayOfDs, i) {
  return d3.max(arrayOfDs, function(d, i) { return d.time; });      
})])

Upvotes: 2

Related Questions