将日期字符串格式化为highcharts

时间:2012-09-29 06:06:30

标签: javascript datetime format highcharts

我一直在尝试将min和max字符串日期插入到我的highcharts图表中,如下所示:

    var formatted_startDate = startDate.replace(/-/g, ",");
    var formatted_endDate = endDate.replace(/-/g, ",");

    //Format now is "yyyy,mm,dd"
    options = {
       chart: {
           renderTo: 'container',
           type: 'bar'
       },
       xAxis: {
          type: "datetime",
        dateTimeLabelFormats: {
            day: '%m-%d'   
            },
        tickInterval: 24 * 3600 * 1000,
        min: Date.UTC(formatted_startDate),
        max: Date.UTC(formatted_endDate)

       },

显然上面的内容不起作用,因为我传入一个字符串。我刚才这样写它来说明我想要做的事情,而不是发布许多失败尝试的代码。我试过使用JSON.parse和eval()但是语法有问题。

任何帮助将不胜感激。干杯

1 个答案:

答案 0 :(得分:0)

最终只使用了highcharts API中的setExtremes方法

    var formatted_startDate = startDate.replace(/-/g, ",");
    var formatted_endDate = endDate.replace(/-/g, ",");
            //Format now is "2000,00,00"

    var startdatearray = formatted_startDate.split(",");
    var enddatearray = formatted_endDate.split(",");

    var sy = parseInt(startdatearray[0]);
    var sm = parseInt(startdatearray[1],10) - 1;
    var sd = parseInt(startdatearray[2]);
    var ey = parseInt(enddatearray[0]);
    var em = parseInt(enddatearray[1],10) - 1;
    var ed = parseInt(enddatearray[2]); 

    chart = new Highcharts.Chart(options);
        chart.xAxis[0].setExtremes(Date.UTC(sy,sm,sd), Date.UTC(ey, em, ed));