Get selected Item for Chart

6 posts, 1 answers
  1. Rostislaw
    Rostislaw avatar
    4 posts
    Member since:
    Nov 2015

    Posted 15 Jan Link to this post

    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 ... 

  2. Martin
    Admin
    Martin avatar
    1101 posts

    Posted 19 Jan Link to this post

    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
  3. UI for WPF is Visual Studio 2017 Ready
  4. Rostislaw
    Rostislaw avatar
    4 posts
    Member since:
    Nov 2015

    Posted 25 Jan in reply to Martin Link to this post

    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

     

  5. Martin
    Admin
    Martin avatar
    1101 posts

    Posted 26 Jan Link to this post

    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
  6. Rostislaw
    Rostislaw avatar
    4 posts
    Member since:
    Nov 2015

    Posted 26 Jan in reply to Martin Link to this post

    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

  7. Answer
    Martin
    Admin
    Martin avatar
    1101 posts

    Posted 27 Jan Link to this post

    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
Back to Top
UI for WPF is Visual Studio 2017 Ready