I have a Column chart using categories on the x-axis, and I’d like to be able to access the categories in the Tooltip. (Basically, we’re using it as a histogram and I want to show the range of values encompassed by each bin in the tooltip.) I’ve managed to do this without too much trouble in a Highcharts jsfiddle with the following in the chart configuration:
xAxis: {
categories: [1, 2, 3, 4, 5, 6]
,
id: 'x-axis'
},
tooltip: {
formatter: function() {
var chart = $('#container').highcharts();
var start = chart.get('x-axis').categories[this.point.x]
;
var end = chart.get('x-axis').categories[this.point.x + 1]
;
return '[' + start + ',' + end + ')';
}
},
...
In Vaadin, I’m passing this same formatter function to the Tooltip’s setFormatter method, and I’ve added the “x-axis” id to the axis configuration JSON manually (I didn’t see anything in the Java API to do this) and then called drawChart with the updated JSON string. However, I think I need to do something differently on line 8; trying to grab the highcharts object this way doesn’t seem to work in Vaadin Charts. I’m not very experienced with Javascript so I don’t really know where to look to find the highcharts object, or an alternate way to get to the xAxis categories. Any hints?
Regarding on how to get the chart, I’ve found diferent alternatives:
If the tooltip is not shared you can use:
var chart = this.series.chart;
If the tooltip is shared you can use:
var chart = this.points[0]
.series.chart;
Or in both cases:
var chart = Highcharts.charts[0]
;
(Highcharts.charts is an array containing the current chart objects in the page, although I’m not sure what the index will be in all the cases)
The chart.axes array is exactly what I was looking for! The only reason I really wanted to set the ID was because I thought I would need it to get the axis object from the chart in the formatter function. It might be helpful to add that to the API documentation. (The Tooltip docs do list some properties that are accessible, but not these ones.)