简体   繁体   English

Chart.js显示时间数据

[英]Chart.js displaying time data

I am trying to render an array of time data on the x axis scale of a line chart in chart.js but despite trying numerous options settings cannot get it to display at all. 我试图在chart.js中以折线图的x轴比例绘制时间数据数组,但是尽管尝试了许多选项,设置却根本无法显示。 The data is in string format ["00:30", "01:00", "01:30"...] and these are my xAxes options settings: 数据为字符串格式[“ 00:30”,“ 01:00”,“ 01:30” ...],这些是我的xAxes选项设置:

xAxes: [{
   type: "time",
   time: {
      parser: "hh:mm",
      unit: "minute",
      displayFormats: {
                  minute: "hh:mm"
      }
   },

Am I missing something obvious? 我是否缺少明显的东西?

Thanks! 谢谢!

Your main issue is that moment.js script tag should be placed early in the "script" tags in your example. 您的主要问题是moment.js脚本标记应放在示例中的“ script”标记的前面。 It will work in your example if you do (also uncomment the "xaxis" section). 如果您这样做,它将在您的示例中起作用(也请取消注释“ xaxis”部分)。

So put script tag above chart.js: 因此,将脚本标签放在chart.js上方:

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

` `

Here is also an easy way to represent whatever you would like in chart.js and get the data and do whatever you'd like (it will also help find your error): 这也是一种简单的方法,可以在chart.js中表示您想要的任何东西,并获取数据并执行您想要的任何事情(这也将有助于发现错误):

xAxes: [{
   ticks: {
       callback: function(value) {
          //"HH:mm:ss"
          return moment(value).format("mm:ss");
       }
   }
}],

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM