繁体   English   中英

highchart自定义图例

[英]highchart customize legend

我要定制highchart的图例,以实现:

  1. 会有点而不是线
  2. 点的颜色和图例文本的颜色将等于this.color
  3. 在不可见状态(点击图例)上,我想将颜色更改为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>'
                             }
                });


            }
        }
    }
},

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM