|
| 1 | +namespace Syncfusion.Maui.Toolkit.Charts |
| 2 | +{ |
| 3 | + /// <summary> |
| 4 | + /// Represents a segment of the <see cref="FastScatterSeries"/> chart, responsible for drawing the scatter and managing its visual appearance. |
| 5 | + /// </summary> |
| 6 | + public partial class FastScatterSegment : CartesianSegment |
| 7 | + { |
| 8 | + #region Fields |
| 9 | + |
| 10 | + float _preXPos; |
| 11 | + float _preYPos; |
| 12 | + float _preXValue; |
| 13 | + float _preYValue; |
| 14 | + readonly List<PointF> _fastScatterPlottingPoints = []; |
| 15 | + RectF _actualRectF; |
| 16 | + |
| 17 | + #endregion |
| 18 | + |
| 19 | + #region Internal Properties |
| 20 | + |
| 21 | + internal List<double>? XValues { get; set; } |
| 22 | + internal IList<double>? YValues { get; set; } |
| 23 | + |
| 24 | + #endregion |
| 25 | + |
| 26 | + #region Methods |
| 27 | + |
| 28 | + #region Internal Methods |
| 29 | + |
| 30 | + internal void SetData(List<double> xValues, IList<double> yValues) |
| 31 | + { |
| 32 | + if (Series is XYDataSeries series) |
| 33 | + { |
| 34 | + // Initialize min/max values |
| 35 | + double xMin = double.MaxValue, xMax = double.MinValue, yMin = double.MaxValue, yMax = double.MinValue; |
| 36 | + XValues = xValues; |
| 37 | + YValues = yValues; |
| 38 | + int dataCount = series.PointsCount; |
| 39 | + |
| 40 | + if (dataCount == 0) |
| 41 | + { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + // Adjust for indexed series by setting xMin/xMax |
| 46 | + if (series.IsIndexed && dataCount > 0) |
| 47 | + { |
| 48 | + xMin = XValues[0]; |
| 49 | + xMax = XValues[dataCount - 1]; |
| 50 | + } |
| 51 | + |
| 52 | + for (int i = 0; i < dataCount; i++) |
| 53 | + { |
| 54 | + // Ensure we are in range for both lists |
| 55 | + if (i >= XValues.Count) |
| 56 | + { |
| 57 | + break; |
| 58 | + } |
| 59 | + |
| 60 | + double xValue = XValues[i]; |
| 61 | + double yValue = YValues[i]; |
| 62 | + |
| 63 | + // Mark as empty if any NaN values are encountered |
| 64 | + if (double.IsNaN(xValue) || double.IsNaN(yValue)) |
| 65 | + { |
| 66 | + continue; |
| 67 | + } |
| 68 | + |
| 69 | + if (!series.IsIndexed) |
| 70 | + { |
| 71 | + // Use Math.Min/Max for cleaner comparisons |
| 72 | + xMin = Math.Min(xMin, xValue); |
| 73 | + xMax = Math.Max(xMax, xValue); |
| 74 | + } |
| 75 | + |
| 76 | + yMin = Math.Min(yMin, yValue); |
| 77 | + yMax = Math.Max(yMax, yValue); |
| 78 | + } |
| 79 | + |
| 80 | + // Handle no valid entries scenario by setting NaNs |
| 81 | + if (xMin == double.MaxValue) |
| 82 | + { |
| 83 | + xMin = double.NaN; |
| 84 | + } |
| 85 | + |
| 86 | + if (xMax == double.MinValue) |
| 87 | + { |
| 88 | + xMax = double.NaN; |
| 89 | + } |
| 90 | + |
| 91 | + if (yMin == double.MaxValue) |
| 92 | + { |
| 93 | + yMin = double.NaN; |
| 94 | + } |
| 95 | + |
| 96 | + if (yMax == double.MinValue) |
| 97 | + { |
| 98 | + yMax = double.NaN; |
| 99 | + } |
| 100 | + |
| 101 | + // Update series range |
| 102 | + Series.XRange += new DoubleRange(xMin, xMax); |
| 103 | + Series.YRange += new DoubleRange(yMin, yMax); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + #endregion |
| 108 | + |
| 109 | + #region Override Methods |
| 110 | + /// <inheritdoc/> |
| 111 | + protected internal override void OnLayout() |
| 112 | + { |
| 113 | + if (Series is FastScatterSeries series && series.ActualXAxis is ChartAxis xAxis && series.ActualYAxis is ChartAxis yAxis && XValues is not null && YValues is not null) |
| 114 | + { |
| 115 | + int dataCount = series.PointsCount; |
| 116 | + |
| 117 | + var _xRange = xAxis.VisibleRange; |
| 118 | + var _yRange = yAxis.VisibleRange; |
| 119 | + |
| 120 | + double xStart = _xRange.Start; |
| 121 | + double xEnd = _xRange.End; |
| 122 | + |
| 123 | + double yStart = _yRange.Start; |
| 124 | + double yEnd = _yRange.End; |
| 125 | + |
| 126 | + _preXValue = (float)XValues[0]; |
| 127 | + _preYValue = (float)YValues[0]; |
| 128 | + |
| 129 | + _preXPos = series.TransformToVisibleX(_preXValue, _preYValue); |
| 130 | + _preYPos = series.TransformToVisibleY(_preXValue, _preYValue); |
| 131 | + |
| 132 | + _fastScatterPlottingPoints.Clear(); |
| 133 | + |
| 134 | + if (!series.IsIndexed) |
| 135 | + { |
| 136 | + for (int i = 1; i < dataCount; i++) |
| 137 | + { |
| 138 | + double xValue = XValues[i]; |
| 139 | + double yValue = YValues[i]; |
| 140 | + |
| 141 | + if (xEnd <= xValue && xStart >= XValues[i - 1]) |
| 142 | + { |
| 143 | + float x = series.TransformToVisibleX(xValue, yValue); |
| 144 | + float y = series.TransformToVisibleY(xValue, yValue); |
| 145 | + _preXPos = series.TransformToVisibleX(XValues[i - 1], YValues[i - 1]); |
| 146 | + _preYPos = series.TransformToVisibleY(XValues[i - 1], YValues[i - 1]); |
| 147 | + |
| 148 | + _fastScatterPlottingPoints.Add(new PointF(_preXPos, _preYPos)); |
| 149 | + |
| 150 | + _preXPos = x; |
| 151 | + _preYPos = y; |
| 152 | + _preXValue = (float)xValue; |
| 153 | + _preYValue = (float)yValue; |
| 154 | + } |
| 155 | + else if ((xValue <= xEnd && xValue >= xStart) || (yValue >= yStart && yValue <= yEnd)) |
| 156 | + { |
| 157 | + float x = series.TransformToVisibleX(xValue, yValue); |
| 158 | + float y = series.TransformToVisibleY(xValue, yValue); |
| 159 | + |
| 160 | + _fastScatterPlottingPoints.Add(new PointF(_preXPos, _preYPos)); |
| 161 | + |
| 162 | + _preXPos = x; |
| 163 | + _preYPos = y; |
| 164 | + _preXValue = (float)xValue; |
| 165 | + _preYValue = (float)yValue; |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + else |
| 170 | + { |
| 171 | + for (int i = 1; i < dataCount; i++) |
| 172 | + { |
| 173 | + double yValue = YValues[i]; |
| 174 | + |
| 175 | + if ((i <= xEnd + 1) && (i >= xStart - 1)) |
| 176 | + { |
| 177 | + float x = series.TransformToVisibleX(i, yValue); |
| 178 | + float y = series.TransformToVisibleY(i, yValue); |
| 179 | + |
| 180 | + _fastScatterPlottingPoints.Add(new PointF(_preXPos, _preYPos)); |
| 181 | + |
| 182 | + _preXPos = x; |
| 183 | + _preYPos = y; |
| 184 | + _preXValue = (float)i; |
| 185 | + _preYValue = (float)yValue; |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + if (_fastScatterPlottingPoints.Count != dataCount) |
| 191 | + { |
| 192 | + float lastX = series.TransformToVisibleX(XValues[dataCount - 1], YValues[dataCount - 1]); |
| 193 | + float lastY = series.TransformToVisibleY(XValues[dataCount - 1], YValues[dataCount - 1]); |
| 194 | + _fastScatterPlottingPoints.Add(new PointF(lastX, lastY)); |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + /// <inheritdoc/> |
| 200 | + protected internal override void Draw(ICanvas canvas) |
| 201 | + { |
| 202 | + if (Series is FastScatterSeries series) |
| 203 | + { |
| 204 | + float scatterHeight = (float)series.PointHeight; |
| 205 | + float scatterWidth = (float)series.PointWidth; |
| 206 | + |
| 207 | + float halfHeight = scatterHeight / 2; |
| 208 | + float halfWidth = scatterWidth / 2; |
| 209 | + |
| 210 | + if (HasStroke) |
| 211 | + { |
| 212 | + canvas.StrokeColor = Stroke.ToColor(); |
| 213 | + canvas.StrokeSize = (float)StrokeWidth; |
| 214 | + } |
| 215 | + |
| 216 | + if (series.Type == ShapeType.Circle) |
| 217 | + { |
| 218 | + foreach (var point in _fastScatterPlottingPoints) |
| 219 | + { |
| 220 | + if (double.IsNaN(point.Y) || double.IsNaN(point.X)) |
| 221 | + { |
| 222 | + continue; |
| 223 | + } |
| 224 | + |
| 225 | + _actualRectF = new RectF((float)point.X - halfWidth, point.Y - halfHeight, scatterWidth, scatterHeight); |
| 226 | + |
| 227 | + canvas.SetFillPaint(Fill, _actualRectF); |
| 228 | + canvas.FillEllipse(_actualRectF); |
| 229 | + |
| 230 | + if (HasStroke) |
| 231 | + { |
| 232 | + canvas.DrawEllipse(_actualRectF); |
| 233 | + } |
| 234 | + } |
| 235 | + } |
| 236 | + else |
| 237 | + { |
| 238 | + foreach (var point in _fastScatterPlottingPoints) |
| 239 | + { |
| 240 | + _actualRectF = new RectF((float)point.X - halfWidth, point.Y - halfHeight, scatterWidth, scatterHeight); |
| 241 | + |
| 242 | + canvas.SetFillPaint(Fill, _actualRectF); |
| 243 | + |
| 244 | + canvas.DrawShape(_actualRectF, series.Type, HasStroke, false); |
| 245 | + } |
| 246 | + |
| 247 | + } |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | + #endregion |
| 252 | + #endregion |
| 253 | + } |
| 254 | +} |
0 commit comments