Finding d3 degrees of several values ​​in an array of objects

My dataset is:

var data = [{glazed: 3.50, jelly: 4.50, powdered: 1.00, sprinkles: 3.50, age: 21, responses: 2,name:"test"},
    {glazed: 2.83, jelly: 3.50, powdered: 1.83, sprinkles: 4.50, age: 22, responses: 6,name:"test"},
    {glazed: 3.25, jelly: 4.75, powdered: 2.25, sprinkles: 3.50, age: 23, responses: 4,name:"test"},
    {glazed: 1.50, jelly: 4.00, powdered: 2.50, sprinkles: 4.00, age: 25, responses: 2,name:"test"}];

If I wanted to find the degree of glazing or jelly or powdered or sprayed for scaling, I would use the code as shown below.

var x = d3.scale.linear()
                .domain(d3.extent(data, function (d) {
                    return d.glazed;//or jelly etc..
                }))
                .range([0, width]);

What do I need to do to get the degree of all the values ​​in the glaze, jelly, powder and spray, and not all the values ​​that are not age, answers and name.

This is due to the fact that the json file is created dynamically, so I will not have an idea of ​​the key values, except for age, answers and name.

So, my requirement is that he should give me min 1.5 (from glazed) and a maximum of 4.75 (from jelly)

Any help is truly appreciated.

thank

+4
source share
2
var x = d3.scale.linear()
   .domain(d3.extent(data.map(function (item) {
        return (item.glazed);
   })))
   .range([0, width]);

map() [3.5, 2.83, 3.25, 1.5]

extent() [1.5, 3.5]

data, :

var x = d3.scale.linear()
   .domain(d3.extent(
    [].concat(data.map(function (item) {
        return (item.glazed);
    }), data.map(function (item) {
        return (item.jelly);
    }), data.map(function (item) {
        return (item.powdered);
    }), data.map(function (item) {
        return (item.sprinkles);
    }))))
   .range([0, width]);

, , [].concat(...) : function(array, names){...}(data, temp). , JavaScript array.property array["property"] - .

var temp = ["glazed", "jelly", "powdered", "sprinkles"];
var width = 1000;
var data = [{glazed: 3.50, jelly: 4.50, powdered: 1.00, sprinkles: 3.50, age: 21, responses: 2,name:"test"},
    {glazed: 2.83, jelly: 3.50, powdered: 1.83, sprinkles: 4.50, age: 22, responses: 6,name:"test"},
    {glazed: 3.25, jelly: 4.75, powdered: 2.25, sprinkles: 3.50, age: 23, responses: 4,name:"test"},
    {glazed: 1.50, jelly: 4.00, powdered: 2.50, sprinkles: 4.00, age: 25, responses: 2,name:"test"}];


var x = d3.scale.linear()
   .domain(d3.extent(
       function(array, names){
          var res = [];
          array.forEach(function(item){
             names.forEach(function(name){
                res = res.concat(item[name]);
             });
          });
          return(res);
       }(data, temp)
    ))
   .range([0, width]);

DEMO: http://jsfiddle.net/v5qzuuhj/

+6

, , age, responses name, glazed, jellied, powdered sprinkled , max min:

var keys_to_ignore = ["age", "responses", "name"]

data.forEach( function (row)
{
    //Use d3.entries to create key\values
    var data_entries = d3.entries(row)
    // Add the 'current maximum' and 'current_min' based off the previous rows
    // If this is the first row, the extent will be undefined
    if (typeof extent !== "undefined") {
        data_entries.push({"key":"current_max", "value":extent[1]})
        data_entries.push({"key":"current_min", "value":extent[0]})
    }
    // now calculate the extent (max / min)
    extent = d3.extent(data_entries, function (d) {
        // Ignore specified keys
        if (keys_to_ignore.indexOf(d.key) == -1) {
            return d.value;
        }
    });
});

console.log(extent)

d3.entries key value, .

: http://jsfiddle.net/henbox/aa4d0z81/

, ,

+1

All Articles