Search code examples
d3.jsstacked

How can I stack a dataset using stack function in D3


I have this dataset:

 var dataset = [
    { "Date":"2014-10", "updates":427, "insert":27, "remove":29},
    { "Date":"2014-12", "updates":27, "insert":57, "remove":9},
    { "Date":"2015-02", "updates":47, "insert":7, "remove":2},
    { "Date":"2015-03", "updates":447, "insert":27, "remove":79}
    ];

I want to stack this dataset, using d3.layout.stack(dataset) function to create a stacked bar chart using D3, like this (http://bl.ocks.org/anupsavvy/9513382). I'm having some problems because this function doesn't stack my data correctly by this three vectors: updates, insert, remove. The dataset stays exactly the same after the calling of stack(dataset) function an I cannot perform the correct integration with the example above.

I any form to transform the dataset or calling specific parameters to create this stacked dataset?

Thanks, Filipe


Solution

  • Maybe this work:

    var output = ["updates", "insert", "remove"].map(
        function (v) {
        return dataSet.map(function (d) {
            return {
                Date : d.Date,
                y : +d[v]
            };
        });
    });