dan
dan

Reputation: 336

highchart customize legend

I will like to custom the legend of highchart, to achieve:

  1. There will be dots instead of lines
  2. The color of the dots and the color of the legend text will be equal to this.color
  3. on invisible status (onclick legend) I will like to change the color to the default option of highchart (gray)

This is what I have so far: enter image description here

what I did:

legend: {
    useHTML: true,
    symbolWidth: 0,
    lableFormat: '<div>' +
                    '<div style="border-radius:50%; width: 10px; height:10px: background-color:{color}; display: inline-block; margin-right:5px"> </div>' +
                    "<span style='color:{color};font-family:proximaNovaBold'> {name} </span>"
                 '<div>',
}

what I am missing: - on click, the legend doesn't change his color to default gray color

I was looking for something like legend-labelFormat for invisible status, but I didn't find it in the docs of highchart (really good by the way), is there any other way to accomplish this?

Upvotes: 0

Views: 5356

Answers (1)

dan
dan

Reputation: 336

I found the answer but it wasn’t easy as I hoped:

I used event listener plotOptions.series.events.legendItemClick, chart.legend.update, and legend.labelFormatter, with external variable

External Variable:

var legendsStatus = {};

Custom legend using labelFormatter:

legend :{
                        useHTML: true,
                        symbolWidth: 0,
                        labelFormatter: function () {
                                     return '<div>' +
                                                '<div style="border-radius: 50%; width: 10px; height: 10px; background-color: ' + this.color +'; display: inline-block; margin-right:5px"> </div>' +
                                                "<span style='color:" + this.color + "; font-family:proximaNovaBold'> " + this.name +  " </span>" +
                                            '</div>'
                                 }

                    },

Event listener using chart.legend.update:

plotOptions: {
    series: {
        marker: {
            enabled: false
        },
        events : {
            legendItemClick : function(){

                legendsStatus[this.name] = this.visible;

                this.chart.legend.update( {
                    useHTML: true,
                    symbolWidth: 0,
                    labelFormatter: function () {

                                // legend status = visible
                                if (!legendsStatus[this.name])
                                     return '<div>' +
                                                '<div style="border-radius: 50%; width: 10px; height: 10px; background-color: ' + this.color +'; display: inline-block; margin-right:5px"> </div>' +
                                                "<span style='color:" + this.color + "; font-family:proximaNovaBold'> " + this.name +  " </span>" +
                                           '</div>'

                                // legend status = invisible
                                return '<div>' +
                                           '<div style="border-radius: 50%; width: 10px; height: 10px; background-color: #cccccc; display: inline-block; margin-right:5px"> </div>' +
                                           "<span style='color: #cccccc; font-family:proximaNovaBold'> " + this.name +  " </span>" +
                                      '</div>'
                             }
                });


            }
        }
    }
},

Upvotes: 1

Related Questions