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.

JavaScript
$(function() {
    $("#chartContainer").dxChart({
        // ...
        onSeriesClick: function (e) {
            var series = e.target;
            if (series.isSelected()) {
                series.clearSelection();
            } else {
                series.select();
            }
        }
    });
});

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.

JavaScript
$(function() {
    $("#chartContainer").dxChart({
        // ...
        onPointClick: function (e) {
            var series = e.target.series;
            if (series.isSelected()) {
                series.clearSelection();
            } else {
                series.select();
            }
        }
    });
});

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.

JavaScript
$("#chartContainer").dxChart("clearSelection");
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.

JavaScript
$(function() {
    $("#chartContainer").dxChart({
        // ...
        series: {
            selectionStyle: {
                // high priority
            }
        },
        commonSeriesSettings: {
            bar: {
                selectionStyle: {
                    // middle priority
                }
            },
            selectionStyle: {
                // low priority
            }
        }
    });
});

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.

JavaScript
$(function() {
    $("#chartContainer").dxChart({
        // ...
        commonSeriesSettings: {
            bar: {
                selectionMode: 'allSeriesPoints' // or 'onlyPoint' | 'allArgumentPoints' | 'none'
            },
            line: {
                selectionMode: 'includePoints' // or 'nearestPoint' | 'excludePoints' | 'none'
            }
        }
    });
});

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.

JavaScript
$(function() {
    $("#chartContainer").dxChart({
        // ...
        seriesSelectionMode: 'multiple' // or 'single'
    });
});

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.

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
            }
        }
    });
});

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.

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