stream-charts 1.0.0 → 1.1.0

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 (271) hide show
  1. package/README.md +758 -345
  2. package/changes.md +10 -0
  3. package/dist/src/app/charts/Chart.d.ts +116 -87
  4. package/dist/src/app/charts/Chart.js +96 -101
  5. package/dist/src/app/charts/Chart.js.map +1 -1
  6. package/dist/src/app/charts/axes/AxesState.d.ts +64 -0
  7. package/dist/src/app/charts/axes/AxesState.js +97 -0
  8. package/dist/src/app/charts/axes/AxesState.js.map +1 -0
  9. package/dist/src/app/charts/axes/AxesState.test.js +30 -0
  10. package/dist/src/app/charts/axes/AxesState.test.js.map +1 -0
  11. package/dist/src/app/charts/axes/AxisInterval.d.ts +92 -0
  12. package/dist/src/app/charts/axes/AxisInterval.js +126 -0
  13. package/dist/src/app/charts/axes/AxisInterval.js.map +1 -0
  14. package/dist/src/app/charts/axes/AxisInterval.test.js +120 -0
  15. package/dist/src/app/charts/axes/AxisInterval.test.js.map +1 -0
  16. package/dist/src/app/charts/axes/BaseAxisRange.d.ts +83 -0
  17. package/dist/src/app/charts/axes/BaseAxisRange.js +58 -0
  18. package/dist/src/app/charts/axes/BaseAxisRange.js.map +1 -0
  19. package/dist/src/app/charts/axes/ContinuousAxis.d.ts +22 -0
  20. package/dist/src/app/charts/axes/ContinuousAxis.js +96 -0
  21. package/dist/src/app/charts/axes/ContinuousAxis.js.map +1 -0
  22. package/dist/src/app/charts/axes/ContinuousAxisRange.d.ts +59 -0
  23. package/dist/src/app/charts/axes/ContinuousAxisRange.js +84 -0
  24. package/dist/src/app/charts/axes/ContinuousAxisRange.js.map +1 -0
  25. package/dist/src/app/charts/axes/ContinuousAxisRange.test.js +47 -0
  26. package/dist/src/app/charts/axes/ContinuousAxisRange.test.js.map +1 -0
  27. package/dist/src/app/charts/axes/OrdinalAxis.d.ts +23 -0
  28. package/dist/src/app/charts/axes/OrdinalAxis.js +124 -0
  29. package/dist/src/app/charts/axes/OrdinalAxis.js.map +1 -0
  30. package/dist/src/app/charts/axes/OrdinalAxisRange.d.ts +69 -0
  31. package/dist/src/app/charts/axes/OrdinalAxisRange.js +100 -0
  32. package/dist/src/app/charts/axes/OrdinalAxisRange.js.map +1 -0
  33. package/dist/src/app/charts/axes/OrdinalAxisRange.test.js +48 -0
  34. package/dist/src/app/charts/axes/OrdinalAxisRange.test.js.map +1 -0
  35. package/dist/src/app/charts/axes/axes.d.ts +391 -0
  36. package/dist/src/app/charts/axes/axes.js +1046 -0
  37. package/dist/src/app/charts/axes/axes.js.map +1 -0
  38. package/dist/src/app/charts/d3types.d.ts +10 -10
  39. package/dist/src/app/charts/filters/regexFilter.d.ts +9 -0
  40. package/dist/src/app/charts/filters/regexFilter.js +18 -0
  41. package/dist/src/app/charts/filters/regexFilter.js.map +1 -0
  42. package/dist/src/app/charts/filters/regexFilter.test.d.ts +1 -0
  43. package/dist/src/app/charts/filters/regexFilter.test.js +11 -0
  44. package/dist/src/app/charts/filters/regexFilter.test.js.map +1 -0
  45. package/dist/src/app/charts/hooks/useAxes.d.ts +144 -0
  46. package/dist/src/app/charts/hooks/useAxes.js +187 -0
  47. package/dist/src/app/charts/hooks/useAxes.js.map +1 -0
  48. package/dist/src/app/charts/hooks/useChart.d.ts +30 -201
  49. package/dist/src/app/charts/hooks/useChart.js +33 -134
  50. package/dist/src/app/charts/hooks/useChart.js.map +1 -1
  51. package/dist/src/app/charts/hooks/useDataObservable.d.ts +74 -0
  52. package/dist/src/app/charts/hooks/useDataObservable.js +76 -0
  53. package/dist/src/app/charts/hooks/useDataObservable.js.map +1 -0
  54. package/dist/src/app/charts/hooks/useInitialData.d.ts +38 -0
  55. package/dist/src/app/charts/hooks/useInitialData.js +29 -0
  56. package/dist/src/app/charts/hooks/useInitialData.js.map +1 -0
  57. package/dist/src/app/charts/hooks/useMouse.d.ts +53 -0
  58. package/dist/src/app/charts/hooks/useMouse.js +65 -0
  59. package/dist/src/app/charts/hooks/useMouse.js.map +1 -0
  60. package/dist/src/app/charts/hooks/usePlotDimensions.d.ts +34 -0
  61. package/dist/src/app/charts/hooks/usePlotDimensions.js +73 -0
  62. package/dist/src/app/charts/hooks/usePlotDimensions.js.map +1 -0
  63. package/dist/src/app/charts/hooks/useTooltip.d.ts +64 -0
  64. package/dist/src/app/charts/hooks/useTooltip.js +52 -0
  65. package/dist/src/app/charts/hooks/useTooltip.js.map +1 -0
  66. package/dist/src/app/charts/observables/ChartData.d.ts +23 -0
  67. package/dist/src/app/charts/observables/ChartData.js +20 -0
  68. package/dist/src/app/charts/observables/ChartData.js.map +1 -0
  69. package/dist/src/app/charts/observables/iterates.d.ts +52 -0
  70. package/dist/src/app/charts/observables/iterates.js +121 -0
  71. package/dist/src/app/charts/observables/iterates.js.map +1 -0
  72. package/dist/src/app/charts/observables/iterates.test.d.ts +1 -0
  73. package/dist/src/app/charts/observables/iterates.test.js +180 -0
  74. package/dist/src/app/charts/observables/iterates.test.js.map +1 -0
  75. package/dist/src/app/charts/observables/ordinals.d.ts +75 -0
  76. package/dist/src/app/charts/observables/ordinals.js +141 -0
  77. package/dist/src/app/charts/observables/ordinals.js.map +1 -0
  78. package/dist/src/app/charts/observables/ordinals.test.d.ts +1 -0
  79. package/dist/src/app/charts/observables/ordinals.test.js +102 -0
  80. package/dist/src/app/charts/observables/ordinals.test.js.map +1 -0
  81. package/dist/src/app/charts/plots/BarPlot.d.ts +81 -0
  82. package/dist/src/app/charts/plots/BarPlot.js +557 -0
  83. package/dist/src/app/charts/plots/BarPlot.js.map +1 -0
  84. package/dist/src/app/charts/plots/PoincarePlot.d.ts +78 -0
  85. package/dist/src/app/charts/plots/PoincarePlot.js +504 -0
  86. package/dist/src/app/charts/plots/PoincarePlot.js.map +1 -0
  87. package/dist/src/app/charts/{RasterPlot.d.ts → plots/RasterPlot.d.ts} +1 -1
  88. package/dist/src/app/charts/{RasterPlot.js → plots/RasterPlot.js} +152 -133
  89. package/dist/src/app/charts/plots/RasterPlot.js.map +1 -0
  90. package/dist/src/app/charts/{ScatterPlot.d.ts → plots/ScatterPlot.d.ts} +10 -8
  91. package/dist/src/app/charts/plots/ScatterPlot.js +379 -0
  92. package/dist/src/app/charts/plots/ScatterPlot.js.map +1 -0
  93. package/dist/src/app/charts/{plot.d.ts → plots/plot.d.ts} +12 -14
  94. package/dist/src/app/charts/{plot.js → plots/plot.js} +19 -17
  95. package/dist/src/app/charts/plots/plot.js.map +1 -0
  96. package/dist/src/app/charts/series/baseSeries.d.ts +38 -0
  97. package/dist/src/app/charts/series/baseSeries.js +36 -0
  98. package/dist/src/app/charts/series/baseSeries.js.map +1 -0
  99. package/dist/src/app/charts/series/datumSeries.test.d.ts +1 -0
  100. package/dist/src/app/charts/series/datumSeries.test.js +50 -0
  101. package/dist/src/app/charts/series/datumSeries.test.js.map +1 -0
  102. package/dist/src/app/charts/series/iterateSeries.d.ts +42 -0
  103. package/dist/src/app/charts/series/iterateSeries.js +34 -0
  104. package/dist/src/app/charts/series/iterateSeries.js.map +1 -0
  105. package/dist/src/app/charts/series/ordinalSeries.d.ts +54 -0
  106. package/dist/src/app/charts/series/ordinalSeries.js +98 -0
  107. package/dist/src/app/charts/series/ordinalSeries.js.map +1 -0
  108. package/dist/src/app/charts/series/timeSeries.d.ts +30 -0
  109. package/dist/src/app/charts/series/timeSeries.js +19 -0
  110. package/dist/src/app/charts/series/timeSeries.js.map +1 -0
  111. package/dist/src/app/charts/{chartData.d.ts → series/timeSeriesChartData.d.ts} +11 -6
  112. package/dist/src/app/charts/series/timeSeriesChartData.js +32 -0
  113. package/dist/src/app/charts/series/timeSeriesChartData.js.map +1 -0
  114. package/dist/src/app/charts/styling/barPlotStyle.d.ts +41 -0
  115. package/dist/src/app/charts/styling/barPlotStyle.js +84 -0
  116. package/dist/src/app/charts/styling/barPlotStyle.js.map +1 -0
  117. package/dist/src/app/charts/{margins.d.ts → styling/margins.d.ts} +7 -5
  118. package/dist/src/app/charts/{margins.js → styling/margins.js} +19 -9
  119. package/dist/src/app/charts/styling/margins.js.map +1 -0
  120. package/dist/src/app/charts/styling/margins.test.d.ts +1 -0
  121. package/dist/src/app/charts/styling/margins.test.js +19 -0
  122. package/dist/src/app/charts/styling/margins.test.js.map +1 -0
  123. package/dist/src/app/charts/styling/svgStyle.d.ts +76 -0
  124. package/dist/src/app/charts/styling/svgStyle.js +84 -0
  125. package/dist/src/app/charts/styling/svgStyle.js.map +1 -0
  126. package/dist/src/app/charts/subscriptions/subscription.test.d.ts +1 -0
  127. package/dist/src/app/charts/subscriptions/subscription.test.js +395 -0
  128. package/dist/src/app/charts/subscriptions/subscription.test.js.map +1 -0
  129. package/dist/src/app/charts/subscriptions/subscriptions.d.ts +100 -0
  130. package/dist/src/app/charts/subscriptions/subscriptions.js +449 -0
  131. package/dist/src/app/charts/subscriptions/subscriptions.js.map +1 -0
  132. package/dist/src/app/charts/tooltips/BarPlotTooltipContent.d.ts +90 -0
  133. package/dist/src/app/charts/tooltips/BarPlotTooltipContent.js +302 -0
  134. package/dist/src/app/charts/tooltips/BarPlotTooltipContent.js.map +1 -0
  135. package/dist/src/app/charts/tooltips/PoincarePlotTooltipContent.d.ts +100 -0
  136. package/dist/src/app/charts/tooltips/PoincarePlotTooltipContent.js +190 -0
  137. package/dist/src/app/charts/tooltips/PoincarePlotTooltipContent.js.map +1 -0
  138. package/dist/src/app/charts/{RasterPlotTooltipContent.js → tooltips/RasterPlotTooltipContent.js} +40 -44
  139. package/dist/src/app/charts/tooltips/RasterPlotTooltipContent.js.map +1 -0
  140. package/dist/src/app/charts/tooltips/ScatterPlotTooltipContent.js +154 -0
  141. package/dist/src/app/charts/tooltips/ScatterPlotTooltipContent.js.map +1 -0
  142. package/dist/src/app/charts/{Tooltip.d.ts → tooltips/Tooltip.d.ts} +4 -2
  143. package/dist/src/app/charts/{Tooltip.js → tooltips/Tooltip.js} +23 -28
  144. package/dist/src/app/charts/tooltips/Tooltip.js.map +1 -0
  145. package/dist/src/app/charts/{tooltipUtils.d.ts → tooltips/tooltipUtils.d.ts} +17 -11
  146. package/dist/src/app/charts/{tooltipUtils.js → tooltips/tooltipUtils.js} +52 -25
  147. package/dist/src/app/charts/tooltips/tooltipUtils.js.map +1 -0
  148. package/dist/src/app/charts/{Tracker.d.ts → trackers/Tracker.d.ts} +6 -5
  149. package/dist/src/app/charts/trackers/Tracker.js +60 -0
  150. package/dist/src/app/charts/trackers/Tracker.js.map +1 -0
  151. package/dist/src/app/charts/{trackerUtils.d.ts → trackers/trackerUtils.d.ts} +13 -8
  152. package/dist/src/app/charts/trackers/trackerUtils.js +359 -0
  153. package/dist/src/app/charts/trackers/trackerUtils.js.map +1 -0
  154. package/dist/src/app/charts/utils.d.ts +22 -32
  155. package/dist/src/app/charts/utils.js +36 -37
  156. package/dist/src/app/charts/utils.js.map +1 -1
  157. package/dist/src/index.d.ts +48 -15
  158. package/dist/src/index.js +71 -15
  159. package/dist/src/index.js.map +1 -1
  160. package/package.json +30 -20
  161. package/src/app/charts/Chart.tsx +334 -0
  162. package/src/app/charts/axes/AxesState.test.ts +34 -0
  163. package/src/app/charts/axes/AxesState.ts +112 -0
  164. package/src/app/charts/axes/AxisInterval.test.ts +143 -0
  165. package/src/app/charts/axes/AxisInterval.ts +139 -0
  166. package/src/app/charts/axes/BaseAxisRange.ts +113 -0
  167. package/src/app/charts/axes/ContinuousAxis.tsx +192 -0
  168. package/src/app/charts/axes/ContinuousAxisRange.test.ts +55 -0
  169. package/src/app/charts/axes/ContinuousAxisRange.ts +96 -0
  170. package/src/app/charts/axes/OrdinalAxis.tsx +223 -0
  171. package/src/app/charts/axes/OrdinalAxisRange.test.ts +55 -0
  172. package/src/app/charts/axes/OrdinalAxisRange.ts +112 -0
  173. package/src/app/charts/axes/axes.ts +1569 -0
  174. package/src/app/charts/d3types.ts +16 -0
  175. package/src/app/charts/filters/regexFilter.test.ts +13 -0
  176. package/src/app/charts/filters/regexFilter.ts +17 -0
  177. package/src/app/charts/hooks/useAxes.tsx +341 -0
  178. package/src/app/charts/hooks/useChart.tsx +159 -0
  179. package/src/app/charts/hooks/useDataObservable.tsx +175 -0
  180. package/src/app/charts/hooks/useInitialData.tsx +65 -0
  181. package/src/app/charts/hooks/useMouse.tsx +125 -0
  182. package/src/app/charts/hooks/usePlotDimensions.tsx +120 -0
  183. package/src/app/charts/hooks/useTooltip.tsx +112 -0
  184. package/src/app/charts/observables/ChartData.ts +31 -0
  185. package/src/app/charts/observables/iterates.test.ts +201 -0
  186. package/src/app/charts/observables/iterates.ts +203 -0
  187. package/src/app/charts/observables/ordinals.test.ts +135 -0
  188. package/src/app/charts/observables/ordinals.ts +248 -0
  189. package/src/app/charts/plots/BarPlot.tsx +1088 -0
  190. package/src/app/charts/plots/PoincarePlot.tsx +832 -0
  191. package/src/app/charts/plots/RasterPlot.tsx +676 -0
  192. package/src/app/charts/plots/ScatterPlot.tsx +653 -0
  193. package/src/app/charts/plots/plot.ts +75 -0
  194. package/src/app/charts/series/baseSeries.ts +52 -0
  195. package/src/app/charts/series/datumSeries.test.ts +60 -0
  196. package/src/app/charts/series/iterateSeries.ts +56 -0
  197. package/src/app/charts/series/ordinalSeries.ts +164 -0
  198. package/src/app/charts/series/timeSeries.ts +40 -0
  199. package/src/app/charts/series/timeSeriesChartData.ts +68 -0
  200. package/src/app/charts/styling/barPlotStyle.ts +126 -0
  201. package/src/app/charts/styling/margins.test.ts +22 -0
  202. package/src/app/charts/styling/margins.ts +58 -0
  203. package/src/app/charts/styling/svgStyle.ts +118 -0
  204. package/src/app/charts/subscriptions/subscription.test.ts +499 -0
  205. package/src/app/charts/subscriptions/subscriptions.ts +635 -0
  206. package/src/app/charts/tooltips/BarPlotTooltipContent.tsx +440 -0
  207. package/src/app/charts/tooltips/PoincarePlotTooltipContent.tsx +352 -0
  208. package/src/app/charts/tooltips/RasterPlotTooltipContent.tsx +274 -0
  209. package/src/app/charts/tooltips/ScatterPlotTooltipContent.tsx +319 -0
  210. package/src/app/charts/tooltips/Tooltip.tsx +148 -0
  211. package/src/app/charts/tooltips/tooltipUtils.ts +216 -0
  212. package/src/app/charts/trackers/Tracker.tsx +129 -0
  213. package/src/app/charts/trackers/trackerUtils.ts +494 -0
  214. package/src/app/charts/utils.ts +109 -0
  215. package/dist/src/app/charts/CategoryAxis.d.ts +0 -20
  216. package/dist/src/app/charts/CategoryAxis.js +0 -112
  217. package/dist/src/app/charts/CategoryAxis.js.map +0 -1
  218. package/dist/src/app/charts/ContinuousAxis.d.ts +0 -16
  219. package/dist/src/app/charts/ContinuousAxis.js +0 -186
  220. package/dist/src/app/charts/ContinuousAxis.js.map +0 -1
  221. package/dist/src/app/charts/RasterPlot.js.map +0 -1
  222. package/dist/src/app/charts/RasterPlotTooltipContent.js.map +0 -1
  223. package/dist/src/app/charts/ScatterPlot.js +0 -351
  224. package/dist/src/app/charts/ScatterPlot.js.map +0 -1
  225. package/dist/src/app/charts/ScatterPlotTooltipContent.js +0 -163
  226. package/dist/src/app/charts/ScatterPlotTooltipContent.js.map +0 -1
  227. package/dist/src/app/charts/Tooltip.js.map +0 -1
  228. package/dist/src/app/charts/Tracker.js +0 -65
  229. package/dist/src/app/charts/Tracker.js.map +0 -1
  230. package/dist/src/app/charts/axes.d.ts +0 -132
  231. package/dist/src/app/charts/axes.js +0 -265
  232. package/dist/src/app/charts/axes.js.map +0 -1
  233. package/dist/src/app/charts/chartData.js +0 -32
  234. package/dist/src/app/charts/chartData.js.map +0 -1
  235. package/dist/src/app/charts/continuousAxisRangeFor.d.ts +0 -20
  236. package/dist/src/app/charts/continuousAxisRangeFor.js +0 -68
  237. package/dist/src/app/charts/continuousAxisRangeFor.js.map +0 -1
  238. package/dist/src/app/charts/datumSeries.d.ts +0 -59
  239. package/dist/src/app/charts/datumSeries.js +0 -55
  240. package/dist/src/app/charts/datumSeries.js.map +0 -1
  241. package/dist/src/app/charts/datumSeries.test.js +0 -50
  242. package/dist/src/app/charts/datumSeries.test.js.map +0 -1
  243. package/dist/src/app/charts/hooks/AxesState.d.ts +0 -37
  244. package/dist/src/app/charts/hooks/AxesState.js +0 -44
  245. package/dist/src/app/charts/hooks/AxesState.js.map +0 -1
  246. package/dist/src/app/charts/margins.js.map +0 -1
  247. package/dist/src/app/charts/margins.test.js +0 -14
  248. package/dist/src/app/charts/margins.test.js.map +0 -1
  249. package/dist/src/app/charts/plot.js.map +0 -1
  250. package/dist/src/app/charts/regexFilter.d.ts +0 -9
  251. package/dist/src/app/charts/regexFilter.js +0 -17
  252. package/dist/src/app/charts/regexFilter.js.map +0 -1
  253. package/dist/src/app/charts/regexFilter.test.js +0 -11
  254. package/dist/src/app/charts/regexFilter.test.js.map +0 -1
  255. package/dist/src/app/charts/subscriptions.d.ts +0 -43
  256. package/dist/src/app/charts/subscriptions.js +0 -213
  257. package/dist/src/app/charts/subscriptions.js.map +0 -1
  258. package/dist/src/app/charts/svgStyle.d.ts +0 -27
  259. package/dist/src/app/charts/svgStyle.js +0 -32
  260. package/dist/src/app/charts/svgStyle.js.map +0 -1
  261. package/dist/src/app/charts/timeRange.test.js +0 -54
  262. package/dist/src/app/charts/timeRange.test.js.map +0 -1
  263. package/dist/src/app/charts/tooltipUtils.js.map +0 -1
  264. package/dist/src/app/charts/trackerUtils.js +0 -130
  265. package/dist/src/app/charts/trackerUtils.js.map +0 -1
  266. /package/dist/src/app/charts/{datumSeries.test.d.ts → axes/AxesState.test.d.ts} +0 -0
  267. /package/dist/src/app/charts/{margins.test.d.ts → axes/AxisInterval.test.d.ts} +0 -0
  268. /package/dist/src/app/charts/{regexFilter.test.d.ts → axes/ContinuousAxisRange.test.d.ts} +0 -0
  269. /package/dist/src/app/charts/{timeRange.test.d.ts → axes/OrdinalAxisRange.test.d.ts} +0 -0
  270. /package/dist/src/app/charts/{RasterPlotTooltipContent.d.ts → tooltips/RasterPlotTooltipContent.d.ts} +0 -0
  271. /package/dist/src/app/charts/{ScatterPlotTooltipContent.d.ts → tooltips/ScatterPlotTooltipContent.d.ts} +0 -0
@@ -0,0 +1,16 @@
1
+ // the axis-element type return when calling the ".call(axis)" function
2
+ import {Selection} from "d3";
3
+ import {Datum} from "./series/timeSeries";
4
+
5
+ export type AxisElementSelection = Selection<SVGGElement, unknown, null, undefined>
6
+ export type SvgSelection = Selection<SVGSVGElement, any, null, undefined>
7
+ export type GSelection = Selection<SVGGElement, any, null, undefined>
8
+ export type LineSelection = Selection<SVGLineElement, any, SVGGElement, undefined>
9
+ export type TextSelection = Selection<SVGTextElement, any, null, undefined>
10
+
11
+ export type RadialMagnifierSelection = Selection<SVGCircleElement, Datum, null, undefined>
12
+ export type BarMagnifierSelection = Selection<SVGRectElement, Datum, null, undefined>;
13
+ export type MagnifierTextSelection = Selection<SVGTextElement, any, SVGGElement, undefined>
14
+
15
+ export type TrackerSelection = Selection<SVGLineElement, Datum, null, undefined>
16
+
@@ -0,0 +1,13 @@
1
+ import {regexFilter} from "./regexFilter";
2
+
3
+ test('option should contain a valid regex', () => {
4
+ expect(regexFilter("^[0-9]+$").succeeded).toEqual(true);
5
+ });
6
+
7
+ test('should allow progression of building regex', () => {
8
+ let filter = regexFilter("^[0-9").map(regex => '100a200'.match(regex)!.map(t => t)).getOrUndefined();
9
+ expect(filter).toBeUndefined();
10
+
11
+ filter = regexFilter("^[0-9]+").map(regex => '100a200'.match(regex)!.map(t => t)).getOrUndefined();
12
+ expect(filter).toEqual(['100']);
13
+ });
@@ -0,0 +1,17 @@
1
+ import {failureResult, Result, successResult} from "result-fn";
2
+
3
+ /**
4
+ * Wraps the creation of the regex in a try/catch and returns the regex, wrapped in an option,
5
+ * when it is valid. This can be used, for example, in a text field where the user types a regex.
6
+ * @param regexString The string representation of the regex
7
+ * @return The regular expression (`RegExp`) wrapped in an option. If the regexp
8
+ * is invalid, then the option is none.
9
+ */
10
+ export function regexFilter(regexString: string): Result<RegExp, string> {
11
+ try {
12
+ const regex = new RegExp(regexString)
13
+ return successResult(regex)
14
+ } catch(error) {
15
+ return failureResult(`Unable to compile the regex expression; regex_expression: ${regexString}`)
16
+ }
17
+ }
@@ -0,0 +1,341 @@
1
+ import {AxesState} from "../axes/AxesState";
2
+ import {BaseAxis} from "../axes/axes";
3
+ import {AxesAssignment} from "../plots/plot";
4
+ import {createContext, JSX, useContext, useRef} from "react";
5
+ import {Dimensions} from "../styling/margins";
6
+ import {usePlotDimensions} from "./usePlotDimensions";
7
+ import {BaseAxisRange} from "../axes/BaseAxisRange";
8
+ import {AxisInterval} from "../axes/AxisInterval";
9
+ import {Optional} from "result-fn";
10
+
11
+ /**
12
+ * No operation function for use when a default function is needed
13
+ */
14
+ const noop = () => {
15
+ /* empty on purpose */
16
+ }
17
+
18
+ /**
19
+ * The values exposed by the hook
20
+ * @template AR The type of the axis range (e.g. {@link ContinuousAxisRange} or {@link OrdinalAxisRange})
21
+ */
22
+ export type UseAxesValues<AR extends BaseAxisRange, A extends BaseAxis> = {
23
+ /**
24
+ * The x-axes state holds the currently set x-axes, manipulation and accessor functions
25
+ */
26
+ xAxesState: AxesState<A>
27
+ /**
28
+ * Adds an x-axis to the axes and updates the internal state
29
+ * @param axis The axis to add
30
+ * @param id The ID of the axis to add
31
+ * @param domain The initial axis range (start, end)
32
+ */
33
+ addXAxis: (axis: A, id: string, range?: AR) => void
34
+ /**
35
+ * The y-axes state holds the currently set x-axes, manipulation and accessor functions
36
+ */
37
+ yAxesState: AxesState<A>
38
+ /**
39
+ * Adds a y-axis to the axes and updates the internal state
40
+ * @param axis The axis to add
41
+ * @param id The ID of the axis to add
42
+ * @param domain The initial axis range (start, end)
43
+ */
44
+ addYAxis: (axis: A, id: string, range?: AR) => void
45
+ /**
46
+ * Sets the axis assigned to each series. This should contain **all** the series used in
47
+ * the chart.
48
+ * @param assignments The assignment of the series to their axes
49
+ */
50
+ setAxisAssignments: (assignments: Map<string, AxesAssignment>) => void
51
+ /**
52
+ * Retrieves the axis assigned to the specified series
53
+ * @return The axes assigned to the specified series
54
+ */
55
+ axisAssignmentsFor: (seriesName: string) => AxesAssignment
56
+ /**
57
+ * Sets the axis ranges specified in the input map
58
+ * @param ranges A map holding the axis ID to the axis range
59
+ */
60
+ setAxesRanges: (ranges: Map<string, AR>) => void
61
+ /**
62
+ * Sets the axis range for the specified axis ID
63
+ * @param axisId The ID of the axis for which to set the range
64
+ * @param range The new axis range
65
+ */
66
+ setAxisRangeFor: (axisId: string, range: AR) => void
67
+ /**
68
+ * Retrieves the current axis range for the specified axis ID
69
+ * @param axisId The ID of the axis for which to retrieve the range
70
+ * @return The current axis range for the specified axis ID wrapped
71
+ * in a n {@link Optional}, or an empty {@link Optional} if the axis
72
+ * ID is not found
73
+ */
74
+ axisRangeFor: (axisId: string) => Optional<AR>
75
+ /**
76
+ * Sets the domain (interval) for the specified axis ID to the specified range
77
+ * @param axisId The ID of the axis for which to set the range
78
+ * @param domain The new domain as a `[start: number, end: number]` tuple
79
+ */
80
+ setAxisIntervalFor: (axisId: string, domain: AxisInterval) => void
81
+ /**
82
+ * Sets the original axis bounds for the specified axis ID to the specified range
83
+ * @param axisId The ID of the axis for which to set the range
84
+ * @param range The new range as a `[start, end]` tuple
85
+ */
86
+ setOriginalAxisIntervalFor: (axisId: string, range: AxisInterval) => void
87
+ /**
88
+ * Callback function that is called when the time ranges change. The time ranges could
89
+ * change because of a zoom action, a pan action, or as new data is streamed in.
90
+ * @param domains A `map(axis_id -> domain)` that associates the axis ID with the
91
+ * current time range.
92
+ */
93
+ updateAxisRanges: (domains: Map<string, AR>) => void
94
+ /**
95
+ * Retrieves the current axis bounds for the specified axis ID
96
+ * @return The current axis bounds as a map(axis_id, (start, end))
97
+ */
98
+ axesRanges: () => Map<string, AR>
99
+ /**
100
+ * Resets the axis bounds to its original bounds
101
+ * @param axisId The ID of the axis
102
+ * @param [axisBounds] An optional bounds that resets the original bounds
103
+ */
104
+ resetAxisIntervalFor: (axisId: string, axisBounds?: AxisInterval) => void
105
+ /**
106
+ * Resets all the axes bound to the original bounds
107
+ * @param [axesBounds] An optional map holds the new bounds for specified axes. The map
108
+ * associates an axis ID with the new bounds.
109
+ */
110
+ resetAxesRanges: (axesBounds?: Map<string, AR>) => void
111
+ /**
112
+ * Callback when the time range changes.
113
+ * @param times The times (start, end) times for each axis in the plot. The `times` argument is a
114
+ * map(axis_id -> (start, end)). Where start and end refer to the time-range for the
115
+ * axis.
116
+ * @return void
117
+ */
118
+ onUpdateAxesInterval?: (times: Map<string, AxisInterval>) => void
119
+ /**
120
+ * Adds a handler for when the axes are updated. An axis domain/range could change because of a zoom action,
121
+ * a pan action, or as new data is streamed in.
122
+ * @param handlerId The unique ID of the handler to register/add
123
+ * @param handler The handler function that accepts a map of updates and a plot dimension
124
+ */
125
+ addAxesRangesUpdateHandler: (handlerId: string, handler: (updates: Map<string, AR>, plotDim: Dimensions) => void) => void
126
+ /**
127
+ * Removes the axis-update handler with the specified ID
128
+ * @param handlerId The ID of the handler to remove
129
+ */
130
+ removeAxesRangesUpdateHandler: (handlerId: string) => void
131
+ }
132
+
133
+ export const defaultAxesValues = (): UseAxesValues<any, any> => ({
134
+ xAxesState: AxesState.empty(),
135
+ yAxesState: AxesState.empty(),
136
+ addXAxis: noop,
137
+ addYAxis: noop,
138
+ setAxisAssignments: noop,
139
+ axisAssignmentsFor: () => ({xAxis: "", yAxis: ""}),
140
+ updateAxisRanges: noop,
141
+ axesRanges: () => new Map<string, any>(),
142
+ axisRangeFor: () => Optional.empty(),
143
+ setAxesRanges: noop,
144
+ setAxisRangeFor: noop,
145
+ setAxisIntervalFor: noop,
146
+ setOriginalAxisIntervalFor: noop,
147
+ resetAxisIntervalFor: noop,
148
+ resetAxesRanges: noop,
149
+ addAxesRangesUpdateHandler: () => noop,
150
+ removeAxesRangesUpdateHandler: () => noop,
151
+ })
152
+
153
+ // the context for axes
154
+ const AxesContext = createContext<UseAxesValues<any, any>>(defaultAxesValues())
155
+
156
+ type Props = {
157
+ /**y
158
+ * Callback when axes bounds change.
159
+ * @param ranges The ranges (start, end) for each axis in the plot
160
+ */
161
+ onUpdateAxesInterval?: (ranges: Map<string, AxisInterval>) => void
162
+
163
+ children: JSX.Element | Array<JSX.Element>
164
+ }
165
+
166
+ /**
167
+ * The React context provider for the {@link UseAxesValues}
168
+ * @param props The properties
169
+ * @return The children wrapped in this provider
170
+ * @constructor
171
+ */
172
+ export default function AxesProvider<AR extends BaseAxisRange, A extends BaseAxis>(props: Props): JSX.Element {
173
+ const {onUpdateAxesInterval, children} = props
174
+
175
+ const plotDimensions = usePlotDimensions()
176
+
177
+ const xAxesStateRef = useRef<AxesState<A>>(AxesState.empty<A>())
178
+ const yAxesStateRef = useRef<AxesState<A>>(AxesState.empty<A>())
179
+ const axisAssignmentsRef = useRef<Map<string, AxesAssignment>>(new Map())
180
+ const axesBoundsUpdateHandlersRef = useRef<Map<string, (updates: Map<string, AR>, plotDim: Dimensions) => void>>(new Map())
181
+ const axesRangeRef = useRef<Map<string, AR>>(new Map())
182
+
183
+ /**
184
+ * Retrieves the x-axis and y-axis assignments for the specified series. If the axis does not have
185
+ * an assignment, then we assume it is using the default x- and y-axes.
186
+ * @param seriesName The name of the series for which to retrieve the axes assignments
187
+ * @return An {@link AxesAssignment} for the specified axes.
188
+ */
189
+ function axisAssignmentsFor(seriesName: string): AxesAssignment {
190
+ return axisAssignmentsRef.current.get(seriesName) || {
191
+ xAxis: xAxesStateRef.current.axisDefaultId().getOrElse(""),
192
+ yAxis: yAxesStateRef.current.axisDefaultId().getOrElse("")
193
+ }
194
+ }
195
+
196
+ /**
197
+ * Called when the domain/range is updated on one or more of the chart's axes (generally x-axes). In turn,
198
+ * dispatches the update to all the internal domain/range update handlers.
199
+ * @param updates A map holding the axis ID to the updated axis time-range (i.e., map(axis_id, axis_time_range))
200
+ */
201
+ function updateAxisRanges(updates: Map<string, AR>): void {
202
+ // update the current time-ranges reference
203
+ updates.forEach((range, id) => {
204
+ axesRangeRef.current.set(id, range)
205
+ })
206
+ // dispatch the updates to all the registered handlers
207
+ axesBoundsUpdateHandlersRef.current
208
+ .forEach((handler, ) => handler(updates, plotDimensions.plotDimensions))
209
+ }
210
+
211
+ /**
212
+ * Sets the axis ranges specified in the input map
213
+ * @param ranges The ranges to set
214
+ */
215
+ function setAxesRanges(ranges: Map<string, AR>): void {
216
+ ranges.forEach((range, id) => {
217
+ axesRangeRef.current.set(id, range)
218
+ })
219
+ }
220
+
221
+ /**
222
+ * Sets the axis range for the specified axis ID
223
+ * @param axisId The axis ID
224
+ * @param range The range to set
225
+ */
226
+ function setAxisRangeFor(axisId: string, range: AR): void {
227
+ axesRangeRef.current.set(axisId, range)
228
+ }
229
+
230
+ /**
231
+ * Sets the axis bounds for the specified axis ID. Note that this does not
232
+ * change the original axis interval
233
+ * @param axisId The axis ID
234
+ * @param interval The interval
235
+ */
236
+ function setAxisIntervalFor(axisId: string, interval: AxisInterval): void {
237
+ Optional.ofNullable(axesRangeRef.current.get(axisId))
238
+ .map(range => range.update(interval.start, interval.end) as AR)
239
+ .ifPresent(updatedRange => axesRangeRef.current.set(axisId, updatedRange))
240
+ }
241
+
242
+ /**
243
+ * Sets the original axis interval for the axis range
244
+ * @param axisId The axis ID
245
+ * @param interval The interval to which to set the origin interval
246
+ */
247
+ function setOriginalAxisIntervalFor(axisId: string, interval: AxisInterval): void {
248
+ Optional.ofNullable(axesRangeRef.current.get(axisId))
249
+ .map(range => range.updateOriginal(interval.start, interval.end) as AR)
250
+ .ifPresent(updatedRange => axesRangeRef.current.set(axisId, updatedRange))
251
+ }
252
+
253
+ /**
254
+ * Resets the bounds for the specified axis to the original range
255
+ * @param axisId The ID of the axis
256
+ */
257
+ function resetAxisIntervalFor(axisId: string): void {
258
+ Optional
259
+ .ofNullable(axesRangeRef.current.get(axisId))
260
+ .map(range => new Map<string, AR>([[axisId, range]]))
261
+ .ifPresent(updates => updateAxisRanges(updates))
262
+ }
263
+
264
+ /**
265
+ * Resets the bounds of all the axes to their original value or to the values specified
266
+ * in the optional bounds map.
267
+ * @param [axesRanges=new Map()] An optional map holds bounds for specified axes. The map
268
+ * associates an axis ID with the new bounds.
269
+ */
270
+ function resetAxesRanges(axesRanges: Map<string, AR> = new Map()): void {
271
+ updateAxisRanges(axesRanges)
272
+ }
273
+
274
+ /**
275
+ * Adds a handler to deal with updates to the bounds of the axes
276
+ * @param handlerId the unique ID of the handler
277
+ * @param handler The handler function that accepts a map of updates and a plot dimension
278
+ * @return A map with all the handlers
279
+ */
280
+ function addAxesRangesUpdateHandler(
281
+ handlerId: string,
282
+ handler: (updates: Map<string, AR>, plotDim: Dimensions) => void
283
+ ): Map<string, (updates: Map<string, AR>, plotDim: Dimensions) => void> {
284
+ if (axesBoundsUpdateHandlersRef.current.has(handlerId)) {
285
+ throw new Error(
286
+ `Handler with ID already exists, please remove it before adding it; ` +
287
+ `handler_id: ${handlerId}; ` +
288
+ `existing_handler_ids: [${Array.from(axesBoundsUpdateHandlersRef.current.keys()).join(", ")}]`
289
+ )
290
+ }
291
+ return axesBoundsUpdateHandlersRef.current.set(handlerId, handler)
292
+ }
293
+
294
+ return <AxesContext.Provider
295
+ value={{
296
+ xAxesState: xAxesStateRef.current,
297
+ yAxesState: yAxesStateRef.current,
298
+ addXAxis: (axis, id, range: AR) => {
299
+ xAxesStateRef.current = xAxesStateRef.current.addAxis(axis, id)
300
+ if (range !== undefined) {
301
+ axesRangeRef.current.set(id, range)
302
+ }
303
+ },
304
+ addYAxis: (axis, id, range: AR) => {
305
+ yAxesStateRef.current = yAxesStateRef.current.addAxis(axis, id)
306
+ if (range !== undefined) {
307
+ axesRangeRef.current.set(id, range)
308
+ }
309
+ },
310
+ setAxisAssignments: assignments => axisAssignmentsRef.current = assignments,
311
+ axisAssignmentsFor: seriesName => axisAssignmentsFor(seriesName),
312
+ updateAxisRanges,
313
+ axesRanges: () => new Map<string, AR>(axesRangeRef.current),
314
+ axisRangeFor: axisId => Optional.ofNullable(axesRangeRef.current.get(axisId)),
315
+ setAxesRanges,
316
+ setAxisRangeFor,
317
+ setAxisIntervalFor,
318
+ setOriginalAxisIntervalFor,
319
+ resetAxesRanges,
320
+ resetAxisIntervalFor,
321
+ onUpdateAxesInterval,
322
+ addAxesRangesUpdateHandler,
323
+ removeAxesRangesUpdateHandler: handlerId => axesBoundsUpdateHandlersRef.current.delete(handlerId),
324
+ }}
325
+ >
326
+ {children}
327
+ </AxesContext.Provider>
328
+ }
329
+
330
+ /**
331
+ * React hook that sets up the React context for the chart values.
332
+ * @return The {@link UseAxesValues} held in the React context.
333
+ */
334
+ export function useAxes<AR extends BaseAxisRange, A extends BaseAxis>(): UseAxesValues<AR, A> {
335
+ const context = useContext<UseAxesValues<AR, A>>(AxesContext)
336
+ const {xAxesState} = context
337
+ if (xAxesState === undefined || xAxesState === null) {
338
+ throw new Error("useAxes can only be used when the parent is a <AxesProvider/>")
339
+ }
340
+ return context
341
+ }
@@ -0,0 +1,159 @@
1
+ import {createContext, JSX, useContext} from "react";
2
+ import {GSelection} from "../d3types";
3
+ import {BaseAxis, SeriesLineStyle, SeriesStyle} from "../axes/axes";
4
+ import {defaultAxesValues, useAxes, UseAxesValues} from "./useAxes";
5
+ import {defaultMouseValues, useMouse, UseMouseValues} from "./useMouse";
6
+ import {defaultTooltipValues, useTooltip, UseTooltipValues} from "./useTooltip";
7
+ import {SvgStyle} from "../styling/svgStyle";
8
+ import {BaseAxisRange} from "../axes/BaseAxisRange";
9
+
10
+ export type NoTooltipMetadata = {}
11
+
12
+ /**
13
+ * The values exposed through the {@link useChart} react hook
14
+ * @param chartId The unique ID for the chart
15
+ * @param mainG The root <g> element for the chart
16
+ * @param container The SVG element which is the container for this chart
17
+ * @template D The type of the series' datum
18
+ * @template S The type of the series style
19
+ * @template TM The type of the tooltip's metadata (data about the series data)
20
+ */
21
+ interface UseChartValues<D, S extends SeriesStyle, TM, AR extends BaseAxisRange, A extends BaseAxis> {
22
+ /**
23
+ * Unique ID for the chart
24
+ */
25
+ chartId: number
26
+ /**
27
+ * The root <g> element for the chart
28
+ */
29
+ mainG: GSelection | null
30
+ /**
31
+ * The SVG element which is the container for this chart
32
+ */
33
+ container: SVGSVGElement | null
34
+ /**
35
+ * Base color
36
+ */
37
+ color: string
38
+ /**
39
+ * The base/default background color. This can be overridden by the {@link Props.svgStyle} property.
40
+ */
41
+ backgroundColor: string
42
+ /**
43
+ * Overrides for the SVG style
44
+ */
45
+ svgStyle: Partial<SvgStyle>
46
+ /**
47
+ * A `map(series_name -> series_line_style)`
48
+ */
49
+ seriesStyles: Map<string, S>
50
+
51
+ /*
52
+ | AXES
53
+ */
54
+ axes: UseAxesValues<AR, A>
55
+
56
+ /*
57
+ | DATA PROCESSING
58
+ */
59
+ /**
60
+ * A regular expression uses against the series names to determine which series to show in the chart
61
+ */
62
+ seriesFilter: RegExp
63
+
64
+ /*
65
+ | INTERNAL INTERACTION EVENT HANDLERS
66
+ */
67
+ mouse: UseMouseValues<D, TM>
68
+ tooltip: UseTooltipValues<D, TM>
69
+ }
70
+
71
+ const defaultUseChartValues: UseChartValues<any, any, any, any, any> = {
72
+ chartId: NaN,
73
+ container: null,
74
+ mainG: null,
75
+ color: '#d2933f',
76
+ backgroundColor: '#EEE',
77
+ svgStyle: new Map(),
78
+ seriesStyles: new Map(),
79
+
80
+ // axes
81
+ axes: defaultAxesValues(),
82
+
83
+ // data
84
+ seriesFilter: /./,
85
+
86
+ // internal chart-interaction event handlers
87
+ mouse: defaultMouseValues(),
88
+ tooltip: defaultTooltipValues()
89
+ }
90
+
91
+ const ChartContext = createContext<UseChartValues<any, any, any, any, any>>(defaultUseChartValues)
92
+
93
+ interface Props {
94
+ chartId: number
95
+ container: SVGSVGElement | null
96
+ mainG: GSelection | null
97
+ color: string
98
+ backgroundColor: string
99
+ svgStyle: Partial<SvgStyle>
100
+ seriesStyles?: Map<string, SeriesLineStyle>
101
+ seriesFilter?: RegExp
102
+
103
+ children: JSX.Element | Array<JSX.Element>
104
+ }
105
+
106
+ /**
107
+ * The React context provider for the {@link UseChartValues}
108
+ * @param props The properties
109
+ * @return The children wrapped in this provider
110
+ * @constructor
111
+ */
112
+ export default function ChartProvider(props: Props): JSX.Element {
113
+ const {
114
+ chartId,
115
+ container,
116
+ mainG,
117
+ color,
118
+ backgroundColor,
119
+ seriesFilter = defaultUseChartValues.seriesFilter,
120
+ svgStyle,
121
+ seriesStyles = new Map<string, SeriesLineStyle>(),
122
+ } = props
123
+
124
+ const axes = useAxes()
125
+ const mouse = useMouse()
126
+ const tooltip = useTooltip()
127
+
128
+ return <ChartContext.Provider
129
+ value={{
130
+ chartId,
131
+ color,
132
+ backgroundColor,
133
+ svgStyle,
134
+ seriesStyles,
135
+ seriesFilter,
136
+ mainG,
137
+ container,
138
+
139
+ axes,
140
+ mouse,
141
+ tooltip,
142
+ }}
143
+ >
144
+ {props.children}
145
+ </ChartContext.Provider>
146
+ }
147
+
148
+ /**
149
+ * React hook that sets up the React context for the chart values.
150
+ * @return The {@link UseChartValues} held in the React context.
151
+ */
152
+ export function useChart<D, S extends SeriesStyle, TM, AR extends BaseAxisRange, A extends BaseAxis>(): UseChartValues<D, S, TM, AR, A> {
153
+ const context = useContext<UseChartValues<D, S, TM, AR, A>>(ChartContext)
154
+ const {chartId} = context
155
+ if (isNaN(chartId)) {
156
+ throw new Error("useChart can only be used when the parent is a <ChartProvider/>")
157
+ }
158
+ return context
159
+ }