Oxyplot:对 TrackerFormatString 参数的操作

Oxyplot: operations on TrackerFormatString arguments

我正在尝试在 Oxyplot 中配置 TrackerFormatString,以便为特定值显示特定字符​​串。 我的代码看起来像这样:

 private void addSeriesToGraph(KeyValuePair<Tuple<string, List<KeyValuePair<long, string>>>, ConcurrentStack<DataPoint>> series)
{
    Graph.Series.Add(
        new LineSeries()
        {
            TrackerFormatString = "{0}\n{1}: {2:hh\:mm\:ss\.fff}\nY: {4}" 
                + ((series.Key.Item2.Count > 0)? " (" + series.Key.Item2.First(x => x.Key.ToString() == "{4}").Value + ")" : ""),
            Title = series.Key.Item1,
            ItemsSource = series.Value,
        }
    );
}

我的问题是我的条件语句中的“{4}”不像第一个那样解释:它应该包含当前数据点的 Y 值,但被解释为文字 {4}

有人知道如何实现我想要做的事情吗?

解决该问题的一种方法是通过扩展 IDataPointProvider 来定义您自己的自定义数据点,并使用附加字段来包含自定义描述。例如

public class CustomDataPoint : IDataPointProvider
{
    public double X { get; set; }
    public double Y { get; set; }
    public string Description { get; set; }
    public DataPoint GetDataPoint() => new DataPoint(X, Y);

    public CustomDataPoint(double x, double y)
    {
        X = x;
        Y = y;
    }
}

现在,您可以将 addSeriesToGraph 方法修改为

private void addSeriesToGraph(KeyValuePair<Tuple<string, List<KeyValuePair<long, string>>>, ConcurrentStack<CustomDataPoint>> series)
{
    foreach (var dataPoint in series.Value)
    {
        dataPoint.Description = series.Key.Item2.Any() ? series.Key.Item2.First(x => x.Key == dataPoint.Y).Value:string.Empty;
    }
    Graph.Series.Add(
        new OxyPlot.Series.LineSeries()
        {
            TrackerFormatString = "{0}\n{1}: {2:hh\:mm\:ss\.fff}\nY: {4} {Description}",
            Title = series.Key.Item1,
            ItemsSource = series.Value,
        }
    ); 

}