Silverlight RadChart :创建十字定位&圈选
//图像加载 void Chart_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; } //进入--添加GridLine private void OnPlotAreaPanelMouseEnter(object sender, MouseEventArgs e) { if (null != xGridLine && null != yGridLine) { xGridLine = new CustomGridLine(); yGridLine = new CustomGridLine(); this.radChart.DefaultView.ChartArea.Annotations.Add(xGridLine); this.radChart.DefaultView.ChartArea.Annotations.Add(yGridLine); } else { this.radChart.DefaultView.ChartArea.Annotations.Add(xGridLine); this.radChart.DefaultView.ChartArea.Annotations.Add(yGridLine); } } //移动,实时跟踪 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); //十字线赋值 xGridLine.XIntercept = x; yGridLine.YIntercept = y; this.textX.Text = string.Format("X: {0:N3}", x); this.textY.Text = string.Format("Y: {0:N3}", y); } //移出-->移除GridLine private void OnPlotAreaPanelMouseLeave(object sender, MouseEventArgs e) { this.radChart.DefaultView.ChartArea.Annotations.Remove(xGridLine); this.radChart.DefaultView.ChartArea.Annotations.Remove(yGridLine); }
这个事件稍一改动,就可以有圈选的功效
(这张图就不搞成动态gif了)
换汤不换药,源码过一遍就明白了
//进入 private void OnPlotAreaPanelMouseEnter(object sender, MouseEventArgs e) { if (null != mz) { mz = new MarkedZone(); mz.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0xCC, 0x00)); this.radChart.DefaultView.ChartArea.Annotations.Add(mz); } else { this.radChart.DefaultView.ChartArea.Annotations.Add(mz); } } //移动 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); mz.StartX = 0; //可自定义,设置初始值,本次案例不做安排 mz.EndX = x; mz.StartY = 0; mz.EndY = y; this.textX.Text = string.Format("X: {0:N3}", x); this.textY.Text = string.Format("Y: {0:N3}", y); } //移出 private void OnPlotAreaPanelMouseLeave(object sender, MouseEventArgs e) { this.radChart.DefaultView.ChartArea.Annotations.Remove(mz); }
原文链接:http://www.telerik.com/help/silverlight/radchart-howto-create-location-crosshair-for-radchart.html