Tool tip in React Accumulation chart component
20 Jan 202320 minutes to read
Tooltip for the accumulation chart can be enabled by using the enable
property.
import * as React from "react";
import * as ReactDOM from "react-dom";
import { AccumulationChartComponent, AccumulationSeriesCollectionDirective, AccumulationSeriesDirective, AccumulationTooltip, Inject } from '@syncfusion/ej2-react-charts';
import { accData } from 'datasource.ts';
function App() {
return <AccumulationChartComponent id='charts' tooltip={{ enable: true }}>
<Inject services={[AccumulationTooltip]}/>
<AccumulationSeriesCollectionDirective>
<AccumulationSeriesDirective dataSource={accData} xName='x' yName='y'>
</AccumulationSeriesDirective>
</AccumulationSeriesCollectionDirective>
</AccumulationChartComponent>;
}
;
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));
import * as React from "react";
import * as ReactDOM from "react-dom";
import { AccumulationChartComponent, AccumulationSeriesCollectionDirective, AccumulationSeriesDirective,
AccumulationTooltip, Inject,TooltipSettingsModel} from'@syncfusion/ej2-react-charts';
import { accData } from 'datasource.ts';
function App() {
return <AccumulationChartComponent id='charts' tooltip={{ enable: true }}>
<Inject services={[AccumulationTooltip]} />
<AccumulationSeriesCollectionDirective>
<AccumulationSeriesDirective dataSource={accData} xName='x' yName='y'>
</AccumulationSeriesDirective>
</AccumulationSeriesCollectionDirective>
</AccumulationChartComponent>
};
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));
Note:To use tooltip feature , we need to inject
AccumulationTooltip
module into theservices
.
Header
We can specify header for the tooltip using header
property.
import * as React from "react";
import * as ReactDOM from "react-dom";
import { AccumulationChartComponent, AccumulationSeriesCollectionDirective, AccumulationSeriesDirective, AccumulationTooltip, Inject } from '@syncfusion/ej2-react-charts';
import { accData } from 'datasource.ts';
function App() {
return <AccumulationChartComponent id='charts' tooltip={{ enable: true, header: "Pie Chart" }}>
<Inject services={[AccumulationTooltip]}/>
<AccumulationSeriesCollectionDirective>
<AccumulationSeriesDirective dataSource={accData} xName='x' yName='y'>
</AccumulationSeriesDirective>
</AccumulationSeriesCollectionDirective>
</AccumulationChartComponent>;
}
;
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));
import * as React from "react";
import * as ReactDOM from "react-dom";
import { AccumulationChartComponent, AccumulationSeriesCollectionDirective, AccumulationSeriesDirective,
AccumulationTooltip, Inject }
from'@syncfusion/ej2-react-charts';
import { accData } from 'datasource.ts';
function App() {
return <AccumulationChartComponent id='charts' tooltip={{ enable: true, header: "Pie Chart" }}>
<Inject services={[AccumulationTooltip]} />
<AccumulationSeriesCollectionDirective>
<AccumulationSeriesDirective dataSource={accData} xName='x' yName='y'>
</AccumulationSeriesDirective>
</AccumulationSeriesCollectionDirective>
</AccumulationChartComponent>
};
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));
Format
By default, tooltip shows information of x and y value in points. In addition to that, you can show more information in tooltip. For example the format ${series.name} ${point.x}
shows series name and point x value.
import * as React from "react";
import * as ReactDOM from "react-dom";
import { AccumulationChartComponent, AccumulationSeriesCollectionDirective, AccumulationSeriesDirective, AccumulationTooltip, Inject, AccumulationLegend } from '@syncfusion/ej2-react-charts';
import { accData } from 'datasource.ts';
function App() {
return <AccumulationChartComponent id='charts' tooltip={{ enable: true, format: '${point.x} : <b>${point.y}%</b>' }}>
<Inject services={[AccumulationLegend, AccumulationTooltip]}/>
<AccumulationSeriesCollectionDirective>
<AccumulationSeriesDirective dataSource={accData} xName='x' yName='y'>
</AccumulationSeriesDirective>
</AccumulationSeriesCollectionDirective>
</AccumulationChartComponent>;
}
;
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));
import * as React from "react";
import * as ReactDOM from "react-dom";
import { AccumulationChartComponent, AccumulationSeriesCollectionDirective,
AccumulationSeriesDirective, AccumulationTooltip, Inject, AccumulationLegend} from'@syncfusion/ej2-react-charts';
import { accData } from 'datasource.ts';
function App() {
return <AccumulationChartComponent id='charts' tooltip={{ enable: true, format: '${point.x} : <b>${point.y}%</b>' }}>
<Inject services={[AccumulationLegend, AccumulationTooltip]} />
<AccumulationSeriesCollectionDirective>
<AccumulationSeriesDirective dataSource={accData} xName='x' yName='y'>
</AccumulationSeriesDirective>
</AccumulationSeriesCollectionDirective>
</AccumulationChartComponent>
};
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));
Tooltip Mapping Name
By default, tooltip shows information of x and y value in points. You can show more information from datasource in tooltip by using the tooltipMappingName
property of the tooltip. You can use the ${point.tooltip}
as place holders to display the specified tooltip content.
import * as React from "react";
import * as ReactDOM from "react-dom";
import { AccumulationChartComponent, AccumulationSeriesCollectionDirective, AccumulationSeriesDirective, AccumulationTooltip, Inject, AccumulationLegend } from '@syncfusion/ej2-react-charts';
import { accData } from 'datasource.ts';
function App() {
return <AccumulationChartComponent id='charts' tooltip={{ enable: true, format: '${point.tooltip}' }}>
<Inject services={[AccumulationLegend, AccumulationTooltip]}/>
<AccumulationSeriesCollectionDirective>
<AccumulationSeriesDirective dataSource={accData} xName='x' yName='y' tooltipMappingName='text'>
</AccumulationSeriesDirective>
</AccumulationSeriesCollectionDirective>
</AccumulationChartComponent>;
}
;
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));
import * as React from "react";
import * as ReactDOM from "react-dom";
import { AccumulationChartComponent, AccumulationSeriesCollectionDirective,
AccumulationSeriesDirective, AccumulationTooltip, Inject, AccumulationLegend}
from'@syncfusion/ej2-react-charts';
import { accData } from 'datasource.ts';
function App() {
return <AccumulationChartComponent id='charts' tooltip={{ enable: true, format: '${point.tooltip}' }}>
<Inject services={[AccumulationLegend, AccumulationTooltip]} />
<AccumulationSeriesCollectionDirective>
<AccumulationSeriesDirective dataSource={accData} xName='x' yName='y' tooltipMappingName='text'>
</AccumulationSeriesDirective>
</AccumulationSeriesCollectionDirective>
</AccumulationChartComponent>
};
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));
Tooltip Template
Any HTML element can be displayed in the tooltip by using the template
property.
import * as React from "react";
import * as ReactDOM from "react-dom";
import { AccumulationChartComponent, AccumulationSeriesCollectionDirective, AccumulationSeriesDirective, AccumulationTooltip, Inject } from '@syncfusion/ej2-react-charts';
import { accData } from 'datasource.ts';
function App() {
const template = chartTemplate;
function chartTemplate(args) {
return (<div style={{ border: '1px solid black', backgroundColor: 'red', padding: '2px', float: 'right', lineHeight: '20px', textAlign: 'center' }}>
<img src='sun_annotation.png'/>
<div style={{ color: 'blue', padding: '2px', fontSize: '14px', fontStyle: 'medium', fontFamily: 'Roboto', float: 'right', lineHeight: '20px', textAlign: 'center', paddingRight: '6px' }}>
<span>{args.y}</span>
</div>
</div>);
}
return <AccumulationChartComponent id='charts' tooltip={{ enable: true, template: template }}>
<Inject services={[AccumulationTooltip]}/>
<AccumulationSeriesCollectionDirective>
<AccumulationSeriesDirective dataSource={accData} xName='x' yName='y'>
</AccumulationSeriesDirective>
</AccumulationSeriesCollectionDirective>
</AccumulationChartComponent>;
}
;
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));
import * as React from "react";
import * as ReactDOM from "react-dom";
import { AccumulationChartComponent, AccumulationSeriesCollectionDirective, AccumulationSeriesDirective,
AccumulationTooltip, Inject} from'@syncfusion/ej2-react-charts';
import { accData } from 'datasource.ts';
function App() {
const template:any =chartTemplate;
function chartTemplate(args:any){
return (<div style={{ border:'1px solid black', backgroundColor:'red', padding:'2px',float:'right', lineHeight:'20px',textAlign:'center'}}>
<img src='sun_annotation.png' />
<div style={{ color:'blue', padding:'2px',fontSize:'14px', fontStyle:'medium',fontFamily:'Roboto' ,float:'right',lineHeight:'20px',textAlign:'center',paddingRight:'6px'}}>
<span>{args.y}</span>
</div>
</div>
);
}
return <AccumulationChartComponent id='charts' tooltip={{ enable: true, template: template }}>
<Inject services={[AccumulationTooltip]} />
<AccumulationSeriesCollectionDirective>
<AccumulationSeriesDirective dataSource={accData} xName='x' yName='y'>
</AccumulationSeriesDirective>
</AccumulationSeriesCollectionDirective>
</AccumulationChartComponent>
};
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));
Customization
The fill
and border
properties are used to customize the background color and border of the tooltip respectively. The textStyle
property in the tooltip is used to customize the font of the tooltip text. The highlightColor
property can be used to change the color of the data point when hovering.
import * as React from "react";
import * as ReactDOM from "react-dom";
import { AccumulationChartComponent, AccumulationSeriesCollectionDirective, AccumulationSeriesDirective, AccumulationTooltip, Inject } from '@syncfusion/ej2-react-charts';
import { accData } from 'datasource.ts';
function App() {
return <AccumulationChartComponent id='charts' tooltip={{
enable: true, format: '${series.name} ${point.x} : ${point.y}',
//fill for tooltip
fill: '#7bb4eb',
//border for tooltip
border: {
width: 2,
color: 'grey'
}
}} highlightColor='red'>
<Inject services={[AccumulationTooltip]}/>
<AccumulationSeriesCollectionDirective>
<AccumulationSeriesDirective dataSource={accData} xName='x' yName='y'>
</AccumulationSeriesDirective>
</AccumulationSeriesCollectionDirective>
</AccumulationChartComponent>;
}
;
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));
import * as React from "react";
import * as ReactDOM from "react-dom";
import { AccumulationChartComponent, AccumulationSeriesCollectionDirective, IAccTooltipRenderEventArgs,
AccumulationSeriesDirective, AccumulationTooltip, Inject} from'@syncfusion/ej2-react-charts';
import { accData } from 'datasource.ts';
function App() {
return <AccumulationChartComponent id='charts' tooltip={{
enable: true, format: '${series.name} ${point.x} : ${point.y}',
//fill for tooltip
fill: '#7bb4eb',
//border for tooltip
border: {
width: 2,
color: 'grey'
}
}} highlightColor='red'>
<Inject services={[AccumulationTooltip]} />
<AccumulationSeriesCollectionDirective>
<AccumulationSeriesDirective dataSource={accData} xName='x' yName='y'>
</AccumulationSeriesDirective>
</AccumulationSeriesCollectionDirective>
</AccumulationChartComponent>
};
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));
To customize individual tooltip
Using tooltipRender
event, you can customize a tooltip for particular point. event, you can customize a tooltip for particular point.
import * as React from "react";
import * as ReactDOM from "react-dom";
import { AccumulationChartComponent, AccumulationSeriesCollectionDirective, AccumulationSeriesDirective, AccumulationTooltip, Inject, AccumulationLegend } from '@syncfusion/ej2-react-charts';
import { accData } from 'datasource.ts';
function App() {
const tooltipRender = (args) => {
if (args.point.index === 3) {
args.text = args.point.x + '' + ':' + args.point.y + '' + ' ' + 'customtext';
args.textStyle.color = '#f48042';
}
};
return <AccumulationChartComponent id='charts' tooltip={{ enable: true }} tooltipRender={tooltipRender}>
<Inject services={[AccumulationLegend, AccumulationTooltip]}/>
<AccumulationSeriesCollectionDirective>
<AccumulationSeriesDirective dataSource={accData} xName='x' yName='y'>
</AccumulationSeriesDirective>
</AccumulationSeriesCollectionDirective>
</AccumulationChartComponent>;
}
;
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));
import * as React from "react";
import * as ReactDOM from "react-dom";
import { AccumulationChartComponent, AccumulationSeriesCollectionDirective, IAccTooltipRenderEventArgs,
AccumulationSeriesDirective, AccumulationTooltip, Inject, AccumulationLegend,TooltipSettingsModel}from'@syncfusion/ej2-react-charts';
import { accData } from 'datasource.ts';
function App() {
const tooltipRender: EmitType<IAccTooltipRenderEventArgs> = (args: IAccTooltipRenderEventArgs): void => {
if (args.point.index === 3) {
args.text = args.point.x + '' + ':' + args.point.y + '' + ' ' + 'customtext';
args.textStyle.color = '#f48042';
}
};
return <AccumulationChartComponent id='charts' tooltip={{ enable: true }} tooltipRender={tooltipRender}>
<Inject services={[AccumulationLegend, AccumulationTooltip]} />
<AccumulationSeriesCollectionDirective>
<AccumulationSeriesDirective dataSource={accData} xName='x' yName='y'>
</AccumulationSeriesDirective>
</AccumulationSeriesCollectionDirective>
</AccumulationChartComponent>
};
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));