highcharts pass multiple values to tooltip

2019-01-17 10:44发布

问题:

I need to display 3 values on the tooltip: the time, the value and another value(change).

I saw this example (but the jsdfiddle is not working).

I tried this

//each loop..
indice.push(["time", "value1", "value2"]);

, the tooltip settings

tooltip:
    {
    useHTML: true,
    formatter: function()
    {
      return '' + Highcharts.dateFormat('%H:%M:%S', this.x) +'; '+ this.y + this.z(<-is this right?);
    }
},

and the series

series:
[{
    type: 'area',
    data: indice
}]

can somone help pls? thsnks.

回答1:

If you want to pass additional data for a point other than the x and y values, then you have to name that value. In the following example I add the following three additional values to each data point:

{
  y: 3,
  locked: 1,
  unlocked: 1,
  potential: 1,
}

Then to access and display those values in the tooltip I use the following:

tooltip: 
{
     formatter: function() { return ' ' +
        'Locked: ' + this.point.locked + '<br />' +
        'Unlocked: ' + this.point.unlocked + '<br />' +
        'Potential: ' + this.point.potential;
     }
}


回答2:

If you, in a multi series Highstocks chart, want to display series specific data in the tooltip you could do something like this.

var series = [];

// Populate our series
series.push({
  name: "small_cities",
  displayName: "Small Cities",
  color: "#123456",
  data: [...]
});
series.push({
  name: "Countries",
  displayName: "Countries",
  color: "#987654",
  data: [...]
});

chart: {
  ...

  tooltip: {
    formatter: function() {
      var s = '';
      $.each(this.points, function(i, point) {
        s += '<br /><span style="color:' + this.series.color + '">' +
             point.series.userOptions.displayName + '</span>: ' + point.y;
      });
      return s;
    }
  },

  ...
}

Now you'll see nice series names "Small Cities" instead of the series.name (small_cities). All attributes you set on the series can be found in this.points[].series.userOptions. For more formatter options take a look at the Highstocks docs.



回答3:

I figured it out, I pass 3 values in the data array

indice.push([(new Date(value.x)).getTime(), val_change[0], val_change[1]]);

series: [{ type: 'area', name: titleindice, data: indice, showInLegend : false //disable the the show/hide icon }]

and in the tooltip

tooltip:
                {
                    useHTML: true,
                    formatter: function()
                    {
                        var color = "";

                        return Highcharts.dateFormat('%H:%M:%S', this.x) + this.y +  this.point.config[2] ;

                    }
                },


回答4:

two ways.

1

series: [
          { ...
            tooltip:
              {pointFormat: "<span style=\"color:{series.color}\">{series.name}</span>: <b>{point.y} {y2}</b><br/>"}
          }, ...          
         ]
o = Highcharts.Point.prototype.tooltipFormatter
Highcharts.Point.prototype.tooltipFormatter=function(format){return o.call(this, format).replace("{y2}", this.config[2]);}

2

 a = new Highcharts.StockChart(options); 
 a.series[0].tooltipFormatter = function(item) { return "<span style=\"color:{" + item.series.color+"\">" +item.series.name+"</span>: <b>"+item.y+item.point.config[2]+"</b><br/>"; }


回答5:

What i have tried is concatenating series name with x-axis and y-axis value and it works fine for me. What issue you are facing?

tooltip: { formatter: function () { return this.x + ': ' + this.series.name + ': ' + this.y; } }