【问题标题】:Shared events highcharts共享事件高图表
【发布时间】:2018-04-20 09:38:41
【问题描述】:

我正在使用 HighCharts Custom Events Module(但即使不使用它,我的问题仍然是一样的)并且我将相同的事件/函数绑定到我的每个元素(系列、yAxis 等......)和它越来越重复:

...
yAxis: [
{
    labels: {
        format: "{value}°C"
    },
    title: {
        text: "Temperature",        
        events: {
            click: function (event) {
                foo(event);
            },
            dblclick: function (event) {
                foo(event);
            },
            contextmenu: function (event) {
                foo(event);
            }
        }
    },
    id: "temperature",
    labels: {
        events: {
            click: function (event) {
                bar(event);
            },
            dblclick: function (event) { 
                bar(event);
            },
            contextmenu: function (event) {
                bar(event);
            }
        }
    }
},
    {
    labels: {
        format: "{value}km"
    },
    title: {
        text: "Width",      
        events: {
            click: function (event) {
                foo(event);
            },
            dblclick: function (event) {
                foo(event);
            },
            contextmenu: function (event) {
                foo(event);
            }
        }
    },
    id: "temperature",
    labels: {
        events: {
            click: function (event) {
                bar(event);
            },
            dblclick: function (event) { 
                bar(event);
            },
            contextmenu: function (event) {
                bar(event);
            }
        }
    }
}
...
],

我什至允许使用添加更多(再次,yAxis、系列等......“渲染后”)。

这就是为什么我想知道是否有更“全局方式”将事件绑定到给定图表的每个 yAxis、series、...。类似于

Highcharts.setOptions({ // Apply to all charts
    series: {    //hypothetically
        events: {
            click: function (event) {
                foo(event)
            }
        }
    }
});

我还考虑过在渲染系列后创建某种更新回调,例如

    Highcharts.setOptions({ // Apply to all charts
        events: {
            addSeries: function (event) {
                event.update(
                    {events{
                        click: function(event){ 
                            foo(event); 
                        }
                    }
                }, false);
            }
        }
    });

但这更像是一种黑客攻击

这个问题并不重要,更多的是出于好奇,但同时,它会让我的(糟糕的)代码更清晰^^

感谢您的阅读。

【问题讨论】:

    标签: javascript events highcharts shared custom-events


    【解决方案1】:

    可以在plotOptions.series 属性中指定将应用于所有系列的选项。使用Highcharts.setOptions函数更改每个图表的默认选项:

    var foo = function() {
      console.log("contextmenu event");
    }
    
    Highcharts.setOptions({
      xAxis: {
        labels: {
          events: {
            contextmenu: foo
          }
        }
      },   
      plotOptions: {
        series: {
          events: {
            contextmenu: foo
          },
          dataLabels: {
            enabled: true,
            events: {
              contextmenu: foo
            }
          }
        }
      }
    });
    
    var chart = Highcharts.chart('container', {
    
      chart: {
        type: 'column'
      },
    
      xAxis: [{
        labels: {
          style: {
            color: '#bada55'
          }
        }
      }, {}],
    
      series: [{
        data: [1, 2]
      }, {
        data: [3, -4],
        xAxis: 1
      }]
    });
    

    现场演示: http://jsfiddle.net/BlackLabel/Lxrg3hsy/

    API 参考: https://api.highcharts.com/class-reference/Highcharts#setOptions

    【讨论】:

    • 顺便说一句@Kamil Kulig,双击不会触发轴标题(X或Y),Demo Fiddle中的事件是否正常?对小标题进行双重“触摸”是否只是“困难”?还是根本不可能? (我使用的是简单的点击,但我很好奇)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-09
    相关资源
    最近更新 更多