简体   繁体   English

自定义Highcharts上下文菜单按钮出现在页面上的每个图表中

[英]Custom Highcharts Context Menu Button Appearing in Every Chart on Page

I have a page with several charts, and I need to be able to add specific options to the exporting context menu for each chart. 我有一个包含多个图表的页面,我需要能够为每个图表的导出上下文菜单添加特定选项。 This is the code I am using: 这是我正在使用的代码:

myChart.options.exporting.buttons.contextButton.menuItems.push({
        text: "Custom Option",
        onclick: someFunction
    });

Unfortunately, this adds the option to every chart, not just the chart myChart references. 不幸的是,这为每个图表添加了选项,而不仅仅是myChart引用的图表。 I'd like to be able to add an option and have it appear on only one chart. 我希望能够添加一个选项并让它只显示在一个图表上。

Here is a fiddle which demonstrates: http://jsfiddle.net/4uP5y/2/ 这是一个小提琴演示: http//jsfiddle.net/4uP5y/2/

Thanks! 谢谢!

To add button use options for chart. 为图表添加按钮使用选项。 Then you can set for each chart different set of options: http://jsfiddle.net/4uP5y/4/ 然后你可以为每个图表设置不同的选项集: http//jsfiddle.net/4uP5y/4/

Get default buttons: 获取默认按钮:

var buttons = Highcharts.getOptions().exporting.buttons.contextButton.menuItems;

buttons.push({
    text: "Tokyo Only Option",
    onclick: HelloWorld
});

And set them for a chart: 并将它们设置为图表:

exporting: {
    buttons: {
        contextButton: {
            menuItems: buttons // or buttons.slice(0,6)
        }
    }
},

See the updated fiddle with result : http://jsfiddle.net/4uP5y/3/ 查看结果的更新小提琴: http//jsfiddle.net/4uP5y/3/

You just needed to mark the newYork chart with exporting enabled false, like this : 您只需要将导出为false的newYork图表标记为false,如下所示:

    exporting: {
        enabled: false
    }

I found another possiblity to add it only to one chart. 我发现另一种可能性只能将它添加到一个图表中。 Add following to the chart where you want to extend the context menu 将以下内容添加到要扩展上下文菜单的图表中

       exporting: {
            buttons: {
                contextButton: {
                    menuItems: [

                    ]
                }
            }
        },

. Now you are able to extend the chart dynamicly with a method like 现在,您可以使用类似的方法动态扩展图表

 function (button) {
    var userMenu = this.chart.userOptions.exporting.buttons.contextButton.menuItems;
    if (userMenu.length === 0) {
        var menuItems = Highcharts.getOptions().exporting.buttons.contextButton.menuItems;
        for (var itemIndex in menuItems) {
            userMenu.push(menuItems[itemIndex]);
        }
    }
    userMenu.push(button);
};

. Where this.chart points to the chart which context menu should be extended this.chart指向图表应该扩展上下文菜单

Starting from Paweł Fus answer, I found out a cleaner solution for the general case. 从PawełFus回答开始,我发现了一般情况的清洁解决方案。 The main issue is you need not to mess around with original object and extend it. 主要问题是你不必乱用原始对象并扩展它。 Instead, you'd be better cloning it. 相反,你最好克隆它。 Please note that my solution requires jQuery. 请注意,我的解决方案需要jQuery。

function appendExportButton(mytext, myfunction){
  var defaultButtons = Highcharts.getOptions().exporting.buttons; // get default Highchart Export buttons
  var myButtons = $.extend(true, {}, defaultButtons);
  myButtons.contextButton.menuItems.push({
    text: mytext,
    onclick: myfunction
  });
  return {buttons: myButtons};
}

To insert this button in the desired chart, define the chart this way: 要在所需图表中插入此按钮,请按以下方式定义图表:

var mychart = new Highcharts.Chart({
  chart: {
    ...whatever...
  },
  plotOptions: {
    ...whatever...
  },
  series: {
    ...whatever...
  },
  exporting: appendExportButton("Save data in CSV format", saveCSV)
});

In the case of OP problem, this is the line you have to use: 在OP问题的情况下,这是您必须使用的行:

exporting: appendExportButton("Tokyo Only Option", HelloWorld)

JSFiddle 的jsfiddle

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

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