This help topic demonstrates how you can make your charts more interactive by enabling a selection behavior.
You can alter the selection mode by altering the chart seriesSelectionMode
and dataPointSelectionMode
properties with the following value:
In addition you can finely tune the selection by setting the selection
property of each series:
This way each series can have a separate selection mode. One series can select a single point, another series can select multiple points and a third series can
be selected as a whole series if necessary. Please note that the series selection has higher priority than the chart selection.
Use the selectedSeries
and selectedPoints
properties to get currently selected series or points respectively.
for (TKChartSeries *series in chart.selectedSeries) {
NSLog(@"selected series at index: %d", (int)series.index);
}
for (TKChartSelectionInfo *info in chart.selectedPoints) {
NSLog(@"selected point at index %d from series %d", (int)info.dataPointIndex, (int)info.series.index);
}
for series in chart.selectedSeries {
print("selected series at index: \(series.index)")
}
for info in chart.selectedPoints {
print("selected point at index \(info.dataPointIndex) from series \(info.series!.index)")
}
foreach (TKChartSeries series in chart.SelectedSeries) {
Console.WriteLine ("selected series at index {0}", series.Index);
}
foreach (TKChartSelectionInfo info in chart.SelectedPoints) {
Console.WriteLine ("selected point at index {0} from series {1}", info.DataPointIndex, info.Series.Index);
}
The isSelected
property of TKChartSeries indicates whether the series is selected.
You can determine whether a selection is changed by adopting TKChartDelegate
protocol and implementing one the following methods:
- (void)chart:(TKChart *)chart didSelectSeries:(TKChartSeries *)series
{
// Here you can perform the desired action when the selection is changed.
}
- (void)chart:(TKChart *)chart didSelectPoint:(id<TKChartData>)point inSeries:(TKChartSeries *)series atIndex:(NSInteger)index
{
// Here you can perform the desired action when the selection is changed.
}
- (void)chart:(TKChart *)chart didDeselectSeries:(TKChartSeries *)series
{
// Here you can perform the desired action when the selection is changed.
}
- (void)chart:(TKChart *)chart didDeselectPoint:(id<TKChartData>)point inSeries:(TKChartSeries *)series atIndex:(NSInteger)index
{
// Here you can perform the desired action when the selection is changed.
}
func chart(_ chart: TKChart, didSelect series: TKChartSeries) {
// Here you can perform the desired action when the selection is changed.
}
func chart(_ chart: TKChart, didSelectPoint point: TKChartData, in series: TKChartSeries, at index: Int) {
// Here you can perform the desired action when the selection is changed.
}
func chart(_ chart: TKChart, didDeselect series: TKChartSeries) {
// Here you can perform the desired action when the selection is changed.
}
func chart(_ chart: TKChart, didDeselectPoint point: TKChartData, in series: TKChartSeries, at index: Int) {
// Here you can perform the desired action when the selection is changed.
}
class ChartDelegate: TKChartDelegate
{
public override void SeriesSelected (TKChart chart, TKChartSeries series)
{
// Here you can perform the desired action when the selection is changed.
}
public override void PointSelected (TKChart chart, TKChartData point, TKChartSeries series, nint index)
{
// Here you can perform the desired action when the selection is changed.
}
public override void SeriesDeselected (TKChart chart, TKChartSeries series)
{
// Here you can perform the desired action when the selection is changed.
}
public override void PointDeselected (TKChart chart, TKChartData point, TKChartSeries series, nint index)
{
// Here you can perform the desired action when the selection is changed.
}
}
In addition, you can change the selection programmatically by calling the select
method in the following manner:
[chart select:[[TKChartSelectionInfo alloc] initWithSeries:chart.series[0] dataPointIndex:0]];
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
chart.select(TKChartSelectionInfo(series: chart.series[0], dataPointIndex: 0))
}
public override void ViewDidAppear (bool animated)
{
base.ViewDidAppear (animated);
chart.Select (new TKChartSelectionInfo (chart.Series [0], 0));
}
Note that you can clear the selection by passing nil value to the series
argument.