简体   繁体   中英

d3.scale on JSON Data

I would like to enable domain and range finding on JSON data with D3. I can easily do so with hard-coded numbers but I'm unsure what I should do for JSON data I'm looking for.

For hard-coded numbers, I can easily get something I'm looking for:

var test = d3.scale.linear()
    .domain([200,1000]);

console.log(test(900)); // returns an obvious value adjusted for scale

But for a JSON Object I'm getting an error "cannot read property map of undefined" in my console. Obviously user error:

var test = d3.scale.linear()
    .domain(testData['Sources']['Value']);

console.log(test(900)); // returns "cannot read property 'map' of undefined

Should I somehow be doing a for-each on the JSON data, or D3's .enter() method?

You indeed need to iterate over the elements in your data, but D3 offers functions that make this easier. For example:

var test = d3.scale.linear()
    .domain(d3.extent(testData, function(d) { return d['Sources']['Value']; }));

This tells D3 to get ['Sources']['Value'] for every element in testData and return the min and max values in an array. I'm assuming that this is the format of your data, if not, adjust appropriately.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM