Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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/

Thanks!

like image 948
user3689417 Avatar asked May 29 '14 23:05

user3689417


3 Answers

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)
        }
    }
},
like image 108
Paweł Fus Avatar answered Oct 16 '22 00:10

Paweł Fus


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
    }
like image 45
Ash Avatar answered Oct 15 '22 23:10

Ash


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

like image 1
Kar.ma Avatar answered Oct 15 '22 23:10

Kar.ma