Javascript examples for Chart.js:Legend
Chart js show/hide legend
<html lang="en"> <head> <title>Chart.js - Hide/Show Legend From Button Click</title> </head> <body translate="no"> <div style="width:45%;"> <canvas id="canvas"></canvas> <button id="hideLEgend">Hide/Show Legend</button> </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> window.chartColors = {/*from www . j a v a2s. c om*/ 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 randomScalingFactor = function() { return (Math.random() > 0.5 ? 1.0 : 1.0) * Math.round(Math.random() * 100); }; var line1 = [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), ]; var line2 = [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), ]; var MONTHS = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; var config = { type: 'line', data: { labels: MONTHS, datasets: [{ label: "My First dataset", backgroundColor: window.chartColors.red, borderColor: window.chartColors.red, data: line1, fill: false, }, { label: "My Second dataset", backgroundColor: window.chartColors.blue, borderColor: window.chartColors.blue, data: line2, fill: false, }] }, options: { responsive: true, title:{ display:true, text:'Chart.js - Hide/Show Legend From Button Click' }, tooltips: { mode: 'index', intersect: false, }, hover: { mode: 'nearest', intersect: true }, scales: { xAxes: [{ display: true, scaleLabel: { display: true, labelString: 'Month' } }], yAxes: [{ display: true, scaleLabel: { display: true, }, }] } } }; var ctx = document.getElementById("canvas").getContext("2d"); var myLine = new Chart(ctx, config); document.getElementById('hideLEgend').addEventListener('click', function() { myLine.options.legend.display = !myLine.options.legend.display; myLine.update(); }); //# sourceURL=pen.js </script> </body> </html>