ChartJS TimeFormat

Как вывести время в формате 12:10:33?

var myChart = new Chart(ctx, {
    type: 'line',
    data:{
        datasets: [{
            label: "Coca-Cola",
            data: [{
                x: new Date(),
                y:10
            },
            {
                x: new Date(),
                y:14
            }],
            borderColor: "red",
            fill: false
        }]
    },
    options: {
        scales: {
            xAxes: [{
                type: 'time'
            }]
        }
    }
});

введите сюда описание изображения


Ответы (1 шт):

Автор решения: Aziz Umarov

Посмотрите сюда

    xAxes: [{
        type: 'time',
        time: {
            unit: 'second',
            displayFormats: {
                second: 'HH:mm:ss'
            }
            
        }
    }]

let canvas = document.getElementsByTagName("canvas")[0];

var myChart = new Chart(canvas, {
    type: 'line',
    data:{
        datasets: [{
            label: "Coca-Cola",
            data: [{
                x: new Date(),
                y:10
            },
            {
                x: new Date(new Date().getTime() + (24*60*60*1000)),
                y:14
            }],
            borderColor: "red",
            fill: false
        }]
    },
    options: {
        scales: {
          xAxes: [{
            type: 'time',
            time: {
                unit: 'second',
                displayFormats: {
                    second: 'HH:mm:ss'
                }
                
            }
        }]
        }
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js" integrity="sha512-d9xgZrVZpmmQlfonhQUvTR7lMPtO7NkZMkA0ABN3PHCbKA5nqylQ/yWlFAyY6hYgdF1Qh6nYiuADWwKB4C2WSw==" crossorigin="anonymous"></script>
<canvas></canvas>

→ Ссылка