我想根据.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,N
。没有任何内容显示在http://bl.ocks.org/korenmiklos/8052011
答案 0 :(得分:0)
您必须以适合stack layout
的形式提供数据,以使该功能按预期工作:
by default the following attributes are required on each value: * x - the x-position of the value. * y - the y-thickness of the value. * y0 - the minimum y-position of the value (baseline).
因此,您需要更多地转换数据:
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 });
});
});