是否可以按字母顺序对 Oxyplot 图表图例中的系列进行排序?

Geo*_*a M 4 wpf charts legend series oxyplot

我想订购legend我的 Oxyplot 图表alphabetical order。里面可能吗Oxyplot

这是我当前的情节:带有图例的情节

我想订购legend我的chart. 我不会先排序绘制数据的方式,因为这意味着太多的条件,而且我希望尽可能保持绘制的通用性。我知道这是一个选择,但我宁愿不采用这种方法。

请告诉我是否可以order alphabetically仅使用 中的图例项目Oxyplot

Jos*_*ose 5

您无法直接修改图例的顺序,但您可以对模型内的系列进行排序,因此您将看到图例按字母顺序排序

有两种方法可以进行排序:

选项 1,简单冒泡排序:

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

选项 2,辅助列表:

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);
Run Code Online (Sandbox Code Playgroud)