代码之家  ›  专栏  ›  技术社区  ›  dan

highchart自定义图例

  •  0
  • dan  · 技术社区  · 7 年前

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

    1. 将有点而不是线
    2. 在不可见状态(onclick legend)下,我想将颜色更改为默认选项highchart(灰色)

    这是我到目前为止得到的: enter image description here

    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的文档中没有找到它(顺便说一句,真的很好),还有其他方法可以实现这一点吗?

    1 回复  |  直到 7 年前
        1
  •  1
  •   dan    7 年前

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

    图表传奇更新和图例。带外部变量的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>'
                                     }
    
                        },
    

    使用图表的事件侦听器。传奇更新:

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