在尝试显示日期时,不能正确格式化JS x轴时间

时间:2016-12-19 22:03:12

标签: javascript datetime flot

使用Flot我正在尝试开发一个图表,其中x轴以%M%D格式显示日期。但是,当运行以下代码时,它只会显示为纪元时间。

http://jsfiddle.net/unwo728s/1/

JavaScript代码:

var data = [ [gd(2016, 12, 3), 1000], [gd(2016, 12, 4), 200], [gd(2016, 12, 5), 3000], [gd(2016, 12, 6), 1000], [gd(2016, 12, 7), 1000], [gd(2016, 12, 8), 1000]];

var dataset = [
    {
        label: "Daily Amount",
        data: data,
        color: "#FFFFFF",
        points: { fillColor: "#FFFFFF", show: true },
        lines: { show: true }
    }
];

var options = {
    xaxes: {
        mode: "time",
        timeformat: "%m/%d",
        tickSize: [3, "day"],
        color: "black",        
        axisLabel: "Date",
    }
};

 $.plot($("#flot-placeholder"), dataset, options);

 function gd(year, month, day) {
    return new Date(year, month, day).getTime();
}

enter image description here

如何更改x轴以正确格式化?

1 个答案:

答案 0 :(得分:0)

应该是

var options = {
    xaxis: {

您还需要从“月”参数(month is zero-based in JS)中减去一个。

请参阅此更新fiddle