第一篇:基础、参考、简单设置 wpf-折线图绘制2-oxyplot-1
第二篇:图和轴设置、简单控制 wpf-折线图绘制2-oxyplot-2
这篇主要想写Annotation。
比如下图这种,浅蓝色bar就是RectangleAnnotation,可以为每个点都加一个Annotation,点击(或别的行为)某点时就显示它带的Annotation。
这种效果的实现思路是:点击某点时,计算坐标,如果在合法范围就清空Annotations,然后构建新的Annotation,加入列表,然后刷新图像。挺简单的哈~
private void Model_MouseDown1(object sender, OxyMouseDownEventArgs e)
{
var position = OxyPlot.Axes.Axis.InverseTransform(e.Position, myPlot.Model.DefaultXAxis, myPlot.Model.DefaultYAxis);
// 这个是与x轴对应的位置
var x = Math.Round(position.X);
var rectAnnotation_y = new RectangleAnnotation
{
MinimumX = x,
MaximumX = x + 5, // 这样宽度是5
MinimumY = 0,
MaximumY = maxY, // 这是我自定义的一个值
Fill = OxyColor.FromArgb(), // 自定义颜色
Text = "...", // 显示在bar中的文字
TextColor = OxyColors.Black,
TextPosition = new DataPoint(position.X, position.Y),
};
if (x < maxXVal) // 这是我自定义的值。因为有一段x是我不需要加Annotation的
{
// 先清空再加当下的
myPlot.Model.Annotations.Clear();
myPlot.Model.Annotations.Add(rectAnnotation_y);
// 重绘
myPlot.Model.InvalidatePlot(true);
}
}
...
// 在构造函数里:
// 注意,前台有:<oxy:PlotView x:Name="myPlot" Model="{Binding Path= SimplePlotModel}"...>
_viewModel.SimplePlotModel.MouseDown += Model_MouseDown1;
var handleClick = new DelegatePlotCommand<OxyMouseDownEventArgs>(
(v, c, e) =>
{
var args = new HitTestArguments(e.Position, 10);
var firstHit = v.ActualModel.HitTest(args).FirstOrDefault(x => x.Element is RectangleAnnotation);
if (firstHit != null)
{
myPlot.Model.InvalidatePlot(false);
}
});
// 绑定
myController.Bind(new OxyMouseDownGesture(OxyMouseButton.Left), handleClick);
现在我头疼的是文字的美化问题。本来我是想加一个tooltip的,PlotView下也有ToolTip标签,但问题是它显示得很诡异:点击完然后晃晃鼠标,tooltip出现,然后又消失;不知道何时出现,也不知道会出现在哪里。后面我会再更新吧!
这是方法:
<oxy:PlotView>
...
<oxy:PlotView.ToolTip>
<Grid >
<DockPanel FlowDirection="LeftToRight">
<TextBlock x:Name="leftText" Foreground="White" />
<TextBlock x:Name="rightText" Foreground="Black" />
</DockPanel>
</Grid>
</oxy:PlotView.ToolTip>
</oxy:PlotView>
后台的话,leftText和rightText的赋值要在重绘前。
...
myPlot.Model.Annotations.Add(rectAnnotation_y);
this.leftText.Text = "";
this.rightText.Text = "";
myPlot.Model.InvalidatePlot(true);