I would like to order the legend
of my Oxyplot chart in an alphabetical order
. Is that possible within Oxyplot
?
Here is my current plot: Plot with legend
I would like to order the legend
of my chart
. I would not order the way in which I plot the data first because it would imply too many conditions and I want to keep the plotting as general as possible. I know this would be an option, but I would rather not go with this method.
Please let me know if it is possible to order alphabetically
only the legend items in Oxyplot
?
You cannot modify the order of the legend directly, but you can sort the series inside the model, so you will see the legend alphabetically sorted:
There you have 2 methods to do the sorting:
Option 1, Simple Bubble Sorting:
Series temp;
int length = plotModel.Series.Count;
for (i = 0; i < length; i++)
{
for (int j = i + 1; j < length; j++)
{
if (string.Compare(plotModel.Series[i].Title, plotModel.Series[j].Title) > 0) //true if second string goes before first string in alphabetical order
{
temp = plotModel.Series[i];
plotModel.Series[i] = plotModel.Series[j];
plotModel.Series[j] = temp;
}
}
}
Option 2, Auxiliary List:
List<Series> sortedList = new List<Series>(plotModel.Series);
sortedList.Sort((x, y) => string.Compare(x.Title, y.Title));
plotModel.Series.Clear();
foreach(Series s in sortedList)
plotModel.Series.Add(s);