此方法仅适用于 telerik控件,且是特定的版本,此处做一个参考事例,不保证复制后直接运行。
要实现十字准线功能,您将向ChartArea添加两个自定义网格线(每个网格线与其中一个轴平行)。
此外,您将需要三个事件 - MouseEnter,MouseLeave和MouseMove来跟踪鼠标光标位置并在绘图区域“移动”网格线(由ChartArea模板中的ClipPanel元素表示)。
首先,您需要连接到图表的Loaded事件。
加载图表后可以通过ChildrenOfType <T>扩展方法(添加对Telerik.Windows.Controls命名空间的引用)获取面板
效果:
1、定义两个全局的纵横线
CustomGridLine gridline = new CustomGridLine();
CustomGridLine gridline2 = new CustomGridLine();
2、相应的事件
/// <summary>
/// 在控件初始加载完成后,将相应的事件加入。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var plotAreaPanel = this.radChart.DefaultView.ChartArea.ChildrenOfType<ClipPanel>().FirstOrDefault();
plotAreaPanel.MouseEnter += this.OnPlotAreaPanelMouseEnter;
plotAreaPanel.MouseMove += this.OnPlotAreaPanelMouseMove;
plotAreaPanel.MouseLeave += this.OnPlotAreaPanelMouseLeave;
//增加一个平行于X轴的横线
gridline.YIntercept = 150;//交点
gridline.Stroke = new SolidColorBrush(Colors.Green);
gridline.StrokeThickness = 2;
//增加一个平行于Y轴的横线
gridline2.XIntercept = 30;//交点
gridline2.Stroke = new SolidColorBrush(Colors.Red);
gridline2.StrokeThickness = 2;
}
private void OnPlotAreaPanelMouseEnter(object sender, MouseEventArgs e)
{
this.radChart.DefaultView.ChartArea.Annotations.Add(gridline);
this.radChart.DefaultView.ChartArea.Annotations.Add(gridline2);
}
private void OnPlotAreaPanelMouseMove(object sender, MouseEventArgs e)
{
var plotAreaPanel = sender as ClipPanel;
var position = e.GetPosition(plotAreaPanel);
//获取鼠标点的纵横坐标
var x = this.radChart.DefaultView.ChartArea.AxisX.ConvertPhysicalUnitsToData(position.X);
var y = this.radChart.DefaultView.ChartArea.AxisY.ConvertPhysicalUnitsToData(position.Y);
//将鼠标所在的坐标给到横线
gridline.YIntercept = y;
gridline2.XIntercept = x;
//将鼠标所在的坐标值显示到控件上
this.b2.Content = string.Format("X: {0:N2}", x);
this.b1.Content = string.Format("Y: {0:N2}", y);
}
private void OnPlotAreaPanelMouseLeave(object sender, MouseEventArgs e)
{
this.radChart.DefaultView.ChartArea.Annotations.Remove(gridline);
this.radChart.DefaultView.ChartArea.Annotations.Remove(gridline2);
}