highchart 自定义图例

highchart customize legend

我想自定义highchart的图例,实现:

  1. 会有点而不是线
  2. 点的颜色和图例文本的颜色将等于 this.color
  3. on invisible status (onclick legend) 我想把颜色改成highchart的默认选项(灰色)

这是我目前所拥有的:

我做了什么:

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>',
}

我错过了什么: - 单击时,图例不会将其颜色更改为默认灰色

我一直在寻找类似 legend-labelFormat 的不可见状态的东西,但我没有在 highchart 的文档中找到它(顺便说一句,真的很好),还有其他方法可以实现吗?

我找到了答案,但并不像我希望的那样容易:

我使用了事件侦听器plotOptions.series.events.legendItemClick, chart.legend.update 和 legend.labelFormatter,带有外部变量

外部变量:

var legendsStatus = {};

使用 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>'
                                 }

                    },

事件侦听器使用 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>'
                             }
                });


            }
        }
    }
},