Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable the built-in legends in chart.js

I'd like to disable the built-in legends (the colored rectangles) in my chart. Ad the same time I want to keep the custom legends I created with :document.getElementById('legendq3').innerHTML = myDoughnutChart.generateLegend();

 <canvas id="myChart"></canvas>
                        <div id="legendq3"></div>
                        <script> 
                            var ctx = document.getElementById("myChart");

                            var data = {
                                labels: [
                                    "Red",
                                    "Green",
                                    "Yellow"
                                ],
                                datasets: [
                                    {
                                        data: [300, 50, 100],
                                        backgroundColor: [
                                            "#FF6384",
                                            "#36A2EB",
                                            "#FFCE56"
                                        ],
                                        hoverBackgroundColor: [
                                            "#FF6384",
                                            "#36A2EB",
                                            "#FFCE56"
                                        ]
                                    }]
                            };

                            var options = {
                                 legendTemplate :'<ul>'
                                                +'<% for (var i=0; i<datasets.length; i++) { %>'
                                                +'</li>'
                                                +'<span style=\"background-color:<%=datasets[i].lineColor%>\"></span>'
                                                +'<% if (datasets[i].label) { %><%= datasets[i].label %><% } %>'
                                                +'</li>'
                                                +'</ul>'

                            }

                            var myDoughnutChart = new Chart(ctx, {
                                type: 'doughnut',
                                data: data,
                                options: options
                            });

                            //myDoughnutChart.defaults.global.legends.display = false;

                            document.getElementById('legendq3').innerHTML = myDoughnutChart.generateLegend();
                        </script>
like image 991
Willy Mercier Avatar asked Dec 04 '25 07:12

Willy Mercier


1 Answers

I found only this solution. Add this to options

legend: {
    display: false,
}

Here is an example http://jsfiddle.net/qy1hr7mx/

like image 79
vegazz Avatar answered Dec 07 '25 04:12

vegazz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!