2013-08-21 48 views
0

请帮我在字符串转换成时间format[Hr:Min:Sec]的Javascript - 转换时间戳到毫秒Highcharts Y轴

这里是我的一块

seriesData_1.push(parseFloat($(this).attr('myVar'))); 

我试图将此更改为:

seriesData_1.push(Date.parse("1-1-1" + ($(this).attr('myVar')))); 

但没有运气。该myVar将持股价值20:57:13

更新:我在Highcharts & Javascript和使用指导#1哑所以,我得到了我想要的东西。

这是它。我有myVar系列20:57:13Hr:Min:Sec格式,需要绘制在Y axis。得知我们在Y轴上有datetime类型,可以为你绘制图表。但为此,您需要将时间戳转换为毫秒。

这是我为

yourtime = $(this).attr('myVar'); 
hms = yourtime.split(':'); 
msecs = hms[0] * 3600000 + hms[1] * 60000 + hms[2]*1000; 
seriesData_1.push(msecs); 

做现在,我已经得到了它转换成毫秒,获得此画在与Y轴H:M:S格式,你需要使用dateTimeLabelFormatsy axis下,如下

  type: 'datetime', 

      dateTimeLabelFormats: { 
      millisecond: '%H:%M:%S', 
      second: '%H:%M:%S', 
      minute: '%H:%M:%S', 
      hour: '%H:%M:%S' 
      }, 

让你的工具提示使用H:M:S格式排列,添加此

  tooltip: { 
        enabled: true, 
        formatter: function() { 
         var main = '<b>'+ Highcharts.dateFormat('%A, %b %e, %Y', this.x) +'</b>'; 
         s = Math.floor(this.y/1000); 
         m = Math.floor(s/60); 
         s = s % 60; 
         h = Math.floor(m/60); 
         m = m % 60; 
         h = h % 24; 
         if (h < 9) h = "0" + h; 
         if (m < 9) m = "0" + m; 
         if (s < 9) s = "0" + s; 
         return '<b>'+ this.x +'</b><br>' + '<span style="color:green">Value-</span>' + [h, m, s].join(':'); 
        } 
      }, 

希望这有助于为新手和我一样:)

+1

什么DOE这与阿贾克斯呢?代码中的XMLHttpRequest对象在哪里? – epascarello

+4

也许我正在密集,但已不是'20:57:13'已经在'小时:分钟:秒'格式? – Amadan

+0

@epascarello这个代码是在高分辨率下的ajax模块 – Sathy

回答

1

正如我在评论说,转换串联成秒:

var hms = val.split(':'); 
var secs = hms[0] * 3600 + hms[1] * 60 + hms[2]; 
series.push(secs); 

然后设置yAxis.formatter这个功能恢复轴上的H:M:S

function() { 
    var h = Math.floor(this.value/3600); 
    var m = Math.floor(this.value/60) % 60; 
    var s = this.value % 60; 
    if (h < 9) h = "0" + h; 
    if (m < 9) m = "0" + m; 
    if (s < 9) s = "0" + s; 
    return [h, m, s].join(':'); 
} 

摆弄日期时间是适得其反,因为它从未打算持有无日期的时间。

(免责声明:这一切都来自API的描述推断,我有Highchart不熟悉)

+0

我的图表没有显示在浏览器中。不得不想一些其他的方式..感谢您的帮助阿马丹 – Sathy