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) {
        const series = e.target;
        if (series.isSelected()) {
            series.clearSelection();
        } else {
            series.select();
        }
    }
}
@NgModule({
    imports: [
        // ...
        DxChartModule
    ],
    // ...
})
Vue
App.vue
<template> 
    <DxChart
        @series-click="onSeriesClick"
    >
    </DxChart>
</template>

<script>
import DxChart from 'devextreme-vue/chart';

export default {
    components: {
        DxChart
    },
    methods: {
        onSeriesClick (e) {
           const series = e.target;
           if (series.isSelected()) {
               series.clearSelection();
           } else {
               series.select();
           }
        }
    }
}
</script>
React
App.js
import React from 'react';
import Chart from 'devextreme-react/chart';

class App extends React.Component {
    render() {
        return (
            <Chart
                onSeriesClick={onSeriesClick}
            >
            </Chart>
        );
    }
}

function onSeriesClick (e) {
    const series = e.target;
    if (series.isSelected()) {
        series.clearSelection();
    } else {
        series.select();
    }
}

export default App;

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) {
        const series = e.target.series;
        if (series.isSelected()) {
            series.clearSelection();
        } else {
            series.select();
        }
    }
}
@NgModule({
    imports: [
        // ...
        DxChartModule
    ],
    // ...
})
Vue
App.vue
<template> 
    <DxChart
        @point-click="onPointClick"
    >
    </DxChart>
</template>

<script>
import DxChart from 'devextreme-vue/chart';

export default {
    components: {
        DxChart
    },
    methods: {
        onPointClick (e) {
            const series = e.target.series;
            if (series.isSelected()) {
                series.clearSelection();
            } else {
                series.select();
            }
        }
    }
}
</script>
React
App.js
import React from 'react';
import Chart from 'devextreme-react/chart';

class App extends React.Component {
    render() {
        return (
            <Chart
                onPointClick={onPointClick}
            >
            </Chart>
        );
    }
}

function onPointClick (e) {
    const series = e.target.series;
    if (series.isSelected()) {
        series.clearSelection();
    } else {
        series.select();
    }
}

export default App;

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, { static: false }) chart: DxChartComponent;
    // Prior to Angular 8
    // @ViewChild(DxChartComponent) chart: DxChartComponent;
    clearSelection() {
        this.chart.instance.clearSelection();
    }
}
@NgModule({
    imports: [
        // ...
        DxChartModule
    ],
    // ...
})
Vue
App.vue
   <template> 
       <DxChart ref="chart">
       </DxChart>
   </template>

<script>
import DxChart from 'devextreme-vue/chart';

export default {
    components: {
        DxChart
    },
    methods: {
        clearSelection () {
            return this.$refs.chart.instance.clearSelection();
        }
    }
}
</script>
React
App.js
import React from 'react';
import Chart from 'devextreme-react/chart';

class App extends React.Component {
    constructor(props) {
        super(props);
        this.chartRef = React.createRef();
    }
    render() {
        return (
            <Chart ref={this.chartRef}></Chart>
        );
    }
    get chart() {
        return this.chartRef.current.instance;
    }
    clearSelection () {
        return this.chart.clearSelection();
    }
}

export default App;
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.selectionStyle
    The selection style for all series in the Chart.

Individual settings override common settings.

jQuery
JavaScript
$(function() {
    $("#chartContainer").dxChart({
        // ...
        series: {
            selectionStyle: {
                // high priority
            }
        },
        commonSeriesSettings: {
            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-common-series-settings>
</dx-chart>
import { DxChartModule } from "devextreme-angular";
// ...
export class AppComponent {
    // ...
}
@NgModule({
    imports: [
        // ...
        DxChartModule
    ],
    // ...
})
Vue
App.vue
<template> 
    <DxChart ... >
        <DxSeries ... >
            <DxSelectionStyle>
                <!-- high priority -->
            </DxSelectionStyle>
        </DxSeries>
        <DxCommonSeriesSettings ... >
            <DxSelectionStyle>
                <!-- low priority -->
            </DxSelectionStyle>
        </DxCommonSeriesSettings>
    </DxChart>
</template>

<script>
import DxChart, {
    DxSeries,
    DxCommonSeriesSettings,
    DxSelectionStyle
} from 'devextreme-vue/chart';

export default {
    components: {
        DxChart,
        DxSeries,
        DxCommonSeriesSettings,
        DxSelectionStyle
    }
}
</script>
React
App.js
import React from 'react';
import Chart, {
    Series,
    CommonSeriesSettings,
    SelectionStyle
} from 'devextreme-react/chart';

class App extends React.Component {
    render() {
        return (
            <Chart ... >
                <Series ... >
                    <SelectionStyle>
                        <!-- high priority -->
                    </SelectionStyle>
                </Series>
                <CommonSeriesSettings ... >
                    <SelectionStyle>
                        <!-- low priority -->
                    </SelectionStyle>
                </CommonSeriesSettings>
            </Chart>
        );
    }
}

export default App;

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 or for an individual series. 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({
        // ...
        series: [{
            // ...
            type: "bar",
            hoverMode: "allSeriesPoints" // or "onlyPoint" | "allArgumentPoints" | "none"
        }, {
            type: "line",
            hoverMode: "includePoints" // or "nearestPoint" | "excludePoints" | "none"
        }]
    });
});
Angular
HTML
TypeScript
<dx-chart ... >
    <dxi-series
        type="bar"
        selectionMode="allSeriesPoints"> <!-- or "onlyPoint" | "allArgumentPoints" | "none" -->
    </dxi-series>
    <dxi-series
        type="line"
        selectionMode="includePoints"> <!-- or "nearestPoint" | "excludePoints" | "none" -->
    </dxi-series>
</dx-chart>
import { DxChartModule } from "devextreme-angular";
// ...
export class AppComponent {
    // ...
}
@NgModule({
    imports: [
        // ...
        DxChartModule
    ],
    // ...
})
Vue
App.vue
<template> 
    <DxChart ... >
        <DxSeries
            type="bar"
            selection-mode="allSeriesPoints" <!-- or "onlyPoint" | "allArgumentPoints" | "none" -->                
        />
        <DxSeries
            type="line"
            selection-mode="includePoints" <!-- or "nearestPoint" | "excludePoints" | "none" -->
        />
    </DxChart>
</template>

<script>
import DxChart, {
    DxSeries
} from 'devextreme-vue/chart';

export default {
    components: {
        DxChart,
        DxSeries
    }
}
</script>
React
App.js
import React from 'react';
import Chart, {
    Series
} from 'devextreme-react/chart';

class App extends React.Component {
    render() {
        return (
            <Chart ... >
                <Series
                    type="bar"
                    selectionMode="allSeriesPoints" <!-- or "onlyPoint" | "allArgumentPoints" | "none" -->
                />
                <Series
                    type="line"
                    selectionMode="includePoints" <!-- or "nearestPoint" | "excludePoints" | "none" -->
                />
            </Chart>
        );
    }
}

export default App;

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
    ],
    // ...
})
Vue
App.vue
<template> 
    <DxChart 
        ...
        series-selection-mode="multiple"> <!-- or 'single' -->
     >
    </DxChart>
</template>

<script>
import DxChart from 'devextreme-vue/chart';

export default {
    components: {
        DxChart
    }
}
</script>
React
App.js
import React from 'react';
import Chart from 'devextreme-react/chart';

class App extends React.Component {
    render() {
        return (
            <Chart 
                ...
                seriesSelectionMode="multiple"> <!-- or 'single' -->
            >
            </Chart>
        );
    }
}

export default App;

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) {
        const 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
    ],
    // ...
})
Vue
App.vue
<template> 
    <DxChart
        @series-selection-changed="onSeriesSelectionChanged"
    >
    </DxChart>
</template>

<script>
import DxChart from 'devextreme-vue/chart';

export default {
    components: {
        DxChart
    },
    methods: {
        onSeriesSelectionChanged (e) {
            const series = e.target;
            if (series.isSelected()) {
                // Commands to execute when the series is selected
            } else {
                // Commands to execute when the selection is cleared
            }
        }
    }
}
</script>
React
App.js
import React from 'react';
import Chart from 'devextreme-react/chart';

class App extends React.Component {
    render() {
        return (
            <Chart
                onSeriesSelectionChanged={onSeriesSelectionChanged}
            >
            </Chart>
        );
    }
}

function onSeriesSelectionChanged (e) {
    const series = e.target;
    if (series.isSelected()) {
        // Commands to execute when the series is selected
    } else {
        // Commands to execute when the selection is cleared
    }
}

export default App;

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