I'm currently trying to add a single point to an OxyPlot graph. I want to use the PointAnnotation class (since it's a single point based on an X and Y value that should be bound to real values in the model). The problem is that the point that I've created through the PointAnnotation is not actually showing up, at all. I've looked at the OxyPlot documentation, and I think I'm doing it right. I've also looked at some other questions (this and this seemed the most relevant), but none of them apply, since I'm trying to add a single point and bind the X and Y coordinates to values in the model. All I want is a nice symbol marking a particular point, and I want to be able to do it in the XAML in order to use binding for automatic updates. Is this not possible?
I'm trying this, in the XAML (this is a snippet of the larger code, I only included the relevant pieces):
<UserControl x:Class="MyNamespace:MyControl"
xmlns:oxy="http://oxyplot.org/wpf"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<oxy:Plot>
<oxy:Plot.Annotations>
<oxy:PointAnnotation X="{Binding Path=XVal, Mode=OneWay}" Y="{Binding Path=YVal, Mode=OneWay}" Shape="Star" Stroke="Black" Fill="Black" Visibility="Visible"/>
</oxy:Plot.Annotations>
</oxy:Plot>
</UserControl>
In the code-behind:
public partial class MyControl : UserControl
{
public MyControl()
{
Model = new MyModel();
DataContext = Model;
InitializeComponent();
}
// Other stuff...
}
And in the model (again, only the relevant part):
public class MyModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
private double _xVal, _yVal;
public double XVal
{
get { return _xVal; }
private set
{
if (_xVal == value) return;
_xVal = value;
OnPropertyChanged("XVal");
}
}
public double YVal
{
get { return _YVal; }
private set
{
if (_yVal == value) return;
_yVal = value;
OnPropertyChanged("YVal");
}
}
// Constructor, etc...
}
You need to add Axes
to your Plot
and you may also be missing some parameters in your PointAnnotation
. Take a look:
<oxy:Plot>
<oxy:Plot.Axes>
<oxy:LinearAxis Position="Bottom" Minimum="0" Maximum="100"></oxy:LinearAxis>
<oxy:LinearAxis Position="Left" Minimum="0" Maximum="100"></oxy:LinearAxis>
</oxy:Plot.Axes>
<oxy:Plot.Annotations>
<oxy:PointAnnotation X="{Binding Path=XVal, Mode=OneWay}" Y="{Binding Path=YVal, Mode=OneWay}"
Shape="Star"
StrokeThickness="3"
Stroke="Black"
Size="14"
Fill="Black"
Text="My Point Annotation"
Visibility="Visible"/>
</oxy:Plot.Annotations>
</oxy:Plot>