Search code examples
javascripthtmlchart.jslegend-properties

onClick event to Hide dataset Chart.js V2


How can I make a html legend from Chart.js to hide/show the dataset of the chart, like the legend generated by the Chart.js itself

enter image description here

Legend on bottom -> Chart.js legend

Legend on right-> HTML

How can I click in "SETOR AGILIZA" and get the dataset relative to it hide/show

I found this code in the chat.js/core.legend.js core.legend

onClick: function(e, legendItem) {
            var index = legendItem.datasetIndex;
            var ci = this.chart;
            var meta = ci.getDatasetMeta(index);

            // See controller.isDatasetVisible comment
            meta.hidden = meta.hidden === null? !ci.data.datasets[index].hidden : null;

            // We hid a dataset ... rerender the chart
            ci.update();
        },

But I have no clue how to get it to work on a onClick event in the html legend

the labels names are get from a database, so they can change.

UPDATE 1

Found this Github, trying to make it work


Solution

  • So, I got it right following this Post

    Here is the code of the post

    var weightChartOptions = {
            responsive: true,
            legendCallback: function(chart) {
                console.log(chart);
                var legendHtml = [];
                legendHtml.push('<table>');
                legendHtml.push('<tr>');
                for (var i=0; i<chart.data.datasets.length; i++) {
                    legendHtml.push('<td><div class="chart-legend" style="background-color:' + chart.data.datasets[i].backgroundColor + '"></div></td>');                    
                    if (chart.data.datasets[i].label) {
                        legendHtml.push('<td class="chart-legend-label-text" onclick="updateDataset(event, ' + '\'' + chart.legend.legendItems[i].datasetIndex + '\'' + ')">' + chart.data.datasets[i].label + '</td>');
                    }                                                                              
                }                                                                                  
                legendHtml.push('</tr>');                                                          
                legendHtml.push('</table>');                                                       
                return legendHtml.join("");                                                        
            },                                                                                     
            legend: {                                                                              
                display: false                                                                     
            }                                                                                      
        };                                                                                         
    
        // Show/hide chart by click legend
        updateDataset = function(e, datasetIndex) {
            var index = datasetIndex;
            var ci = e.view.weightChart;
            var meta = ci.getDatasetMeta(index);
    
            // See controller.isDatasetVisible comment
            meta.hidden = meta.hidden === null? !ci.data.datasets[index].hidden : null;
    
            // We hid a dataset ... rerender the chart
            ci.update();
        };
    
        var ctx = document.getElementById("weightChart").getContext("2d");
        window.weightChart = new Chart(ctx, {
            type: 'line',
            data: weightChartData, 
            options: weightChartOptions
        });
        document.getElementById("weightChartLegend").innerHTML = weightChart.generateLegend();
    };
    

    the secret here is the legendCallback in line 3

    In this example he uses line chart, in my case I used bars

    So i changed the table tags for list tags for me worked better this way

    He emphasizes to put "window" before the variable who gets the "= new Chart"

    window.weightChart = new Chart(ctx, {.....

    Then with a little of CSS you can get a nice legend with a hide/show option