如何垂直制作D3 Streamgraph

时间:2013-11-16 12:08:30

标签: javascript d3.js stream-graph

我是D3的新手,所以我的问题可能非常简单,但对我来说这是一个很大的问题。

问题:

如何垂直制作Streamgraph? my JSFiddle

我有这个:

enter image description here

我应该这样:

enter image description here

Fiddle is here

JS

var dataMetric = [
    {
        "id": 1,
        "title": "Little Inc",
        "metrics": {
            "offers": 665,
            "shares": 20,
            "landings": 1124,
            "leads": 1102,
            "purchases": 88,
            "friends": 74
        }
    },
    {
        "id": 2,
        "title": "Marvin LLC",
        "metrics": {
            "offers": 20,
            "shares": 2,
            "landings": 20,
            "leads": 25,
            "purchases": 28,
            "friends": 18
        }
    },
    {
        "id": 3,
        "title": "Hodkiewicz, Jacobson and O'Conner",
        "metrics": {
            "offers": 834,
            "shares": 8,
            "landings": 759,
            "leads": 683,
            "purchases": 41,
            "friends": 35
        }
    },
    {
        "id": 4,
        "title": "Harber, Fahey and Berge",
        "metrics": {
            "offers": 233,
            "shares": 5,
            "landings": 352,
            "leads": 348,
            "purchases": 31,
            "friends": 25
        }
    }
];

var metrics = ["offers", "shares", "landings", "leads", "purchases", "friends"];
var width = 500,
    height = 200,
    colors = {'0': '#6ff500', '1': '#ffad0a', '2': '#f90035', '3': '#6fD000'},
    stack = d3.layout.stack().offset("expand");
var svg = d3.select("#timeline").append("svg")
    .attr("width", width)
    .attr("height", height);
var y = d3.scale.linear()
    .domain([0, 1])
    .range([height, 0]);
var color = d3.scale.linear()
    .range(["#0D0", "#060"]);


streamed_history(dataMetric)

function streamed_history(data) {
    var m = [];
    data_array = metrics.forEach(function (f, metricIndex) {
        for (var index in data) {
            m[index] = m[index] || [];
            m[index].push({x: metricIndex, y:data[index].metrics[f]});
        }
    })
    layers = stack(m)
    console.log(m)
    layers = dataMetric.map(function (f, i) {
        return {layer: layers[i], companyName: f.title, color: color(i/(dataMetric.length -1))}
    })

    var x = d3.scale.linear()
        .domain([0, metrics.length - 1])
        .range([0, width]);

    var area = d3.svg.area()/*.interpolate("basis")*/
        .x(function(d) { return x(d.x); })
        .y0(function(d) { return y(null); })
        .y1(function(d) { return y(d.y0 + d.y); });

    function fixPath (path) {
        var Lidx = path.indexOf('L');
        var Cidx =  path.slice(Lidx).indexOf('C');
        var PCidx = path.slice(0,Lidx).lastIndexOf('C');
        var lp = path.substr(PCidx, Lidx-PCidx);
        var ss = path.substr(Lidx, Cidx);
        return (path.slice(0,Lidx) + lp + ss + path.slice(Lidx));
    }

    //enter
    svg.selectAll("path")
        .data(layers.reverse())
        .enter().append("path")
        .attr("d", function (d) {
            return fixPath(area(d.layer));
        })
        .style("fill", function(d) { return d.color; });

    //update
    d3.selectAll("path")
        .data(layers)
        .transition()
        .duration(2000)
        .attr("d", function (d) {return fixPath(area(d.layer));});
}

1 个答案:

答案 0 :(得分:4)

您需要做的就是在区域定义中将x更改为y,将y更改为x:

var area = d3.svg.area()/*.interpolate("basis")*/
    .y(function(d) { return x(d.x); })
    .x0(function(d) { return y(null); })
    .x1(function(d) { return y(d.y0 + d.y); });

完整示例here