【问题标题】:streamgraph from .csv file in D3来自 D3 中 .csv 文件的流图
【发布时间】:2014-01-09 04:25:43
【问题描述】:

我想从 .csv 文件中的数据构建流图。我分叉了http://bl.ocks.org/lgrammel/1935509 来生成流图,但我在加载数据时遇到了困难。我的JS在下面。

    var n = 8, // number of layers
    m = 54; // number of samples per layer

var colors = d3.range(n).map(function() { return d3.interpolateRgb("#aad", "#556")(Math.random()); });

var streamgraph = streamgraphChart()
        .margin({top: 10, right: 10, bottom: 10, left: 10})
        .color(function(d, i) { return colors[i]; }) // use same colors for both data sets
        .transitionDuration(1500);
d3.text("weekly_hours.csv", function(text) {
    var data = d3.csv.parseRows(text).map(function(row) {
        return row.map(function(value) {
          return +value;
        });
    });
    console.log(data);
    d3.select("#chart")
            .datum(data)
            .call(streamgraph);
  });

控制台日志很好地显示了数据数组,但我也收到错误Error: Problem parsing d="MNaN,NaNLNaN,NaNLNaN,NaNLNaN,NaNLNaN,Nhttp://bl.ocks.org/korenmiklos/8052011 没有显示任何内容

【问题讨论】:

    标签: javascript csv d3.js stream-graph


    【解决方案1】:

    您必须以符合stack layout 的格式提供数据,该功能才能按预期工作:

    默认情况下,每个值都需要以下属性: * x - 值的 x 位置。 * y - 值的 y 厚度。 * y0 - 值(基线)的最小 y 位置。

    因此,您需要对数据进行更多转换:

    var data = [];
    d3.csv.parseRows(text).forEach(function(row, idx) {
        row.forEach(function(value, layer) {
          if (typeof data[layer] === 'undefined') data[layer] = [];
          data[layer].push({ x : idx, y : +value });
        });
    });
    

    工作演示:http://plnkr.co/edit/P8Q4UGNpdUMw1V824vlK?p=preview

    【讨论】:

    • 谢谢!我试过了,但得到了一些奇怪的图表。我想我使用了错误的数据结构。这是我的 console.log 记录的内容: Array[54] 0: Array[8] 0: Object x: 0 y: 0 y0: 19.022782976059872 proto: Object 1: Object 2: Object 3: Object 4:对象 5:对象 6:对象 7:对象长度:8 proto:Array[0] 1:Array[8] 2:Array[8]
    猜你喜欢
    • 2016-03-01
    • 2013-01-25
    • 2023-03-30
    • 2016-10-27
    • 2021-02-17
    • 1970-01-01
    • 1970-01-01
    • 2012-12-02
    • 2016-11-13
    相关资源
    最近更新 更多