|
| 1 | +using Syncfusion.Maui.Toolkit.Charts; |
| 2 | +using Font = Microsoft.Maui.Graphics.Font; |
| 3 | + |
| 4 | +namespace ChartCrossHair.UsingGraphicsView |
| 5 | +{ |
| 6 | + public class CrossHairBehavior : GraphicsView, IDrawable |
| 7 | + { |
| 8 | + private bool isTouchActive = false; |
| 9 | + private string? CrossHairXValue; |
| 10 | + private string? CrossHairYValue; |
| 11 | + private string? CrossHairPointValue; |
| 12 | + private PointF CrossHairX1; |
| 13 | + private PointF CrossHairX2; |
| 14 | + private PointF CrossHairY1; |
| 15 | + private PointF CrossHairY2; |
| 16 | + |
| 17 | + public static readonly BindableProperty ShowCrossHairProperty = BindableProperty.Create(nameof(ShowCrossHair), typeof(bool), typeof(CrossHairBehavior), false, BindingMode.Default, null); |
| 18 | + |
| 19 | + public bool ShowCrossHair |
| 20 | + { |
| 21 | + get { return (bool)GetValue(ShowCrossHairProperty); } |
| 22 | + set { SetValue(ShowCrossHairProperty, value); } |
| 23 | + } |
| 24 | + |
| 25 | + public CrossHairBehavior() |
| 26 | + { |
| 27 | + Drawable = this; |
| 28 | + } |
| 29 | + |
| 30 | + public void Draw(ICanvas canvas, RectF dirtyRect) |
| 31 | + { |
| 32 | + if (ShowCrossHair && isTouchActive) |
| 33 | + { |
| 34 | + canvas.StrokeColor = Colors.Red; |
| 35 | + canvas.DrawLine(CrossHairX1, CrossHairX2); |
| 36 | + canvas.DrawLine(CrossHairY1, CrossHairY2); |
| 37 | + |
| 38 | + canvas.FontColor = Colors.Black; |
| 39 | + canvas.Font = Font.DefaultBold; |
| 40 | + canvas.DrawString(CrossHairPointValue, CrossHairX1.X + 2, CrossHairY1.Y - 5, HorizontalAlignment.Left); |
| 41 | + canvas.DrawString(CrossHairXValue, CrossHairX1.X + 2, CrossHairX2.Y - 5, HorizontalAlignment.Left); |
| 42 | + canvas.DrawString(CrossHairYValue, CrossHairY1.X + 2, CrossHairY1.Y - 5, HorizontalAlignment.Left); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public void OnTouchMove(ChartBase chart, float pointX, float pointY) |
| 47 | + { |
| 48 | + isTouchActive = true; |
| 49 | + CalculateCrossHairPoints(chart, pointX, pointY); |
| 50 | + Invalidate(); |
| 51 | + } |
| 52 | + |
| 53 | + public void OnTouchUp(ChartBase chart, float pointX, float pointY) |
| 54 | + { |
| 55 | + isTouchActive = false; |
| 56 | + Invalidate(); |
| 57 | + } |
| 58 | + |
| 59 | + private void CalculateCrossHairPoints(ChartBase chart, float pointX, float pointY) |
| 60 | + { |
| 61 | + if (chart is SfCartesianChart cartesianChart) |
| 62 | + { |
| 63 | + bool isCrossHairArea = cartesianChart.SeriesBounds.Contains(pointX, pointY); |
| 64 | + |
| 65 | + if (isCrossHairArea) |
| 66 | + { |
| 67 | + double x = cartesianChart.PointToValue(cartesianChart.XAxes[0], pointX, pointY); |
| 68 | + double y = cartesianChart.PointToValue(cartesianChart.YAxes[0], pointX, pointY); |
| 69 | + |
| 70 | + float crossHairX = cartesianChart.XAxes[0].ValueToPoint(x); |
| 71 | + float crossHairY = cartesianChart.YAxes[0].ValueToPoint(y); |
| 72 | + |
| 73 | + CrossHairXValue = cartesianChart.XAxes[0].PointToValue(crossHairX, crossHairY).ToString("0.00"); |
| 74 | + CrossHairYValue = cartesianChart.YAxes[0].PointToValue(crossHairX, crossHairY).ToString("0.00"); |
| 75 | + |
| 76 | + CrossHairPointValue = CrossHairXValue + ", " + CrossHairYValue; |
| 77 | + |
| 78 | + float crossHairLeft = cartesianChart.XAxes[0].ValueToPoint(cartesianChart.XAxes[0].VisibleMinimum); |
| 79 | + float crossHairRight = cartesianChart.XAxes[0].ValueToPoint(cartesianChart.XAxes[0].VisibleMaximum); |
| 80 | + float crossHairTop = cartesianChart.YAxes[0].ValueToPoint(cartesianChart.YAxes[0].VisibleMaximum); |
| 81 | + float crossHairBottom = cartesianChart.YAxes[0].ValueToPoint(cartesianChart.YAxes[0].VisibleMinimum); |
| 82 | + |
| 83 | + CrossHairX1 = new PointF(crossHairX, crossHairTop); |
| 84 | + CrossHairX2 = new PointF(crossHairX, crossHairBottom); |
| 85 | + CrossHairY1 = new PointF(crossHairLeft, crossHairY); |
| 86 | + CrossHairY2 = new PointF(crossHairRight, crossHairY); |
| 87 | + } |
| 88 | + else |
| 89 | + { |
| 90 | + Reset(); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private void Reset() |
| 96 | + { |
| 97 | + CrossHairX1 = CrossHairX2 = CrossHairY1 = CrossHairY2 = new PointF(0, 0); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments