开满天机
您的图表配置在更改Type: 'time'为type: 'time'. 您可以运行以下版本,它会替换您的 Python 模板变量。这里有一些其他的事情要检查更正type错字后,查找控制台错误。确保 moment.js 正确加载。检查 moment.js 和 Chart.js 的版本(下面的示例分别使用 2.26.0 和 2.9.3)。提供实际的 HTML 源代码(而不是模板),因为如果它仍然损坏,则意味着模板/传递值有问题。const config = { // The type of chart we want to create type: 'line', //types: bar, horizontalBar, pie, line, doughnut, radar, polarArea // The data for our dataset data: { labels: [new Date('2019-12-19T13:36:29-08:00'), new Date('2019-12-19T13:36:59-08:00'), new Date('2019-12-19T13:37:29-08:00'), new Date('2019-12-19T13:37:59-08:00'), new Date('2019-12-19T13:38:29-08:00')], datasets: [{ label: 'Test String Dates', backgroundColor: 'rgba(255, 99, 132, 0)', borderColor: 'rgb(117, 4, 28)', borderWidth: 1, hoverBorderWidth: 3, hoverBorderColor: '#000', data: [0.05, 0.07, 0.15, 0.08, 0.05], }] }, options: { title: { display: true, text: 'test string date time', fontSize: 25, }, legend: { //display:false //to hide legend position: 'right', labels: { fontColor: '#000' } }, tooltips: { //enabled:false, }, scales: { yAxes: [{ scaleLabel: { display: true, labelString: 'mg/m3', fontColor: '#000', fontWeight: 'bold', fontSize: 25 } }], xAxes: [{ type: 'time', time: { parser: 'HH:mm:ss a', //these formatting values do nothing, I've tried a few different ones unit: 'second', //I have tried minutes and hours too, same result displayFormats: { 'millisecond': 'HH:mm:ss a', //I have tried without the 'a' too, same result 'second': 'HH:mm:ss a', 'minute': 'HH:mm:ss a', 'hour': 'HH:mm:ss a', 'day': 'HH:mm:ss a', 'week': 'HH:mm:ss a', 'month': 'HH:mm:ss a', 'quarter': 'HH:mm:ss a', 'year': 'HH:mm:ss a', } }, ticks: { source: 'auto' }, scaleLabel: { display: true, labelString: 'Recording Time', fontColor: '#000', fontWeight: 'bold', fontSize: 25 } }] }, responsive: true, maintainAspectRatio: false, elements: { point: { radius: 0 }, line: { tension: 0 } }, }};const ctx = document.getElementById('canvas').getContext('2d');new Chart(ctx, config);<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.26.0/moment.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.js"></script><body> <canvas id="canvas" width="600" height="400"></canvas></body>