var chartColors = { red: 'rgb(255, 99, 132)', orange: 'rgb(255, 159, 64)', yellow: 'rgb(255, 205, 86)', green: 'rgb(75, 192, 192)', blue: 'rgb(54, 162, 235)', purple: 'rgb(153, 102, 255)', grey: 'rgb(201, 203, 207)' }; function randomScalingFactor() { return (Math.random() > 0.5 ? 1.0 : -1.0) * Math.round(Math.random() * 100); } function onRefresh(chart) { chart.config.data.datasets.forEach(function(dataset) { dataset.data.push({ x: randomScalingFactor(), y: Date.now() }); }); } var color = Chart.helpers.color; var config = { type: 'horizontalBar', data: { datasets: [{ label: 'Dataset 1 (line)', type: 'line', backgroundColor: color(chartColors.red).alpha(0.5).rgbString(), borderColor: chartColors.red, fill: false, data: [] }, { label: 'Dataset 2 (bars)', backgroundColor: color(chartColors.blue).alpha(0.5).rgbString(), borderColor: chartColors.blue, borderWidth: 1, data: [] }] }, options: { title: { display: true, text: 'Mixed chart (vertical scroll) sample' }, scales: { xAxes: [{ scaleLabel: { display: true, labelString: 'value' } }], yAxes: [{ type: 'realtime', realtime: { duration: 20000, refresh: 1000, delay: 2000, onRefresh: onRefresh } }] }, tooltips: { mode: 'nearest', intersect: false, callbacks: { title: function(tooltipItems) { return tooltipItems[0].yLabel; }, label: function(tooltipItem, data) { var label = data.datasets[tooltipItem.datasetIndex].label || ''; if (label) { label += ': '; } label += tooltipItem.xLabel; return label; } } }, hover: { mode: 'nearest', intersect: false } } }; window.onload = function() { var ctx = document.getElementById('myChart').getContext('2d'); window.myChart = new Chart(ctx, config); }; document.getElementById('randomizeData').addEventListener('click', function() { config.data.datasets.forEach(function(dataset) { dataset.data.forEach(function(dataObj) { dataObj.x = randomScalingFactor(); }); }); window.myChart.update(); }); var colorNames = Object.keys(chartColors); document.getElementById('addDataset').addEventListener('click', function() { var colorName = colorNames[config.data.datasets.length % colorNames.length]; var newColor = chartColors[colorName]; var newDataset = { label: 'Dataset ' + (config.data.datasets.length + 1), backgroundColor: color(newColor).alpha(0.5).rgbString(), borderColor: newColor, borderWidth: 1, data: [] }; config.data.datasets.push(newDataset); window.myChart.update(); }); document.getElementById('removeDataset').addEventListener('click', function() { config.data.datasets.pop(); window.myChart.update(); }); document.getElementById('addData').addEventListener('click', function() { onRefresh(window.myChart); window.myChart.update(); });
<head> <script src="https://cdn.jsdelivr.net/npm/moment@2.24.0/min/moment.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script> <script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-streaming@1.8.0"></script> </head> <body> <div> <canvas id="myChart"></canvas> </div> <p> <button id="randomizeData">Randomize Data</button> <button id="addDataset">Add Dataset</button> <button id="removeDataset">Remove Dataset</button> <button id="addData">Add Data</button> </p> </body>