Javascript examples for Chart.js:Chart Data
Chart.js: different dataset size
<html lang="en"> <head> <title>Chart.js - Combo Chart With Multiple Scales (X Axis)</title> </head> <body translate="no"> <div style="width: 45%"> <canvas id="canvas"></canvas> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script> <script> var chartColors = {/* w w w. ja va 2 s . com*/ 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(231,233,237)' }; var getRandomValue = function() { min = Math.ceil(0); max = Math.floor(50); return Math.floor(Math.random() * (max - min + 1)) + min; }; var generateHourlyAvgData = function() { var data = []; for (var i = 0; i < 168; i++) { data.push({ x: i, y: getRandomValue() }); } return data; }; var generateDailyAvgData = function(hourlyAvgData) { var chunkSize = 24; var data = []; for (var i = 0; i < hourlyAvgData.length / chunkSize; i++) { var startIndex = i * chunkSize; var slicedData = hourlyAvgData.slice(startIndex, startIndex + chunkSize); var sum = slicedData.reduce(function(acc, val, index) { return acc + slicedData[index].y; }, slicedData[0].y); data.push(sum / chunkSize); } return data; }; var hourlyAvgData = generateHourlyAvgData(); var dailyAvgData = generateDailyAvgData(hourlyAvgData); var ctx = document.getElementById("canvas").getContext("2d"); var myChart = new Chart(ctx, { type: 'bar', data: { labels: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"], datasets: [{ type: 'bar', label: 'Daily Avg', backgroundColor: chartColors.red, data: dailyAvgData, borderColor: 'white', borderWidth: 2 }, { type: 'line', label: 'Hourly Avg', borderColor: chartColors.green, backgroundColor: chartColors.green, borderWidth: 1, fill: false, pointRadius: 0, xAxisID: 'x-axis-2', data: hourlyAvgData }] }, options: { responsive: true, title: { display: true, text: 'Chart.js - Combo Chart With Multiple Scales (X Axis)' }, tooltips: { mode: 'nearest', intersect: true }, scales: { xAxes: [{}, { id: 'x-axis-2', type: 'linear', position: 'bottom', display: false, }], yAxes: [{ ticks: { min: 0, max: 50 } }] } } }); </script> </body> </html>