ChartJS Line Chart

Multiple Line Chart

by Luke Marlow

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.3.0/Chart.js"></script>
<body>
    <canvas id="chartFive" width="600" height="400"></canvas>
</body>

CSS

canvas { background-color : #eee;
}

JavaScript

var ctx = document.getElementById('chartFive').getContext('2d');

let timeData ={
  "2019-08-10T06:24": 2,
  "2019-08-10T06:25": 8,
  "2019-08-10T06:26": 10,
  "2019-08-10T06:27": 12,
  "2019-08-10T06:28": 1
};
let entries = Object.entries(timeData);
entries.sort((a,b) => {
  let aDate = moment(a[0]);
  let bDate = moment(b[0]);
  return aDate.toDate() - bDate.toDate();
});

let labels = entries.map(e => e[0]);
let data = entries.map(e => e[1]);

var chart = new Chart(ctx, {
    type: 'line',
    data: {
        labels,
        datasets: [{
            label: '# of Messages',
            data,
            backgroundColor: 'rgba(0,0,255,0.3)',
            borderColor: 'blue',
            borderWidth: 1
        }]
    },
    options: {
        title: {
            display: true,
            text: 'Messages Received'
    },
    legend: {
        display: false
    },
    scales: {
        xAxes: [{
            type: 'time',
            time: {
                displayFormats: {
                    minute: 'HH:mm'
                },
                unit: 'minute',
                min: '2019-08-10T06:00',
                max: '2019-08-10T07:00'
            }
            }],
            yAxes: [{
                gridLines: {
                    display: false
                },
                ticks: {
                    beginAtZero: true,
                    stepSize: 1
                }
            }]
        }
    }
});