繁体   English   中英

将源从 tsv 更改为 JSON 后,D3.js 不绘制图形

[英]D3.js doesn´t draw graph after changing source from tsv to JSON

我有一个使用 TSV 文件绘制的 D3.js 折线图。 现在我有一个以时间戳和值作为键的 JSON 文件。 这是带有 TSV 文件的代码:

<script language="javascript" type="text/javascript">
    window.onload = function(){
        var svg = d3.select("svg"),
                margin = {top: 20, right: 20, bottom: 30, left: 50},
                width = +svg.attr("width") - margin.left - margin.right,
                height = +svg.attr("height") - margin.top - margin.bottom,
                g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
        var parseTime = d3.timeParse("%d-%b-%y");
        var x = d3.scaleTime()
                .rangeRound([0, width]);
        var y = d3.scaleLinear()
                .rangeRound([height, 0]);
        var line = d3.line()
                .x(function(d) { return x(d.date); })
                .y(function(d) { return y(d.close); });
        d3.tsv(" /data.tsv ", function(d) {
            d.date = parseTime(d.date);
            d.close = +d.close;
            return d;
        }, function(error, data) {
            x.domain(d3.extent(data, function(d) { return d.date; }));
            y.domain(d3.extent(data, function(d) { return d.close; }));
            g.append("g")
                    .attr("transform", "translate(0," + height + ")")
                    .call(d3.axisBottom(x))
                    .append("text")
                    .attr("fill", "#000")
                    .attr("transform", "rotate(0)")
                    .attr("x", 6)
                    .attr("dx", "1em")
                    .attr("text-anchor", "begin   ")
                    .text("time")
                    .select(".domain")
                    .remove();
            g.append("g")
                    .call(d3.axisLeft(y))
                    .append("text")
                    .attr("fill", "#000")
                    .attr("transform", "rotate(-90)")
                    .attr("y", 6)
                    .attr("dy", "0.71em")
                    .attr("text-anchor", "end")
                    .text("Price ($)");
            g.append("path")
                    .datum(data)
                    .attr("fill", "none")
                    .attr("stroke", "steelblue")
                    .attr("stroke-linejoin", "round")
                    .attr("stroke-linecap", "round")
                    .attr("stroke-width", 1.5)
                    .attr("d", line);
        });
    }
    </script>

这是 JSON 文件的代码:

var svg = d3.select("svg"),
                margin = {top: 20, right: 20, bottom: 30, left: 50},
                width = +svg.attr("width") - margin.left - margin.right,
                height = +svg.attr("height") - margin.top - margin.bottom,
                g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");

        var parseTime = d3.timeParse("%y-%m-%d");

       @* x-Axis goes from 0 to width*@
        var x = d3.scaleTime()
                .rangeRound([0, width]);

        @* y-Axis goes from 0 to height*@
        var y = d3.scaleLinear()
                .rangeRound([height, 0]);

        @* line generator, returns timestamp on x-axis and value on y-axis *@
        var line = d3.line()
                .x(function(d) { return x(d.timestamp); })
                .y(function(d) { return y(d.value); });



        d3.json(" /all.json ", function(d) {

            d.forEach(function(d){
                d.timestamp = d.timestamp;
                d.value = d.value;
                console.log(d.timestamp);
                console.log(d.value);
            });

            return d;
        }, function(error, data) {

            @* returns maximum and minimum of array d (data) *@
            x.domain(d3.extent(data, function(d) { return d.timestamp; }));
            y.domain(d3.extent(data, function(d) { return d.value; }));



            g.append("g")
                    .attr("transform", "translate(0," + height + ")")
                    .call(d3.axisBottom(x))
                    .append("text")
                    .attr("fill", "#000")
                    .attr("transform", "rotate(0)")
                    .attr("x", 6)
                    .attr("dx", "1em")
                    .attr("text-anchor", "begin   ")
                    .text("time")
                    .select(".domain")
                    .remove();

            g.append("g")
                    .call(d3.axisLeft(y))
                    .append("text")
                    .attr("fill", "#000")
                    .attr("transform", "rotate(-90)")
                    .attr("y", 6)
                    .attr("dy", "0.71em")
                    .attr("text-anchor", "end")
                    .text("Price ($)");


            g.append("path")
                        @* https://github.com/d3/d3-selection/blob/master/README.md#selection_datum *@
                    .data(data)
                    .attr("fill", "none")
                    .attr("stroke", "steelblue")
                    .attr("stroke-linejoin", "round")
                    .attr("stroke-linecap", "round")
                    .attr("stroke-width", 1.5)
                    .attr("d", line);
        });

    }

TSV 文件有键:“日期”和“关闭”。 我的 JSON 对象具有键:我需要的“时间戳”、“值”和我不需要的“有效”、“通道 ID”,因此它几乎相同。 一个示例 JSON 文件如下所示:

Object { timestamp: "2017-10-15 19:45", ChannelID: 13, value: 65469, valid: false }

为什么 D3 不再显示图形? 我需要改变什么?

不像d3.tsvd3.json接受一个行转换功能。

在您的代码中...

d3.json("/all.json ", function(d) {
    d.forEach(function(d){
        d.timestamp = d.timestamp;
        d.value = d.value;
        console.log(d.timestamp);
        console.log(d.value);
    });
    return d;
}, function(error, data) {

... "/all.json "function(error, data)之间的所有内容都是行转换函数。

解决方案:将行函数移动到回调内部...

d3.json("/all.json ", function(error, data) {
    //this is "inside the callback"
});

或者,或者,因为它没有做任何事情,就放弃它。

此外,您必须解析您的日期(您在 TSV 版本中这样做):

d3.json("/all.json ", function(error, data) {
    data.forEach(function(d){
        d.timestamp = parseTime(d.timestamp);
    });
    //the rest of the code
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM