# .NET MAUI Chart Selection Behavior

The Selection behavior of the Chart handles selecting, deselecting, and reporting the selection of either data points or series. In other words, the selection behavior can target data points, series or both if required.

> important This article applies to charts in the `Telerik.Maui.Controls.Compatibility.Chart` namespace. Its `ChartSelectionBehavior` examples do not apply to the new charts in `Telerik.Maui.Controls.Charts`. See the [migration guide's feature comparison](https://www.telerik.com/maui-ui/documentation/controls/charts/migration#features) for feature availability.

## Features

The Chart exposes properties, methods, events, and commands for handling its Selection behavior.

### Properties

The Selection behavior supports the following properties:

* `DataPointSelectionMode`—Gets or sets the `ChartSelectionMode` that controls the selection behavior of the data points within the chart series. The available values are:
  * `None`
  * `Single`
  * `Multiple`
* `SeriesSelectionMode`—Gets or sets the `ChartSelectionMode` that controls the selection behavior of the series within the plot area. The available values are:
  * `None`
  * `Single`
  * `Multiple`
* `SelectedPoints`—Retrieves all the points from all currently selected series within the chart plot area.
* `SelectedSeries`—Retrieves all currently selected series instances within the plot area.
* `Command`—Gets or sets the `ICommand` invoked when selection changes.
* `CommandParameter`—Gets or sets the optional parameter passed to `Command`.

### Methods

The Selection behavior supports the `ClearSelection()` method, which removes the current selection within the chart.

### Events

The Selection behavior supports the `SelectionChanged` event, which occurs when a selection has been made.

### Commands

The Selection behavior supports the `Command` and `CommandParameter` properties.

## Example

The following example shows how the Selection behavior of the Chart works with a `Command`:

**1.** Create the business objects:

```C#
public class CategoricalData
{
    public object Category { get; set; }
    public double Value { get; set; }
}
```

**2.** Create a `ViewModel`:

```C#
public class ViewModel : NotifyPropertyChangedBase
{
    private int counter = 0;
    private string displayCount;
    public ObservableCollection<CategoricalData> Data1 { get; set; }
    public ObservableCollection<CategoricalData> Data2 { get; set; }
    public ICommand IsSelectionChangedCommand { get; }

    public ViewModel()
    {
        this.Data1 = GetCategoricalData1();
        this.Data2 = GetCategoricalData2();
        this.IsSelectionChangedCommand = new Command(this.IncreaseCount);
    }
    public int Counter
    {
        get
        {
            return this.counter;
        }
        set
        {
            this.counter = value;
            this.DisplayCount = $"Command executed {counter} times.";
        }
    }
    public string DisplayCount
    {
        get
        {
            return this.displayCount;
        }
        set
        {
            if (this.displayCount != value)
            {
                this.displayCount = value;
                this.OnPropertyChanged();
            }
        }
    }

    private static ObservableCollection<CategoricalData> GetCategoricalData1()
    {
        var data = new ObservableCollection<CategoricalData>
        {
            new CategoricalData { Category = "Greenings", Value = 52 },
            new CategoricalData { Category = "Perfecto", Value = 60 },
            new CategoricalData { Category = "NearBy", Value = 77 },
            new CategoricalData { Category = "Family", Value = 50 },
            new CategoricalData { Category = "Fresh", Value = 56 },
        };
        return data;
    }

    private static ObservableCollection<CategoricalData> GetCategoricalData2()
    {
        var data = new ObservableCollection<CategoricalData>  
        {
            new CategoricalData { Category = "Greenings", Value = 33 },
            new CategoricalData { Category = "Perfecto", Value = 88 },
            new CategoricalData { Category = "NearBy", Value = 61 },
            new CategoricalData { Category = "Family", Value = 94 },
            new CategoricalData { Category = "Fresh", Value = 72 },
        };
        return data;
    }
    private void IncreaseCount()
    {
        this.Counter++;
    }
}
```

**3.** Declare a `RadCartesianChart` in XAML:

```XAML
<ContentView.BindingContext>
    <local:ViewModel/>
</ContentView.BindingContext>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="0.3*"/>
    </Grid.RowDefinitions>

    <telerik:RadCartesianChart Grid.Row="0">
        <telerik:RadCartesianChart.HorizontalAxis>
            <telerik:CategoricalAxis LabelFitMode="MultiLine" />
        </telerik:RadCartesianChart.HorizontalAxis>
        <telerik:RadCartesianChart.VerticalAxis>
            <telerik:NumericalAxis />
        </telerik:RadCartesianChart.VerticalAxis>
        <telerik:RadCartesianChart.Series>
            <telerik:BarSeries ValueBinding="Value"
                                CategoryBinding="Category"
                                CombineMode="Stack"
                                StackGroupKey="1"
                                AllowSelect="True"
                                ItemsSource="{Binding Data1}" />
            <telerik:BarSeries ValueBinding="Value"
                                CategoryBinding="Category"
                                CombineMode="Stack"
                                StackGroupKey="1"
                                AllowSelect="True"
                                ItemsSource="{Binding Data2}" />
        </telerik:RadCartesianChart.Series>
        <telerik:RadCartesianChart.ChartBehaviors>
            <telerik:ChartSelectionBehavior DataPointSelectionMode="Single"
                                                 Command="{Binding IsSelectionChangedCommand}"
                                                 SeriesSelectionMode="None" />
        </telerik:RadCartesianChart.ChartBehaviors>
    </telerik:RadCartesianChart>

    <Label Grid.Row="2" Text="{Binding DisplayCount}"/>
</Grid>
```

The following image shows how the selection looks:

![Telerik UI for .NET MAUI Cartesian Chart with a selected stacked bar data point](https://www.telerik.com/maui-ui/documentation/assets/c6ba9f0e017999fafc8e9f81acc6ab0e/chart-behaviors-selection.png)

## See Also

* [Chart Track Ball Behavior](https://www.telerik.com/maui-ui/documentation/controls/chart/behaviors/trackball-behavior)
* [Chart Tool Tip Behavior](https://www.telerik.com/maui-ui/documentation/controls/chart/behaviors/tooltip-behavior)
* [Chart Pan And Zoom Behavior](https://www.telerik.com/maui-ui/documentation/controls/chart/behaviors/pan-and-zoom-behavior)
