【问题标题】:d3.js creating a simple line chart with array of DateTime and intd3.js 使用 DateTime 和 int 数组创建一个简单的折线图
【发布时间】:2017-08-16 00:50:17
【问题描述】:

我在创建包含一组数据的简单折线图时遇到问题。

我收到错误:错误:属性 d:预期数字,“MNaN,NaNLNaN,NaNL...”

我通过 c# 将这些数据导入并转换为如下所示的数组:

[
{date: "2017-08-15 16:00:00", high: "73.41", low: "73.2"},
{date: "2017-08-15 15:45:00", high: "73.38", low: "73.2715"},
{date: "2017-08-15 15:30:00", high: "73.33", low: "73.28"},
{date: "2017-08-15 15:15:00", high: "73.305", low: "73.185"},
{date: "2017-08-15 15:00:00", high: "73.22", low: "73.15"},
{date: "2017-08-15 14:45:00", high: "73.24", low: "73.1816"},
]

我试图在 x 轴上绘制日期,在 y 轴上绘制高位。我发现了很多关于这个的问题,我在下面引用了几个。

Draw D3 Simple Line chart With an Array

Creating a line graph from array of data objects

使用我的 type() 函数,我将“日期”对象转换为 DateTime 类型,并将“高”转换为数字。然后我使用渲染来显示图表。

@foreach (var i in Model.Data)
{
    <div>@i.Value.high</div>
}



<script>

    var arrayData = [];

var i in Model.Data)
    {
        @:arrayData.push({date : '@i.Key', high : '@i.Value.high', low : '@i.Value.low'});
    }


    var outerWidth = 500;
    var outerHeight = 500;
    var margin = { left: 30, top: 30, right: 30, bottom: 30 };
    var xColumn = "date";
    var yColumn = "high";

    // parse the date / time
    var parseTime = d3.timeParse("%d-%b-%y");

    //Create SVG element
    var svg = d3.select("body")
        .append("svg")
        .attr("width", outerWidth)
        .attr("height", outerHeight);

    //create a group
    var g = svg.append("g")
        .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

    //append a path element to our group
    var path = g.append("path");

    var innerWidth = outerWidth - margin.left - margin.right;
    var innerHeight = outerHeight - margin.top - margin.bottom;

    //set ranges
    var xScale = d3.scaleTime().range([0, innerWidth]);
    var yScale = d3.scaleLinear().range([innerHeight, 0]);

    //changed the below from d[xColumn] to d.date
    var line = d3.line()
        .x(function (d) { return xScale(d.xColumn); })
        .y(function (d) { return yScale(d.yColumn); });


    //changed the below from d[xColumn] to d.date
    function render(data) {
        xScale.domain(d3.extent(data, function (d) { return d.date; }));
        yScale.domain(d3.extent(data, function (d) { return d.high; }));
        path.attr("d", line(data));
    }

    function type(d) {
        d.date = new Date(d.date);
        d.date = parseTime(d.date);
        d.high = +d.high;
        d.low = + d.low;
        return d;
    }


    //below i have deterrmined that the type conversion is working by calling type() on the arrayData; i checked the arrayData.date before then after and saw it was converted to a object; I then checked
    //to see if this was an instance of a date
    console.log(arrayData.date);
    type(arrayData);

    var temp3 = (arrayData.date instanceof Date);
    console.log(temp3);

    var temp = typeof arrayData.date;
    var temp2 = typeof arrayData.high;
    console.log(temp);
    console.log(temp2);


    //try and run graph now
    render(arrayData);



</script>

在搜索完所有内容后,对我来说,我的格式似乎正确。我是 d3 的新手,无法弄清楚我做错了什么。有人可以帮忙吗?

【问题讨论】:

    标签: javascript c# html d3.js svg


    【解决方案1】:

    您引用对象属性的方式在 javascript 中不起作用。

    你分配:

    var xColumn = "date";
    var yColumn = "high";
    

    然后尝试引用d.dated.high

     .x(function (d) { return xScale(d.xColumn); })
     .y(function (d) { return yScale(d.yColumn); });
    

    d.yColumnd.xColumn 将返回 undefined。如果您想以这种方式引用它们,您需要使用如下符号:d[xColumn]d[yColumn]

    【讨论】:

    • 这成功了!非常感谢楼主,这个回答我会接受的。关于我引用对象的方式,我认为在 javascript 中允许通过点符号进行引用,如下所述:w3schools.com/js/js_properties.asp
    • 很高兴它成功了!点符号确实有效,只是不能在点之后使用变量。
    • 我之前在 x 轴上插入“数字”以使其正常工作。但是,当我再次开始在 x 轴上使用日期时,我得到了错误:' 属性 d:预期数字,“MNaN,72.084347120……”'你知道这是为什么吗?似乎它仍然期待 x 轴上的数字
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 2020-08-29
    相关资源
    最近更新 更多