How to show tooltip when clicking the axis label of React Chart?
This article explains how to display a tooltip when clicking the axis label in a React Chart.
Customize the chart to display a tooltip when clicking the axis label
You can programmatically show the tooltip image by clicking the axis label using the axisLabelClick event and the showTooltip method. Also, set the enable property to true in the tooltip.
You can also utilize the template property to customize the appearance of the tooltip, including showing an image.
In the following code example, the tooltip image is displayed when clicking the axis label.
Index.js
import { createRoot } from 'react-dom/client';
import * as React from 'react';
import { ChartComponent, SeriesCollectionDirective, SeriesDirective, Inject, Category,
Tooltip, ColumnSeries } from '@syncfusion/ej2-react-charts';
const Column = () => {
let template =
'<div id="chart_cloud"><img src="https://ej2.syncfusion.com/react/demos/src/chart/images/cloud.png" style="width: 41px; height: 41px"/></div>';
const axisLabelClick = (args) => {
let x = args.chart.series[0].points[args.index].x;
let y = args.chart.series[0].points[args.index].y;
args.chart.showTooltip(x, y, true);
};
return (
<ChartComponent
id="charts"
legendSettings={{ enableHighlight: true }}
primaryXAxis={{
valueType: 'Category',
interval: 1
}}
primaryYAxis={{
maximum: 10,
interval: 2,
labelFormat: '{value}°C'
}}
tooltip={{
enable: true,
template: template
}}
title="Weather Report"
axisLabelClick={axisLabelClick.bind(this)}
>
<Inject services={[ColumnSeries, Tooltip, Category]} />
<SeriesCollectionDirective>
<SeriesDirective
dataSource={data}
xName="x"
columnSpacing={0.1}
yName="y"
type="Column"
/>
</SeriesCollectionDirective>
</ChartComponent>
);
};
export default Column;
const root = createRoot(document.getElementById('sample'));
root.render(<Column />);
The following screenshot portrays the results of the code snippet mentioned above.
Screenshot:
Conclusion
I hope you enjoyed learning how to display a tooltip when clicking the axis label in a React Chart Component.
You can refer to our React Chart feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our React Chart example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, support portal, or feedback portal. We are always happy to assist you!