【问题标题】:Read json data in d3.js [duplicate]读取 d3.js 中的 json 数据 [重复]
【发布时间】:2014-06-17 06:42:09
【问题描述】:

我在 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_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.5299997329712 36.9699997901917
人力资源 43.6424999237061 56.3575010299683
生产 43.1399993896484 57.0625009536743 销售 65.1949987411499 34.8049998283386

任何帮助表示赞赏。 我只需要知道为了从 json 读取代码需要做哪些更改。 因为现在数据将在 json 中,而不是在文件中。 在 d3 中读取数据而不是使用文件有哪些可能的方法?

【问题讨论】:

    标签: javascript json d3.js


    【解决方案1】:

    您当前正在使用 d3.tsv() 从 tsv 文件中读取数据。

    要读取 JSON 文件,您必须使用 d3.json() 方法

    【讨论】:

    • d3.json() 从 json 文件中读取。但我需要从 json 对象中读取。还有其他可能的读取方式而不是文件。
    猜你喜欢
    • 2018-08-22
    • 1970-01-01
    • 2014-07-29
    • 2015-11-27
    • 2017-10-06
    • 1970-01-01
    • 1970-01-01
    • 2018-10-10
    • 2016-09-13
    相关资源
    最近更新 更多