【问题标题】:D3.js Stacked Bar Chart, from vertical to horizontalD3.js 堆积条形图,从垂直到水平
【发布时间】:2015-08-30 16:40:18
【问题描述】:

我喜欢将这个垂直条形图 (http://bl.ocks.org/mbostock/3886208) 制作成水平条形图。

感谢您的帮助!

代码示例会很好。

【问题讨论】:

    标签: d3.js charts stacked-chart


    【解决方案1】:

    这只是反转域、轴,然后是 rect 计算的问题:

    var y = d3.scale.ordinal()
        .rangeRoundBands([height, 0], .1); // y becomes ordinal
    
    var x = d3.scale.linear()
        .rangeRound([0, width]); // x becomes linear
    
    // change state group to be positioned in the y now instead of x
    var state = svg.selectAll(".state")
          .data(data)
          .enter().append("g")
          .attr("class", "g")
          .attr("transform", function(d) { return "translate(0," + y(d.State) + ")"; });
    
    // rect calculations become
     state.selectAll("rect")
        .data(function(d) { return d.ages; })
        .enter().append("rect")
        .attr("height", y.rangeBand()) // height in now the rangeband
        .attr("x", function(d) { return x(d.y0); }) // this is the horizontal position in the stack
        .attr("width", function(d) { return x(d.y1) - x(d.y0); }) // this is the horizontal "height" of the bar
        .style("fill", function(d) { return color(d.name); });
    

    这是完整的工作example

    【讨论】:

    • 我喜欢添加两个功能:第一个:条形内的条形标签,第二个:x 轴带有此数据输入的时间刻度:State, int, ext AL,00:13:00,00: 16:02 AK,00:20:03,0 AZ,00:58:52,0 AR,00:45:30,00:17:39 CA,0,00:54:08 CO,00:35:01 ,0 CT,0,00:03:39
    猜你喜欢
    • 2018-09-12
    • 1970-01-01
    • 2020-04-10
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    • 2016-10-13
    • 2018-02-03
    • 1970-01-01
    相关资源
    最近更新 更多