Allow me to start with stating everything works beautifully without trying to sort my legend. See attached.
However; sorting the legend has been a problem. I created a LegendDataConverter class (IValueConverter) which felt like the appropriate method to achieve a sorted legend, but all I have managed to do is cause the Legend to stop working. I.e. an empty legend.
My SciChart instance uses a custom LegendModifier. Each series in my chart is a custom class derived from “IRenderableSeriesViewModel”, named “CustomLineRenderableSeriesViewModel”. There are several bindable properties in my derived class. These properties are used in XAML to customize the multiple series displayed.
My intent is to use those same properties in the Legend converter class to sort the items displayed in the legend. I’ve gone through several iterations of changes and have still not been successful. Here is the latest implementation, which does not provide appropriate results.
public class LegendDataConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is ChartDataObject)
{
IEnumerable<SeriesInfo> seriesInfo = (value as ChartDataObject).SeriesInfo;
// Extract the RenderableSeries from SeriesInfo
var renderableSeries = seriesInfo.Select(si => si.RenderableSeries).OfType<IRenderableSeries>().ToList();
// Separate the series based on the custom properties
var passingLineSeries = renderableSeries.FirstOrDefault(rs =>
((CustomLineRenderableSeriesViewModel)rs.DataContext).IsPassingLine &&
((CustomLineRenderableSeriesViewModel)rs.DataContext).IsSecondAxis);
var secondaryAxisSeries = renderableSeries.FirstOrDefault(rs =>
!((CustomLineRenderableSeriesViewModel)rs.DataContext).IsPassingLine &&
((CustomLineRenderableSeriesViewModel)rs.DataContext).IsSecondAxis);
var otherSeries = renderableSeries.Where(rs =>
!((CustomLineRenderableSeriesViewModel)rs.DataContext).IsSecondAxis).ToList();
// Create the ordered list
ObservableCollection<IRenderableSeriesViewModel> ordered = new ObservableCollection<IRenderableSeriesViewModel>();
otherSeries.ForEach(s => ordered.Add((CustomLineRenderableSeriesViewModel)s.DataContext));
if (secondaryAxisSeries != null)
{
ordered.Add((CustomLineRenderableSeriesViewModel)secondaryAxisSeries.DataContext);
}
if (passingLineSeries != null)
{
ordered.Add((CustomLineRenderableSeriesViewModel)passingLineSeries.DataContext);
}
return ordered;
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
I welcome any suggestions.
- Chris Kirkman asked 2 weeks ago
-
Hi Chris, Thanks for your inquiry. I’ll discuss the provided details with our team and will get back to you with an update. Kind regards, Lex, SciChart Technical Support Engineer
- You must login to post comments
Hi Chris,
Hope you are doing well.
I discussed your inquiry with our team.
Your approach is reasonable and valid. The issue is that you return an incorrect object from the ValueConverter. You should return ChartDataObject instead of a collection of RenderableSeries.
Please use the following constructor to create ChartDataObject using a collection of RenderableSeries:
SciChart WPF Documentation – ChartDataObject Constructor(IEnumerable_SeriesInfo_)
Hope this helps,
Kind regards,
Lex
- Lex answered 1 week ago
- You must login to post comments
Please login first to submit.