//圖像加載 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); }
這個事件稍一改動,就能夠有圈選的功效html
(這張圖就不搞成動態gif了)ssh
換湯不換藥,源碼過一遍就明白了this
//進入 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.htmlspa