将日期字符串格式化为highcharts [英] Format date string into highcharts

查看:200
本文介绍了将日期字符串格式化为highcharts的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试将最小和最大字符串日期插入到我的高分析图中,如下所示:

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)

       },

显然,上面的代码不会运行,因为我传递一个字符串。我刚刚写了这个,以说明我正在努力做什么,而不是发布许多失败的尝试的代码。我尝试使用JSON.parse和eval(),但语法有问题。

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.

任何帮助将不胜感激。 cheers

Any help would be appreciated. Cheers

推荐答案

最终只是在highcharts API中使用setExtremes方法

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)); 

这篇关于将日期字符串格式化为highcharts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆