Javascript 读取d3.js中的json数据
我在d3.js中创建了一个堆叠图表。从文件中读取数据。现在我想阅读json。为了读取json,我需要做哪些更改Javascript 读取d3.js中的json数据,javascript,json,d3.js,Javascript,Json,D3.js,我在d3.js中创建了一个堆叠图表。从文件中读取数据。现在我想阅读json。为了读取json,我需要做哪些更改 Below is code for the chart. d3.tsv("./StackedChart/stackeddata_dept1.tsv", type, function(error, data) { x_stacked.domain(data.map(function(d) { return d.department; })); y0_st
Below is code for the chart.
d3.tsv("./StackedChart/stackeddata_dept1.tsv", type, function(error, data) {
x_stacked.domain(data.map(function(d) { return d.department; }));
y0_stacked.domain([0, d3.max(data, function(d) { return 100; })]);
svg1.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height_stacked + ")")
.call(xAxis_stacked);
svg1.append("g")
.attr("class", "y axis axisLeft")
.attr("transform", "translate(0,0)")
.call(yAxisLeft_stacked)
.append("text")
.attr("y", 6)
.attr("dy", "-2em")
.style("text-anchor", "end")
.style("text-anchor", "end");
bars = svg1.selectAll(".bar").data(data).enter();
bars.append("rect")
.attr("class", "bar1")
.attr("x", function(d) { return x_stacked(d.department); })
.attr("width", x_stacked.rangeBand()/2)
.attr("y", function(d) { return y0_stacked(d.positive); })
.attr("height", function(d,i,j) { return height_stacked - y0_stacked(d.positive); });
});
function type(d) {
d.positive = +d.positive;
return d;
}
文件中的数据为:
部门正负
财务63.529997329712 36.969997901917
人力资源43.6424999237061 56.3575010299683
生产43.1399993896484 57.0625009536743
销售额65.1949987411499 34.8049998283386
谢谢你的帮助。
我需要知道的是,为了从json中读取,需要对代码进行哪些更改。
现在,数据将以json格式显示,而不是文件格式。
在d3中读取数据而不是使用文件的可能方式有哪些?您当前正在使用d3.tsv从tsv文件中读取数据
为了读取JSON文件,您必须使用方法d3.JSON读取JSON文件。但我需要从json对象读取数据。还有哪些其他可能的读取方式,而不是文件。