Working with OxyPlot sometimes throw these interesting problems, mainly due to lack of documentation. One of the recent challenges involved creating a custom tooltip, which at the hindsight, was pretty straightforward – only if the developers had continued their effort in completing the documentation.
Alright back to out problem. Oxyplot, by default provides a neat little tooltip for the Data Points in the graph. On most occasions, these are more than enough. On others, you need to tweak the tooltip a bit more to include additional information.
If you browse through the Series Class (LineSeries, BarSeries etc), you would notice it has a property named TrackerFormatString. This is the key to unlocking the tooltip. For sake of example, we will examine the LineSeries in this post. By default, following is the format of TrackerFormatString
"{0}\n{1}: {2:0.###}\n{3}: {4:0.###}"
Where
{0} = Title of Series
{1} = Title of X-Axis
{2} = X Value
{3} = Title of Y-Axis
{4} = Y Value
Some of the basic customization can happen within the TrackerFormatString property itself, say, suppose you want format the values to display only 2 decimal places. This again would be sufficient to cover a lot of cases.
However, at times, you might be interested to display additional information. Unfortunately the DataPoint class has only two properties, which you could use for loading your data. This cripples you desire to add a third value associated with the DataPoint in addition to X & Y, especially if you notice that the DataPoint is a sealed class and Series.Points.Points.AddRange accepts only DataPoints as parameter.
Thankfully, Oxyplot developers has left another door open while closing the DataPoint class. It allows you to assign the Points to Series using the Series.ItemSource Property, which accepts an IEnumerable. The only criteria for your IEnumerable is to the type needs to implement IDataPointProvider.
Let’s go ahead and implement our demo class.
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,string description) { X = x; Y = y; Description = description; } } var points = new CustomDataPoint[] { new CustomDataPoint(5,12,"Info 1"), new CustomDataPoint(6,10,"Info 2"), new CustomDataPoint(7,9,"Info 3"), new CustomDataPoint(8,13,"Info 4"), new CustomDataPoint(9,14,"Info 5"), new CustomDataPoint(10,10,"Info 6") };
And now we will use the ItemSource property to assign the points to Series. We will also use the TrackerFormatString to format our tooltip to display additional information.
var seriesVisible = new OxyPlot.Series.LineSeries(); seriesVisible.ItemsSource = points; seriesVisible.TrackerFormatString = "X={2},\nY={4},\nAdditionalInfo={Description}";
That’s it and we have our additional information displayed in tooltip. Go and run your application
Thank you, this helped a lot
LikeLike
Glad to help you. Cheers
LikeLike
This is super useful for line plots, thank you! However, nothing shows up when applied to a scatter plot and there are no errors. Any suggestions for that?
LikeLike
This is super useful for line plots, thank you! However, nothing shows up when applied to a scatter plot and there are no errors. Any suggestions for that?
LikeLiked by 1 person
Make sure to inherit from IScatterPointProvider and change the GetDataPoint accordingly. Then it should work.
LikeLiked by 2 people
Thanks, it worked!
LikeLike
@J Thank you for the update.
LikeLike