API
The selection capability is not provided out of the box, but it can be implemented using the seriesClick event. The following code gives an example for the scenario when a click on a series selects it, and a subsequent click on the same series clears the selection. To check whether the series is already selected, its isSelected() method is called.
jQuery
$(function() { $("#chartContainer").dxChart({ // ... onSeriesClick: function (e) { var series = e.target; if (series.isSelected()) { series.clearSelection(); } else { series.select(); } } }); });
Angular
<dx-chart (onSeriesClick)="onSeriesClick($event)"> </dx-chart>
import { DxChartModule } from "devextreme-angular"; // ... export class AppComponent { onSeriesClick (e) { let series = e.target; if (series.isSelected()) { series.clearSelection(); } else { series.select(); } }; } @NgModule({ imports: [ // ... DxChartModule ], // ... })
There are series that consist of points only, for example, bar or financial series. For these series, subscribe to the pointClick event instead of seriesClick.
jQuery
$(function() { $("#chartContainer").dxChart({ // ... onPointClick: function (e) { var series = e.target.series; if (series.isSelected()) { series.clearSelection(); } else { series.select(); } } }); });
Angular
<dx-chart (onPointClick)="onPointClick($event)"> </dx-chart>
import { DxChartModule } from "devextreme-angular"; // ... export class AppComponent { onPointClick (e) { let series = e.target.series; if (series.isSelected()) { series.clearSelection(); } else { series.select(); } }; } @NgModule({ imports: [ // ... DxChartModule ], // ... })
In the previous code examples, selection was cleared of a specific series. If you need to clear selection of all series, call the clearSelection() method in the Chart instance.
jQuery
$("#chartContainer").dxChart("clearSelection");
Angular
import { ..., ViewChild } from "@angular/core"; import { DxChartModule, DxChartComponent } from "devextreme-angular"; // ... export class AppComponent { @ViewChild(DxChartComponent, { static: false }) chart: DxChartComponent; // Prior to Angular 8 // @ViewChild(DxChartComponent) chart: DxChartComponent; clearSelection() { this.chart.instance.clearSelection(); } } @NgModule({ imports: [ // ... DxChartModule ], // ... })
See Also
- Access a Series Using the API
- Handle Events: Angular | Vue | React | jQuery | AngularJS | Knockout | ASP.NET MVC 5 | ASP.NET Core
- Call Methods: Angular | Vue | React | jQuery | AngularJS | Knockout | ASP.NET MVC 5 | ASP.NET Core
- Point Selection Using the API
User Interaction
When a user selects a series, the series changes its style to the one specified by the following objects.
series.selectionStyle
The selection style for an individual series.commonSeriesSettings.%seriesType%.selectionStyle
The selection style for all series of a specific type (line, bar, etc.).commonSeriesSettings.selectionStyle
The selection style for all series in the Chart.
Note that individual settings override type-specific settings which, in turn, override common settings.
jQuery
$(function() { $("#chartContainer").dxChart({ // ... series: { selectionStyle: { // high priority } }, commonSeriesSettings: { bar: { selectionStyle: { // middle priority } }, selectionStyle: { // low priority } } }); });
Angular
<dx-chart ... > <dxi-series ... > <dxo-selection-style> <!-- high priority --> </dxo-selection-style> </dxi-series> <dxo-common-series-settings ... > <dxo-selection-style> <!-- low priority --> </dxo-selection-style> <dxo-bar> <dxo-selection-style> <!-- middle priority --> </dxo-selection-style> </dxo-bar> </dxo-common-series-settings> </dx-chart>
import { DxChartModule } from "devextreme-angular"; // ... export class AppComponent { // ... } @NgModule({ imports: [ // ... DxChartModule ], // ... })
To choose which series elements should be highlighted when a user selects a series, specify the selectionMode option. Just like selectionStyle, this option can be specified for all series in the Chart, for all series of a specific type, or for an individual series. Note also that depending on the series type, the selectionMode option accepts different values. For information about them, visit the Series Types section of the API reference, choose the employed series type, and refer to its selectionMode option description.
jQuery
$(function() { $("#chartContainer").dxChart({ // ... commonSeriesSettings: { bar: { selectionMode: 'allSeriesPoints' // or 'onlyPoint' | 'allArgumentPoints' | 'none' }, line: { selectionMode: 'includePoints' // or 'nearestPoint' | 'excludePoints' | 'none' } } }); });
Angular
<dx-chart ... > <dxo-common-series-settings ... > <dxo-bar selectionMode="allSeriesPoints"> <!-- or 'onlyPoint' | 'allArgumentPoints' | 'none' --> </dxo-bar> <dxo-line selectionMode="includePoints"> <!-- or 'nearestPoint' | 'excludePoints' | 'none' --> </dxo-line> </dxo-common-series-settings> </dx-chart>
import { DxChartModule } from "devextreme-angular"; // ... export class AppComponent { // ... } @NgModule({ imports: [ // ... DxChartModule ], // ... })
By default, only a single series can be in the selected state at a time. If you need to allow multiple series to be in this state, assign "multiple" to the seriesSelectionMode option.
jQuery
$(function() { $("#chartContainer").dxChart({ // ... seriesSelectionMode: 'multiple' // or 'single' }); });
Angular
<dx-chart seriesSelectionMode="multiple"> <!-- or 'single' --> </dx-chart>
import { DxChartModule } from "devextreme-angular"; // ... export class AppComponent { // ... } @NgModule({ imports: [ // ... DxChartModule ], // ... })
See Also
Events
When a user selects a series, the Chart fires the seriesSelectionChanged event that you can handle with a function. If the handling function is not going to be changed during the lifetime of the widget, assign it to the onSeriesSelectionChanged option when you configure the widget. To check whether a series was selected or the selection was cleared, call the isSelected() method of the series.
jQuery
$(function() { $("#chartContainer").dxChart({ // ... onSeriesSelectionChanged: function (e) { var series = e.target; if (series.isSelected()) { // Commands to execute when the series is selected } else { // Commands to execute when the selection is cleared } } }); });
Angular
<dx-chart (onSeriesSelectionChanged)="onSeriesSelectionChanged($event)"> </dx-chart>
import { DxChartModule } from "devextreme-angular"; // ... export class AppComponent { onSeriesSelectionChanged (e) { let series = e.target; if (series.isSelected()) { // Commands to execute when the series is selected } else { // Commands to execute when the selection is cleared } }; } @NgModule({ imports: [ // ... DxChartModule ], // ... })
If you are going to change the event handler at runtime or if you need to attach several handlers to the seriesSelectionChanged event, subscribe to this event using the on(eventName, eventHandler) method. This approach is more typical of jQuery.
var seriesSelectionChangedHandler1 = function (e) { var series = e.target; // First handler of the "seriesSelectionChanged" event }; var seriesSelectionChangedHandler2 = function (e) { var series = e.target; // Second handler of the "seriesSelectionChanged" event }; $("#chartContainer").dxChart("instance") .on("seriesSelectionChanged", seriesSelectionChangedHandler1) .on("seriesSelectionChanged", seriesSelectionChangedHandler2);
See Also
- Handle Events: Angular | Vue | React | jQuery | AngularJS | Knockout | ASP.NET MVC 5 | ASP.NET Core
- Point Selection Events
- Chart API Reference
If you have technical questions, please create a support ticket in the DevExpress Support Center.