@coinbase/cds-mobile-visualization 3.3.0 → 3.4.0-beta.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (265) hide show
  1. package/CHANGELOG.md +80 -0
  2. package/dts/chart/CartesianChart.d.ts +125 -0
  3. package/dts/chart/CartesianChart.d.ts.map +1 -0
  4. package/dts/chart/ChartContextBridge.d.ts +28 -0
  5. package/dts/chart/ChartContextBridge.d.ts.map +1 -0
  6. package/dts/chart/ChartProvider.d.ts +6 -0
  7. package/dts/chart/ChartProvider.d.ts.map +1 -0
  8. package/dts/chart/Path.d.ts +91 -0
  9. package/dts/chart/Path.d.ts.map +1 -0
  10. package/dts/chart/PeriodSelector.d.ts +85 -0
  11. package/dts/chart/PeriodSelector.d.ts.map +1 -0
  12. package/dts/chart/area/Area.d.ts +77 -0
  13. package/dts/chart/area/Area.d.ts.map +1 -0
  14. package/dts/chart/area/AreaChart.d.ts +131 -0
  15. package/dts/chart/area/AreaChart.d.ts.map +1 -0
  16. package/dts/chart/area/DottedArea.d.ts +46 -0
  17. package/dts/chart/area/DottedArea.d.ts.map +1 -0
  18. package/dts/chart/area/GradientArea.d.ts +36 -0
  19. package/dts/chart/area/GradientArea.d.ts.map +1 -0
  20. package/dts/chart/area/SolidArea.d.ts +23 -0
  21. package/dts/chart/area/SolidArea.d.ts.map +1 -0
  22. package/dts/chart/area/index.d.ts +6 -0
  23. package/dts/chart/area/index.d.ts.map +1 -0
  24. package/dts/chart/axis/Axis.d.ts +194 -0
  25. package/dts/chart/axis/Axis.d.ts.map +1 -0
  26. package/dts/chart/axis/DefaultAxisTickLabel.d.ts +8 -0
  27. package/dts/chart/axis/DefaultAxisTickLabel.d.ts.map +1 -0
  28. package/dts/chart/axis/XAxis.d.ts +16 -0
  29. package/dts/chart/axis/XAxis.d.ts.map +1 -0
  30. package/dts/chart/axis/YAxis.d.ts +21 -0
  31. package/dts/chart/axis/YAxis.d.ts.map +1 -0
  32. package/dts/chart/axis/index.d.ts +5 -0
  33. package/dts/chart/axis/index.d.ts.map +1 -0
  34. package/dts/chart/bar/Bar.d.ts +92 -0
  35. package/dts/chart/bar/Bar.d.ts.map +1 -0
  36. package/dts/chart/bar/BarChart.d.ts +113 -0
  37. package/dts/chart/bar/BarChart.d.ts.map +1 -0
  38. package/dts/chart/bar/BarPlot.d.ts +30 -0
  39. package/dts/chart/bar/BarPlot.d.ts.map +1 -0
  40. package/dts/chart/bar/BarStack.d.ts +102 -0
  41. package/dts/chart/bar/BarStack.d.ts.map +1 -0
  42. package/dts/chart/bar/BarStackGroup.d.ts +36 -0
  43. package/dts/chart/bar/BarStackGroup.d.ts.map +1 -0
  44. package/dts/chart/bar/DefaultBar.d.ts +7 -0
  45. package/dts/chart/bar/DefaultBar.d.ts.map +1 -0
  46. package/dts/chart/bar/DefaultBarStack.d.ts +7 -0
  47. package/dts/chart/bar/DefaultBarStack.d.ts.map +1 -0
  48. package/dts/chart/bar/index.d.ts +8 -0
  49. package/dts/chart/bar/index.d.ts.map +1 -0
  50. package/dts/chart/gradient/Gradient.d.ts +25 -0
  51. package/dts/chart/gradient/Gradient.d.ts.map +1 -0
  52. package/dts/chart/gradient/index.d.ts +2 -0
  53. package/dts/chart/gradient/index.d.ts.map +1 -0
  54. package/dts/chart/index.d.ts +15 -0
  55. package/dts/chart/index.d.ts.map +1 -0
  56. package/dts/chart/line/DefaultReferenceLineLabel.d.ts +9 -0
  57. package/dts/chart/line/DefaultReferenceLineLabel.d.ts.map +1 -0
  58. package/dts/chart/line/DottedLine.d.ts +20 -0
  59. package/dts/chart/line/DottedLine.d.ts.map +1 -0
  60. package/dts/chart/line/Line.d.ts +115 -0
  61. package/dts/chart/line/Line.d.ts.map +1 -0
  62. package/dts/chart/line/LineChart.d.ts +118 -0
  63. package/dts/chart/line/LineChart.d.ts.map +1 -0
  64. package/dts/chart/line/ReferenceLine.d.ts +139 -0
  65. package/dts/chart/line/ReferenceLine.d.ts.map +1 -0
  66. package/dts/chart/line/SolidLine.d.ts +15 -0
  67. package/dts/chart/line/SolidLine.d.ts.map +1 -0
  68. package/dts/chart/line/index.d.ts +7 -0
  69. package/dts/chart/line/index.d.ts.map +1 -0
  70. package/dts/chart/point/DefaultPointLabel.d.ts +10 -0
  71. package/dts/chart/point/DefaultPointLabel.d.ts.map +1 -0
  72. package/dts/chart/point/Point.d.ts +120 -0
  73. package/dts/chart/point/Point.d.ts.map +1 -0
  74. package/dts/chart/point/index.d.ts +3 -0
  75. package/dts/chart/point/index.d.ts.map +1 -0
  76. package/dts/chart/scrubber/DefaultScrubberBeacon.d.ts +8 -0
  77. package/dts/chart/scrubber/DefaultScrubberBeacon.d.ts.map +1 -0
  78. package/dts/chart/scrubber/DefaultScrubberBeaconLabel.d.ts +12 -0
  79. package/dts/chart/scrubber/DefaultScrubberBeaconLabel.d.ts.map +1 -0
  80. package/dts/chart/scrubber/DefaultScrubberLabel.d.ts +11 -0
  81. package/dts/chart/scrubber/DefaultScrubberLabel.d.ts.map +1 -0
  82. package/dts/chart/scrubber/Scrubber.d.ts +233 -0
  83. package/dts/chart/scrubber/Scrubber.d.ts.map +1 -0
  84. package/dts/chart/scrubber/ScrubberBeaconGroup.d.ts +44 -0
  85. package/dts/chart/scrubber/ScrubberBeaconGroup.d.ts.map +1 -0
  86. package/dts/chart/scrubber/ScrubberBeaconLabelGroup.d.ts +31 -0
  87. package/dts/chart/scrubber/ScrubberBeaconLabelGroup.d.ts.map +1 -0
  88. package/dts/chart/scrubber/ScrubberProvider.d.ts +20 -0
  89. package/dts/chart/scrubber/ScrubberProvider.d.ts.map +1 -0
  90. package/dts/chart/scrubber/index.d.ts +5 -0
  91. package/dts/chart/scrubber/index.d.ts.map +1 -0
  92. package/dts/chart/text/ChartText.d.ts +164 -0
  93. package/dts/chart/text/ChartText.d.ts.map +1 -0
  94. package/dts/chart/text/ChartTextGroup.d.ts +61 -0
  95. package/dts/chart/text/ChartTextGroup.d.ts.map +1 -0
  96. package/dts/chart/text/index.d.ts +3 -0
  97. package/dts/chart/text/index.d.ts.map +1 -0
  98. package/dts/chart/utils/axis.d.ts +342 -0
  99. package/dts/chart/utils/axis.d.ts.map +1 -0
  100. package/dts/chart/utils/bar.d.ts +20 -0
  101. package/dts/chart/utils/bar.d.ts.map +1 -0
  102. package/dts/chart/utils/chart.d.ts +124 -0
  103. package/dts/chart/utils/chart.d.ts.map +1 -0
  104. package/dts/chart/utils/context.d.ts +116 -0
  105. package/dts/chart/utils/context.d.ts.map +1 -0
  106. package/dts/chart/utils/gradient.d.ts +117 -0
  107. package/dts/chart/utils/gradient.d.ts.map +1 -0
  108. package/dts/chart/utils/index.d.ts +11 -0
  109. package/dts/chart/utils/index.d.ts.map +1 -0
  110. package/dts/chart/utils/path.d.ts +160 -0
  111. package/dts/chart/utils/path.d.ts.map +1 -0
  112. package/dts/chart/utils/point.d.ts +134 -0
  113. package/dts/chart/utils/point.d.ts.map +1 -0
  114. package/dts/chart/utils/scale.d.ts +134 -0
  115. package/dts/chart/utils/scale.d.ts.map +1 -0
  116. package/dts/chart/utils/scrubber.d.ts +39 -0
  117. package/dts/chart/utils/scrubber.d.ts.map +1 -0
  118. package/dts/chart/utils/transition.d.ts +140 -0
  119. package/dts/chart/utils/transition.d.ts.map +1 -0
  120. package/dts/index.d.ts +2 -1
  121. package/dts/index.d.ts.map +1 -1
  122. package/dts/sparkline/Counter.d.ts +7 -2
  123. package/dts/sparkline/Sparkline.d.ts +67 -16
  124. package/dts/sparkline/Sparkline.d.ts.map +1 -1
  125. package/dts/sparkline/SparklineArea.d.ts +10 -4
  126. package/dts/sparkline/SparklineArea.d.ts.map +1 -1
  127. package/dts/sparkline/SparklineAreaPattern.d.ts +12 -4
  128. package/dts/sparkline/SparklineAreaPattern.d.ts.map +1 -1
  129. package/dts/sparkline/SparklineGradient.d.ts +21 -10
  130. package/dts/sparkline/SparklineGradient.d.ts.map +1 -1
  131. package/dts/sparkline/__figma__/Sparkline.figma.d.ts +1 -1
  132. package/dts/sparkline/generateSparklineWithId.d.ts +8 -2
  133. package/dts/sparkline/generateSparklineWithId.d.ts.map +1 -1
  134. package/dts/sparkline/index.d.ts +1 -1
  135. package/dts/sparkline/sparkline-interactive/SparklineAccessibleView.d.ts +8 -3
  136. package/dts/sparkline/sparkline-interactive/SparklineInteractive.d.ts +132 -110
  137. package/dts/sparkline/sparkline-interactive/SparklineInteractive.d.ts.map +1 -1
  138. package/dts/sparkline/sparkline-interactive/SparklineInteractiveAnimatedPath.d.ts +22 -9
  139. package/dts/sparkline/sparkline-interactive/SparklineInteractiveAnimatedPath.d.ts.map +1 -1
  140. package/dts/sparkline/sparkline-interactive/SparklineInteractiveHoverDate.d.ts +18 -7
  141. package/dts/sparkline/sparkline-interactive/SparklineInteractiveLineVertical.d.ts +9 -4
  142. package/dts/sparkline/sparkline-interactive/SparklineInteractiveMarkerDates.d.ts +11 -6
  143. package/dts/sparkline/sparkline-interactive/SparklineInteractiveMinMax.d.ts +7 -5
  144. package/dts/sparkline/sparkline-interactive/SparklineInteractivePanGestureHandler.d.ts +22 -10
  145. package/dts/sparkline/sparkline-interactive/SparklineInteractivePaths.d.ts +21 -7
  146. package/dts/sparkline/sparkline-interactive/SparklineInteractivePaths.d.ts.map +1 -1
  147. package/dts/sparkline/sparkline-interactive/SparklineInteractivePeriodSelector.d.ts +21 -16
  148. package/dts/sparkline/sparkline-interactive/SparklineInteractiveProvider.d.ts +29 -23
  149. package/dts/sparkline/sparkline-interactive/SparklineInteractiveTimeseriesPaths.d.ts +22 -14
  150. package/dts/sparkline/sparkline-interactive/__figma__/SparklineInteractive.figma.d.ts +1 -1
  151. package/dts/sparkline/sparkline-interactive/useInterruptiblePathAnimation.d.ts +9 -5
  152. package/dts/sparkline/sparkline-interactive/useMinMaxTransform.d.ts +11 -6
  153. package/dts/sparkline/sparkline-interactive/useOpacityAnimation.d.ts +5 -2
  154. package/dts/sparkline/sparkline-interactive/useSparklineInteractiveConstants.d.ts +17 -17
  155. package/dts/sparkline/sparkline-interactive/useSparklineInteractiveLineStyles.d.ts +16 -13
  156. package/dts/sparkline/sparkline-interactive-header/SparklineInteractiveHeader.d.ts +106 -98
  157. package/dts/sparkline/sparkline-interactive-header/__figma__/SparklineInteractiveHeader.figma.d.ts +1 -1
  158. package/dts/sparkline/sparkline-interactive-header/useSparklineInteractiveHeaderStyles.d.ts +22 -19
  159. package/esm/chart/CartesianChart.js +335 -0
  160. package/esm/chart/ChartContextBridge.js +148 -0
  161. package/esm/chart/ChartProvider.js +10 -0
  162. package/esm/chart/Path.js +218 -0
  163. package/esm/chart/PeriodSelector.js +136 -0
  164. package/esm/chart/__stories__/CartesianChart.stories.js +723 -0
  165. package/esm/chart/__stories__/Chart.stories.js +77 -0
  166. package/esm/chart/__stories__/PeriodSelector.stories.js +322 -0
  167. package/esm/chart/area/Area.js +75 -0
  168. package/esm/chart/area/AreaChart.js +151 -0
  169. package/esm/chart/area/DottedArea.js +80 -0
  170. package/esm/chart/area/GradientArea.js +54 -0
  171. package/esm/chart/area/SolidArea.js +38 -0
  172. package/esm/chart/area/__stories__/AreaChart.stories.js +100 -0
  173. package/esm/chart/area/index.js +7 -0
  174. package/esm/chart/axis/Axis.js +45 -0
  175. package/esm/chart/axis/DefaultAxisTickLabel.js +11 -0
  176. package/esm/chart/axis/XAxis.js +188 -0
  177. package/esm/chart/axis/YAxis.js +177 -0
  178. package/esm/chart/axis/__stories__/Axis.stories.js +276 -0
  179. package/esm/chart/axis/index.js +6 -0
  180. package/esm/chart/bar/Bar.js +69 -0
  181. package/esm/chart/bar/BarChart.js +125 -0
  182. package/esm/chart/bar/BarPlot.js +102 -0
  183. package/esm/chart/bar/BarStack.js +551 -0
  184. package/esm/chart/bar/BarStackGroup.js +79 -0
  185. package/esm/chart/bar/DefaultBar.js +56 -0
  186. package/esm/chart/bar/DefaultBarStack.js +47 -0
  187. package/esm/chart/bar/__stories__/BarChart.stories.js +668 -0
  188. package/esm/chart/bar/index.js +9 -0
  189. package/esm/chart/gradient/Gradient.js +53 -0
  190. package/esm/chart/gradient/index.js +1 -0
  191. package/esm/chart/index.js +16 -0
  192. package/esm/chart/line/DefaultReferenceLineLabel.js +66 -0
  193. package/esm/chart/line/DottedLine.js +50 -0
  194. package/esm/chart/line/Line.js +178 -0
  195. package/esm/chart/line/LineChart.js +121 -0
  196. package/esm/chart/line/ReferenceLine.js +132 -0
  197. package/esm/chart/line/SolidLine.js +46 -0
  198. package/esm/chart/line/__stories__/LineChart.stories.js +2372 -0
  199. package/esm/chart/line/__stories__/ReferenceLine.stories.js +132 -0
  200. package/esm/chart/line/index.js +8 -0
  201. package/esm/chart/point/DefaultPointLabel.js +39 -0
  202. package/esm/chart/point/Point.js +188 -0
  203. package/esm/chart/point/index.js +2 -0
  204. package/esm/chart/scrubber/DefaultScrubberBeacon.js +179 -0
  205. package/esm/chart/scrubber/DefaultScrubberBeaconLabel.js +43 -0
  206. package/esm/chart/scrubber/DefaultScrubberLabel.js +28 -0
  207. package/esm/chart/scrubber/Scrubber.js +166 -0
  208. package/esm/chart/scrubber/ScrubberBeaconGroup.js +161 -0
  209. package/esm/chart/scrubber/ScrubberBeaconLabelGroup.js +185 -0
  210. package/esm/chart/scrubber/ScrubberProvider.js +135 -0
  211. package/esm/chart/scrubber/index.js +4 -0
  212. package/esm/chart/text/ChartText.js +305 -0
  213. package/esm/chart/text/ChartTextGroup.js +211 -0
  214. package/esm/chart/text/index.js +4 -0
  215. package/esm/chart/utils/axis.js +592 -0
  216. package/esm/chart/utils/bar.js +24 -0
  217. package/esm/chart/utils/chart.js +270 -0
  218. package/esm/chart/utils/context.js +15 -0
  219. package/esm/chart/utils/gradient.js +305 -0
  220. package/esm/chart/utils/index.js +12 -0
  221. package/esm/chart/utils/path.js +274 -0
  222. package/esm/chart/utils/point.js +229 -0
  223. package/esm/chart/utils/scale.js +277 -0
  224. package/esm/chart/utils/scrubber.js +139 -0
  225. package/esm/chart/utils/transition.js +185 -0
  226. package/esm/index.js +4 -1
  227. package/esm/sparkline/Sparkline.js +129 -16
  228. package/esm/sparkline/SparklineArea.js +7 -2
  229. package/esm/sparkline/SparklineAreaPattern.js +4 -2
  230. package/esm/sparkline/SparklineGradient.js +4 -0
  231. package/esm/sparkline/__stories__/Sparkline.stories.js +11 -7
  232. package/esm/sparkline/__stories__/SparklineGradient.stories.js +7 -4
  233. package/esm/sparkline/generateSparklineWithId.js +3 -2
  234. package/esm/sparkline/sparkline-interactive/SparklineInteractive.js +5 -1
  235. package/esm/sparkline/sparkline-interactive/SparklineInteractiveAnimatedPath.js +5 -2
  236. package/esm/sparkline/sparkline-interactive/SparklineInteractivePaths.js +4 -0
  237. package/esm/sparkline/sparkline-interactive/__stories__/SparklineInteractive.stories.js +76 -24
  238. package/esm/sparkline/sparkline-interactive-header/__stories__/SparklineInteractiveHeader.stories.js +17 -9
  239. package/package.json +17 -11
  240. package/dts/sparkline/__stories__/Sparkline.stories.d.ts +0 -3
  241. package/dts/sparkline/__stories__/Sparkline.stories.d.ts.map +0 -1
  242. package/dts/sparkline/__stories__/SparklineGradient.stories.d.ts +0 -3
  243. package/dts/sparkline/__stories__/SparklineGradient.stories.d.ts.map +0 -1
  244. package/dts/sparkline/sparkline-interactive/__stories__/SparklineInteractive.stories.d.ts +0 -3
  245. package/dts/sparkline/sparkline-interactive/__stories__/SparklineInteractive.stories.d.ts.map +0 -1
  246. package/dts/sparkline/sparkline-interactive/__tests__/SparklineInteractive.test.d.ts +0 -2
  247. package/dts/sparkline/sparkline-interactive/__tests__/SparklineInteractive.test.d.ts.map +0 -1
  248. package/dts/sparkline/sparkline-interactive/__tests__/SparklineInteractiveHoverDate.test.d.ts +0 -2
  249. package/dts/sparkline/sparkline-interactive/__tests__/SparklineInteractiveHoverDate.test.d.ts.map +0 -1
  250. package/dts/sparkline/sparkline-interactive/__tests__/SparklineInteractivePanGestureHandler.test.d.ts +0 -2
  251. package/dts/sparkline/sparkline-interactive/__tests__/SparklineInteractivePanGestureHandler.test.d.ts.map +0 -1
  252. package/dts/sparkline/sparkline-interactive/__tests__/SparklineInteractivePeriodSelector.test.d.ts +0 -2
  253. package/dts/sparkline/sparkline-interactive/__tests__/SparklineInteractivePeriodSelector.test.d.ts.map +0 -1
  254. package/dts/sparkline/sparkline-interactive/__tests__/SparklineInteractiveTimeseriesPaths.test.d.ts +0 -2
  255. package/dts/sparkline/sparkline-interactive/__tests__/SparklineInteractiveTimeseriesPaths.test.d.ts.map +0 -1
  256. package/dts/sparkline/sparkline-interactive/__tests__/useMinMaxTransform.test.d.ts +0 -2
  257. package/dts/sparkline/sparkline-interactive/__tests__/useMinMaxTransform.test.d.ts.map +0 -1
  258. package/dts/sparkline/sparkline-interactive/useInterruptiblePathAnimation.test.disable.d.ts +0 -2
  259. package/dts/sparkline/sparkline-interactive/useInterruptiblePathAnimation.test.disable.d.ts.map +0 -1
  260. package/dts/sparkline/sparkline-interactive-header/__stories__/SparklineInteractiveHeader.stories.d.ts +0 -4
  261. package/dts/sparkline/sparkline-interactive-header/__stories__/SparklineInteractiveHeader.stories.d.ts.map +0 -1
  262. package/dts/sparkline/sparkline-interactive-header/__tests__/SparklineInteractiveHeader.test.d.ts +0 -2
  263. package/dts/sparkline/sparkline-interactive-header/__tests__/SparklineInteractiveHeader.test.d.ts.map +0 -1
  264. package/dts/sparkline/sparkline-interactive-header/__tests__/useSparklineInteractiveHeaderStyles.test.d.ts +0 -2
  265. package/dts/sparkline/sparkline-interactive-header/__tests__/useSparklineInteractiveHeaderStyles.test.d.ts.map +0 -1
@@ -0,0 +1,274 @@
1
+ import { area as d3Area, curveBumpX, curveCatmullRom, curveLinear, curveLinearClosed, curveMonotoneX, curveNatural, curveStep, curveStepAfter, curveStepBefore, line as d3Line } from 'd3-shape';
2
+ import { projectPoint, projectPoints } from './point';
3
+ import { isCategoricalScale } from './scale';
4
+ /**
5
+ * Get the d3 curve function for a path.
6
+ * See https://d3js.org/d3-shape/curve
7
+ * @param curve - The curve type. Defaults to 'linear'.
8
+ * @returns The d3 curve function.
9
+ */
10
+ export const getPathCurveFunction = function (curve) {
11
+ if (curve === void 0) {
12
+ curve = 'linear';
13
+ }
14
+ switch (curve) {
15
+ case 'catmullRom':
16
+ return curveCatmullRom;
17
+ case 'monotone':
18
+ // When we support layout="vertical" this should dynamically switch to curveMonotoneY
19
+ return curveMonotoneX;
20
+ case 'natural':
21
+ return curveNatural;
22
+ case 'step':
23
+ return curveStep;
24
+ case 'stepBefore':
25
+ return curveStepBefore;
26
+ case 'stepAfter':
27
+ return curveStepAfter;
28
+ case 'bump':
29
+ // When we support layout="vertical" this should dynamically switch to curveBumpY
30
+ return curveBumpX;
31
+ case 'linearClosed':
32
+ return curveLinearClosed;
33
+ case 'linear':
34
+ default:
35
+ return curveLinear;
36
+ }
37
+ };
38
+
39
+ /**
40
+ * Generates an SVG line path string from data using chart scale functions.
41
+ *
42
+ * @example
43
+ * ```typescript
44
+ * const chartScale = getChartScale({ chartRect, domain, range, xScale, yScale });
45
+ * const path = getLinePath({ data: [1, 2, 3], chartScale, curve: 'linear' });
46
+ * ```
47
+ */
48
+ export const getLinePath = _ref => {
49
+ var _pathGenerator;
50
+ let {
51
+ data,
52
+ curve = 'bump',
53
+ xScale,
54
+ yScale,
55
+ xData,
56
+ connectNulls = false
57
+ } = _ref;
58
+ if (data.length === 0) {
59
+ return '';
60
+ }
61
+ const curveFunction = getPathCurveFunction(curve);
62
+ const dataPoints = projectPoints({
63
+ data,
64
+ xScale,
65
+ yScale,
66
+ xData
67
+ });
68
+
69
+ // When connectNulls is true, filter out null values before rendering
70
+ // When false, use defined() to create gaps in the line
71
+ const filteredPoints = connectNulls ? dataPoints.filter(d => d !== null) : dataPoints;
72
+ const pathGenerator = d3Line().x(d => d.x).y(d => d.y).curve(curveFunction).defined(d => connectNulls || d !== null);
73
+ return (_pathGenerator = pathGenerator(filteredPoints)) != null ? _pathGenerator : '';
74
+ };
75
+
76
+ /**
77
+ * Generates an SVG area path string from data using chart scale functions.
78
+ * Supports both single values (area from baseline to value) and tuples ([baseline, value]).
79
+ *
80
+ * @example
81
+ * ```typescript
82
+ * // Single values - area from baseline to value
83
+ * const area = getAreaPath({
84
+ * data: [1, 2, 3],
85
+ * xScale,
86
+ * yScale,
87
+ * });
88
+ *
89
+ * // Range values - area from low to high
90
+ * const rangeArea = getAreaPath({
91
+ * data: [[0, 3], [2, 4], [1, 5]],
92
+ * xScale,
93
+ * yScale,
94
+ * curve: 'monotone'
95
+ * });
96
+ * ```
97
+ */
98
+ export const getAreaPath = _ref2 => {
99
+ let {
100
+ data,
101
+ curve = 'bump',
102
+ xScale,
103
+ yScale,
104
+ xData,
105
+ connectNulls = false
106
+ } = _ref2;
107
+ if (data.length === 0) {
108
+ return '';
109
+ }
110
+ const curveFunction = getPathCurveFunction(curve);
111
+ const yDomain = yScale.domain();
112
+ const yMin = Math.min(...yDomain);
113
+ const normalizedData = data.map((item, index) => {
114
+ if (item === null) {
115
+ return null;
116
+ }
117
+ if (Array.isArray(item)) {
118
+ if (item.length >= 2 && typeof item[0] === 'number' && typeof item[1] === 'number') {
119
+ return [item[0], item[1]];
120
+ }
121
+ return null;
122
+ }
123
+ if (typeof item === 'number') {
124
+ return [yMin, item];
125
+ }
126
+ return null;
127
+ });
128
+ const dataPoints = normalizedData.map((range, index) => {
129
+ if (range === null) {
130
+ return {
131
+ x: 0,
132
+ low: null,
133
+ high: null,
134
+ isValid: false
135
+ };
136
+ }
137
+ let xValue = index;
138
+ if (!isCategoricalScale(xScale) && xData && xData[index] !== undefined) {
139
+ xValue = xData[index];
140
+ }
141
+ const xPoint = projectPoint({
142
+ x: xValue,
143
+ y: 0,
144
+ xScale,
145
+ yScale
146
+ });
147
+ const lowPoint = projectPoint({
148
+ x: xValue,
149
+ y: range[0],
150
+ xScale,
151
+ yScale
152
+ });
153
+ const highPoint = projectPoint({
154
+ x: xValue,
155
+ y: range[1],
156
+ xScale,
157
+ yScale
158
+ });
159
+ return {
160
+ x: xPoint.x,
161
+ low: lowPoint.y,
162
+ high: highPoint.y,
163
+ isValid: true
164
+ };
165
+ });
166
+
167
+ // When connectNulls is true, filter out invalid points before rendering
168
+ // When false, use defined() to create gaps in the area
169
+ const filteredPoints = connectNulls ? dataPoints.filter(d => d.isValid) : dataPoints;
170
+ const areaGenerator = d3Area().x(d => d.x).y0(d => {
171
+ var _d$low;
172
+ return (_d$low = d.low) != null ? _d$low : 0;
173
+ }) // Bottom boundary (low values), fallback to 0
174
+ .y1(d => {
175
+ var _d$high;
176
+ return (_d$high = d.high) != null ? _d$high : 0;
177
+ }) // Top boundary (high values), fallback to 0
178
+ .curve(curveFunction).defined(d => connectNulls || d.isValid && d.low != null && d.high != null); // Only draw where both values exist
179
+
180
+ const result = areaGenerator(filteredPoints);
181
+ return result != null ? result : '';
182
+ };
183
+
184
+ /**
185
+ * Converts line coordinates to an SVG path string.
186
+ * Useful for rendering axis lines and tick marks.
187
+ *
188
+ * @example
189
+ * ```typescript
190
+ * const path = lineToPath(0, 0, 100, 100);
191
+ * // Returns: "M 0 0 L 100 100"
192
+ * ```
193
+ */
194
+ export const lineToPath = (x1, y1, x2, y2) => {
195
+ return "M " + x1 + " " + y1 + " L " + x2 + " " + y2;
196
+ };
197
+
198
+ /**
199
+ * Creates an SVG path string for a rectangle with selective corner rounding.
200
+ * Useful for creating bars in charts with optional rounded corners.
201
+ *
202
+ * @example
203
+ * ```typescript
204
+ * // Simple rectangle bar
205
+ * const barPath = getBarPath(10, 20, 50, 100, 0, false, false);
206
+ *
207
+ * // Bar with rounded top corners
208
+ * const roundedPath = getBarPath(10, 20, 50, 100, 8, true, false);
209
+ * ```
210
+ */
211
+ export const getBarPath = (x, y, width, height, radius, roundTop, roundBottom) => {
212
+ const roundBothSides = roundTop && roundBottom;
213
+ const r = Math.min(radius, width / 2, roundBothSides ? height / 2 : height);
214
+ const topR = roundTop ? r : 0;
215
+ const bottomR = roundBottom ? r : 0;
216
+
217
+ // Build path with selective rounding
218
+ let path = "M " + (x + (roundTop ? r : 0)) + " " + y;
219
+ path += " L " + (x + width - topR) + " " + y;
220
+ path += " A " + topR + " " + topR + " 0 0 1 " + (x + width) + " " + (y + topR);
221
+ path += " L " + (x + width) + " " + (y + height - bottomR);
222
+ path += " A " + bottomR + " " + bottomR + " 0 0 1 " + (x + width - bottomR) + " " + (y + height);
223
+ path += " L " + (x + bottomR) + " " + (y + height);
224
+ path += " A " + bottomR + " " + bottomR + " 0 0 1 " + x + " " + (y + height - bottomR);
225
+ path += " L " + x + " " + (y + topR);
226
+ path += " A " + topR + " " + topR + " 0 0 1 " + (x + topR) + " " + y;
227
+ path += ' Z';
228
+ return path;
229
+ };
230
+
231
+ /**
232
+ * Generates an SVG path string with circles arranged in a dotted pattern within a bounding area.
233
+ * Creates circles at regular intervals based on the pattern size and dot size parameters.
234
+ *
235
+ * @param bounds - The bounding rectangle to fill with dots
236
+ * @param patternSize - Size of the pattern unit (spacing between dots)
237
+ * @param dotSize - Radius of each dot
238
+ * @returns SVG path string containing all the circles
239
+ *
240
+ * @example
241
+ * ```typescript
242
+ * const dotsPath = getDottedAreaPath(
243
+ * { x: 0, y: 0, width: 100, height: 50 },
244
+ * 8, // 8px spacing
245
+ * 2 // 2px radius dots
246
+ * );
247
+ * ```
248
+ */
249
+ export const getDottedAreaPath = (bounds, patternSize, dotSize) => {
250
+ if (bounds.width <= 0 || bounds.height <= 0 || patternSize <= 0 || dotSize <= 0) {
251
+ return '';
252
+ }
253
+ let path = '';
254
+
255
+ // Calculate the number of dots that fit in each dimension
256
+ const dotsX = Math.ceil(bounds.width / patternSize);
257
+ const dotsY = Math.ceil(bounds.height / patternSize);
258
+
259
+ // Generate circles in a grid pattern
260
+ for (let row = 0; row < dotsY; row++) {
261
+ for (let col = 0; col < dotsX; col++) {
262
+ const centerX = bounds.x + col * patternSize + patternSize / 2;
263
+ const centerY = bounds.y + row * patternSize + patternSize / 2;
264
+
265
+ // Only draw dots that are within the bounds
266
+ if (centerX >= bounds.x && centerX <= bounds.x + bounds.width && centerY >= bounds.y && centerY <= bounds.y + bounds.height) {
267
+ // Create circle using SVG arc commands
268
+ // M cx,cy-r a r,r 0 1,0 0,2r a r,r 0 1,0 0,-2r
269
+ path += "M " + centerX + "," + (centerY - dotSize) + " a " + dotSize + "," + dotSize + " 0 1,0 0," + dotSize * 2 + " a " + dotSize + "," + dotSize + " 0 1,0 0," + -dotSize * 2 + " ";
270
+ }
271
+ }
272
+ }
273
+ return path.trim();
274
+ };
@@ -0,0 +1,229 @@
1
+ import { applySerializableScale, isCategoricalScale, isLogScale, isNumericScale } from './scale';
2
+
3
+ /**
4
+ * Position a label should be placed relative to the point
5
+ *
6
+ * @example
7
+ * 'top' would have the label be located above the point itself,
8
+ * and thus the vertical alignment of that text would be bottom.
9
+ */
10
+
11
+ /**
12
+ * Get a point from a data value and a scale.
13
+ * @note for categorical scales, the point will be centered within the band.
14
+ * @note for log scales, zero and negative values are clamped to a small positive value.
15
+ * @param data - the data value.
16
+ * @param scale - the scale function.
17
+ * @returns the pixel value (defaulting to 0 if data value is not defined in scale).
18
+ */
19
+ export const getPointOnScale = (dataValue, scale) => {
20
+ var _scale2;
21
+ if (isCategoricalScale(scale)) {
22
+ var _scale, _scale$bandwidth;
23
+ const bandStart = (_scale = scale(dataValue)) != null ? _scale : 0;
24
+ const bandwidth = (_scale$bandwidth = scale.bandwidth()) != null ? _scale$bandwidth : 0;
25
+ return bandStart + bandwidth / 2;
26
+ }
27
+
28
+ // For log scales, ensure the value is positive
29
+ let adjustedValue = dataValue;
30
+ if (isLogScale(scale) && dataValue <= 0) {
31
+ adjustedValue = 0.001; // Use a small positive value for log scales
32
+ }
33
+ return (_scale2 = scale(adjustedValue)) != null ? _scale2 : 0;
34
+ };
35
+
36
+ /**
37
+ * Get a point from a data value and a serializable scale (worklet-compatible).
38
+ * @note for categorical scales, the point will be centered within the band.
39
+ * @note for log scales, zero and negative values are clamped to a small positive value.
40
+ * @param dataValue - the data value.
41
+ * @param scale - the serializable scale object.
42
+ * @returns the pixel value (defaulting to 0 if data value is not defined in scale).
43
+ */
44
+ export function getPointOnSerializableScale(dataValue, scale) {
45
+ 'worklet';
46
+
47
+ if (scale.type === 'band') {
48
+ const bandStart = applySerializableScale(dataValue, scale);
49
+ return bandStart + scale.bandwidth / 2;
50
+ }
51
+
52
+ // For log scales, ensure the value is positive
53
+ if (scale.type === 'log' && dataValue <= 0) {
54
+ dataValue = 0.001; // Use a small positive value for log scales
55
+ }
56
+ return applySerializableScale(dataValue, scale);
57
+ }
58
+
59
+ /**
60
+ * Projects a single data point to pixel coordinates using serializable scales.
61
+ * This is the worklet-compatible version for use in react-native-reanimated.
62
+ */
63
+ export function projectPointWithSerializableScale(_ref) {
64
+ 'worklet';
65
+
66
+ let {
67
+ x,
68
+ y,
69
+ xScale,
70
+ yScale
71
+ } = _ref;
72
+ return {
73
+ x: getPointOnSerializableScale(x, xScale),
74
+ y: getPointOnSerializableScale(y, yScale)
75
+ };
76
+ }
77
+
78
+ /**
79
+ * Projects a data point to pixel coordinates using the chart scale.
80
+ * Automatically handles log scale transformations for zero/negative values.
81
+ *
82
+ * @example
83
+ * ```typescript
84
+ * const chartScale = getChartScale({ chartRect, domain, range, xScale, yScale });
85
+ * const pixelCoord = projectPoint({ x: 5, y: 10, chartScale });
86
+ * ```
87
+ * @example
88
+ * ```typescript
89
+ * const chartScale = getChartScale({ chartRect, domain, range, xScale, yScale });
90
+ * const pixelCoord = projectPoint({ x: 2, y: 10, chartScale, xData: ['Jan', 'Feb', 'Mar'] });
91
+ * ```
92
+ */
93
+ export const projectPoint = _ref2 => {
94
+ let {
95
+ x,
96
+ y,
97
+ xScale,
98
+ yScale
99
+ } = _ref2;
100
+ return {
101
+ x: getPointOnScale(x, xScale),
102
+ y: getPointOnScale(y, yScale)
103
+ };
104
+ };
105
+
106
+ /**
107
+ * Projects multiple data points to pixel coordinates using chart scale functions.
108
+ * Handles both numeric and band scales automatically.
109
+ *
110
+ * @example
111
+ * ```typescript
112
+ * const chartScale = getChartScale({ chartRect, domain, range, xScale, yScale });
113
+ * const pixelPoints = projectPoints({ data, chartScale });
114
+ * // For mixed scales
115
+ * const pixelPoints = projectPoints({ data, chartScale, xData: ['Jan', 'Feb', 'Mar'] });
116
+ * ```
117
+ */
118
+ export const projectPoints = _ref3 => {
119
+ let {
120
+ data,
121
+ xScale,
122
+ yScale,
123
+ xData,
124
+ yData
125
+ } = _ref3;
126
+ if (data.length === 0) {
127
+ return [];
128
+ }
129
+ return data.map((value, index) => {
130
+ if (value === null) {
131
+ return null;
132
+ }
133
+ if (typeof value === 'object' && 'x' in value && 'y' in value) {
134
+ return projectPoint({
135
+ x: value.x,
136
+ y: value.y,
137
+ xScale,
138
+ yScale
139
+ });
140
+ }
141
+
142
+ // For scales with axis data, determine the correct x value
143
+ let xValue = index;
144
+
145
+ // For band scales, always use the index
146
+ if (!isCategoricalScale(xScale)) {
147
+ // For numeric scales with axis data, use the axis data values instead of indices
148
+ if (xData && Array.isArray(xData) && xData.length > 0) {
149
+ // Check if it's numeric data
150
+ if (typeof xData[0] === 'number') {
151
+ var _numericXData$index;
152
+ const numericXData = xData;
153
+ xValue = (_numericXData$index = numericXData[index]) != null ? _numericXData$index : index;
154
+ }
155
+ }
156
+ }
157
+ let yValue = value;
158
+ if (isNumericScale(yScale) && yData && Array.isArray(yData) && yData.length > 0 && typeof yData[0] === 'number' && typeof value === 'number') {
159
+ yValue = value;
160
+ }
161
+ return projectPoint({
162
+ x: xValue,
163
+ y: yValue,
164
+ xScale,
165
+ yScale
166
+ });
167
+ });
168
+ };
169
+
170
+ /**
171
+ * Determines text alignment based on label position.
172
+ * For example, a 'top' position needs the text aligned to the 'bottom' so it appears above the point.
173
+ */
174
+ export const getAlignmentFromPosition = position => {
175
+ let horizontalAlignment = 'center';
176
+ let verticalAlignment = 'middle';
177
+ switch (position) {
178
+ case 'top':
179
+ verticalAlignment = 'bottom';
180
+ break;
181
+ case 'bottom':
182
+ verticalAlignment = 'top';
183
+ break;
184
+ case 'left':
185
+ horizontalAlignment = 'right';
186
+ break;
187
+ case 'right':
188
+ horizontalAlignment = 'left';
189
+ break;
190
+ case 'center':
191
+ default:
192
+ horizontalAlignment = 'center';
193
+ verticalAlignment = 'middle';
194
+ break;
195
+ }
196
+ return {
197
+ horizontalAlignment,
198
+ verticalAlignment
199
+ };
200
+ };
201
+
202
+ /**
203
+ * Calculates the final label coordinates by applying offset based on position.
204
+ */
205
+ export const getLabelCoordinates = (x, y, position, offset) => {
206
+ let dx = 0;
207
+ let dy = 0;
208
+ switch (position) {
209
+ case 'top':
210
+ dy = -offset;
211
+ break;
212
+ case 'bottom':
213
+ dy = offset;
214
+ break;
215
+ case 'left':
216
+ dx = -offset;
217
+ break;
218
+ case 'right':
219
+ dx = offset;
220
+ break;
221
+ case 'center':
222
+ default:
223
+ break;
224
+ }
225
+ return {
226
+ x: x + dx,
227
+ y: y + dy
228
+ };
229
+ };