I have a WPF MVVM application with a screen that has a Telerik Chart(RadCartesianChart) on it. The chart can contain more than one series in it. I need to have at least one of the series be clickable and based on the click execute a command in the View Model sending it the point (or at least the X axis) on the line that was clicked.
I believe I need to set the Command and CommandParameter in the codebehind of the page, but not sure how.
In the end I need the click command of the series to execute the PlotChartItemClickedCommand Sending it a parameter containing the X axis data which in my case is the Category.
Below is my code XAML, CodeBehind and VM. Any help would be greatly appreciated
XAML:
I believe I need to set the Command and CommandParameter in the codebehind of the page, but not sure how.
In the end I need the click command of the series to execute the PlotChartItemClickedCommand Sending it a parameter containing the X axis data which in my case is the Category.
Below is my code XAML, CodeBehind and VM. Any help would be greatly appreciated
XAML:
<
telerik:RadCartesianChart
x:Name
=
"RccLineChart"
Width
=
"354"
Height
=
"300"
>
<
telerik:RadCartesianChart.Resources
>
<
DataTemplate
x:Key
=
"PointTemplate"
>
<
Ellipse
Height
=
"6"
Width
=
"6"
Fill
=
"red"
/>
</
DataTemplate
>
</
telerik:RadCartesianChart.Resources
>
<
telerik:RadCartesianChart.HorizontalAxis
>
<
telerik:CategoricalAxis
ShowLabels
=
"False"
Title
=
"Count"
x:Name
=
"LineHorzAxis"
/>
</
telerik:RadCartesianChart.HorizontalAxis
>
<
telerik:RadCartesianChart.VerticalAxis
>
<
telerik:LinearAxis
Maximum
=
"{Binding Path=MaxYValue}"
Minimum
=
"{Binding Path=MinYValue}"
Title
=
"C"
/>
</
telerik:RadCartesianChart.VerticalAxis
>
<
telerik:RadCartesianChart.Series
>
</
telerik:RadCartesianChart.Series
>
</
telerik:RadCartesianChart
>
CodeBehind:
public CodeBehindConstructor()
{
InitializeComponent();
InitialIzeLineChart();
}
private void InitialIzeLineChart()
{
foreach (CartesianSeries series in GetSeries())
{
RccLineChart.Series.Add(series);
}
}
private IEnumerable<
CartesianSeries
> GetSeries()
{
string Line1ItemsSourcePath = string.Empty;
string PointsItemsSourcePath = string.Empty;
string referenceLineItemsSourcePath = string.Empty;
string Line1Resource = string.Empty;
string PointsResource = string.Empty;
Line1Resource = "Line1Template";
PointsResource = "PointTemplate";
CategoricalSeries Line1Series = null;
CategoricalSeries PointSeries = null;
CategoricalSeries referenceLineSeries = null;
Line1Series = new LineSeries();
PointSeries = new PointSeries();
referenceLineSeries = new LineSeries();
Line1ItemsSourcePath = "Line1Data";
PointsItemsSourcePath = "PointsData";
referenceLineItemsSourcePath = "ReferenceLine";
List<
CartesianSeries
> generatedSeries = new List<
CartesianSeries
>();
Line1Series.CategoryBinding = new PropertyNameDataPointBinding("Category");//this is what we want to send to the VM on the click of the chart
Line1Series.ValueBinding = new PropertyNameDataPointBinding("Data");
Line1Series.ShowLabels = false;
Line1Series.CombineMode = ChartSeriesCombineMode.None;
Line1Series.IsHitTestVisible = true;
Line1Series.SetBinding(CategoricalSeries.ItemsSourceProperty, new Binding(Line1ItemsSourcePath));
Line1Series.PointTemplate = this.Resources[Line1Resource] as DataTemplate;
//CommandBinding lineCB = new CommandBinding();
//ICommand lineCommand = new ICommand();
generatedSeries.Add(Line1Series);
PointSeries.CategoryBinding = new PropertyNameDataPointBinding("Category");
PointSeries.ValueBinding = new PropertyNameDataPointBinding("Data");
PointSeries.ShowLabels = true;
PointSeries.CombineMode = ChartSeriesCombineMode.None;
PointSeries.SetBinding(CategoricalSeries.ItemsSourceProperty, new Binding(PointsItemsSourcePath));
PointSeries.PointTemplate = this.Resources[PointsResource] as DataTemplate;
generatedSeries.Add(PointSeries);
referenceLineSeries.CategoryBinding = new PropertyNameDataPointBinding("Category");
referenceLineSeries.ValueBinding = new PropertyNameDataPointBinding("Data");
referenceLineSeries.ShowLabels = false;
referenceLineSeries.CombineMode = ChartSeriesCombineMode.None;
referenceLineSeries.IsHitTestVisible = true;
referenceLineSeries.SetBinding(CategoricalSeries.ItemsSourceProperty, new Binding(referenceLineItemsSourcePath));
referenceLineSeries.PointTemplate = this.Resources[Line1Resource] as DataTemplate;
generatedSeries.Add(referenceLineSeries);
return generatedSeries;
}
ViewModel:
public RelayCommand<
ChartItemClickEventArgs
> PlotChartItemClickedCommand
{
get { return new RelayCommand<
ChartItemClickEventArgs
>(PlotChartItemClickedExecute, AlwaysTrueCanExecute); }
}
private void ProfilePlotChartItemClickedExecute(object e)
{
int xPointClicked = ?????
}