score:5
I've already encountered this problem. I've solved it by creating a function to format the tooltip, and applying default values to the data I wanted.
Here is a live example, with the code below:
import React, { Component } from 'react';
import { render } from 'react-dom';
import ReactHighcharts from 'react-highcharts';
import './style.css';
class App extends Component {
static formatTooltip(tooltip, x = this.x, points = this.points) {
let s = `<b>${x}</b>`;
points.forEach((point) =>
s += `<br/>${point.series.name}: ${point.y}m`
);
return s;
}
static getConfig = () => ({
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
tooltip: {
formatter: App.formatTooltip,
shared: true,
},
series: [{
data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}, {
data: [194.1, 95.6, 54.4, 29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4]
}],
})
render() {
return (
<div>
<ReactHighcharts config={App.getConfig())} />
</div>
);
}
}
render(<App />, document.getElementById('root'));
score:-2
I have created a tooltip switcher which can be used as a regular tooltip or as shared.
The isTooltipShared
is a boolean prop that indicates if a tooltip is shared or not.
const options = {
tooltip: {
formatter: props.isTooltipShared ?
function () {
let s = `<b> ${this.x} </b>`;
this.points.forEach((point) => {
s += `<br/> ${point.series.name} : ${point.y}`;
});
return s;
} : function () {
return `${this.series.name} : <b> ${this.x} </b> - <b> ${this.y} </b>`;
},
shared: props.isTooltipShared,
},
};
And used with,
<HighchartsReact
highcharts={Highcharts}
options={options}
/>
score:1
Highcharts expects that tootlip fromatter will return an HTML string. When using react wrapper for Highcharts, it is possible to write a custom tooltip component with formatted data, then in the options when setting up formatter function you can use ReactDOMServer's renderToString
method which will create HTML string from the given element.
Though renderToString is intended for server-side rendering mainly, it can be used in browsers' environment without any problem.
import ReactDOMServer from 'react-dom/server';
import CustomTooltip from './CustomTooltip';
const options = {
tooltip: {
formatter() {
return ReactDOMServer.renderToString(<CustomTooltip {...this} />);
},
},
};
score:2
here's another way that also helps you use React components as part of the tooltip itself.
const Item = ({name, value}) => <div> {name}: {value}</div>
const config = {
formatter(this) {
const container = document.createElement("div");
return this.points.map(point => {
ReactDOM.render(
<Item name={point.series.name} value={point.y}/>
)
return container.innerHTML
}
}
}
score:11
OP couldn't figure out how to access the scope of the chart using the this
keyword. The simple answer is because OP was using a fat arrow function. Instead, try using a normal function as per this modified version of the OP's code:
const CHART_CONFIG = {
...
tooltip:
{
formatter() { // Use a normal fn, not a fat arrow fn
// Access properties using `this`
// Return HTML string
},
shared: true
},
...
}
Source: stackoverflow.com
Related Query
- How can I use chart tooltip formatter in react-highcharts?
- how to use highcharts tooltip formatter in python code
- How to use the tooltip formatter and still display chart color (like it does by default)?
- How to use Highcharts React to create chart with multiple lines for same XAxis?
- How can I make React Native in Android aware of a click event in a tooltip in a Highcharts chart?
- how can I use rangeselector and navigation in highcharts in the given code
- How can I get access to a Highcharts chart through a DOM-Container?
- Change Highcharts tooltip formatter from chart Object , after chart is rendered
- Highcharts - How can I remove starting and ending padding from area chart
- How can i force Highcharts to use same symbols in Legend and Series?
- Highcharts - how to set custom content in point tooltip (popup) on 3D scatter chart or how to customize point tooltip information?
- Highcharts : How can i move the tooltip to external DIV?
- How to use Highcharts Gantt chart in angular?
- Highcharts - In area chart how to use gradient color for multiple series?
- How can I pass values from Highcharts event callbacks (click, mouseOver, mouseOut) back to React component member function?
- With HighCharts, how can I have the same tooltip formatter as my y axis
- How to use Highcharts in typescript and react
- how can I use highcharts no-data-to-display module as es6-module?
- How to edit tooltip in Highcharts C# code
- How to create a HighCharts chart in react
- How can I make milestone lines with a GANTT chart using the highcharts library?
- How can I display crosshair labels on top of the chart with Highcharts
- How can I get a chart only with legends using highcharts
- How can I get highcharts Donut chart to auto calculate percentages
- How to change the chart title dynamically on tooltip hover in the points Highcharts.. React JS
- How can I inspect the Highcharts tooltip in the Chrome inspector?
- How can I reveal my chart tooltip programatically when the tooltip combines data from multiple series? (sync charts)
- How to call Highcharts tooltip formatter function from outside the config object?
- How can i make my gnatt highcharts tooltip in this shape
- How can I apply different background color to entire tooltip for different series in highcharts
More Query from same tag
- Charts rendered using highcharts in angular2 do not take the height of the parent
- Resize data points with highcharts
- resetting chart title on zoom button click in highcharts
- Highcharts, datetime, timestamp. What format of date needs when axis has type 'datetime'?
- Highcharts, change line type if more than one series on the chart
- R Highcharter: Dynamic multi level drilldown in Shiny
- Poorly Ordered Data Over Websockets
- Displaying a target area / value on a HighCharts graph
- Highchart columns chart overlaps yAxis.plotLines labels
- Highcharts - how to dynamically disable plotBorder without recreating a chart
- How to show multiple Highcharts graph on one page
- Highcharts Angular update chart with new options - including linked series
- Multiple series in Highcharts with multiple drilldowns
- The data is not completely displayed in the stack bar Highcharts
- Highcharts does not display years on Xaxis when data with NULL values
- non programmer--Read data from mySQL into bar chart
- How to skip one level of data in a sankey chart?
- DataLabels in Highcharts are outside the container / not showing
- Highcharts -- Can't apply style to x axis labels
- Why my polar chart is rendered as a line chart? (Highcharts latest version)
- highcharts missing markers occasionally
- keeping highcharts stacked columns borderWdith but removing stacking gap
- Highstock Range Selector Buttons to PDF Export Dropdown
- Highcharts - set column width dynamically
- How to add the "isSum" bar in Highchart Waterfall chart in R?
- Highchart - Changing label text of the yaxis programatically
- Tooltip: how do I retrieve the position of the point when the mouse hover it?
- Series markers disable on lines and enable on legend in Highchart
- Highcharts menus bottom into right side of the graph
- Donut Inner Text