Using d3.layout.stack () and csv parsing for a histogram

In the breakdown example by Mike Bostock, he uses the following to generate his data. I have my data in a CSV file, thereby decrypting it and how I can eliminate it and using my own data from a CSV is the key here.

// Inspired by Lee Byron test data generator.
function bumpLayer(n, o) {

  function bump(a) {
    var x = 1 / (.1 + Math.random()),
        y = 2 * Math.random() - .5,
        z = 10 / (.1 + Math.random());
    for (var i = 0; i < n; i++) {
      var w = (i / n - y) * z;
      a[i] += x * Math.exp(-w * w);
    }
  }

  var a = [], i;
  for (i = 0; i < n; ++i) a[i] = o + o * Math.random();
  for (i = 0; i < 5; ++i) bump(a);
  return a.map(function(d, i) { return {x: i, y: Math.max(0, d)}; });
}

It is difficult to understand, as already mentioned, especially since after that they are manipulated as follows:

n = 6, // number of layers
m = 12, // number of samples per layer
stack = d3.layout.stack(),
layers = stack(d3.range(n).map(function() { return bumpLayer(m, .1); })),

Each step is registered on the console in the example of my working code: http://tributary.io/inlet/8827504

PURPOSE: take the csv file and process it into a 2D array that d3 can handle.

Something like this that doesn't work for me could be the starting point.

// store the names of each column in csv file in array
var headers = ["Under $1000","$1000-$9999","$10000-19999","$20000-99999","100K - $999999","Over $1 Million"];


var myData = function(mycsv){

    d3.layout.stack()(headers
              .map(function(value){
                      return mycsv.map(function(d) {

                        return {x: d.Category, y: +d[value]};
                      });
                 }))
};

Thank!

* EDIT ***

d3.layout.stack() csv :

d3.csv("crimea.csv", function(crimea) {

  // Transpose the data into layers by cause.
  var causes = d3.layout.stack()(["wounds", "other", "disease"].map(function(cause) {
    return crimea.map(function(d) {
      return {x: parse(d.date), y: +d[cause]};
    });
  }));

  // Compute the x-domain (by date) and y-domain (by top).
  x.domain(causes[0].map(function(d) { return d.x; }));
  y.domain([0, d3.max(causes[causes.length - 1], function(d) { return d.y0 + d.y; })]);

: http://bl.ocks.org/mbostock/1134768

+3
1

var d3.layout.stack(), d3 wiki : https://github.com/mbostock/d3/wiki/Stack-Layout

, , d3.csv d3:

d3.csv("kick.csv", function (data){

    // each string is its own column in the csv file, this hard-coding is sub-optimal
    var headers = ["Under $1000","$1000-$9999","$10000-19999","$20000-99999","100K - $999999","Over $1 Million"];

    // Category = strings that start each row, priceRange are data values 
    var layers = d3.layout.stack()(headers.map(function(priceRange) {
        return data.map(function(d) {
          return {x: d.Category, y: +d[priceRange]};
        });
    }));

    // insert rest of d3js chart code
});

GitHub: https://github.com/DeBraid/www.cacheflow.ca/blob/chart/styles/js/d3kickchart.js

EDIT: , : http://youtu.be/n9kKdbzCiLI

+2

All Articles