Hello,
Is there an easy why to show always the last value from the series in the legend?
Hello Bin,
Thank you for posting to Infragistics Community!
I have been looking into your question and created a small sample with a XamDataChart with ColumnSeries demonstrating how your requirement about only showing the last item’s value in the legend can be achieved.
A custom LegendItemTemplate DataTemplate can be created for the target series. For the purposes of the example, the template is really simple, however it can be styled as per your taste. The TextBlock within it is bound to the corresponding Series’ ItemsSource. With the help of a converter, returning the last item of a collection, the last object’s target property value is accessed and a custom string is returned from the Convert method. This is what is finally shown in the legend. This is simply one approach to how this can be achieved, for example you could keep the last item of the collection in your view model or local data, so that the binding does not use a converter.
Please, test the below attached sample on your side and if you require any further assistance on the matter, please let me know.
Sincerely,Bozhidara PachilovaAssociate Software Developer
2308.XDCLegendLastSeriesItem.zip
Thanks for the solution. This works perfectly.
I also want to update the last item value in the legend when a new Item is added. I am using a ObservableCollectionExtended datasource. In my scenary, my chart is refrehsed every minute with the current data.
What is the easiest way to trigger a refresh of the legend item?
Thank you for following up.
I have modified the previous sample, so that a new item is added to the data collection every two seconds. Updating the legend with each new item can be achieved if your data context class implements the INotifyPropertyChanged interface and the NotifyPropertyChanged event with the name of the corresponding member is raised whenever a new item is added to the collection. The binding has to always be modified with an UpdateSourceTrigger set to PropertyChanged:
<TextBlock Text="{Binding Path=DataContext.Data,RelativeSource={RelativeSource AncestorType={x:Type Window}}, Converter={StaticResource DataSourceToLastItemConverter}, UpdateSourceTrigger=PropertyChanged, StringFormat='#'}" HorizontalAlignment="Right" />
Please, check out the updated sample and let me know of any other questions.
4682.XDCLiveData.zip
This does actually not work, since I have more than one series in a chart. I just bind my data to the series ItemsSource (Path="Series.ItemsSource" UpdateSourceTrigger="PropertyChanged).
Any other idea?
Yes, it really depends on how your project is structured. In the previous sample, the configuration did allow for easier binding to a single collection within the ViewModel. PropertyChanged UpdateSourceTrigger would not work in your case as it observes for changes in the Series.ItemsSource property. When simply adding an item to the collection, the ItemsSource itself is not changed, so this is why the binding is not updated. Provided that you have a mechanism, indicating when a new item is added, or simply doing it as it is, the most straightforward approach to update the binding is to reset the ItemsSource collection by firstly setting it to some other value (i.e.null) and then back to the target collection:
this.xdc.Series[0].ItemsSource = null; this.xdc.Series[0].ItemsSource = (this.DataContext as ViewModel).Data;
Here “xdc” is the Name of the XamDataChart.
Additionally, as I mentioned in my initial reply, the binding in the LegendItemTemplate does not necessarily need to be set to the Series’ ItemsSource. For example, you could store the last item of the collection in your view model or local data and bind directly to it. In this way, the binding will not have to use a converter either. I also think that this would be the better approach.