X轴日期格式dygraph

时间:2014-06-06 15:41:48

标签: javascript dygraphs

我正在尝试格式化轴日期数据以使其显示年份但我没有使用dygraph-combined.js

document.getElementById("graphdiv"),

// CSV or path to a CSV file.
"Date,Temperature\n" +
"2014-05-07,75\n" +
"2014-05-15,70\n" + 
"2014-05-23,80\n"+
"2014-05-30,72\n" ,{ 
          axes: {
            x: {
              axisLabelFormatter: function(d, gran) {
                  return Dygraph.dateAxisFormatter(new Date(d.getTime() + 7200*1000), gran);
              }
            }
          }
        }
);

1 个答案:

答案 0 :(得分:2)

因为你的所有日期都在同一个月,所以dygraphs已经决定显示这一年是多余的。要覆盖它,您需要自己格式化日期:

g = new Dygraph(document.getElementById("graphdiv"),
    "Date,Temperature\n" +
    "2014-05-07,75\n" +
    "2014-05-15,70\n" + 
    "2014-05-23,80\n"+
    "2014-05-30,72\n" ,{ 
      axes: {
        x: {
          axisLabelFormatter: function(d, gran) {
              var d = new Date(d.getTime() + 7200*1000);
              return d.strftime("%Y-%m-%d");
          }
        }
      }
    });

请参阅小提琴here。请注意,Date.strftime()不是标准的 - 它只能使用,因为dygraphs包含strftime.js。这可能会在将来发生变化,在这种情况下,您需要自己包含图书馆或以其他方式格式化日期。

相关问题