【问题标题】:How to get current date and time on highcharts如何在highcharts上获取当前日期和时间
【发布时间】:2013-06-18 13:38:36
【问题描述】:

我无法将当前日期和时间显示在图表的 x 轴上。到目前为止,它只是一个随机的日期和时间。价格在图表中正确加载,但日期和时间不正确。使用高图表。感谢任何帮助。

    $(document).ready(function () {

    $.ajax({
        url: "/chart/ajax_get_chart", // the URL of the controller action method
        dataType: "json",
        type: "GET",
        success: function (result) {
            var result = JSON.parse(result);
            var date = new Array();
            var price = new Array();
            var d = new Date();
            series = [];
            for (var i = 0; i < result.length; i++) {
                tempArray = Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate());
                tempArray = parseFloat(result[i]['price']);
                tempArray = [result[i]['date']*1000, parseFloat(result[i]['price'])];
                series.push(tempArray);
            }



            console.log(tempArray);
            // var now = new Date();

            Highcharts.setOptions({
                global: {
                    useUTC: false
                }
            });
            $('#container').highcharts({
                chart: {
                    type: 'line',
                },
                title: {
                    text: 'Bitcoin Price',
                },
                subtitle: {
                    text: 'Source: MtGox.com',
                },
                xAxis: {
                    type: 'datetime'
                },
                plotOptions: {
                    series: {
                        pointStart: d.getUTCDate()*1000,
                        pointInterval: 24 * 3600 * 1000 // one day
                    }
                },
                yAxis: {
                    title: {
                        text: 'Price'
                    },
                },

                series: [{
                    name: 'Bitcoin',
                    data: series
                }]
            });

        }
    });

});

【问题讨论】:

    标签: javascript highcharts utc


    【解决方案1】:

    好的,您在同一时间使用 pointStart+pointInterval 和 [timestamp, value] - 所以这不起作用。删除那个pointStart+pointInterval。

     tempArray = [result[i]['date'], parseFloat(result[i]['price'])];
    

    date 是时间戳(以毫秒为单位)吗(数字,而不是字符串)?

    【讨论】:

    • 我删除了 pointStart 和 pointInterval,但我仍然没有得到当前时间。我也有点疑惑你说的是哪个系列。
    • 当我使用 console.log 时,我会以 unix 时间返回当前时间,但在图表上它总是从 20:58:00 开始并从那里上升。
    • 设置 useUTC: true ,文档:api.highcharts.com/highcharts#global.useUTC 或确保已设置。另外,你能展示数据的例子吗?
    • 这就是原因 - 从 1371481921 更改为 1371481921000(乘以 1000) - 在 Javascript 中时间戳是基于毫秒的。
    • 我正在从 api 中提取数据,我不知道如何使提取的每个时间戳都乘以 1000
    猜你喜欢
    • 2011-01-01
    • 2012-12-04
    • 1970-01-01
    • 2010-10-03
    • 2019-08-13
    • 2017-10-05
    • 2013-10-15
    相关资源
    最近更新 更多