Jimmy
Jimmy

Reputation: 12497

Find max value of JSON and store as a variable

I have a d3 graph which specifies the range on the Y axis like this:

  var yScale = d3.scaleLinear()
    .domain([0, 1]) // input 
    .range([height, 0]); // output 

However a scale of 0 to 1 isn't very useful. I want it to work out what is a suitable range for me. I'm a bit new to this so I thought if I did something like this:

// Data
var dataset = [{
      y: 0.1
    },
    {
      y: 0.6
    },
        {
      y: 0.6
    },
    {
      y: 0.7
    }
  ];

var mymax = Math.max(dataset);

Then I can find the maximum value in my dataset, and then feed that into my .domain range like this:

.domain([0, mymax]) // input 

However, I appreciate my attempt at this is wrong because I get NaN returned. I think it might be pointing at the letter Y and not my numbers.

I don't feel like this is a duplicate of the question.

Upvotes: 1

Views: 88

Answers (1)

Saurabh Yadav
Saurabh Yadav

Reputation: 3386

var mymax = Math.max.apply(Math, dataset.map(function(o) { return o.y; }));

Upvotes: 1

Related Questions