Linus
Linus

Reputation: 29

Min and Max function in d3.js

I have my data in a nested JSON format. I tried using the d3.extent function but it did not give me the results.

My data is in the following format :

carrier.push({ "level": Number(c[i]), "time": { "atime": new Date(v[i]), "btime": new Date(v1[i]) }

The following is my code:

x.domain(d3.extent(carrier, function (d) { return d.time; }));
y.domain(d3.extent(carrier, function (d) { return d.level; }));

My plot does not work when I just use "time". It works when I use one of the nested data like return d.time.atime but the x axis and y axis ticks does not cover the whole plot. It just covers the time.atime part. So is there a way where I could get the d3.extent for both atime and btime? I searched a lot maybe I am searching in the wrong direction.

Upvotes: 0

Views: 98

Answers (1)

mdml
mdml

Reputation: 22892

One simple method is to create two lists -- one of "atimes" and "btimes" -- and then take the d3.extent of those lists concatenated:

>>> // Some example data
>>> arr = [ {atime: 0, btime: 1}, {atime: 12, btime: -1}, {atime: 7, btime: 3} ]
>>> atimes = arr.map(function(d){ return d.atime })
>>> btimes = arr.map(function(d){ return d.btime })
>>> d3.extent(atimes.concat(btimes))
[-1, 12]

Or as a one-liner:

>>> d3.extent(arr.map(function(d){ return d.atime }).concat(arr.map(function(d){ return d.btime; })))
[-1, 12]

Upvotes: 2

Related Questions