如何将日期时间字符串转换为UTC以在Highcharts上绘制点 [英] How to convert datetime string to UTC to plot points on Highcharts

查看:141
本文介绍了如何将日期时间字符串转换为UTC以在Highcharts上绘制点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理HighCharts样条不规则数据.此处的数据传递为

data: [
      [Date.UTC(1970,  9, 27), 0   ],
      [Date.UTC(1970, 10, 10), 0.6 ],
      [Date.UTC(1970, 10, 18), 0.7 ],
      [Date.UTC(1970, 11,  2), 0.8 ],
      [Date.UTC(1970, 11,  9), 0.6 ],
      [Date.UTC(1970, 11, 16), 0.6 ],
      ]

但是,我正在通过AJAX调用从python后端发送数据,

data: [
      "2013-11-07 00:10:27", 0   ],
      "2013-11-07 00:10:27", 1   ],
      "2013-11-07 00:10:27", 2   ],
      "2013-11-07 00:10:27", 3   ],
      "2013-11-07 00:10:27", 4   ],
      "2013-11-07 00:10:27", 5   ],
      ]

因此,不会绘制数据点.如何在后端(python)将日期时间转换为UTC或日期时间的任何其他格式,这将使Highcharts能够绘制正确的X和Y轴点.

这是我尝试过的. 由于UTC时间Date.UTC(1970, 9, 27)返回秒数,因此我将时间转换为从1970-1-1开始在幕后经过的总秒数.

time = "2013-11-06 18:00:00"
time = ((datetime.datetime.strptime(time, '%Y-%m-%d %H:%M:%S'))-datetime.datetime(1970,1,1)).total_seconds()            

然后将其传递到UI.因此,我的数据将以

的形式发送

[[1383760800.0, 1870.2000000000007], [1383762600.0, 0.30000000000000004], [1383761700.0, 4.299999999999802]]

现在,当我将此数据传递到高图时,Y轴显示正确的值,而X轴(需要绘制日期时间的X轴)显示的时间为05:52.

我该如何纠正?

这是我的Highcharts代码.

function comparison_chart(result) {
  var MAXPOINTS = 20;

  Highcharts.setOptions({
      global: {
          useUTC: false,
      }
  });

$('#container').highcharts({
    chart: {
      events: {
      load: function() {

          // set up the updating of the chart each second
          var series = this.series[0];


          setInterval(function() {
              var x = (new Date()).getTime(), // current time
                  y = Math.random();
              if(series.data.length > MAXPOINTS){
                series.addPoint([x, y], true, true);  
              }
              else{
                series.addPoint([x, y], true, false);
              }



          },5000);
      },
  }

    },
    title: {
        text: 'Energy Chart'
    },
    xAxis: {
        type: 'datetime',
        dateTimeLabelFormats: { // don't display the dummy year
            month: '%e. %b',
            year: '%b'
        }
    },

    tooltip: {
        formatter: function() {
            var s;
                s = this.y + 'kWh';
            return s;
        }
    },

    series: [{
        type: 'spline',
        name: 'Random data',
        data: result,
        showInLegend: true,

    }]
  });
}

解决方案

问题是HighCharts希望时间序列数据以毫秒为单位,而不是秒,因此您需要输入乘数1000.

您可以将时间段从字符串转换为毫秒,如下所示:

>>> int(datetime.datetime.strptime("2013-11-07 00:10:27", "%Y-%m-%d %H:%M:%S").strftime('%s')) * 1000
1383811827000

I'm working on HighCharts Spline Irregular Data. Here the data is passed as

data: [
      [Date.UTC(1970,  9, 27), 0   ],
      [Date.UTC(1970, 10, 10), 0.6 ],
      [Date.UTC(1970, 10, 18), 0.7 ],
      [Date.UTC(1970, 11,  2), 0.8 ],
      [Date.UTC(1970, 11,  9), 0.6 ],
      [Date.UTC(1970, 11, 16), 0.6 ],
      ]

But, I'm sending the data from the python backend via AJAX call as

data: [
      "2013-11-07 00:10:27", 0   ],
      "2013-11-07 00:10:27", 1   ],
      "2013-11-07 00:10:27", 2   ],
      "2013-11-07 00:10:27", 3   ],
      "2013-11-07 00:10:27", 4   ],
      "2013-11-07 00:10:27", 5   ],
      ]

So, the data points does not gets plotted. How do I convert the datetime to UTC or any other format of datetime at the backend(python), which will enable Highcharts to plot the correct X and Y-axis points.?

Here is what I've tried. Since the UTC time Date.UTC(1970, 9, 27) returns the number of seconds, hence I'm converting the time to the total number of secs passed since 1970-1-1 behind the scenes.

time = "2013-11-06 18:00:00"
time = ((datetime.datetime.strptime(time, '%Y-%m-%d %H:%M:%S'))-datetime.datetime(1970,1,1)).total_seconds()            

and then passing it to the UI., hence my data is send as

[[1383760800.0, 1870.2000000000007], [1383762600.0, 0.30000000000000004], [1383761700.0, 4.299999999999802]]

Now when I pass this data to the highcharts, the Y-axis shows correct value but the X-axis(on which the datetime needed to plotted) shows time as 05:52.

How can I correct it?

Here is my Highcharts code.

function comparison_chart(result) {
  var MAXPOINTS = 20;

  Highcharts.setOptions({
      global: {
          useUTC: false,
      }
  });

$('#container').highcharts({
    chart: {
      events: {
      load: function() {

          // set up the updating of the chart each second
          var series = this.series[0];


          setInterval(function() {
              var x = (new Date()).getTime(), // current time
                  y = Math.random();
              if(series.data.length > MAXPOINTS){
                series.addPoint([x, y], true, true);  
              }
              else{
                series.addPoint([x, y], true, false);
              }



          },5000);
      },
  }

    },
    title: {
        text: 'Energy Chart'
    },
    xAxis: {
        type: 'datetime',
        dateTimeLabelFormats: { // don't display the dummy year
            month: '%e. %b',
            year: '%b'
        }
    },

    tooltip: {
        formatter: function() {
            var s;
                s = this.y + 'kWh';
            return s;
        }
    },

    series: [{
        type: 'spline',
        name: 'Random data',
        data: result,
        showInLegend: true,

    }]
  });
}

解决方案

The problem is that HighCharts expects time series data to be given in milliseconds, not seconds so you will need to put in a multiplier of 1000.

You can convert from your strings to milliseconds since epoch like so:

>>> int(datetime.datetime.strptime("2013-11-07 00:10:27", "%Y-%m-%d %H:%M:%S").strftime('%s')) * 1000
1383811827000

这篇关于如何将日期时间字符串转换为UTC以在Highcharts上绘制点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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