Metricsgraphics日期访问 - 折线图

时间:2015-12-25 21:17:18

标签: javascript json d3.js metricsgraphicsjs

我使用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
} 

1 个答案:

答案 0 :(得分: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'
    });
});