var chartColors = { red: 'rgb(255, 99, 132)', blue: 'rgb(54, 162, 235)' }; var effectColors = { highlight: 'rgba(255, 255, 255, 0.75)', shadow: 'rgba(0, 0, 0, 0.5)', glow: 'rgb(255, 255, 0)' }; function randomScalingFactor() { return (Math.random() > 0.5 ? 1.0 : -1.0) * Math.round(Math.random() * 100); } var color = Chart.helpers.color; var config = { type: 'bar', data: { labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'], datasets: [{ label: 'Dataset 1', backgroundColor: chartColors.red, borderWidth: 0, data: [0, 0, 0, 0, 0, 0, 0].map(function() { return randomScalingFactor(); }), shadowOffsetX: 3, shadowOffsetY: 3, shadowBlur: 10, shadowColor: effectColors.shadow, bevelWidth: function() {return Math.random() * 6 + 2;}, bevelHighlightColor: effectColors.highlight, bevelShadowColor: effectColors.shadow, hoverInnerGlowWidth: 20, hoverInnerGlowColor: effectColors.glow, hoverOuterGlowWidth: 20, hoverOuterGlowColor: effectColors.glow }, { label: 'Dataset 2', backgroundColor: chartColors.blue, borderWidth: 0, data: [0, 0, 0, 0, 0, 0, 0].map(function() { return randomScalingFactor(); }), shadowOffsetX: 3, shadowOffsetY: 3, shadowBlur: 10, shadowColor: effectColors.shadow, bevelWidth: function() {return Math.random() * 6 + 2;}, bevelHighlightColor: effectColors.highlight, bevelShadowColor: effectColors.shadow, hoverInnerGlowWidth: 20, hoverInnerGlowColor: effectColors.glow, hoverOuterGlowWidth: 20, hoverOuterGlowColor: effectColors.glow }] }, options: { title: { display: true, text: 'Scriptable options sample' }, scales: { xAxes: [{ scaleLabel: { display: true, labelString: 'Month' } }], yAxes: [{ scaleLabel: { display: true, labelString: 'Value' } }] }, tooltips: { shadowOffsetX: 3, shadowOffsetY: 3, shadowBlur: 10, shadowColor: effectColors.shadow, bevelWidth: 2, bevelHighlightColor: effectColors.highlight, bevelShadowColor: effectColors.shadow } } }; 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 = dataset.data.map(function() { return randomScalingFactor(); }); }); window.myChart.update(); });
<head> <script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script> <script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-style@0.5.0"></script> </head> <body> <div> <canvas id="myChart"></canvas> </div> <p> <button id="randomizeData">Randomize Data</button> </p> </body>