/// <summary> /// Draw Ellipse around the Intersection of /// Horizontal and Vertical markers /// </summary> /// <param name="pnt"></param> ChartPanelObject CreateEllipse(Point pnt) { Border sp = new Border(); sp.Height = 50; sp.Width = 50; var obj = new ChartPanelObject(); Ellipse el = new Ellipse(); el.Height = 50; el.Width = 50; el.StrokeThickness = 2; el.Stroke = new SolidColorBrush(Colors.Red); sp.Child = el; obj.DataPoint = new Point(ellipseDataPoint.X, ellipseDataPoint.Y); obj.Content = sp; return(obj); }
ChartPanelObject CreateMarker(bool isHorizontal) { var obj = new ChartPanelObject(); var bdr = new Border() { BorderBrush = Background = new SolidColorBrush(Color.FromArgb(150, 255, 2, 2)), Padding = new Thickness(2), }; var tb = new TextBlock(); var bind = new Binding(); bind.Source = obj; if (isHorizontal) { bdr.BorderThickness = new Thickness(0, 4, 0, 0); bdr.Margin = new Thickness(0, -1, 0, 0); obj.HorizontalContentAlignment = HorizontalAlignment.Stretch; bind.StringFormat = "y={0:#.#}"; bind.Path = new PropertyPath("DataPoint.Y"); obj.DataPoint = new Point(double.NaN, 0.5); } else { bdr.BorderThickness = new Thickness(4, 0, 0, 0); bdr.Margin = new Thickness(-1, 0, 0, 0); obj.VerticalContentAlignment = VerticalAlignment.Stretch; bind.StringFormat = "x={0:#.#}"; bind.Path = new PropertyPath("DataPoint.X"); obj.DataPoint = new Point(0.5, double.NaN); } tb.SetBinding(TextBlock.TextProperty, bind); bdr.Child = tb; bdr.IsHitTestVisible = false; obj.Content = bdr; return(obj); }
ChartPanelObject CreateMarker(bool isHorizontal) { var obj = new ChartPanelObject(); var bdr = new Border() { BorderBrush = Background = new SolidColorBrush(Color.FromArgb(150, 255, 2, 2)), Padding = new Thickness(2), }; var tb = new TextBlock(); var bind = new Binding(); bind.Source = obj; if (isHorizontal) { bdr.BorderThickness = new Thickness(0, 4, 0, 0); bdr.Margin = new Thickness(0, -1, 0, 0); obj.HorizontalContentAlignment = HorizontalAlignment.Stretch; bind.StringFormat = "y={0:#.#}"; bind.Path = new PropertyPath("DataPoint.Y"); obj.DataPoint = new Point(double.NaN, 0.5); } else { bdr.BorderThickness = new Thickness(4, 0, 0, 0); bdr.Margin = new Thickness(-1, 0, 0, 0); obj.VerticalContentAlignment = VerticalAlignment.Stretch; bind.StringFormat = "x={0:#.#}"; bind.Path = new PropertyPath("DataPoint.X"); obj.DataPoint = new Point(0.5, double.NaN); } tb.SetBinding(TextBlock.TextProperty, bind); bdr.Child = tb; bdr.IsHitTestVisible = false; obj.Content = bdr; return obj; }
ChartPanelObject CreateMarker(bool isHorizontal, Point pnt) { var obj = new ChartPanelObject(); var bdr = new Border() { BorderBrush = Background = new SolidColorBrush(Colors.Red), Padding = new Thickness(2), }; if (isHorizontal) { bdr.BorderThickness = new Thickness(0, 2, 0, 0); bdr.Margin = new Thickness(0, -1, 0, 0); obj.HorizontalContentAlignment = HorizontalAlignment.Stretch; if (pnt.X == 0 && pnt.Y == 0) { obj.DataPoint = new Point(double.NaN, 0.5); } else { //Data Point where Mouse cursor is present Point dpt = chart.View.PointToData(pnt); ///Markers are created based on the Horizontal movement ///of mouse cursor. We have to track whether the mouse ///cursor moved towards left or right from the last position. ///For this we need to determine the two DataPoints plotted ///between which the current mouse position is placed. double val; // Gets the index of the nearest data point int ptIndex = chart.View.DataIndexFromPoint(pnt, 0, MeasureOption.X, out val); // Use the above index to calculate the Coordinates of that nearest Data Point Point closeScreenPoint = chart.View.DataIndexToPoint(0, ptIndex); // Get the location of the next data point depending on whether mouse was // was moved towards left or right Point awayScreenPoint; if (pnt.X > closeScreenPoint.X) { awayScreenPoint = chart.View.DataIndexToPoint(0, ptIndex + 1); } else { awayScreenPoint = chart.View.DataIndexToPoint(0, ptIndex - 1); } //Get coordinates where Vertical marker will intersect the chart line double crossY = GetY(closeScreenPoint, awayScreenPoint, pnt); //Get DataPoint for the above intersecting coordinates Point closeDataPoint = chart.View.PointToData(new Point(pnt.X, crossY)); // set the DataPoint as the Attached data source for the Horizontal marker obj.DataPoint = new Point(double.NaN, closeDataPoint.Y); ellipseDataPoint.Y = closeDataPoint.Y; // ellipseDataPoint.Y = obj.DataPoint.Y; } } else { bdr.BorderThickness = new Thickness(2, 0, 0, 0); bdr.Margin = new Thickness(-1, 0, 0, 0); obj.VerticalContentAlignment = VerticalAlignment.Stretch; Point dpt = chart.View.PointToData(pnt); obj.DataPoint = new Point(dpt.X, double.NaN); ellipseDataPoint.X = dpt.X; } bdr.IsHitTestVisible = false; obj.Content = bdr; return(obj); }
public MouseMarker() { InitializeComponent(); SampleData.CreateData(chart); chart.ChartType = ChartType.LineSymbols; var pnl = new ChartPanel(); var obj = new ChartPanelObject() { HorizontalAlignment = HorizontalAlignment.Right, VerticalAlignment = VerticalAlignment.Bottom }; var bdr = new Border() { Background = new SolidColorBrush(Colors.Green) { Opacity = 0.4 }, BorderBrush = new SolidColorBrush(Colors.Green), BorderThickness = new Thickness(1, 1, 3, 3), CornerRadius = new CornerRadius(6, 6, 0, 6), Padding = new Thickness(3) }; //bdr.Padding = new Thickness(16, 16, 0, 0); var sp = new StackPanel(); var tb1 = new TextBlock(); var bind1 = new Binding(); bind1.Source = obj; bind1.StringFormat = "x={0:#.##}"; bind1.Path = new PropertyPath("DataPoint.X"); tb1.SetBinding(TextBlock.TextProperty, bind1); var tb2 = new TextBlock(); var bind2 = new Binding(); bind2.Source = obj; bind2.StringFormat = "y={0:#.##}"; bind2.Path = new PropertyPath("DataPoint.Y"); tb2.SetBinding(TextBlock.TextProperty, bind2); sp.Children.Add(tb1); sp.Children.Add(tb2); bdr.Child = sp; obj.Content = bdr; obj.DataPoint = new Point(); obj.Action = ChartPanelAction.MouseMove; pnl.Children.Add(obj); chart.View.Layers.Add(pnl); cbAttach.ItemsSource = Utils.GetEnumValues(typeof(ChartPanelAttach)); cbAttach.SelectedIndex = 0; cbAttach.SelectionChanged += (s, e) => { obj.Attach = (ChartPanelAttach)Enum.Parse(typeof(ChartPanelAttach), cbAttach.SelectedValue.ToString(), true); }; }
private void handleDraw() { if (c1Chart.Data.Children.Count > 0) { clearMajorGrid(); cp.Children.Clear(); var obj = new ChartPanelObject(); Size stringSize = Hammergo.Utility.Helper.MeasureTextSize("1983", c1Chart.FontFamily, c1Chart.FontStyle, c1Chart.FontWeight, c1Chart.FontStretch, c1Chart.FontSize); double stringWidth = stringSize.Width; //计算坐标 calcAxis(); var rect = c1Chart.View.AxisX.GetAxisRect(); Canvas canvas = new Canvas(); canvas.Width = rect.Width; canvas.Height = rect.Height; //Canvas.SetLeft(canvas, 0); ////矩形对象相对于父容器对象Canvas的位置,左边距、上边距 //Canvas.SetTop(canvas, 0); obj.Content = canvas; Rectangle rectangle = new Rectangle();//矩形对象 //属性设置,填充颜色、边粗细、边颜色、宽、高等 rectangle.StrokeThickness = c1Chart.View.AxisX.AxisLine.StrokeThickness; rectangle.Stroke = c1Chart.View.AxisX.AxisLine.Stroke; rectangle.Width = rect.Width + 2 * c1Chart.View.AxisY.AxisLine.StrokeThickness; rectangle.Height = rect.Height; //矩形对象相对于父容器对象Canvas的位置,左边距、上边距 Canvas.SetLeft(rectangle, -1); Canvas.SetTop(rectangle, 0); canvas.Children.Add(rectangle); //draw line for (int i = 0; i < cors.GetLength(0); i++) { //第一个和最后一个竖线不画 if (i > 0 && i < cors.GetLength(0) - 1) { Line line = new Line(); line.Stroke = c1Chart.View.AxisX.AxisLine.Stroke; line.StrokeThickness = c1Chart.View.AxisX.AxisLine.StrokeThickness; //相对于canvas的坐标 line.X1 = cors[i, 0] - rect.Left; line.Y1 = 0; line.X2 = line.X1; line.Y2 = line.Y1 + rect.Height; //Canvas.SetLeft(line, 0); //Canvas.SetTop(line, 0); canvas.Children.Add(line); Line dashLine = new Line(); dashLine.Stroke = Brushes.Gray; dashLine.StrokeThickness = 1; dashLine.StrokeDashArray = new DoubleCollection(new double[] { 2, 2 }); dashLine.X1 = line.X1; dashLine.Y1 = line.Y1; dashLine.X2 = line.X2; dashLine.Y2 = -c1Chart.View.AxisY.GetAxisRect().Height; canvas.Children.Add(dashLine); } //显示年份 double distance = cors[i, 1] - cors[i, 0]; if (distance >= stringWidth) { //相对于canvas的坐标系 double startXCor = cors[i, 0] + (distance - stringWidth) / 2.0f - rect.Left; double startYCor = (rect.Height - stringSize.Height) / 2.0f; TextBlock tb = new TextBlock(); tb.Text = (minYear + i).ToString(); Canvas.SetTop(tb, startYCor); Canvas.SetLeft(tb, startXCor); canvas.Children.Add(tb); //ghs.DrawString((minYear + i).ToString(), smallFont, Brushes.Black, new PointF(startXCor, startYCor)); } } obj.Action = ChartPanelAction.None; cp.Children.Add(obj); if (c1Chart.View.Layers.Contains(cp) == false) { c1Chart.View.Layers.Add(cp); } cp.SetValue(Canvas.LeftProperty, rect.Left); cp.SetValue(Canvas.TopProperty, rect.Top); cp.Height = rect.Height; cp.Width = rect.Width; } }
/// <summary> /// Draw Ellipse around the Intersection of /// Horizontal and Vertical markers /// </summary> /// <param name="pnt"></param> ChartPanelObject CreateEllipse(Point pnt) { Border sp = new Border(); sp.Height = 50; sp.Width = 50; var obj = new ChartPanelObject(); Ellipse el = new Ellipse(); el.Height = 50; el.Width = 50; el.StrokeThickness = 2; el.Stroke = new SolidColorBrush(Colors.Red); sp.Child = el; obj.DataPoint = new Point(ellipseDataPoint.X, ellipseDataPoint.Y ); obj.Content = sp; return obj; }
ChartPanelObject CreateMarker(bool isHorizontal, Point pnt) { var obj = new ChartPanelObject(); var bdr = new Border() { BorderBrush = Background = new SolidColorBrush(Colors.Red), Padding = new Thickness(2), }; if (isHorizontal) { bdr.BorderThickness = new Thickness(0, 2, 0, 0); bdr.Margin = new Thickness(0, -1, 0, 0); obj.HorizontalContentAlignment = HorizontalAlignment.Stretch; if (pnt.X == 0 && pnt.Y == 0) obj.DataPoint = new Point(double.NaN, 0.5); else { //Data Point where Mouse cursor is present Point dpt = chart.View.PointToData(pnt); ///Markers are created based on the Horizontal movement ///of mouse cursor. We have to track whether the mouse ///cursor moved towards left or right from the last position. ///For this we need to determine the two DataPoints plotted ///between which the current mouse position is placed. double val; // Gets the index of the nearest data point int ptIndex = chart.View.DataIndexFromPoint(pnt, 0, MeasureOption.X, out val); // Use the above index to calculate the Coordinates of that nearest Data Point Point closeScreenPoint = chart.View.DataIndexToPoint(0, ptIndex); // Get the location of the next data point depending on whether mouse was // was moved towards left or right Point awayScreenPoint; if (pnt.X > closeScreenPoint.X) awayScreenPoint = chart.View.DataIndexToPoint(0, ptIndex + 1); else awayScreenPoint = chart.View.DataIndexToPoint(0, ptIndex - 1); //Get coordinates where Vertical marker will intersect the chart line double crossY = GetY(closeScreenPoint, awayScreenPoint, pnt); //Get DataPoint for the above intersecting coordinates Point closeDataPoint = chart.View.PointToData(new Point(pnt.X, crossY)); // set the DataPoint as the Attached data source for the Horizontal marker obj.DataPoint = new Point(double.NaN, closeDataPoint.Y); ellipseDataPoint.Y = closeDataPoint.Y; // ellipseDataPoint.Y = obj.DataPoint.Y; } } else { bdr.BorderThickness = new Thickness(2, 0, 0, 0); bdr.Margin = new Thickness(-1, 0, 0, 0); obj.VerticalContentAlignment = VerticalAlignment.Stretch; Point dpt = chart.View.PointToData(pnt); obj.DataPoint = new Point(dpt.X, double.NaN); ellipseDataPoint.X = dpt.X; } bdr.IsHitTestVisible = false; obj.Content = bdr; return obj; }