0

I am trying to configure the TrackerFormatString in Oxyplot, so that a particular string is shown for specific values. My code looks something like this:

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

My Problem is that "{4}" in my conditional statement isn't interpreted like the first one: it should contain the Y-Value of the current DataPoint but is interpreted as literal {4}.

Does anyone know how to achieve what I am trying to do?

Tranbi
  • 11,407
  • 6
  • 16
  • 33

1 Answers1

1

One approach to the problem would be define your own custom DataPoint by extending the IDataPointProvider, with additional field to include the custom description. For Example

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

And now, you could modify your addSeriesToGraph method as

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

}
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51
  • Thank you for your input! I guess it's a way to do it... Obviously I would have preferred a solution where I would not have to define a new class (and thus edit all methods referring to those series). But I might resolve to go your way if no one comes with an easier solution :-) – Tranbi May 14 '20 at 12:25