Search code examples
oxyplottracker

OxyPlot: How to calculate in TrackerFormatString?


I try to create a line series where not only x- and y-values shall be displayed in the tracker, but one more property:

var series = new LineSeries {TrackerFormatString = "X:{2}\nY:{4}\nZ:{2*(1-4)}"};
series.Points.AddRange(...);

I want another property Z = (1-Y) * X to be displayed in the third row of the tracker. How is this possible?


Solution

  • It is possible by creating a custom

    public class CustomDataPoint : IDataPointProvider
    {
            public double X { get; }
            public double Y { get; }
            public double Z{ get; }
    
            public CustomDataPoint(double x, double y)
            {
                X = x;
                Y = y;
                Z = (1 - Y) * X;
            }
    
            public DataPoint GetDataPoint()
            {
                return new DataPoint(X, Y);
            }
    }
    

    And then add it to the series:

    var series = new LineSeries {TrackerFormatString = "X:{2}\nY:{4}\nZ:{Z}"};
    series.ItemsSource = new [] {
        new CustomDataPoint(...),
        new CustomDataPoint(...)
    };
    

    Note that you need to use the ItemsSource property. You cannot simply add points with series.Point.AddRange(...).