Search code examples
c#stringoxyplot

Oxyplot: operations on TrackerFormatString arguments


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?


Solution

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