How to select the series through legend?
This article explains how to select the series through the legend in the React Chart.
Select the series through legend
The series visibility collapses based on the legend visibility. By default, legend click enables you to collapse the series visibility. On other hand, if you need to select a series through legend click, disable the toggleVisibility. By using the selection, you need to specify the selectionMode.
In the following code example, the toggle visibility is disabled.
Index.js
import { createRoot } from 'react-dom/client';
import * as React from 'react';
import { ChartComponent, SeriesCollectionDirective, Highlight, SeriesDirective, Inject, Legend, Category, LineSeries,
Tooltip, Selection } from '@syncfusion/ej2-react-charts';
const Line = () => {
return (
<ChartComponent
id="charts"
primaryXAxis={{
interval: 1,
valueType: 'Category'
}}
primaryYAxis={{
minimum: 0,
maximum: 400,
interval: 100,
labelFormat: '${value}'
}}
legendSettings={{
visible: true,
enableHighlight: true,
toggleVisibility: false
}}
title="Family Expenses for Year"
tooltip={{ enable: true }}
selectionMode="Series"
>
<Inject
services={[ LineSeries, Category, Legend, Tooltip, Highlight, Selection ]}
/>
<SeriesCollectionDirective>
<SeriesDirective
dataSource={chartData}
xName="x"
yName="y"
name="John"
type="Line"
/>
<SeriesDirective
dataSource={chartData}
xName="x"
yName="y1"
name="Peter"
type="Line"
/>
</SeriesCollectionDirective>
</ChartComponent>
);
};
export default Line;
const root = createRoot(document.getElementById('sample'));
root.render(<Line />);
The following screenshot portrays the results of the code snippet mentioned above.
Screenshot:
Before Legend Click
After Legend Click
Refer to the working sample for additional details and implementation: View Sample in Stackblitz
Conclusion
We hope you enjoyed learning how to select the series through the legend in the React Chart Component.
You can refer to our React Charts feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our React Charts 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, BoldDesk Support, or feedback portal. We are always happy to assist you!