@orbcharts/plugins-basic 3.0.0-beta.2 → 3.0.0-beta.20

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 (182) hide show
  1. package/LICENSE +200 -200
  2. package/dist/orbcharts-plugins-basic.es.js +15169 -10684
  3. package/dist/orbcharts-plugins-basic.umd.js +296 -83
  4. package/dist/src/base/BaseBars.d.ts +4 -4
  5. package/dist/src/base/BaseBarsTriangle.d.ts +4 -4
  6. package/dist/src/base/BaseDots.d.ts +4 -4
  7. package/dist/src/base/BaseGroupAxis.d.ts +2 -2
  8. package/dist/src/base/BaseLegend.d.ts +1 -1
  9. package/dist/src/base/BaseLineAreas.d.ts +5 -5
  10. package/dist/src/base/BaseLines.d.ts +5 -5
  11. package/dist/src/base/BaseRacingBars.d.ts +23 -0
  12. package/dist/src/base/BaseRacingLabels.d.ts +20 -0
  13. package/dist/src/base/BaseRacingValueLabels.d.ts +20 -0
  14. package/dist/src/base/{BaseBarStack.d.ts → BaseStackedBars.d.ts} +8 -8
  15. package/dist/src/base/BaseValueAxis.d.ts +6 -8
  16. package/dist/src/base/BaseXAxis.d.ts +20 -0
  17. package/dist/src/base/BaseYAxis.d.ts +18 -0
  18. package/dist/src/grid/defaults.d.ts +5 -5
  19. package/dist/src/grid/gridObservables.d.ts +12 -5
  20. package/dist/src/grid/index.d.ts +3 -3
  21. package/dist/src/grid/plugins/GroupZoom.d.ts +1 -0
  22. package/dist/src/grid/plugins/StackedBars.d.ts +1 -0
  23. package/dist/src/grid/plugins/StackedValueAxis.d.ts +1 -0
  24. package/dist/src/index.d.ts +2 -0
  25. package/dist/src/multiGrid/defaults.d.ts +4 -4
  26. package/dist/src/multiGrid/index.d.ts +3 -3
  27. package/dist/src/multiGrid/plugins/MultiStackedBars.d.ts +1 -0
  28. package/dist/src/multiGrid/plugins/MultiStackedValueAxis.d.ts +1 -0
  29. package/dist/src/multiGrid/plugins/OverlappingStackedValueAxes.d.ts +1 -0
  30. package/dist/src/multiValue/defaults.d.ts +13 -0
  31. package/dist/src/multiValue/index.d.ts +12 -0
  32. package/dist/src/multiValue/multiValueObservables.d.ts +56 -0
  33. package/dist/src/multiValue/plugins/MultiValueLegend.d.ts +1 -0
  34. package/dist/src/multiValue/plugins/MultiValueTooltip.d.ts +1 -0
  35. package/dist/src/multiValue/plugins/RacingBars.d.ts +1 -0
  36. package/dist/src/multiValue/plugins/RacingCounterTexts.d.ts +3 -0
  37. package/dist/src/multiValue/plugins/RacingValueAxis.d.ts +1 -0
  38. package/dist/src/multiValue/plugins/Scatter.d.ts +3 -0
  39. package/dist/src/multiValue/plugins/ScatterBubbles.d.ts +3 -0
  40. package/dist/src/multiValue/plugins/XAxis.d.ts +1 -0
  41. package/dist/src/multiValue/plugins/XYAux.d.ts +3 -0
  42. package/dist/src/multiValue/plugins/XYAxes.d.ts +1 -0
  43. package/dist/src/multiValue/plugins/XYAxes_legacy.d.ts +3 -0
  44. package/dist/src/multiValue/plugins/XZoom.d.ts +1 -0
  45. package/dist/src/noneData/plugins/Container.d.ts +0 -1
  46. package/dist/src/noneData/plugins/Tooltip.d.ts +0 -3
  47. package/dist/src/relationship/defaults.d.ts +6 -0
  48. package/dist/src/relationship/index.d.ts +5 -0
  49. package/dist/src/relationship/plugins/ForceDirected.d.ts +3 -0
  50. package/dist/src/relationship/plugins/ForceDirectedBubbles.d.ts +3 -0
  51. package/dist/src/relationship/plugins/RelationshipLegend.d.ts +1 -0
  52. package/dist/src/relationship/plugins/RelationshipTooltip.d.ts +1 -0
  53. package/dist/src/relationship/relationshipObservables.d.ts +1 -0
  54. package/dist/src/series/seriesObservables.d.ts +3 -3
  55. package/dist/src/utils/commonUtils.d.ts +2 -1
  56. package/dist/src/utils/d3Graphics.d.ts +2 -1
  57. package/dist/src/utils/d3Utils.d.ts +3 -2
  58. package/dist/src/utils/observables.d.ts +1 -1
  59. package/dist/src/utils/orbchartsUtils.d.ts +3 -3
  60. package/lib/core-types.ts +7 -7
  61. package/lib/core.ts +6 -6
  62. package/lib/plugins-basic-types.ts +6 -6
  63. package/package.json +44 -44
  64. package/src/base/BaseBars.ts +765 -765
  65. package/src/base/BaseBarsTriangle.ts +676 -674
  66. package/src/base/BaseDots.ts +464 -515
  67. package/src/base/BaseGroupAxis.ts +691 -679
  68. package/src/base/BaseLegend.ts +684 -680
  69. package/src/base/BaseLineAreas.ts +629 -629
  70. package/src/base/BaseLines.ts +706 -706
  71. package/src/base/BaseRacingBars.ts +552 -0
  72. package/src/base/BaseRacingLabels.ts +396 -0
  73. package/src/base/BaseRacingValueLabels.ts +403 -0
  74. package/src/base/{BaseBarStack.ts → BaseStackedBars.ts} +782 -780
  75. package/src/base/BaseTooltip.ts +386 -385
  76. package/src/base/BaseValueAxis.ts +600 -580
  77. package/src/base/BaseXAxis.ts +427 -0
  78. package/src/base/BaseYAxis.ts +389 -0
  79. package/src/base/types.ts +2 -2
  80. package/src/const.ts +30 -30
  81. package/src/grid/defaults.ts +213 -244
  82. package/src/grid/gridObservables.ts +612 -545
  83. package/src/grid/index.ts +16 -16
  84. package/src/grid/plugins/Bars.ts +69 -69
  85. package/src/grid/plugins/BarsPN.ts +66 -66
  86. package/src/grid/plugins/BarsTriangle.ts +73 -73
  87. package/src/grid/plugins/Dots.ts +68 -68
  88. package/src/grid/plugins/GridLegend.ts +107 -107
  89. package/src/grid/plugins/GridTooltip.ts +66 -66
  90. package/src/grid/plugins/GroupAux.ts +1120 -1098
  91. package/src/grid/plugins/GroupAxis.ts +73 -97
  92. package/src/grid/plugins/{GridZoom.ts → GroupZoom.ts} +218 -218
  93. package/src/grid/plugins/LineAreas.ts +65 -65
  94. package/src/grid/plugins/Lines.ts +59 -59
  95. package/src/grid/plugins/{BarStack.ts → StackedBars.ts} +64 -64
  96. package/src/grid/plugins/{ValueStackAxis.ts → StackedValueAxis.ts} +96 -95
  97. package/src/grid/plugins/ValueAxis.ts +94 -93
  98. package/src/index.ts +6 -9
  99. package/src/multiGrid/defaults.ts +244 -224
  100. package/src/multiGrid/index.ts +15 -15
  101. package/src/multiGrid/multiGridObservables.ts +50 -49
  102. package/src/multiGrid/plugins/MultiBars.ts +108 -108
  103. package/src/multiGrid/plugins/MultiBarsTriangle.ts +114 -114
  104. package/src/multiGrid/plugins/MultiDots.ts +102 -102
  105. package/src/multiGrid/plugins/MultiGridLegend.ts +169 -159
  106. package/src/multiGrid/plugins/MultiGridTooltip.ts +66 -66
  107. package/src/multiGrid/plugins/MultiGroupAxis.ts +137 -137
  108. package/src/multiGrid/plugins/MultiLineAreas.ts +107 -107
  109. package/src/multiGrid/plugins/MultiLines.ts +101 -101
  110. package/src/multiGrid/plugins/{MultiBarStack.ts → MultiStackedBars.ts} +106 -106
  111. package/src/multiGrid/plugins/{MultiValueStackAxis.ts → MultiStackedValueAxis.ts} +134 -133
  112. package/src/multiGrid/plugins/MultiValueAxis.ts +134 -133
  113. package/src/multiGrid/plugins/{OverlappingValueStackAxes.ts → OverlappingStackedValueAxes.ts} +300 -298
  114. package/src/multiGrid/plugins/OverlappingValueAxes.ts +300 -299
  115. package/src/multiValue/defaults.ts +432 -0
  116. package/src/multiValue/index.ts +13 -0
  117. package/src/multiValue/multiValueObservables.ts +667 -0
  118. package/src/multiValue/plugins/MultiValueLegend.ts +107 -0
  119. package/src/multiValue/plugins/MultiValueTooltip.ts +66 -0
  120. package/src/multiValue/plugins/RacingBars.ts +373 -0
  121. package/src/multiValue/plugins/RacingCounterTexts.ts +300 -0
  122. package/src/multiValue/plugins/RacingValueAxis.ts +115 -0
  123. package/src/multiValue/plugins/RankingAxis_legacy.ts +109 -0
  124. package/src/multiValue/plugins/Scatter.ts +426 -0
  125. package/src/multiValue/plugins/ScatterBubbles.ts +554 -0
  126. package/src/multiValue/plugins/XAxis.ts +108 -0
  127. package/src/multiValue/plugins/XYAux.ts +683 -0
  128. package/src/multiValue/plugins/XYAxes.ts +195 -0
  129. package/src/multiValue/plugins/XYAxes_legacy.ts +684 -0
  130. package/src/multiValue/plugins/XZoom.ts +300 -0
  131. package/src/noneData/defaults.ts +102 -102
  132. package/src/noneData/index.ts +3 -3
  133. package/src/noneData/plugins/Container.ts +28 -28
  134. package/src/noneData/plugins/Tooltip.ts +374 -374
  135. package/src/relationship/defaults.ts +222 -0
  136. package/src/relationship/index.ts +5 -0
  137. package/src/relationship/plugins/ForceDirected.ts +1174 -0
  138. package/src/relationship/plugins/ForceDirectedBubbles.ts +1412 -0
  139. package/src/relationship/plugins/RelationshipLegend.ts +100 -0
  140. package/src/relationship/plugins/RelationshipTooltip.ts +66 -0
  141. package/src/relationship/relationshipObservables.ts +50 -0
  142. package/src/series/defaults.ts +221 -206
  143. package/src/series/index.ts +9 -9
  144. package/src/series/plugins/Bubbles.ts +636 -603
  145. package/src/series/plugins/Pie.ts +623 -623
  146. package/src/series/plugins/PieEventTexts.ts +284 -283
  147. package/src/series/plugins/PieLabels.ts +640 -640
  148. package/src/series/plugins/Rose.ts +516 -516
  149. package/src/series/plugins/RoseLabels.ts +600 -600
  150. package/src/series/plugins/SeriesLegend.ts +107 -107
  151. package/src/series/plugins/SeriesTooltip.ts +66 -66
  152. package/src/series/seriesObservables.ts +145 -145
  153. package/src/series/seriesUtils.ts +51 -51
  154. package/src/tree/defaults.ts +102 -78
  155. package/src/tree/index.ts +4 -4
  156. package/src/tree/plugins/TreeLegend.ts +100 -107
  157. package/src/tree/plugins/TreeMap.ts +341 -333
  158. package/src/tree/plugins/TreeTooltip.ts +66 -66
  159. package/src/utils/commonUtils.ts +31 -22
  160. package/src/utils/d3Graphics.ts +176 -174
  161. package/src/utils/d3Utils.ts +92 -73
  162. package/src/utils/observables.ts +14 -14
  163. package/src/utils/orbchartsUtils.ts +130 -100
  164. package/tsconfig.base.json +13 -13
  165. package/tsconfig.json +2 -2
  166. package/vite.config.js +22 -22
  167. package/dist/src/grid/plugins/BarStack.d.ts +0 -1
  168. package/dist/src/grid/plugins/GridZoom.d.ts +0 -1
  169. package/dist/src/grid/plugins/ValueStackAxis.d.ts +0 -1
  170. package/dist/src/multiGrid/plugins/MultiBarStack.d.ts +0 -1
  171. package/dist/src/multiGrid/plugins/MultiValueStackAxis.d.ts +0 -1
  172. package/dist/src/multiGrid/plugins/OverlappingValueStackAxes.d.ts +0 -1
  173. package/src/grid/plugins/RankingAxis.ts +0 -0
  174. package/src/multiValue/plugins/ScatterAxes.ts +0 -0
  175. package/src/relationship/plugins/Relationship.ts +0 -0
  176. /package/dist/src/{base/BaseGroupArea.d.ts → multiValue/plugins/OrdinalBubbles.d.ts} +0 -0
  177. /package/dist/src/{grid/plugins/Ranking.d.ts → multiValue/plugins/OrdinalXAxis.d.ts} +0 -0
  178. /package/dist/src/{grid/plugins/RankingAxis.d.ts → multiValue/plugins/RacingCounterAxis.d.ts} +0 -0
  179. /package/dist/src/multiValue/plugins/{ScatterAxes.d.ts → RankingAxis_legacy.d.ts} +0 -0
  180. /package/{dist/src/relationship/plugins/Relationship.d.ts → src/multiValue/plugins/OrdinalBubbles.ts} +0 -0
  181. /package/src/{base/BaseGroupArea.ts → multiValue/plugins/OrdinalXAxis.ts} +0 -0
  182. /package/src/{grid/plugins/Ranking.ts → multiValue/plugins/RacingCounterAxis.ts} +0 -0
@@ -1,546 +1,613 @@
1
- import * as d3 from 'd3'
2
- import {
3
- Observable,
4
- Subject,
5
- of,
6
- takeUntil,
7
- filter,
8
- map,
9
- switchMap,
10
- combineLatest,
11
- merge,
12
- shareReplay,
13
- distinctUntilChanged
14
- } from 'rxjs'
15
- import type {
16
- ChartParams,
17
- HighlightTarget,
18
- DataFormatterGrid,
19
- ComputedDataGrid,
20
- ComputedDatumGrid,
21
- TransformData,
22
- GridContainerPosition,
23
- Layout } from '../../lib/core-types'
24
- import { createAxisQuantizeScale } from '../../lib/core'
25
- import { getClassName, getUniID } from '../utils/orbchartsUtils'
26
- import { d3EventObservable } from '../utils/observables'
27
-
28
- // grid選取器
29
- export const gridSelectionsObservable = ({ selection, pluginName, clipPathID, seriesLabels$, gridContainerPosition$, gridAxesTransform$, gridGraphicTransform$ }: {
30
- selection: d3.Selection<any, unknown, any, unknown>
31
- pluginName: string
32
- clipPathID: string
33
- // computedData$: Observable<ComputedDataGrid>
34
- seriesLabels$: Observable<string[]>
35
- gridContainerPosition$: Observable<GridContainerPosition[]>
36
- gridAxesTransform$: Observable<TransformData>
37
- gridGraphicTransform$: Observable<TransformData>
38
- }) => {
39
- const seriesClassName = getClassName(pluginName, 'series')
40
- const axesClassName = getClassName(pluginName, 'axes')
41
- const graphicClassName = getClassName(pluginName, 'graphic')
42
-
43
- const seriesSelection$ = seriesLabels$.pipe(
44
- map((existSeriesLabels, i) => {
45
- return selection
46
- .selectAll<SVGGElement, string>(`g.${seriesClassName}`)
47
- .data(existSeriesLabels, d => d)
48
- .join(
49
- enter => {
50
- return enter
51
- .append('g')
52
- .classed(seriesClassName, true)
53
- .each((d, i, g) => {
54
- const axesSelection = d3.select(g[i])
55
- .selectAll<SVGGElement, ComputedDatumGrid[]>(`g.${axesClassName}`)
56
- .data([i])
57
- .join(
58
- enter => {
59
- return enter
60
- .append('g')
61
- .classed(axesClassName, true)
62
- .attr('clip-path', `url(#${clipPathID})`)
63
- .each((d, i, g) => {
64
- const defsSelection = d3.select(g[i])
65
- .selectAll<SVGDefsElement, any>('defs')
66
- .data([i])
67
- .join('defs')
68
-
69
- const graphicGSelection = d3.select(g[i])
70
- .selectAll<SVGGElement, any>('g')
71
- .data([i])
72
- .join('g')
73
- .classed(graphicClassName, true)
74
- })
75
- },
76
- update => update,
77
- exit => exit.remove()
78
- )
79
- })
80
- },
81
- update => update,
82
- exit => exit.remove()
83
- )
84
- }),
85
- shareReplay(1)
86
- )
87
-
88
- combineLatest({
89
- seriesSelection: seriesSelection$,
90
- gridContainerPosition: gridContainerPosition$
91
- }).pipe(
92
- switchMap(async d => d)
93
- ).subscribe(data => {
94
- data.seriesSelection
95
- .transition()
96
- .attr('transform', (d, i) => {
97
- const gridContainerPosition = data.gridContainerPosition[i] ?? data.gridContainerPosition[0]
98
- const translate = gridContainerPosition.translate
99
- const scale = gridContainerPosition.scale
100
- return `translate(${translate[0]}, ${translate[1]}) scale(${scale[0]}, ${scale[1]})`
101
- })
102
- })
103
-
104
- const axesSelection$ = combineLatest({
105
- seriesSelection: seriesSelection$,
106
- gridAxesTransform: gridAxesTransform$
107
- }).pipe(
108
- switchMap(async d => d),
109
- map(data => {
110
- return data.seriesSelection
111
- .select<SVGGElement>(`g.${axesClassName}`)
112
- .style('transform', data.gridAxesTransform.value)
113
- }),
114
- shareReplay(1)
115
- )
116
- const defsSelection$ = axesSelection$.pipe(
117
- map(axesSelection => {
118
- return axesSelection.select<SVGDefsElement>('defs')
119
- }),
120
- shareReplay(1)
121
- )
122
- const graphicGSelection$ = combineLatest({
123
- axesSelection: axesSelection$,
124
- gridGraphicTransform: gridGraphicTransform$
125
- }).pipe(
126
- switchMap(async d => d),
127
- map(data => {
128
- const graphicGSelection = data.axesSelection
129
- .select<SVGGElement>(`g.${graphicClassName}`)
130
- graphicGSelection
131
- .transition()
132
- .duration(50)
133
- .style('transform', data.gridGraphicTransform.value)
134
- return graphicGSelection
135
- }),
136
- shareReplay(1)
137
- )
138
-
139
- return {
140
- seriesSelection$,
141
- axesSelection$,
142
- defsSelection$,
143
- graphicGSelection$
144
- }
145
- }
146
-
147
- // 由事件取得group data的function
148
- // @Q@ 之後重構改用 gridGroupPosition
149
- export const gridGroupPositionFnObservable = ({ fullDataFormatter$, gridAxesSize$, computedData$, fullChartParams$, gridContainerPosition$, layout$ }: {
150
- fullDataFormatter$: Observable<DataFormatterGrid>
151
- gridAxesSize$: Observable<{
152
- width: number;
153
- height: number;
154
- }>
155
- computedData$: Observable<ComputedDataGrid>
156
- // GroupDataMap$: Observable<Map<string, ComputedDatumGrid[]>>
157
- fullChartParams$: Observable<ChartParams>
158
- gridContainerPosition$: Observable<GridContainerPosition[]>
159
- layout$: Observable<Layout>
160
- }): Observable<(event: any) => { groupIndex: number; groupLabel: string }> => {
161
- const destroy$ = new Subject()
162
-
163
- // 顯示範圍內的group labels
164
- // const scaleRangeGroupLabels$: Observable<string[]> = new Observable(subscriber => {
165
- // combineLatest({
166
- // dataFormatter: fullDataFormatter$,
167
- // computedData: computedData$
168
- // }).pipe(
169
- // takeUntil(destroy$),
170
- // switchMap(async (d) => d),
171
- // ).subscribe(data => {
172
- // const groupMin = 0
173
- // const groupMax = data.computedData[0] ? data.computedData[0].length - 1 : 0
174
- // const groupScaleDomainMin = data.dataFormatter.grid.groupAxis.scaleDomain[0] === 'auto'
175
- // ? groupMin - data.dataFormatter.grid.groupAxis.scalePadding
176
- // : data.dataFormatter.grid.groupAxis.scaleDomain[0] as number - data.dataFormatter.grid.groupAxis.scalePadding
177
- // const groupScaleDomainMax = data.dataFormatter.grid.groupAxis.scaleDomain[1] === 'auto'
178
- // ? groupMax + data.dataFormatter.grid.groupAxis.scalePadding
179
- // : data.dataFormatter.grid.groupAxis.scaleDomain[1] as number + data.dataFormatter.grid.groupAxis.scalePadding
180
-
181
- // // const groupingAmount = data.computedData[0]
182
- // // ? data.computedData[0].length
183
- // // : 0
184
-
185
- // let _labels = data.dataFormatter.grid.seriesDirection === 'row'
186
- // ? (data.computedData[0] ?? []).map(d => d.groupLabel)
187
- // : data.computedData.map(d => d[0].groupLabel)
188
-
189
- // const _axisLabels =
190
- // // new Array(groupingAmount).fill(0)
191
- // // .map((d, i) => {
192
- // // return _labels[i] != null
193
- // // ? _labels[i]
194
- // // : String(i) // 沒有label則用序列號填充
195
- // // })
196
- // _labels
197
- // .filter((d, i) => {
198
- // return i >= groupScaleDomainMin && i <= groupScaleDomainMax
199
- // })
200
- // subscriber.next(_axisLabels)
201
- // })
202
- // })
203
- const groupScaleDomain$ = combineLatest({
204
- fullDataFormatter: fullDataFormatter$,
205
- gridAxesSize: gridAxesSize$,
206
- computedData: computedData$
207
- }).pipe(
208
- switchMap(async (d) => d),
209
- map(data => {
210
- const groupMin = 0
211
- const groupMax = data.computedData[0] ? data.computedData[0].length - 1 : 0
212
- // const groupScaleDomainMin = data.fullDataFormatter.grid.groupAxis.scaleDomain[0] === 'auto'
213
- // ? groupMin - data.fullDataFormatter.grid.groupAxis.scalePadding
214
- // : data.fullDataFormatter.grid.groupAxis.scaleDomain[0] as number - data.fullDataFormatter.grid.groupAxis.scalePadding
215
- const groupScaleDomainMin = data.fullDataFormatter.grid.groupAxis.scaleDomain[0] - data.fullDataFormatter.grid.groupAxis.scalePadding
216
- const groupScaleDomainMax = data.fullDataFormatter.grid.groupAxis.scaleDomain[1] === 'max'
217
- ? groupMax + data.fullDataFormatter.grid.groupAxis.scalePadding
218
- : data.fullDataFormatter.grid.groupAxis.scaleDomain[1] as number + data.fullDataFormatter.grid.groupAxis.scalePadding
219
-
220
- return [groupScaleDomainMin, groupScaleDomainMax]
221
- }),
222
- shareReplay(1)
223
- )
224
-
225
- const groupLabels$ = combineLatest({
226
- fullDataFormatter: fullDataFormatter$,
227
- computedData: computedData$
228
- }).pipe(
229
- switchMap(async d => d),
230
- map(data => {
231
- return data.fullDataFormatter.grid.seriesDirection === 'row'
232
- ? (data.computedData[0] ?? []).map(d => d.groupLabel)
233
- : data.computedData.map(d => d[0].groupLabel)
234
- })
235
- )
236
-
237
- // 顯示範圍內的group labels
238
- const scaleRangeGroupLabels$ = combineLatest({
239
- groupScaleDomain: groupScaleDomain$,
240
- groupLabels: groupLabels$
241
- }).pipe(
242
- switchMap(async d => d),
243
- map(data => {
244
- return data.groupLabels
245
- .filter((d, i) => {
246
- return i >= data.groupScaleDomain[0] && i <= data.groupScaleDomain[1]
247
- })
248
- })
249
- )
250
-
251
- const columnAmount$ = gridContainerPosition$.pipe(
252
- map(gridContainerPosition => {
253
- const maxColumnIndex = gridContainerPosition.reduce((acc, current) => {
254
- return current.columnIndex > acc ? current.columnIndex : acc
255
- }, 0)
256
- return maxColumnIndex + 1
257
- }),
258
- distinctUntilChanged()
259
- )
260
-
261
- const rowAmount$ = gridContainerPosition$.pipe(
262
- map(gridContainerPosition => {
263
- const maxRowIndex = gridContainerPosition.reduce((acc, current) => {
264
- return current.rowIndex > acc ? current.rowIndex : acc
265
- }, 0)
266
- return maxRowIndex + 1
267
- }),
268
- distinctUntilChanged()
269
- )
270
-
271
- return new Observable<(event: any) => { groupIndex: number; groupLabel: string }>(subscriber => {
272
- combineLatest({
273
- dataFormatter: fullDataFormatter$,
274
- axisSize: gridAxesSize$,
275
- fullChartParams: fullChartParams$,
276
- scaleRangeGroupLabels: scaleRangeGroupLabels$,
277
- groupLabels: groupLabels$,
278
- groupScaleDomain: groupScaleDomain$,
279
- columnAmount: columnAmount$,
280
- rowAmount: rowAmount$,
281
- layout: layout$
282
- }).pipe(
283
- takeUntil(destroy$),
284
- switchMap(async (d) => d),
285
- ).subscribe(data => {
286
-
287
- const reverse = data.dataFormatter.grid.valueAxis.position === 'right'
288
- || data.dataFormatter.grid.valueAxis.position === 'bottom'
289
- ? true : false
290
-
291
- // 比例尺座標對應非連續資料索引
292
- const xIndexScale = createAxisQuantizeScale({
293
- axisLabels: data.scaleRangeGroupLabels,
294
- axisWidth: data.axisSize.width,
295
- padding: data.dataFormatter.grid.groupAxis.scalePadding,
296
- reverse
297
- })
298
-
299
- // 依比例尺位置計算座標
300
- const axisValuePredicate = (event: any) => {
301
- return data.dataFormatter.grid.groupAxis.position === 'bottom'
302
- || data.dataFormatter.grid.groupAxis.position === 'top'
303
- ? event.offsetX - data.fullChartParams.padding.left
304
- : event.offsetY - data.fullChartParams.padding.top
305
- }
306
-
307
- // 比例尺座標取得groupData的function
308
- const createEventGroupData: (event: any) => { groupIndex: number; groupLabel: string } = (event: any) => {
309
- // 由於event座標是基於底層的,但是container會有多欄,所以要重新計算
310
- const eventData = {
311
- offsetX: event.offsetX * data.columnAmount % data.layout.rootWidth,
312
- offsetY: event.offsetY * data.rowAmount % data.layout.rootHeight
313
- }
314
- // console.log('data.columnAmount', data.columnAmount, 'data.rowAmount', data.rowAmount, 'data.layout.rootWidth', data.layout.rootWidth, 'data.layout.rootHeight', data.layout.rootHeight)
315
- const axisValue = axisValuePredicate(eventData)
316
- const xIndex = xIndexScale(axisValue)
317
- const currentxIndexStart = Math.ceil(data.groupScaleDomain[0]) // 因為有padding所以會有小數點,所以要無條件進位
318
- const groupIndex = xIndex + currentxIndexStart
319
-
320
- return {
321
- groupIndex,
322
- groupLabel: data.groupLabels[groupIndex] ?? ''
323
- }
324
- }
325
-
326
- subscriber.next(createEventGroupData)
327
-
328
- return function unsubscribe () {
329
- destroy$.next(undefined)
330
- }
331
- })
332
- })
333
- }
334
-
335
- export const gridGroupPosition = ({ rootSelection, fullDataFormatter$, gridAxesSize$, computedData$, fullChartParams$, gridContainerPosition$, layout$ }: {
336
- rootSelection: d3.Selection<any, unknown, any, unknown>
337
- fullDataFormatter$: Observable<DataFormatterGrid>
338
- gridAxesSize$: Observable<{
339
- width: number;
340
- height: number;
341
- }>
342
- computedData$: Observable<ComputedDataGrid>
343
- fullChartParams$: Observable<ChartParams>
344
- gridContainerPosition$: Observable<GridContainerPosition[]>
345
- layout$: Observable<Layout>
346
- }) => {
347
- const rootMousemove$: Observable<any> = d3EventObservable(rootSelection, 'mousemove')
348
-
349
- const groupScaleDomain$ = combineLatest({
350
- fullDataFormatter: fullDataFormatter$,
351
- gridAxesSize: gridAxesSize$,
352
- computedData: computedData$
353
- }).pipe(
354
- switchMap(async (d) => d),
355
- map(data => {
356
- const groupMin = 0
357
- const groupMax = data.computedData[0] ? data.computedData[0].length - 1 : 0
358
- // const groupScaleDomainMin = data.fullDataFormatter.grid.groupAxis.scaleDomain[0] === 'auto'
359
- // ? groupMin - data.fullDataFormatter.grid.groupAxis.scalePadding
360
- // : data.fullDataFormatter.grid.groupAxis.scaleDomain[0] as number - data.fullDataFormatter.grid.groupAxis.scalePadding
361
- const groupScaleDomainMin = data.fullDataFormatter.grid.groupAxis.scaleDomain[0] - data.fullDataFormatter.grid.groupAxis.scalePadding
362
- const groupScaleDomainMax = data.fullDataFormatter.grid.groupAxis.scaleDomain[1] === 'max'
363
- ? groupMax + data.fullDataFormatter.grid.groupAxis.scalePadding
364
- : data.fullDataFormatter.grid.groupAxis.scaleDomain[1] as number + data.fullDataFormatter.grid.groupAxis.scalePadding
365
-
366
- return [groupScaleDomainMin, groupScaleDomainMax]
367
- }),
368
- shareReplay(1)
369
- )
370
-
371
- const groupLabels$ = combineLatest({
372
- fullDataFormatter: fullDataFormatter$,
373
- computedData: computedData$
374
- }).pipe(
375
- switchMap(async d => d),
376
- map(data => {
377
- return data.fullDataFormatter.grid.seriesDirection === 'row'
378
- ? (data.computedData[0] ?? []).map(d => d.groupLabel)
379
- : data.computedData.map(d => d[0].groupLabel)
380
- })
381
- )
382
-
383
- const scaleRangeGroupLabels$ = combineLatest({
384
- groupScaleDomain: groupScaleDomain$,
385
- groupLabels: groupLabels$
386
- }).pipe(
387
- switchMap(async d => d),
388
- map(data => {
389
- return data.groupLabels
390
- .filter((d, i) => {
391
- return i >= data.groupScaleDomain[0] && i <= data.groupScaleDomain[1]
392
- })
393
- })
394
- )
395
-
396
- const reverse$ = fullDataFormatter$.pipe(
397
- map(d => {
398
- return d.grid.valueAxis.position === 'right' || d.grid.valueAxis.position === 'bottom'
399
- ? true
400
- : false
401
- })
402
- )
403
-
404
- // 比例尺座標對應非連續資料索引
405
- const xIndexScale$ = combineLatest({
406
- reverse: reverse$,
407
- gridAxesSize: gridAxesSize$,
408
- scaleRangeGroupLabels: scaleRangeGroupLabels$,
409
- fullDataFormatter: fullDataFormatter$
410
- }).pipe(
411
- switchMap(async d => d),
412
- map(data => {
413
- return createAxisQuantizeScale({
414
- axisLabels: data.scaleRangeGroupLabels,
415
- axisWidth: data.gridAxesSize.width,
416
- padding: data.fullDataFormatter.grid.groupAxis.scalePadding,
417
- reverse: data.reverse
418
- })
419
- })
420
- )
421
-
422
- const columnAmount$ = gridContainerPosition$.pipe(
423
- map(gridContainerPosition => {
424
- const maxColumnIndex = gridContainerPosition.reduce((acc, current) => {
425
- return current.columnIndex > acc ? current.columnIndex : acc
426
- }, 0)
427
- return maxColumnIndex + 1
428
- }),
429
- distinctUntilChanged()
430
- )
431
-
432
- const rowAmount$ = gridContainerPosition$.pipe(
433
- map(gridContainerPosition => {
434
- const maxRowIndex = gridContainerPosition.reduce((acc, current) => {
435
- return current.rowIndex > acc ? current.rowIndex : acc
436
- }, 0)
437
- return maxRowIndex + 1
438
- }),
439
- distinctUntilChanged()
440
- )
441
-
442
- const axisValue$ = combineLatest({
443
- fullDataFormatter: fullDataFormatter$,
444
- fullChartParams: fullChartParams$,
445
- rootMousemove: rootMousemove$,
446
- columnAmount: columnAmount$,
447
- rowAmount: rowAmount$,
448
- layout: layout$
449
- }).pipe(
450
- switchMap(async d => d),
451
- map(data => {
452
- // 由於event座標是基於底層的,但是container會有多欄,所以要重新計算
453
- const eventData = {
454
- offsetX: data.rootMousemove.offsetX * data.columnAmount % data.layout.rootWidth,
455
- offsetY: data.rootMousemove.offsetY * data.rowAmount % data.layout.rootHeight
456
- }
457
- return data.fullDataFormatter.grid.groupAxis.position === 'bottom'
458
- || data.fullDataFormatter.grid.groupAxis.position === 'top'
459
- ? eventData.offsetX - data.fullChartParams.padding.left
460
- : eventData.offsetY - data.fullChartParams.padding.top
461
- })
462
- )
463
-
464
- const groupIndex$ = combineLatest({
465
- xIndexScale: xIndexScale$,
466
- axisValue: axisValue$,
467
- groupScaleDomain: groupScaleDomain$
468
- }).pipe(
469
- switchMap(async d => d),
470
- map(data => {
471
- const xIndex = data.xIndexScale(data.axisValue)
472
- const currentxIndexStart = Math.ceil(data.groupScaleDomain[0]) // 因為有padding所以會有小數點,所以要無條件進位
473
- return xIndex + currentxIndexStart
474
- })
475
- )
476
-
477
- const groupLabel$ = combineLatest({
478
- groupIndex: groupIndex$,
479
- groupLabels: groupLabels$
480
- }).pipe(
481
- switchMap(async d => d),
482
- map(data => {
483
- return data.groupLabels[data.groupIndex] ?? ''
484
- })
485
- )
486
-
487
- return combineLatest({
488
- groupIndex: groupIndex$,
489
- groupLabel: groupLabel$
490
- }).pipe(
491
- switchMap(async d => d),
492
- map(data => {
493
- return {
494
- groupIndex: data.groupIndex,
495
- groupLabel: data.groupLabel
496
- }
497
- })
498
- )
499
- }
500
-
501
- // const gridContainerEventData$ = ({ eventData$, gridContainerPosition$, layout$ }: {
502
- // eventData$: Observable<any>
503
- // gridContainerPosition$: Observable<GridContainerPosition[]>
504
- // layout$: Observable<Layout>
505
- // }): Observable<{
506
- // offsetX: number;
507
- // offsetY: number;
508
- // }> => {
509
- // const columnAmount$ = gridContainerPosition$.pipe(
510
- // map(gridContainerPosition => {
511
- // const maxColumnIndex = gridContainerPosition.reduce((acc, current) => {
512
- // return current.columnIndex > acc ? current.columnIndex : acc
513
- // }, 0)
514
- // return maxColumnIndex + 1
515
- // }),
516
- // distinctUntilChanged()
517
- // )
518
-
519
- // const rowAmount$ = gridContainerPosition$.pipe(
520
- // map(gridContainerPosition => {
521
- // const maxRowIndex = gridContainerPosition.reduce((acc, current) => {
522
- // return current.rowIndex > acc ? current.rowIndex : acc
523
- // }, 0)
524
- // return maxRowIndex + 1
525
- // }),
526
- // distinctUntilChanged()
527
- // )
528
-
529
- // return combineLatest({
530
- // eventData: eventData$,
531
- // gridContainerPosition: gridContainerPosition$,
532
- // layout: layout$,
533
- // columnAmount: columnAmount$,
534
- // rowAmount: rowAmount$
535
- // }).pipe(
536
- // switchMap(async d => d),
537
- // map(data => {
538
- // // 由於event座標是基於底層的,但是container會有多欄,所以要重新計算
539
- // const eventData = {
540
- // offsetX: data.eventData.offsetX * data.columnAmount % data.layout.rootWidth,
541
- // offsetY: data.eventData.offsetY * data.rowAmount % data.layout.rootHeight
542
- // }
543
- // return eventData
544
- // })
545
- // )
1
+ import * as d3 from 'd3'
2
+ import {
3
+ Observable,
4
+ Subject,
5
+ of,
6
+ takeUntil,
7
+ filter,
8
+ map,
9
+ switchMap,
10
+ combineLatest,
11
+ merge,
12
+ shareReplay,
13
+ distinctUntilChanged
14
+ } from 'rxjs'
15
+ import type {
16
+ ChartParams,
17
+ HighlightTarget,
18
+ DataFormatterGrid,
19
+ ComputedDataGrid,
20
+ ComputedDatumGrid,
21
+ TransformData,
22
+ ContainerPositionScaled,
23
+ Layout } from '../../lib/core-types'
24
+ import { createAxisToLabelIndexScale } from '../../lib/core'
25
+ import { getClassName, getUniID } from '../utils/orbchartsUtils'
26
+ import { d3EventObservable } from '../utils/observables'
27
+
28
+ // 建立 grid 主要的 selection
29
+ export const gridSelectionsObservable = ({ selection, pluginName, clipPathID, seriesLabels$, gridContainerPosition$, gridAxesTransform$, gridGraphicTransform$ }: {
30
+ selection: d3.Selection<any, unknown, any, unknown>
31
+ pluginName: string
32
+ clipPathID: string
33
+ // computedData$: Observable<ComputedDataGrid>
34
+ seriesLabels$: Observable<string[]>
35
+ gridContainerPosition$: Observable<ContainerPositionScaled[]>
36
+ gridAxesTransform$: Observable<TransformData>
37
+ gridGraphicTransform$: Observable<TransformData>
38
+ }) => {
39
+ const seriesClassName = getClassName(pluginName, 'series')
40
+ const axesClassName = getClassName(pluginName, 'axes')
41
+ const graphicClassName = getClassName(pluginName, 'graphic')
42
+
43
+ // <g> series selection(container排放位置)
44
+ // <g> axes selection(旋轉圖軸方向)
45
+ // <defs> clipPath selection
46
+ // <g> graphic selection(圖形 scale 範圍的變形)
47
+ const seriesSelection$ = seriesLabels$.pipe(
48
+ map((seriesLabels, i) => {
49
+ return selection
50
+ .selectAll<SVGGElement, string>(`g.${seriesClassName}`)
51
+ .data(seriesLabels, d => d)
52
+ .join(
53
+ enter => {
54
+ return enter
55
+ .append('g')
56
+ .classed(seriesClassName, true)
57
+ .each((d, i, g) => {
58
+ const axesSelection = d3.select(g[i])
59
+ .selectAll<SVGGElement, ComputedDatumGrid[]>(`g.${axesClassName}`)
60
+ .data([i])
61
+ .join(
62
+ enter => {
63
+ return enter
64
+ .append('g')
65
+ .classed(axesClassName, true)
66
+ .attr('clip-path', `url(#${clipPathID})`)
67
+ .each((d, i, g) => {
68
+ const defsSelection = d3.select(g[i])
69
+ .selectAll<SVGDefsElement, any>('defs')
70
+ .data([i])
71
+ .join('defs')
72
+
73
+ const graphicGSelection = d3.select(g[i])
74
+ .selectAll<SVGGElement, any>('g')
75
+ .data([i])
76
+ .join('g')
77
+ .classed(graphicClassName, true)
78
+ })
79
+ },
80
+ update => update,
81
+ exit => exit.remove()
82
+ )
83
+ })
84
+ },
85
+ update => update,
86
+ exit => exit.remove()
87
+ )
88
+ }),
89
+ shareReplay(1)
90
+ )
91
+
92
+ // <g> series selection
93
+ combineLatest({
94
+ seriesSelection: seriesSelection$,
95
+ gridContainerPosition: gridContainerPosition$
96
+ }).pipe(
97
+ switchMap(async d => d)
98
+ ).subscribe(data => {
99
+ data.seriesSelection
100
+ .transition()
101
+ .attr('transform', (d, i) => {
102
+ const gridContainerPosition = data.gridContainerPosition[i] ?? data.gridContainerPosition[0]
103
+ const translate = gridContainerPosition.translate
104
+ const scale = gridContainerPosition.scale
105
+ return `translate(${translate[0]}, ${translate[1]}) scale(${scale[0]}, ${scale[1]})`
106
+ })
107
+ })
108
+
109
+ // <g> axes selection
110
+ const axesSelection$ = combineLatest({
111
+ seriesSelection: seriesSelection$,
112
+ gridAxesTransform: gridAxesTransform$
113
+ }).pipe(
114
+ switchMap(async d => d),
115
+ map(data => {
116
+ return data.seriesSelection
117
+ .select<SVGGElement>(`g.${axesClassName}`)
118
+ .style('transform', data.gridAxesTransform.value)
119
+ }),
120
+ shareReplay(1)
121
+ )
122
+
123
+ // <defs> clipPath selection
124
+ const defsSelection$ = axesSelection$.pipe(
125
+ map(axesSelection => {
126
+ return axesSelection.select<SVGDefsElement>('defs')
127
+ }),
128
+ shareReplay(1)
129
+ )
130
+
131
+ // <g> graphic selection
132
+ const graphicGSelection$ = combineLatest({
133
+ axesSelection: axesSelection$,
134
+ gridGraphicTransform: gridGraphicTransform$
135
+ }).pipe(
136
+ switchMap(async d => d),
137
+ map(data => {
138
+ const graphicGSelection = data.axesSelection
139
+ .select<SVGGElement>(`g.${graphicClassName}`)
140
+ graphicGSelection
141
+ .transition()
142
+ .duration(50)
143
+ .style('transform', data.gridGraphicTransform.value)
144
+ return graphicGSelection
145
+ }),
146
+ shareReplay(1)
147
+ )
148
+
149
+ return {
150
+ seriesSelection$,
151
+ axesSelection$,
152
+ defsSelection$,
153
+ graphicGSelection$
154
+ }
155
+ }
156
+
157
+ // 建立 grid 主要的 selection - 只取的container
158
+ export const gridContainerSelectionsObservable = ({ selection, pluginName, computedData$, gridContainerPosition$, isSeriesSeprate$ }: {
159
+ selection: d3.Selection<any, unknown, any, unknown>
160
+ pluginName: string
161
+ computedData$: Observable<ComputedDataGrid>
162
+ gridContainerPosition$: Observable<ContainerPositionScaled[]>
163
+ isSeriesSeprate$: Observable<boolean>
164
+ }) => {
165
+ const containerClassName = getClassName(pluginName, 'container')
166
+
167
+ const containerSelection$ = combineLatest({
168
+ computedData: computedData$.pipe(
169
+ distinctUntilChanged((a, b) => {
170
+ // 只有當series的數量改變時,才重新計算
171
+ return a.length === b.length
172
+ }),
173
+ ),
174
+ isSeriesSeprate: isSeriesSeprate$
175
+ }).pipe(
176
+ switchMap(async (d) => d),
177
+ map(data => {
178
+ return data.isSeriesSeprate
179
+ // series分開的時候顯示各別axis
180
+ ? data.computedData
181
+ // series合併的時候只顯示第一個axis
182
+ : [data.computedData[0]]
183
+ }),
184
+ map((computedData, i) => {
185
+ return selection
186
+ .selectAll<SVGGElement, ComputedDatumGrid[]>(`g.${containerClassName}`)
187
+ .data(computedData, d => d[0] ? d[0].seriesIndex : i)
188
+ .join('g')
189
+ .classed(containerClassName, true)
190
+ }),
191
+ shareReplay(1)
192
+ )
193
+
194
+ combineLatest({
195
+ containerSelection: containerSelection$,
196
+ gridContainerPosition: gridContainerPosition$
197
+ }).pipe(
198
+ switchMap(async d => d)
199
+ ).subscribe(data => {
200
+ data.containerSelection
201
+ .attr('transform', (d, i) => {
202
+ const gridContainerPosition = data.gridContainerPosition[i] ?? data.gridContainerPosition[0]
203
+ const translate = gridContainerPosition.translate
204
+ const scale = gridContainerPosition.scale
205
+ return `translate(${translate[0]}, ${translate[1]}) scale(${scale[0]}, ${scale[1]})`
206
+ })
207
+ // .attr('opacity', 0)
208
+ // .transition()
209
+ // .attr('opacity', 1)
210
+ })
211
+
212
+ return containerSelection$
213
+ }
214
+
215
+ // 由事件取得group data的function
216
+ export const gridGroupPositionFnObservable = ({ fullDataFormatter$, gridAxesSize$, computedData$, fullChartParams$, gridContainerPosition$, layout$ }: {
217
+ fullDataFormatter$: Observable<DataFormatterGrid>
218
+ gridAxesSize$: Observable<{
219
+ width: number;
220
+ height: number;
221
+ }>
222
+ computedData$: Observable<ComputedDataGrid>
223
+ // GroupDataMap$: Observable<Map<string, ComputedDatumGrid[]>>
224
+ fullChartParams$: Observable<ChartParams>
225
+ gridContainerPosition$: Observable<ContainerPositionScaled[]>
226
+ layout$: Observable<Layout>
227
+ }): Observable<(event: any) => { groupIndex: number; groupLabel: string }> => {
228
+ const destroy$ = new Subject()
229
+
230
+ // 顯示範圍內的group labels
231
+ // const scaleRangeGroupLabels$: Observable<string[]> = new Observable(subscriber => {
232
+ // combineLatest({
233
+ // dataFormatter: fullDataFormatter$,
234
+ // computedData: computedData$
235
+ // }).pipe(
236
+ // takeUntil(destroy$),
237
+ // switchMap(async (d) => d),
238
+ // ).subscribe(data => {
239
+ // const groupMin = 0
240
+ // const groupMax = data.computedData[0] ? data.computedData[0].length - 1 : 0
241
+ // const groupScaleDomainMin = data.dataFormatter.groupAxis.scaleDomain[0] === 'auto'
242
+ // ? groupMin - data.dataFormatter.groupAxis.scalePadding
243
+ // : data.dataFormatter.groupAxis.scaleDomain[0] as number - data.dataFormatter.groupAxis.scalePadding
244
+ // const groupScaleDomainMax = data.dataFormatter.groupAxis.scaleDomain[1] === 'auto'
245
+ // ? groupMax + data.dataFormatter.groupAxis.scalePadding
246
+ // : data.dataFormatter.groupAxis.scaleDomain[1] as number + data.dataFormatter.groupAxis.scalePadding
247
+
248
+ // // const groupingAmount = data.computedData[0]
249
+ // // ? data.computedData[0].length
250
+ // // : 0
251
+
252
+ // let _labels = data.dataFormatter.seriesDirection === 'row'
253
+ // ? (data.computedData[0] ?? []).map(d => d.groupLabel)
254
+ // : data.computedData.map(d => d[0].groupLabel)
255
+
256
+ // const _axisLabels =
257
+ // // new Array(groupingAmount).fill(0)
258
+ // // .map((d, i) => {
259
+ // // return _labels[i] != null
260
+ // // ? _labels[i]
261
+ // // : String(i) // 沒有label則用序列號填充
262
+ // // })
263
+ // _labels
264
+ // .filter((d, i) => {
265
+ // return i >= groupScaleDomainMin && i <= groupScaleDomainMax
266
+ // })
267
+ // subscriber.next(_axisLabels)
268
+ // })
269
+ // })
270
+ const groupScaleDomain$ = combineLatest({
271
+ fullDataFormatter: fullDataFormatter$,
272
+ gridAxesSize: gridAxesSize$,
273
+ computedData: computedData$
274
+ }).pipe(
275
+ switchMap(async (d) => d),
276
+ map(data => {
277
+ const groupMin = 0
278
+ const groupMax = data.computedData[0] ? data.computedData[0].length - 1 : 0
279
+ // const groupScaleDomainMin = data.fullDataFormatter.groupAxis.scaleDomain[0] === 'auto'
280
+ // ? groupMin - data.fullDataFormatter.groupAxis.scalePadding
281
+ // : data.fullDataFormatter.groupAxis.scaleDomain[0] as number - data.fullDataFormatter.groupAxis.scalePadding
282
+ const groupScaleDomainMin = data.fullDataFormatter.groupAxis.scaleDomain[0] - data.fullDataFormatter.groupAxis.scalePadding
283
+ const groupScaleDomainMax = data.fullDataFormatter.groupAxis.scaleDomain[1] === 'max'
284
+ ? groupMax + data.fullDataFormatter.groupAxis.scalePadding
285
+ : data.fullDataFormatter.groupAxis.scaleDomain[1] as number + data.fullDataFormatter.groupAxis.scalePadding
286
+
287
+ return [groupScaleDomainMin, groupScaleDomainMax]
288
+ }),
289
+ shareReplay(1)
290
+ )
291
+
292
+ const groupLabels$ = combineLatest({
293
+ fullDataFormatter: fullDataFormatter$,
294
+ computedData: computedData$
295
+ }).pipe(
296
+ switchMap(async d => d),
297
+ map(data => {
298
+ return data.fullDataFormatter.seriesDirection === 'row'
299
+ ? (data.computedData[0] ?? []).map(d => d.groupLabel)
300
+ : data.computedData.map(d => d[0].groupLabel)
301
+ })
302
+ )
303
+
304
+ // 顯示範圍內的group labels
305
+ const scaleRangeGroupLabels$ = combineLatest({
306
+ groupScaleDomain: groupScaleDomain$,
307
+ groupLabels: groupLabels$
308
+ }).pipe(
309
+ switchMap(async d => d),
310
+ map(data => {
311
+ return data.groupLabels
312
+ .filter((d, i) => {
313
+ return i >= data.groupScaleDomain[0] && i <= data.groupScaleDomain[1]
314
+ })
315
+ })
316
+ )
317
+
318
+ const columnAmount$ = gridContainerPosition$.pipe(
319
+ map(gridContainerPosition => {
320
+ const maxColumnIndex = gridContainerPosition.reduce((acc, current) => {
321
+ return current.columnIndex > acc ? current.columnIndex : acc
322
+ }, 0)
323
+ return maxColumnIndex + 1
324
+ }),
325
+ distinctUntilChanged()
326
+ )
327
+
328
+ const rowAmount$ = gridContainerPosition$.pipe(
329
+ map(gridContainerPosition => {
330
+ const maxRowIndex = gridContainerPosition.reduce((acc, current) => {
331
+ return current.rowIndex > acc ? current.rowIndex : acc
332
+ }, 0)
333
+ return maxRowIndex + 1
334
+ }),
335
+ distinctUntilChanged()
336
+ )
337
+
338
+ return new Observable<(event: any) => { groupIndex: number; groupLabel: string }>(subscriber => {
339
+ combineLatest({
340
+ dataFormatter: fullDataFormatter$,
341
+ axisSize: gridAxesSize$,
342
+ fullChartParams: fullChartParams$,
343
+ scaleRangeGroupLabels: scaleRangeGroupLabels$,
344
+ groupLabels: groupLabels$,
345
+ groupScaleDomain: groupScaleDomain$,
346
+ columnAmount: columnAmount$,
347
+ rowAmount: rowAmount$,
348
+ layout: layout$
349
+ }).pipe(
350
+ takeUntil(destroy$),
351
+ switchMap(async (d) => d),
352
+ ).subscribe(data => {
353
+
354
+ const reverse = data.dataFormatter.valueAxis.position === 'right'
355
+ || data.dataFormatter.valueAxis.position === 'bottom'
356
+ ? true : false
357
+
358
+ // 比例尺座標對應非連續資料索引
359
+ const xIndexScale = createAxisToLabelIndexScale({
360
+ axisLabels: data.scaleRangeGroupLabels,
361
+ axisWidth: data.axisSize.width,
362
+ padding: data.dataFormatter.groupAxis.scalePadding,
363
+ reverse
364
+ })
365
+
366
+ // 依比例尺位置計算座標
367
+ const axisValuePredicate = (event: any) => {
368
+ return data.dataFormatter.groupAxis.position === 'bottom'
369
+ || data.dataFormatter.groupAxis.position === 'top'
370
+ ? event.offsetX - data.fullChartParams.padding.left
371
+ : event.offsetY - data.fullChartParams.padding.top
372
+ }
373
+
374
+ // 比例尺座標取得groupData的function
375
+ const createEventGroupData: (event: MouseEvent) => { groupIndex: number; groupLabel: string } = (event: any) => {
376
+ // 由於event座標是基於底層的,但是container會有多欄,所以要重新計算
377
+ const eventData = {
378
+ offsetX: event.offsetX * data.columnAmount % data.layout.rootWidth,
379
+ offsetY: event.offsetY * data.rowAmount % data.layout.rootHeight
380
+ }
381
+ // console.log('data.columnAmount', data.columnAmount, 'data.rowAmount', data.rowAmount, 'data.layout.rootWidth', data.layout.rootWidth, 'data.layout.rootHeight', data.layout.rootHeight)
382
+ const axisValue = axisValuePredicate(eventData)
383
+ const xIndex = xIndexScale(axisValue)
384
+ const currentxIndexStart = Math.ceil(data.groupScaleDomain[0]) // 因為有padding所以會有小數點,所以要無條件進位
385
+ const groupIndex = xIndex + currentxIndexStart
386
+
387
+ return {
388
+ groupIndex,
389
+ groupLabel: data.groupLabels[groupIndex] ?? ''
390
+ }
391
+ }
392
+
393
+ subscriber.next(createEventGroupData)
394
+
395
+ return function unsubscribe () {
396
+ destroy$.next(undefined)
397
+ }
398
+ })
399
+ })
400
+ }
401
+
402
+ export const gridGroupPositionObservable = ({ rootSelection, fullDataFormatter$, gridAxesSize$, computedData$, fullChartParams$, gridContainerPosition$, layout$ }: {
403
+ rootSelection: d3.Selection<any, unknown, any, unknown>
404
+ fullDataFormatter$: Observable<DataFormatterGrid>
405
+ gridAxesSize$: Observable<{
406
+ width: number;
407
+ height: number;
408
+ }>
409
+ computedData$: Observable<ComputedDataGrid>
410
+ fullChartParams$: Observable<ChartParams>
411
+ gridContainerPosition$: Observable<ContainerPositionScaled[]>
412
+ layout$: Observable<Layout>
413
+ }) => {
414
+ const rootMousemove$ = d3EventObservable(rootSelection, 'mousemove')
415
+
416
+ const groupScaleDomain$ = combineLatest({
417
+ fullDataFormatter: fullDataFormatter$,
418
+ gridAxesSize: gridAxesSize$,
419
+ computedData: computedData$
420
+ }).pipe(
421
+ switchMap(async (d) => d),
422
+ map(data => {
423
+ const groupMin = 0
424
+ const groupMax = data.computedData[0] ? data.computedData[0].length - 1 : 0
425
+ // const groupScaleDomainMin = data.fullDataFormatter.groupAxis.scaleDomain[0] === 'auto'
426
+ // ? groupMin - data.fullDataFormatter.groupAxis.scalePadding
427
+ // : data.fullDataFormatter.groupAxis.scaleDomain[0] as number - data.fullDataFormatter.groupAxis.scalePadding
428
+ const groupScaleDomainMin = data.fullDataFormatter.groupAxis.scaleDomain[0] - data.fullDataFormatter.groupAxis.scalePadding
429
+ const groupScaleDomainMax = data.fullDataFormatter.groupAxis.scaleDomain[1] === 'max'
430
+ ? groupMax + data.fullDataFormatter.groupAxis.scalePadding
431
+ : data.fullDataFormatter.groupAxis.scaleDomain[1] as number + data.fullDataFormatter.groupAxis.scalePadding
432
+
433
+ return [groupScaleDomainMin, groupScaleDomainMax]
434
+ }),
435
+ shareReplay(1)
436
+ )
437
+
438
+ const groupLabels$ = combineLatest({
439
+ fullDataFormatter: fullDataFormatter$,
440
+ computedData: computedData$
441
+ }).pipe(
442
+ switchMap(async d => d),
443
+ map(data => {
444
+ return data.fullDataFormatter.seriesDirection === 'row'
445
+ ? (data.computedData[0] ?? []).map(d => d.groupLabel)
446
+ : data.computedData.map(d => d[0].groupLabel)
447
+ })
448
+ )
449
+
450
+ const scaleRangeGroupLabels$ = combineLatest({
451
+ groupScaleDomain: groupScaleDomain$,
452
+ groupLabels: groupLabels$
453
+ }).pipe(
454
+ switchMap(async d => d),
455
+ map(data => {
456
+ return data.groupLabels
457
+ .filter((d, i) => {
458
+ return i >= data.groupScaleDomain[0] && i <= data.groupScaleDomain[1]
459
+ })
460
+ })
461
+ )
462
+
463
+ const reverse$ = fullDataFormatter$.pipe(
464
+ map(d => {
465
+ return d.valueAxis.position === 'right' || d.valueAxis.position === 'bottom'
466
+ ? true
467
+ : false
468
+ })
469
+ )
470
+
471
+ // 比例尺座標對應非連續資料索引
472
+ const xIndexScale$ = combineLatest({
473
+ reverse: reverse$,
474
+ gridAxesSize: gridAxesSize$,
475
+ scaleRangeGroupLabels: scaleRangeGroupLabels$,
476
+ fullDataFormatter: fullDataFormatter$
477
+ }).pipe(
478
+ switchMap(async d => d),
479
+ map(data => {
480
+ return createAxisToLabelIndexScale({
481
+ axisLabels: data.scaleRangeGroupLabels,
482
+ axisWidth: data.gridAxesSize.width,
483
+ padding: data.fullDataFormatter.groupAxis.scalePadding,
484
+ reverse: data.reverse
485
+ })
486
+ })
487
+ )
488
+
489
+ const columnAmount$ = gridContainerPosition$.pipe(
490
+ map(gridContainerPosition => {
491
+ const maxColumnIndex = gridContainerPosition.reduce((acc, current) => {
492
+ return current.columnIndex > acc ? current.columnIndex : acc
493
+ }, 0)
494
+ return maxColumnIndex + 1
495
+ }),
496
+ distinctUntilChanged()
497
+ )
498
+
499
+ const rowAmount$ = gridContainerPosition$.pipe(
500
+ map(gridContainerPosition => {
501
+ const maxRowIndex = gridContainerPosition.reduce((acc, current) => {
502
+ return current.rowIndex > acc ? current.rowIndex : acc
503
+ }, 0)
504
+ return maxRowIndex + 1
505
+ }),
506
+ distinctUntilChanged()
507
+ )
508
+
509
+ const axisValue$ = combineLatest({
510
+ fullDataFormatter: fullDataFormatter$,
511
+ fullChartParams: fullChartParams$,
512
+ rootMousemove: rootMousemove$,
513
+ columnAmount: columnAmount$,
514
+ rowAmount: rowAmount$,
515
+ layout: layout$
516
+ }).pipe(
517
+ switchMap(async d => d),
518
+ map(data => {
519
+ // 由於event座標是基於底層的,但是container會有多欄,所以要重新計算
520
+ const eventData = {
521
+ offsetX: data.rootMousemove.offsetX * data.columnAmount % data.layout.rootWidth,
522
+ offsetY: data.rootMousemove.offsetY * data.rowAmount % data.layout.rootHeight
523
+ }
524
+ return data.fullDataFormatter.groupAxis.position === 'bottom'
525
+ || data.fullDataFormatter.groupAxis.position === 'top'
526
+ ? eventData.offsetX - data.fullChartParams.padding.left
527
+ : eventData.offsetY - data.fullChartParams.padding.top
528
+ })
529
+ )
530
+
531
+ const groupIndex$ = combineLatest({
532
+ xIndexScale: xIndexScale$,
533
+ axisValue: axisValue$,
534
+ groupScaleDomain: groupScaleDomain$
535
+ }).pipe(
536
+ switchMap(async d => d),
537
+ map(data => {
538
+ const xIndex = data.xIndexScale(data.axisValue)
539
+ const currentxIndexStart = Math.ceil(data.groupScaleDomain[0]) // 因為有padding所以會有小數點,所以要無條件進位
540
+ return xIndex + currentxIndexStart
541
+ })
542
+ )
543
+
544
+ const groupLabel$ = combineLatest({
545
+ groupIndex: groupIndex$,
546
+ groupLabels: groupLabels$
547
+ }).pipe(
548
+ switchMap(async d => d),
549
+ map(data => {
550
+ return data.groupLabels[data.groupIndex] ?? ''
551
+ })
552
+ )
553
+
554
+ return combineLatest({
555
+ groupIndex: groupIndex$,
556
+ groupLabel: groupLabel$
557
+ }).pipe(
558
+ switchMap(async d => d),
559
+ map(data => {
560
+ return {
561
+ groupIndex: data.groupIndex,
562
+ groupLabel: data.groupLabel
563
+ }
564
+ })
565
+ )
566
+ }
567
+
568
+ // const gridContainerEventData$ = ({ eventData$, gridContainerPosition$, layout$ }: {
569
+ // eventData$: Observable<any>
570
+ // gridContainerPosition$: Observable<ContainerPositionScaled[]>
571
+ // layout$: Observable<Layout>
572
+ // }): Observable<{
573
+ // offsetX: number;
574
+ // offsetY: number;
575
+ // }> => {
576
+ // const columnAmount$ = gridContainerPosition$.pipe(
577
+ // map(gridContainerPosition => {
578
+ // const maxColumnIndex = gridContainerPosition.reduce((acc, current) => {
579
+ // return current.columnIndex > acc ? current.columnIndex : acc
580
+ // }, 0)
581
+ // return maxColumnIndex + 1
582
+ // }),
583
+ // distinctUntilChanged()
584
+ // )
585
+
586
+ // const rowAmount$ = gridContainerPosition$.pipe(
587
+ // map(gridContainerPosition => {
588
+ // const maxRowIndex = gridContainerPosition.reduce((acc, current) => {
589
+ // return current.rowIndex > acc ? current.rowIndex : acc
590
+ // }, 0)
591
+ // return maxRowIndex + 1
592
+ // }),
593
+ // distinctUntilChanged()
594
+ // )
595
+
596
+ // return combineLatest({
597
+ // eventData: eventData$,
598
+ // gridContainerPosition: gridContainerPosition$,
599
+ // layout: layout$,
600
+ // columnAmount: columnAmount$,
601
+ // rowAmount: rowAmount$
602
+ // }).pipe(
603
+ // switchMap(async d => d),
604
+ // map(data => {
605
+ // // 由於event座標是基於底層的,但是container會有多欄,所以要重新計算
606
+ // const eventData = {
607
+ // offsetX: data.eventData.offsetX * data.columnAmount % data.layout.rootWidth,
608
+ // offsetY: data.eventData.offsetY * data.rowAmount % data.layout.rootHeight
609
+ // }
610
+ // return eventData
611
+ // })
612
+ // )
546
613
  // }