Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using d3.layout.stack() and parsing csv for bar chart

In the Stacked-to-Grouped example by Mike Bostock he uses the following to generate his data. I have my own data in a CSV file, thus deciphering this and how I can eliminate it and use my own data from CSV is the key here.

// Inspired by Lee Byron's 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)}; });
}

This is tricky to understand, as mentioned, especially since it is then 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 logged in the console in my working code example here: http://tributary.io/inlet/8827504

GOAL: To take my csv file and manipulate it into 2D array which d3 can handle.

Something like this, which doesn't work for me, might provide a 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]};
                      });
                 }))
};

Thanks!

*EDIT***

In another example using d3.layout.stack() and csv the code to parse goes as follows:

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; })]);

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

like image 678
DeBraid Avatar asked Dec 01 '25 03:12

DeBraid


1 Answers

The solution to the stacked var chart lies in understanding d3.layout.stack() detailed in the d3 wiki here: https://github.com/mbostock/d3/wiki/Stack-Layout

The method shown below combines using d3.csv & and d3 stack layout:

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
});

The full code is on GitHub: https://github.com/DeBraid/www.cacheflow.ca/blob/chart/styles/js/d3kickchart.js

EDIT: I recorded a video tutorial describing this solution in detail: http://youtu.be/n9kKdbzCiLI

like image 175
DeBraid Avatar answered Dec 02 '25 18:12

DeBraid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!