Format date string into highcharts

2019-08-09 00:21发布

I've been trying to insert min and max string dates into my highcharts graph as follows:

    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)

       },

Clearly the above won't work because I'm passing in a string. I've just written it like this to illustrate what I am trying to do rather than post code of numerous failed attempts. I've attempted using JSON.parse and eval() but had problems with the syntax.

Any help would be appreciated. Cheers

1条回答
孤傲高冷的网名
2楼-- · 2019-08-09 01:08

ended up just uses the setExtremes method within highcharts API

    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)); 
查看更多
登录 后发表回答