【问题标题】:Metricsgraphics Date access - Line graphMetricsgraphics 日期访问 - 折线图
【发布时间】:2015-12-25 21:17:18
【问题描述】:

我正在使用 MetricsGraphic 库进行 D3 可视化。出于某种原因,我无法弄清楚为什么我的日期解析不起作用。

d3.json('/api/data', function (data){
    data = MG.convert.date(data, "Date"["%d/%m/%y"]);
    MG.data_graphic({
        title: "Points per Matchday",
        description: "Points earned each matchday of 2014/2015 season",
        data: dataSet,
        width: 600,
        height: 200,
        right: 40,
        target: document.getElementById('arsenalPoints'),
        x_accessor: 'Date',
        y_accessor: 'Points'
    });
});

我不断收到一个类型错误,说它是 can't read property of 'time' undefined 与我的 MG.convert.date 函数。我也不知道我的日期格式是否正确。

这是我的 JSON 示例:

{
    "Date" : "26/04/15",
    "HomeTeam" : "Arsenal",
    "AwayTeam" : "Chelsea",
    "FTR" : "D",
    "Points": 1
} 

【问题讨论】:

    标签: javascript json d3.js metricsgraphicsjs


    【解决方案1】:

    一种方法是使用 MG:

    这是错误的:

    data = MG.convert.date(data, "Date"["%d/%m/%y"]);
    

    这是对的:

    data = MG.convert.date(data, "Date", "%d/%m/%y");//its not an array
    

    完整代码如下

    d3.json('/api/data', function (data){
        data = MG.convert.date(data, "Date", "%d/%m/%y");//its not an array
        MG.data_graphic({
            title: "Points per Matchday",
            description: "Points earned each matchday of 2014/2015 season",
            data: data, // Send in our data
            width: 600,
            height: 200,
            right: 40,
            target: document.getElementById('arsenalPoints'),
            x_accessor: 'Date',
            y_accessor: 'Points'
        });
    });
    

    另一种方法是使用 d3 转换日期:

    d3.json('/api/data', function (data){
      data.forEach(function(j){
        j.Date = d3.time.format("%d/%m/%y").parse(j.Date);//convert it into date
      });
      MG.data_graphic({
            title: "Points per Matchday",
            description: "Points earned each matchday of 2014/2015 season",
            data: data, // Send in our data
            width: 600,
            height: 200,
            right: 40,
            target: document.getElementById('arsenalPoints'),
            x_accessor: 'Date',
            y_accessor: 'Points'
        });
    });
    

    【讨论】:

    • 完美运行!但是后来我在MG.data_graphic 得到另一个Uncaught typeerror 怀疑sum is undefined. 我知道我正在发送一个对象数组(我怀疑这是你应该做的),因为api/data 持有一个json数组我在上面展示了。
    • sry 我没有收到任何错误在这里查看我的小提琴jsfiddle.net/cyril123/b2gxejgt 可能这可以帮助您修复错误
    猜你喜欢
    • 1970-01-01
    • 2017-02-28
    • 2015-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多