Custom Highcharts Context Menu Button Appearing in Every Chart on Page

前端 未结 4 1498
执笔经年
执笔经年 2021-01-12 10:37

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:

         


        
相关标签:
4条回答
  • 2021-01-12 11:02

    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

    0 讨论(0)
  • 2021-01-12 11:03

    To add button use options for chart. Then you can set for each chart different set of options: 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)
            }
        }
    },
    
    0 讨论(0)
  • 2021-01-12 11:06

    Starting from Paweł Fus answer, I found out a cleaner solution for the general case. 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.

    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:

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

    JSFiddle

    0 讨论(0)
  • 2021-01-12 11:07

    See the updated fiddle with result : http://jsfiddle.net/4uP5y/3/

    You just needed to mark the newYork chart with exporting enabled false, like this :

        exporting: {
            enabled: false
        }
    
    0 讨论(0)
提交回复
热议问题