This is a migrated thread and some comments may be shown as answers.

Get selected Item for Chart

5 Answers 63 Views
ChartView
This is a migrated thread and some comments may be shown as answers.
Rostislaw
Top achievements
Rank 1
Rostislaw asked on 15 Jan 2016, 02:33 PM

Hi *,

I use a Chart Control (see below)

Items is a collection of Objects with different properties like X, Y, Z and each item has an Id.

My question is how can I get the value of the ID Property having a selected Item (DataPoint) by SelectedValue = "X"? 

All what I have is an object of type SelectionChangedEventArgs contains AddedPoints [SelectionChanged] but there I see only the values I render in the Chart.

Please help.

Thx

R

<telerik:BarSeries ShowLabels="True"
          ValueBinding="{Binding ElementName=radListBox, Path=SelectedValue}"                        ItemsSource="{Binding ElementName=ChartDataSource1}"/>        
     ...

<telerik:ChartDataSource  x:Name="ChartDataSource1"                              ItemsSource="{Binding Items ... 

5 Answers, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 19 Jan 2016, 09:54 AM
Hello Rostislaw,

In order to get the view model of the DataPoint you can use its DataItem property.
var myViewModel = e.AddedPoints[0].DataItem as MyViewModel;
var id = myViewModel.ID;
You can also consider using the SelectedPoints collection of the chart.

I hope this information is useful.

Regards,
Martin
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Rostislaw
Top achievements
Rank 1
answered on 25 Jan 2016, 06:44 AM

Martin, thx for the answer, but in my case this property is null

new System.Collections.Generic.Mscorlib_CollectionDebugView<Telerik.Charting.DataPoint>(e.AddedPoints).Items[0].DataItem Value : null

Am I missing something?

Thanks

Rostislaw

 

0
Martin Ivanov
Telerik team
answered on 26 Jan 2016, 10:02 AM
Hi Rostislaw,

The DataItem property will have a value only if the series' ItemsSource is populated. Otherwise, it will be null. I attached a sample project which demonstrates how you can get the DataItem property. Also, you can take a look at the Create Data-Bound Chart help article.

Regards,
Martin
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Rostislaw
Top achievements
Rank 1
answered on 26 Jan 2016, 04:28 PM

Martin,
Thank you for the example. In your sample app the data item property is not null (as expected).
I found out what the problem is but I do not know if this is an expected behavior.
Working code (data item is valid):
<telerik:BarSeries ShowLabels="True"
                        CategoryBinding="Id"           
                        ValueBinding="{Binding SelectedChart}"
                        ItemsSource="{Binding Items}">

                    </telerik:BarSeries>
Code with data item null: 
<telerik:ChartDataSource  x:Name="ChartDataSource1" ItemsSource="{Binding Items}" />

</telerik:BarSeries>

                <telerik:BarSeries ShowLabels="True"   

CategoryBinding="Id"

 ValueBinding="{Binding SelectedChart}"                        

ItemsSource="{Binding ElementName=ChartDataSource1}">                                   

 </telerik:BarSeries>

It looks like the indirection for the ItemSource in XAML does not provide this information.
Anyway for me the problem is solved, thank you for the excellent support.
Rostislaw

0
Accepted
Martin Ivanov
Telerik team
answered on 27 Jan 2016, 08:29 AM
Hi Rostislaw,

This behavior is expected with RadChartView and its sampling source. ChartDataSource samples the original items which means that it combines several data items into one and the DataItem property doesn't know which one to use. This is why it is empty. However, we have a feature request for adding support for this scenario. You can find it in our feedback portal where you can track its status and vote for its implementation.

Regards,
Martin
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
ChartView
Asked by
Rostislaw
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Rostislaw
Top achievements
Rank 1
Share this question
or