【发布时间】:2016-09-13 06:58:30
【问题描述】:
我正在尝试使用Mike Bostock 提供的饼图示例,但是当我尝试将数据转换为 csv 时它不起作用,我不确定为什么?
这是一个 plnk 和违规代码;
http://plnkr.co/edit/C0bJ0gM1Q9gIGxVe0vyF?p=preview
d3.csv("data.csv", type, function(error, data) {
if (error) throw error;
var path = svg.datum(data).selectAll("path")
.data(pie)
.enter().append("path")
.attr("fill", function(d, i) {
return color(i);
})
.attr("d", arc)
.each(function(d) {
this._current = d;
}); // store the initial angles
d3.selectAll("input")
.on("change", change);
var update = function() {
d3.select("input[value=\"oranges\"]").property("checked", true).each(change);
};
function change() {
var value = this.value;
clearTimeout(update);
pie.value(function(d) {
return d[value];
}); // change the value function
path = path.data(pie); // compute the new angles
path.transition().duration(750).attrTween("d", arcTween); // redraw the arcs
}
});
function type(d) {
return {
apples: d.apples,
oranges: d.oranges
};
}
// Store the displayed angles in _current.
// Then, interpolate from _current to the new angles.
// During the transition, _current is updated in-place by d3.interpolate.
function arcTween(a) {
var i = d3.interpolate(this._current, a);
this._current = i(0);
return function(t) {
return arc(i(t));
};
}
另外,您可能会猜到我是 d3 的新手。我想知道什么会更有效,使用来自 excel 文件的原始 csv 数据或将其转换为 JSON 并以这种方式将数据解析到 d3? (我知道这是主观的,只是希望获得一些意见,这对于编码问题并不重要。
感谢您的宝贵时间
【问题讨论】:
-
我更喜欢 JSON,更容易阅读,我认为它可能更快,也许......至于转换,你为什么要转换为 csv?
-
我也更喜欢 JSON。我可以根据我将创建的 dataviz 构建 JSON,这样更方便,但需要更多的工作。
标签: javascript csv d3.js