Oxyplot:对TrackerFormatString参数的操作

问题描述 投票:0回答:1

我长期潜伏,这是我的第一个问题。我只有很少的C#经验,所以请放轻松!

我正在尝试在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}”不像第一个那样解释:它应包含当前DataPoint的Y值,但应解释为文字“ {4}”。

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

如果您知道更简单的方法,我也会考虑的! ;-)

最诚挚的问候,

c# string oxyplot
1个回答
0
投票

解决该问题的一种方法是,通过扩展IDataPointProvider来定义自己的自定义DataPoint,并使用其他字段包括自定义描述。例如

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,
        }
    ); 

}
© www.soinside.com 2019 - 2024. All rights reserved.