DevExtreme jQuery/JS - Selection

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
JavaScript
$(function() {
    $("#chartContainer").dxChart({
        // ...
        onSeriesClick: function (e) {
            var series = e.target;
            if (series.isSelected()) {
                series.clearSelection();
            } else {
                series.select();
            }
        }
    });
});
Angular
HTML
TypeScript
<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
JavaScript
$(function() {
    $("#chartContainer").dxChart({
        // ...
        onPointClick: function (e) {
            var series = e.target.series;
            if (series.isSelected()) {
                series.clearSelection();
            } else {
                series.select();
            }
        }
    });
});
Angular
HTML
TypeScript
<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
JavaScript
$("#chartContainer").dxChart("clearSelection");
Angular
TypeScript
import { ..., ViewChild } from '@angular/core';
import { DxChartModule, DxChartComponent } from 'devextreme-angular';
// ...
export class AppComponent {
    @ViewChild(DxChartComponent) chart: DxChartComponent;
    clearSelection() {
        this.chart.instance.clearSelection();
    }
}
@NgModule({
    imports: [
        // ...
        DxChartModule
    ],
    // ...
})
See Also

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
JavaScript
$(function() {
    $("#chartContainer").dxChart({
        // ...
        series: {
            selectionStyle: {
                // high priority
            }
        },
        commonSeriesSettings: {
            bar: {
                selectionStyle: {
                    // middle priority
                }
            },
            selectionStyle: {
                // low priority
            }
        }
    });
});
Angular
HTML
TypeScript
<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
JavaScript
$(function() {
    $("#chartContainer").dxChart({
        // ...
        commonSeriesSettings: {
            bar: {
                selectionMode: 'allSeriesPoints' // or 'onlyPoint' | 'allArgumentPoints' | 'none'
            },
            line: {
                selectionMode: 'includePoints' // or 'nearestPoint' | 'excludePoints' | 'none'
            }
        }
    });
});
Angular
HTML
TypeScript
<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
    ],
    // ...
})

View Demo

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
JavaScript
$(function() {
    $("#chartContainer").dxChart({
        // ...
        seriesSelectionMode: 'multiple' // or 'single'
    });
});
Angular
HTML
TypeScript
<dx-chart 
    seriesSelectionMode="multiple"> <!-- or 'single' -->
</dx-chart>
import { DxChartModule } from 'devextreme-angular';
// ...
export class AppComponent {
    // ...
}
@NgModule({
    imports: [
        // ...
        DxChartModule
    ],
    // ...
})

View Demo

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
JavaScript
$(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
HTML
TypeScript
<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.

JavaScript
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);
NOTE
There are series that consist of points only, for example, bar or financial series. For these series, subscribe to the pointSelectionChanged event instead of seriesSelectionChanged (see the Point Selection Events topic).
See Also