NativeScript Core

Chart Selection

If you followed the getting started article, you now know how to create a chart and add it to a NativeScript page. In this article, you will learn how to enable selection of series or selection of single data points.

Getting Started

You can make your charts more interactive by enabling selection. When selection is enabled you can select, deselect and handle the selection events of either the data points or the series in RadCartesianChart and RadPieChart.

The selection can be set on the whole chart with the seriesSelectionMode and pointSelectionMode properties. The values for these properties can be:

  • None - Selection is disabled.
  • Single - Only one series/point can be selected at a time.
  • Multiple - More one series/point can be selected simultaneously.

For finer tuning of the selection behavior you can also set the selectionMode property of each series. The individual series selection mode enables you to specify multiple data point selection for one series, single data point selection for another series and even disable selection for a third series all the same time. With the combination of the chart selection properties and series selectionMode property, any selection scenario can be implemented. The series selectionMode property can have the following values:

  • None - Selection is disabled.
  • NotSet - The selection mode will be determined by chart's properties.
  • DataPoint - Only one point can be selected at a time.
  • DataPointMultiple - More than one point can be selected at a time.
  • Series - All data points are selected simultaneously.

Example 1: Enabling Data Point Selection

<chart:RadPieChart.series>
    <chart:PieSeries
        selectionMode="DataPoint"
        outerRadiusFactor="0.8"
        expandRadius="0.2"
        showLabels="true"
        items="{{ sourceItems }}"
        valueProperty="Sales" >
        <chart:PieSeries.labelStyle>
            <chart:PointLabelStyle margin="15"/>
        </chart:PieSeries.labelStyle>
    </chart:PieSeries>
</chart:RadPieChart.series>

Example 2: Enabling Series Selection

<chart:LineSeries selectionMode="Series" showLabels="true"
    items="{{ categoricalSource }}" categoryProperty="Country" valueProperty="Amount"/>

Selection Events

To notify you when the selection state of an item is changed, the chart exposes the following events:

  • seriesSelected - Fired after a series is selected. The event data argument provides information about the event name, the chart that the series belongs to and the ChartSeries class instance for the selected series
  • seriesDeselected - Fired after a series is deselected. The event data argument provides information about the event name, the chart that the series belongs to and the ChartSeries class instance for the selected series
  • pointSelected - Fired after a data point is selected. The event data argument provides information about the event name, the chart that the data point belongs to, the point index and the native data point class instances(TKChartData for iOS, DataPoint for Android)
  • pointDeselected - Fired after a data point is deselected. The event data argument provides information about the event name, the chart that the data point belongs to, the point index and the native data point class instances(TKChartData for iOS, DataPoint for Android)

References

Want to see this scenario in action? Check our SDK Examples repository on GitHub. You will find this and many other practical examples with NativeScript UI.

Examples used in this article:

Related articles you might find useful: