从简单的 JSON 字符串加载 D3.js 数据 [英] Loading D3.js data from a simple JSON string
问题描述
图库中的大多数示例都从 TSV 文件加载数据.
Most of the examples in gallery load data from TSV files.
如何将以下内容转换为使用本地 json 变量而不是 TSV 数据?
How can I convert the following to use a local json variable instead of TSV data?
d3.tsv("data.tsv", function(error, data) {
var myEntitiesJson = getEntitiesJson(); // <------ use this instead of "data"
data.forEach(function(d) {
d.frequency = +d.frequency;
});
x.domain(data.map(function(d) { return d.letter; }));
y.domain([0, d3.max(data, function(d) { return d.frequency; })]);
...
svg.selectAll(".bar")
.data(data) // <----- bind to myEntities instead
}
据我所知,我只需要对我的 entityJson 做一些事情,以便 data-fy 它以便图表可以绑定到它.
As far as I can tell, I just need to do something to my entitiesJson, in order to data-fy it so that the chart could bind to it.
更新
我正在取得一些进展.我从 JSON 插入了我的实体,图形开始呈现新的形状.
I am making some progress. I plugged in my entities from JSON and the graph is starting to take new shape.
目前以下代码中断:
svg.selectAll(".bar")
.data(myEntities) // <-- this is an array of objects
.enter().append("rect")
这是造成:
错误:属性 y=NaN"的无效值
Error: Invalid value for attribute y="NaN"
错误:属性 height=NaN"的无效值
Error: Invalid value for attribute height="NaN"
推荐答案
for remote data.json替换:
for remote data.json replace :
d3.tsv("data.tsv", function(error, data) {...}
与:
d3.json("data.json", function(error, data) {
console.log(data); // this is your data
});
对于本地数据:
var myData = { {date:'2013-05-01', frequency:99},
{date:'2013-05-02', frequency:24} };
function draw(data) {
console.log(data); // this is your data
}
draw(myData);
这篇关于从简单的 JSON 字符串加载 D3.js 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!