创建包含日期的Flot折线图

时间:2014-07-14 18:09:21

标签: javascript jquery flot

我正在尝试使用Flot创建折线图,该图表显示x轴上的月份和年份,对应于y轴上的数字。

这是我的代码:

var data= [];
var y = 1;
var x;  

var record=xmlDoc.getElementsByTagName("record");
for(i=0;i<record.length;i++)
{
    x = record[i].getElementsByTagName("date_of_consent")[0].childNodes[0].nodeValue;
    x = x * 1000
    y = y + i;
    newArray = [x, y];
    data.push(newArray);
}

    var dataset = [{label: "Enrollment Over Time",data: data}];

    var options = {
        series: {
            lines: { show: true },
            points: {
                radius: 3,
                show: false
            }
        }

    xaxis: {
        mode: "time",
        timeformat: "%y/%m"
    }
    };

    $(document).ready(function () {
        $.plot($("#flot-placeholder"), dataset, options);
    });

作为参考,date_of_consent字段的值与此类似:1376092800000。问题似乎是将此数字转换为日期。我将不胜感激任何建议。感谢。

1 个答案:

答案 0 :(得分:3)

您有一个javascript语法错误:

var options = {
    series: {
        lines: { show: true },
        points: {
            radius: 3,
            show: false
        }
    }, <-- YOU ARE MISSING THIS COMMA!!
    xaxis: {
        mode: "time",
        timeformat: "%y/%m"
    }
};

在编码时,经常检查javascript控制台是否有错误。