I am dynamically adding and removing items from an observablecollection of Irenderable series in my viewmodel, and binding it to the renderableseries on the chart surface. However, when I add and remove items that I want to be ordered, and are appropriately ordered in the collection, they are not ordered on the legend.
Is there a way to reorder the series in the legend (MVVM solution would be great)?
You can replicate the issue (or the symptom), by toggling the visibilty of the series in the below example by clicking the checkbox within the legend and then switching the getlegenddatafrom to visible series, and then switch back. (no longer in ABC order).
Thanks,
Jason
- morseja asked 9 years ago
- You must login to post comments
Hi Jason,
I’ve just pushed a change to our v3.1 Nightly build (r5373)
This just makes some base methods in the LegendModifier protected virtual, or public virtual. Now you can override the UpdateLegend() method as follows:
public class LegendModifierEx : LegendModifier
{
/// <summary>
/// Refreshes the legend with up-to-date <see cref="SeriesInfo" /> with series names, latest values
/// </summary>
public override void UpdateLegend()
{
if (IsEnabled && IsAttached)
{
// Get the series to check. In the base this checks Visible, Invisible, Selected series etc...
// against legend flag GetLegendDataFor
// THIS IS THE LINE YOU NEED TO CHANGE IF YOU WANT TO ALTER WHICH SERIES ARE IN THE LEGEND
var allSeries = ParentSurface.RenderableSeries.Where(IsSeriesValid);
// Get the series info collection for the renderable series
var newSerieesInfoCollection = GetSeriesInfo(allSeries);
var oldSeriesInfoCollection = SeriesData.SeriesInfo;
// We need this trick to prevent visibility checkboxes flickering in .NET4.5
// Do a diff on the Old and New seriesinfo objects
var newRenderableSeries = newSerieesInfoCollection.Select(x => x.RenderableSeries).ToArray();
oldSeriesInfoCollection.RemoveWhere(info => !newRenderableSeries.Contains(info.RenderableSeries));
// Update old elements and add new elements
foreach (var seriesInfo in newSerieesInfoCollection)
{
var newSeriesInfo = seriesInfo;
var oldSeriesInfo =
oldSeriesInfoCollection.FirstOrDefault(
x => x.RenderableSeries.Equals(newSeriesInfo.RenderableSeries));
if (oldSeriesInfo != null)
{
// This replaces data on the oldSeriesInfo instance with data from newSeriesInfo
// Rather than changing the instance, this results in a less flickery, less performance intensive legend
UpdateSeriesInfo(oldSeriesInfo, newSeriesInfo);
}
else
{
oldSeriesInfoCollection.Add(newSeriesInfo);
}
}
}
}
}
Altering the highlighted line to do an OrderBy() will allow you to order your legend items as you wish. Also, you can use this method to further customize the legend output such as getting LegendData for only certain series.
Try getting the nightly build and let us know if it works. This change will also be merged to v3.2 and pushed in the next v3.2 release.
Best regards,
Andrew
- Andrew Burnett-Thompson answered 9 years ago
- last edited 4 months ago
- You must login to post comments
Please login first to submit.