简体   繁体   中英

How do I pass in parameters to the Highcharts pointFormatter callback function

Highcharts has a pointFormatter option that accepts a callback function. I want to use a couple variables in that formatter. How do I do that? Sorry if this is obvious; I'm new to JS. This is what I have so far:

chart.addSeries({
      id: "buy1",
      name: "Buys for SD #1",
      type: "scatter",
      color: "#23DA47",
      data: buySellCleaner(buysPoints1),
      tooltip: { 
         pointFormatter: function(priceArr, buysPoints1) {
             return "Time" + "<b>" + unixToEST(this.x) + "</b>" + "<br/>Price: <b>" + priceArr[buySellCleaner(buysPoints1)[this.series.data.indexOf( this.point )][2]][1] + "</b>";
        }
   },
});

Obviously priceArr and buysPoints1 aren't defined in the scope...how do I fix that?

If they're in the scope of chart.addSeries then they should be in scope for the pointFormatter callback function.

From the pointformatter examples it looks like highcharts does not pass any parameters to the callback.

What you are doing is adding unnecessary parameters to the pointformatter callback function and overwriting your previous values for priceArr and buysPoints1 with undefined . They are undefined because when highcharts calls the pointformatter callback for you, it does not pass any values into the call, so your parameters (priceArr and buysPoints1) are undefined inside the function.

Try removing them from the callback parameters.

pointFormatter: function() {
    return "Time" + "<b>" + unixToEST(this.x) + "</b>" + "<br/>Price: <b>" + priceArr[buySellCleaner(buysPoints1)[this.series.data.indexOf( this.point )][2]][1] + "</b>";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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