@orbcharts/plugins-basic 3.0.0-alpha.75 → 3.0.0-alpha.77

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 (143) hide show
  1. package/LICENSE +200 -200
  2. package/dist/lib/plugins-basic-types.d.ts +1 -0
  3. package/dist/orbcharts-plugins-basic.es.js +3461 -3468
  4. package/dist/orbcharts-plugins-basic.umd.js +17 -17
  5. package/dist/src/base/BaseBarStack.d.ts +1 -5
  6. package/dist/src/base/BaseBars.d.ts +1 -6
  7. package/dist/src/base/BaseBarsTriangle.d.ts +1 -6
  8. package/dist/src/base/BaseDots.d.ts +2 -8
  9. package/dist/src/base/BaseGroupAxis.d.ts +2 -16
  10. package/dist/src/base/BaseLegend.d.ts +2 -15
  11. package/dist/src/base/BaseLineAreas.d.ts +1 -4
  12. package/dist/src/base/BaseLines.d.ts +1 -4
  13. package/dist/src/base/BaseValueAxis.d.ts +2 -16
  14. package/dist/src/grid/defaults.d.ts +1 -1
  15. package/dist/src/grid/index.d.ts +0 -1
  16. package/dist/src/grid/plugins/BarStack.d.ts +1 -1
  17. package/dist/src/grid/plugins/Bars.d.ts +1 -1
  18. package/dist/src/grid/plugins/BarsPN.d.ts +1 -1
  19. package/dist/src/grid/plugins/BarsTriangle.d.ts +1 -1
  20. package/dist/src/grid/plugins/Dots.d.ts +1 -1
  21. package/dist/src/grid/plugins/GridLegend.d.ts +1 -1
  22. package/dist/src/grid/plugins/GroupAux.d.ts +1 -1
  23. package/dist/src/grid/plugins/GroupAxis.d.ts +1 -1
  24. package/dist/src/grid/plugins/LineAreas.d.ts +1 -1
  25. package/dist/src/grid/plugins/Lines.d.ts +1 -1
  26. package/dist/src/grid/plugins/ScalingArea.d.ts +1 -1
  27. package/dist/src/grid/plugins/ValueAxis.d.ts +1 -1
  28. package/dist/src/grid/plugins/ValueStackAxis.d.ts +1 -1
  29. package/dist/src/multiGrid/defaults.d.ts +1 -1
  30. package/dist/src/multiGrid/index.d.ts +0 -1
  31. package/dist/src/multiGrid/plugins/MultiBarStack.d.ts +1 -1
  32. package/dist/src/multiGrid/plugins/MultiBars.d.ts +1 -1
  33. package/dist/src/multiGrid/plugins/MultiBarsTriangle.d.ts +1 -1
  34. package/dist/src/multiGrid/plugins/MultiDots.d.ts +1 -1
  35. package/dist/src/multiGrid/plugins/MultiGridLegend.d.ts +1 -1
  36. package/dist/src/multiGrid/plugins/MultiGroupAxis.d.ts +1 -1
  37. package/dist/src/multiGrid/plugins/MultiLineAreas.d.ts +1 -1
  38. package/dist/src/multiGrid/plugins/MultiLines.d.ts +1 -1
  39. package/dist/src/multiGrid/plugins/MultiValueAxis.d.ts +1 -1
  40. package/dist/src/multiGrid/plugins/MultiValueStackAxis.d.ts +1 -1
  41. package/dist/src/multiGrid/plugins/OverlappingValueAxes.d.ts +1 -1
  42. package/dist/src/multiGrid/plugins/OverlappingValueStackAxes.d.ts +1 -1
  43. package/dist/src/noneData/defaults.d.ts +1 -1
  44. package/dist/src/noneData/index.d.ts +0 -1
  45. package/dist/src/noneData/plugins/Container.d.ts +1 -1
  46. package/dist/src/noneData/plugins/Tooltip.d.ts +1 -1
  47. package/dist/src/series/defaults.d.ts +1 -1
  48. package/dist/src/series/index.d.ts +0 -1
  49. package/dist/src/series/plugins/Bubbles.d.ts +1 -1
  50. package/dist/src/series/plugins/Pie.d.ts +1 -1
  51. package/dist/src/series/plugins/PieEventTexts.d.ts +1 -1
  52. package/dist/src/series/plugins/PieLabels.d.ts +1 -1
  53. package/dist/src/series/plugins/Rose.d.ts +1 -1
  54. package/dist/src/series/plugins/RoseLabels.d.ts +1 -1
  55. package/dist/src/series/plugins/SeriesLegend.d.ts +1 -1
  56. package/dist/src/tree/defaults.d.ts +1 -1
  57. package/dist/src/tree/index.d.ts +0 -1
  58. package/dist/src/tree/plugins/TreeLegend.d.ts +1 -1
  59. package/dist/src/tree/plugins/TreeMap.d.ts +1 -1
  60. package/lib/core-types.ts +7 -7
  61. package/lib/core.ts +6 -6
  62. package/lib/plugins-basic-types.ts +6 -0
  63. package/package.json +44 -43
  64. package/src/base/BaseBarStack.ts +780 -779
  65. package/src/base/BaseBars.ts +765 -764
  66. package/src/base/BaseBarsTriangle.ts +674 -672
  67. package/src/base/BaseDots.ts +515 -513
  68. package/src/base/BaseGroupAxis.ts +679 -678
  69. package/src/base/BaseLegend.ts +644 -642
  70. package/src/base/BaseLineAreas.ts +629 -627
  71. package/src/base/BaseLines.ts +706 -704
  72. package/src/base/BaseValueAxis.ts +580 -578
  73. package/src/base/types.ts +2 -2
  74. package/src/const.ts +30 -30
  75. package/src/grid/defaults.ts +128 -128
  76. package/src/grid/gridObservables.ts +545 -545
  77. package/src/grid/index.ts +15 -15
  78. package/src/grid/plugins/BarStack.ts +64 -64
  79. package/src/grid/plugins/Bars.ts +69 -69
  80. package/src/grid/plugins/BarsPN.ts +66 -66
  81. package/src/grid/plugins/BarsTriangle.ts +73 -73
  82. package/src/grid/plugins/Dots.ts +68 -68
  83. package/src/grid/plugins/GridLegend.ts +96 -96
  84. package/src/grid/plugins/GroupAux.ts +1098 -1098
  85. package/src/grid/plugins/GroupAxis.ts +97 -97
  86. package/src/grid/plugins/LineAreas.ts +65 -65
  87. package/src/grid/plugins/Lines.ts +59 -59
  88. package/src/grid/plugins/ScalingArea.ts +218 -218
  89. package/src/grid/plugins/ValueAxis.ts +93 -93
  90. package/src/grid/plugins/ValueStackAxis.ts +95 -95
  91. package/src/index.ts +9 -9
  92. package/src/multiGrid/defaults.ts +158 -158
  93. package/src/multiGrid/index.ts +13 -13
  94. package/src/multiGrid/multiGridObservables.ts +49 -49
  95. package/src/multiGrid/plugins/MultiBarStack.ts +106 -106
  96. package/src/multiGrid/plugins/MultiBars.ts +108 -108
  97. package/src/multiGrid/plugins/MultiBarsTriangle.ts +114 -114
  98. package/src/multiGrid/plugins/MultiDots.ts +102 -102
  99. package/src/multiGrid/plugins/MultiGridLegend.ts +148 -148
  100. package/src/multiGrid/plugins/MultiGroupAxis.ts +137 -137
  101. package/src/multiGrid/plugins/MultiLineAreas.ts +107 -107
  102. package/src/multiGrid/plugins/MultiLines.ts +101 -101
  103. package/src/multiGrid/plugins/MultiValueAxis.ts +133 -133
  104. package/src/multiGrid/plugins/MultiValueStackAxis.ts +133 -133
  105. package/src/multiGrid/plugins/OverlappingValueAxes.ts +299 -299
  106. package/src/multiGrid/plugins/OverlappingValueStackAxes.ts +298 -298
  107. package/src/noneData/defaults.ts +102 -102
  108. package/src/noneData/index.ts +3 -3
  109. package/src/noneData/plugins/Container.ts +27 -27
  110. package/src/noneData/plugins/Tooltip.ts +373 -373
  111. package/src/series/defaults.ts +149 -149
  112. package/src/series/index.ts +9 -9
  113. package/src/series/plugins/Bubbles.ts +603 -603
  114. package/src/series/plugins/Pie.ts +623 -623
  115. package/src/series/plugins/PieEventTexts.ts +283 -283
  116. package/src/series/plugins/PieLabels.ts +640 -640
  117. package/src/series/plugins/Rose.ts +516 -516
  118. package/src/series/plugins/RoseLabels.ts +600 -600
  119. package/src/series/plugins/SeriesLegend.ts +96 -96
  120. package/src/series/seriesObservables.ts +145 -145
  121. package/src/series/seriesUtils.ts +51 -51
  122. package/src/tree/defaults.ts +23 -23
  123. package/src/tree/index.ts +3 -3
  124. package/src/tree/plugins/TreeLegend.ts +96 -96
  125. package/src/tree/plugins/TreeMap.ts +333 -333
  126. package/src/utils/commonUtils.ts +21 -21
  127. package/src/utils/d3Graphics.ts +174 -174
  128. package/src/utils/d3Utils.ts +73 -73
  129. package/src/utils/observables.ts +14 -14
  130. package/src/utils/orbchartsUtils.ts +100 -100
  131. package/tsconfig.base.json +13 -13
  132. package/tsconfig.json +2 -2
  133. package/vite.config.js +22 -22
  134. package/dist/src/grid/types.d.ts +0 -93
  135. package/dist/src/multiGrid/types.d.ts +0 -61
  136. package/dist/src/noneData/types.d.ts +0 -24
  137. package/dist/src/series/types.d.ts +0 -73
  138. package/dist/src/tree/types.d.ts +0 -21
  139. package/src/grid/types.ts +0 -124
  140. package/src/multiGrid/types.ts +0 -73
  141. package/src/noneData/types.ts +0 -26
  142. package/src/series/types.ts +0 -87
  143. package/src/tree/types.ts +0 -23
@@ -1,780 +1,781 @@
1
- import * as d3 from 'd3'
2
- import {
3
- iif,
4
- combineLatest,
5
- map,
6
- switchMap,
7
- takeUntil,
8
- distinctUntilChanged,
9
- Observable,
10
- Subject } from 'rxjs'
11
- import type { BasePluginFn } from './types'
12
- import type {
13
- ComputedDatumGrid,
14
- ComputedDataGrid,
15
- ComputedLayoutDatumGrid,
16
- ComputedLayoutDataGrid,
17
- DataFormatterGrid,
18
- EventGrid,
19
- ChartParams,
20
- GridContainerPosition,
21
- Layout,
22
- TransformData } from '../../lib/core-types'
23
- import { getD3TransitionEase } from '../utils/d3Utils'
24
- import { getClassName, getUniID } from '../utils/orbchartsUtils'
25
- import { gridSelectionsObservable } from '../grid/gridObservables'
26
- import { shareReplay } from 'rxjs/operators'
27
-
28
- export interface BaseBarStackParams {
29
- barWidth: number
30
- barGroupPadding: number
31
- barRadius: number | boolean
32
- }
33
-
34
- interface BaseBarStackContext {
35
- selection: d3.Selection<any, unknown, any, unknown>
36
- computedData$: Observable<ComputedDataGrid>
37
- computedLayoutData$: Observable<ComputedLayoutDataGrid>
38
- visibleComputedData$: Observable<ComputedDatumGrid[][]>
39
- visibleComputedLayoutData$: Observable<ComputedLayoutDataGrid>
40
- seriesLabels$: Observable<string[]>
41
- SeriesDataMap$: Observable<Map<string, ComputedDatumGrid[]>>
42
- GroupDataMap$: Observable<Map<string, ComputedDatumGrid[]>>
43
- fullParams$: Observable<BaseBarStackParams>
44
- fullDataFormatter$: Observable<DataFormatterGrid>
45
- fullChartParams$: Observable<ChartParams>
46
- gridAxesTransform$: Observable<TransformData>
47
- gridGraphicTransform$: Observable<TransformData>
48
- gridGraphicReverseScale$: Observable<[number, number][]>
49
- gridAxesSize$: Observable<{
50
- width: number;
51
- height: number;
52
- }>
53
- gridHighlight$: Observable<ComputedDatumGrid[]>
54
- gridContainerPosition$: Observable<GridContainerPosition[]>
55
- isSeriesSeprate$: Observable<boolean>
56
- event$: Subject<EventGrid>
57
- }
58
-
59
-
60
- interface GraphicDatum extends ComputedLayoutDatumGrid {
61
- _barStartY: number // bar的起點y座標
62
- _barHeight: number // bar的高度
63
- }
64
-
65
- interface RenderBarParams {
66
- graphicGSelection: d3.Selection<SVGGElement, unknown, any, any>
67
- rectClassName: string
68
- barData: GraphicDatum[][]
69
- zeroY: number
70
- groupLabels: string[]
71
- // barScale: d3.ScalePoint<string>
72
- params: BaseBarStackParams
73
- chartParams: ChartParams
74
- barWidth: number
75
- transformedBarRadius: [number, number][]
76
- delayGroup: number
77
- transitionItem: number
78
- isSeriesSeprate: boolean
79
- }
80
-
81
- type ClipPathDatum = {
82
- id: string;
83
- // x: number;
84
- // y: number;
85
- width: number;
86
- height: number;
87
- }
88
-
89
- // const pluginName = 'BarStack'
90
- // const rectClassName = getClassName(pluginName, 'rect')
91
- // group的delay在動畫中的佔比(剩餘部份的時間為圖形本身的動畫時間,因為delay時間和最後一個group的動畫時間加總為1)
92
- const groupDelayProportionOfDuration = 0.3
93
-
94
- function calcBarWidth ({ axisWidth, groupAmount, barGroupPadding = 0 }: {
95
- axisWidth: number
96
- groupAmount: number
97
- barGroupPadding: number
98
- }) {
99
- const eachGroupWidth = axisWidth / (groupAmount - 1)
100
- const width = eachGroupWidth - barGroupPadding
101
- return width > 1 ? width : 1
102
-
103
- }
104
-
105
- // function makeBarScale (barWidth: number, seriesLabels: string[], params: BarStackParams) {
106
- // const barHalfWidth = barWidth! / 2
107
- // const barGroupWidth = barWidth * seriesLabels.length + params.barPadding! * seriesLabels.length
108
- // return d3.scalePoint()
109
- // .domain(seriesLabels)
110
- // .range([-barGroupWidth / 2 + barHalfWidth, barGroupWidth / 2 - barHalfWidth])
111
- // }
112
-
113
- function calcDelayGroup (barGroupAmount: number, totalDuration: number) {
114
- if (barGroupAmount <= 1) {
115
- // 一筆內計算會出錯所以不算
116
- return 0
117
- }
118
- return totalDuration / (barGroupAmount - 1) * groupDelayProportionOfDuration // 依group數量計算
119
- }
120
-
121
- function calctransitionItem (barGroupAmount: number, totalDuration: number) {
122
- if (barGroupAmount <= 1) {
123
- // 一筆內不會有delay
124
- return totalDuration
125
- }
126
- return totalDuration * (1 - groupDelayProportionOfDuration) // delay後剩餘的時間
127
- }
128
-
129
- function renderRectBars ({ graphicGSelection, rectClassName, barData, zeroY, groupLabels, params, chartParams, barWidth, transformedBarRadius, delayGroup, transitionItem, isSeriesSeprate }: RenderBarParams) {
130
-
131
- const barHalfWidth = barWidth! / 2
132
-
133
- graphicGSelection
134
- .each((seriesData, seriesIndex, g) => {
135
- d3.select(g[seriesIndex])
136
- .selectAll<SVGGElement, ComputedDatumGrid>(`rect.${rectClassName}`)
137
- .data(barData[seriesIndex] ?? [], d => d.id)
138
- .join(
139
- enter => {
140
- // console.log('enter')
141
- return enter
142
- .append('rect')
143
- .classed(rectClassName, true)
144
- .attr('cursor', 'pointer')
145
- .attr('height', d => 0)
146
- },
147
- update => update,
148
- exit => exit.remove()
149
- )
150
- .attr('transform', (d, i) => `translate(${(d ? d.axisX : 0) - barHalfWidth}, ${0})`)
151
- .attr('fill', d => d.color)
152
- .attr('y', d => zeroY)
153
- .attr('x', d =>0)
154
- .attr('width', barWidth!)
155
- .attr('rx', transformedBarRadius[seriesIndex][0] ?? 1)
156
- .attr('ry', transformedBarRadius[seriesIndex][1] ?? 1)
157
- .transition()
158
- .duration(transitionItem)
159
- .ease(getD3TransitionEase(chartParams.transitionEase))
160
- .delay((d, i) => d.groupIndex * delayGroup)
161
- .attr('y', d => d._barStartY)
162
- .attr('height', d => Math.abs(d._barHeight))
163
- })
164
-
165
- // const barGroup = graphicGSelection
166
- // .selectAll<SVGGElement, ComputedDatumGrid[]>(`g.${gClassName}`)
167
- // .data(data, (d, i) => groupLabels[i])
168
- // .join(
169
- // enter => {
170
- // return enter
171
- // .append('g')
172
- // .classed(gClassName, true)
173
- // .attr('cursor', 'pointer')
174
- // },
175
- // update => update,
176
- // exit => exit.remove()
177
- // )
178
- // .attr('transform', (d, i) => `translate(${d[0] ? d[0].axisX : 0}, ${0})`)
179
- // .each((d, i, g) => {
180
- // const bars = d3.select(g[i])
181
- // .selectAll<SVGGElement, ComputedDatumGrid>('g')
182
- // .data(d, _d => _d.id)
183
- // .join(
184
- // enter => {
185
- // return enter
186
- // .append('g')
187
- // .classed(gContentClassName, true)
188
- // },
189
- // update => update,
190
- // exit => exit.remove()
191
- // )
192
- // .each((_d, _i, _g) => {
193
- // const rect = d3.select(_g[_i])
194
- // .selectAll<SVGRectElement, ComputedDatumGrid>('rect')
195
- // .data([_d], _d => _d.id)
196
- // .join(
197
- // enter => {
198
- // return enter
199
- // .append('rect')
200
- // .attr('y', d => zeroY)
201
- // .attr('height', d => 0)
202
- // },
203
- // update => update,
204
- // exit => exit.remove()
205
- // )
206
- // .attr('rx', transformedBarRadius[0])
207
- // .attr('ry', transformedBarRadius[1])
208
- // .attr('fill', d => d.color)
209
- // .attr('transform', `translate(${-barHalfWidth}, 0)`)
210
- // .attr('x', d => 0)
211
- // .attr('width', barWidth!)
212
- // .transition()
213
- // .duration(transitionItem)
214
- // .ease(getD3TransitionEase(chartParams.transitionEase))
215
- // .delay((d, i) => d.groupIndex * delayGroup)
216
- // .attr('y', d => d._barStartY)
217
- // .attr('height', d => Math.abs(d._barHeight))
218
- // })
219
-
220
- // })
221
-
222
- const graphicBarSelection: d3.Selection<SVGRectElement, ComputedDatumGrid, SVGGElement, unknown> = graphicGSelection.selectAll(`rect.${rectClassName}`)
223
-
224
-
225
- return graphicBarSelection
226
- }
227
-
228
- function renderClipPath ({ defsSelection, clipPathData }: {
229
- defsSelection: d3.Selection<SVGDefsElement, any, any, any>
230
- clipPathData: ClipPathDatum[]
231
- }) {
232
- const clipPath = defsSelection
233
- .selectAll<SVGClipPathElement, Layout>('clipPath')
234
- .data(clipPathData)
235
- .join(
236
- enter => {
237
- return enter
238
- .append('clipPath')
239
- },
240
- update => update,
241
- exit => exit.remove()
242
- )
243
- .attr('id', d => d.id)
244
- .each((d, i, g) => {
245
- const rect = d3.select(g[i])
246
- .selectAll<SVGRectElement, typeof d>('rect')
247
- .data([d])
248
- .join(
249
- enter => {
250
- return enter
251
- .append('rect')
252
- },
253
- update => update,
254
- exit => exit.remove()
255
- )
256
- .attr('x', 0)
257
- .attr('y', 0)
258
- .attr('width', _d => _d.width)
259
- .attr('height', _d => _d.height)
260
- })
261
- }
262
-
263
- function highlight ({ selection, ids, fullChartParams }: {
264
- selection: d3.Selection<any, ComputedDatumGrid, any, any>
265
- ids: string[]
266
- fullChartParams: ChartParams
267
- }) {
268
- selection.interrupt('highlight')
269
-
270
- if (!ids.length) {
271
- // remove highlight
272
- selection
273
- .transition('highlight')
274
- .duration(200)
275
- .style('opacity', 1)
276
- return
277
- }
278
-
279
- selection
280
- .each((d, i, n) => {
281
- if (ids.includes(d.id)) {
282
- d3.select(n[i])
283
- .style('opacity', 1)
284
- } else {
285
- d3.select(n[i])
286
- .style('opacity', fullChartParams.styles.unhighlightedOpacity)
287
- }
288
- })
289
- }
290
-
291
-
292
- export const createBaseBarStack: BasePluginFn<BaseBarStackContext> = (pluginName: string, {
293
- selection,
294
- computedData$,
295
- computedLayoutData$,
296
- visibleComputedData$,
297
- visibleComputedLayoutData$,
298
- seriesLabels$,
299
- SeriesDataMap$,
300
- GroupDataMap$,
301
- fullParams$,
302
- fullDataFormatter$,
303
- fullChartParams$,
304
- gridAxesTransform$,
305
- gridGraphicTransform$,
306
- gridGraphicReverseScale$,
307
- gridAxesSize$,
308
- gridHighlight$,
309
- gridContainerPosition$,
310
- isSeriesSeprate$,
311
- event$
312
- }) => {
313
-
314
- const destroy$ = new Subject()
315
-
316
- const clipPathID = getUniID(pluginName, 'clipPath-box')
317
- const rectClassName = getClassName(pluginName, 'rect')
318
-
319
- const {
320
- seriesSelection$,
321
- axesSelection$,
322
- defsSelection$,
323
- graphicGSelection$
324
- } = gridSelectionsObservable({
325
- selection,
326
- pluginName,
327
- clipPathID,
328
- seriesLabels$,
329
- gridContainerPosition$,
330
- gridAxesTransform$,
331
- gridGraphicTransform$
332
- })
333
-
334
-
335
- const zeroY$ = visibleComputedLayoutData$.pipe(
336
- takeUntil(destroy$),
337
- map(d => d[0] && d[0][0]
338
- ? d[0][0].axisY - d[0][0].axisYFromZero
339
- : 0),
340
- distinctUntilChanged()
341
- )
342
-
343
- const barWidth$ = combineLatest({
344
- computedData: computedData$,
345
- // visibleComputedData: visibleComputedData$,
346
- params: fullParams$,
347
- axisSize: gridAxesSize$,
348
- isSeriesSeprate: isSeriesSeprate$
349
- }).pipe(
350
- takeUntil(destroy$),
351
- switchMap(async d => d),
352
- map(data => {
353
- const barWidth = data.params.barWidth
354
- ? data.params.barWidth
355
- : calcBarWidth({
356
- axisWidth: data.axisSize.width,
357
- groupAmount: data.computedData[0] ? data.computedData[0].length : 0,
358
- barGroupPadding: data.params.barGroupPadding
359
- })
360
- return barWidth
361
- }),
362
- distinctUntilChanged()
363
- )
364
-
365
- // 圓角的值 [rx, ry]
366
- const transformedBarRadius$: Observable<[number, number][]> = combineLatest({
367
- computedData: computedData$,
368
- // gridGraphicTransform: gridGraphicTransform$,
369
- barWidth: barWidth$,
370
- params: fullParams$,
371
- gridGraphicReverseScale: gridGraphicReverseScale$
372
- }).pipe(
373
- takeUntil(destroy$),
374
- switchMap(async data => data),
375
- map(data => {
376
- const barHalfWidth = data.barWidth! / 2
377
- const radius = data.params.barRadius === true ? barHalfWidth
378
- : data.params.barRadius === false ? 0
379
- : typeof data.params.barRadius == 'number' ? data.params.barRadius
380
- : 0
381
-
382
- return data.computedData.map((series, seriesIndex) => {
383
- const gridGraphicReverseScale = data.gridGraphicReverseScale[seriesIndex] ?? data.gridGraphicReverseScale[0]
384
-
385
- const transformedRx = radius * gridGraphicReverseScale[0]
386
- const transformedRy = radius * gridGraphicReverseScale[1]
387
-
388
- return [transformedRx, transformedRy]
389
- })
390
- })
391
- )
392
-
393
- // const seriesLabels$ = visibleComputedData$.pipe(
394
- // takeUntil(destroy$),
395
- // map(data => {
396
- // const SeriesLabelSet: Set<string> = new Set()
397
- // data.forEach(d => {
398
- // d.forEach(_d => {
399
- // SeriesLabelSet.add(_d.seriesLabel)
400
- // })
401
- // })
402
- // return Array.from(SeriesLabelSet)
403
- // })
404
- // )
405
-
406
- const groupLabels$ = visibleComputedData$.pipe(
407
- takeUntil(destroy$),
408
- map(data => {
409
- const GroupLabelSet: Set<string> = new Set()
410
- data.forEach(d => {
411
- d.forEach(_d => {
412
- GroupLabelSet.add(_d.groupLabel)
413
- })
414
- })
415
- return Array.from(GroupLabelSet)
416
- }),
417
- shareReplay(1)
418
- )
419
-
420
- const transitionDuration$ = fullChartParams$.pipe(
421
- takeUntil(destroy$),
422
- map(d => d.transitionDuration),
423
- distinctUntilChanged()
424
- )
425
-
426
- const delayGroup$ = new Observable<number>(subscriber => {
427
- combineLatest({
428
- groupLabels: groupLabels$,
429
- transitionDuration: transitionDuration$,
430
- }).pipe(
431
- switchMap(async d => d)
432
- ).subscribe(data => {
433
- const delay = calcDelayGroup(data.groupLabels.length, data.transitionDuration)
434
- subscriber.next(delay)
435
- })
436
- }).pipe(
437
- takeUntil(destroy$),
438
- distinctUntilChanged()
439
- )
440
-
441
- const transitionItem$ = new Observable<number>(subscriber => {
442
- combineLatest({
443
- groupLabels: groupLabels$,
444
- transitionDuration: transitionDuration$
445
- }).pipe(
446
- switchMap(async d => d)
447
- ).subscribe(data => {
448
- const transition = calctransitionItem(data.groupLabels.length, data.transitionDuration)
449
- subscriber.next(transition)
450
- })
451
- }).pipe(
452
- takeUntil(destroy$),
453
- distinctUntilChanged()
454
- )
455
-
456
- // const transposedVisibleData$: Observable<ComputedDataGrid> = visibleComputedData$.pipe(
457
- // takeUntil(destroy$),
458
- // map(data => {
459
- // console.log('visibleComputedData', data)
460
- // // 取得原始陣列的維度
461
- // const rows = data.length;
462
- // const cols = data.reduce((prev, current) => {
463
- // return Math.max(prev, current.length)
464
- // }, 0)
465
-
466
- // // 初始化轉換後的陣列
467
- // const transposedArray = new Array(cols).fill(null).map(() => new Array(rows).fill(null))
468
-
469
- // // 遍歷原始陣列,進行轉換
470
- // for (let i = 0; i < rows; i++) {
471
- // for (let j = 0; j < cols; j++) {
472
- // transposedArray[j][i] = data[i][j]
473
- // }
474
- // }
475
- // return transposedArray
476
- // })
477
- // )
478
-
479
- const groupScaleDomain$ = combineLatest({
480
- computedData: computedData$,
481
- dataFormatter: fullDataFormatter$,
482
- }).pipe(
483
- takeUntil(destroy$),
484
- switchMap(async d => d),
485
- map(data => {
486
- const groupMin = 0
487
- const groupMax = data.computedData[0] ? data.computedData[0].length - 1 : 0
488
- // const groupScaleDomainMin = data.dataFormatter.grid.groupAxis.scaleDomain[0] === 'auto'
489
- // ? groupMin // - data.dataFormatter.grid.groupAxis.scalePadding
490
- // : data.dataFormatter.grid.groupAxis.scaleDomain[0] as number // - data.dataFormatter.grid.groupAxis.scalePadding
491
- const groupScaleDomainMin = data.dataFormatter.grid.groupAxis.scaleDomain[0]
492
- const groupScaleDomainMax = data.dataFormatter.grid.groupAxis.scaleDomain[1] === 'max'
493
- ? groupMax // + data.dataFormatter.grid.groupAxis.scalePadding
494
- : data.dataFormatter.grid.groupAxis.scaleDomain[1] as number // + data.dataFormatter.grid.groupAxis.scalePadding
495
-
496
- return [groupScaleDomainMin, groupScaleDomainMax]
497
- })
498
- )
499
-
500
- // 堆疊後的高度對應圖軸的比例(最大值/最大值所在group的總和)
501
- const yRatio$ = combineLatest({
502
- visibleComputedLayoutData: visibleComputedLayoutData$,
503
- groupScaleDomain: groupScaleDomain$
504
- }).pipe(
505
- takeUntil(destroy$),
506
- switchMap(async d => d),
507
- map(data => {
508
- const groupScaleDomainMin = data.groupScaleDomain[0]
509
- const groupScaleDomainMax = data.groupScaleDomain[1]
510
- // 只選取group篩選範圍內的資料
511
- const filteredData = data.visibleComputedLayoutData
512
- .map(d => {
513
- return d.filter((_d, i) => {
514
- return _d.groupIndex >= groupScaleDomainMin && _d.groupIndex <= groupScaleDomainMax
515
- })
516
- })
517
-
518
- const filteredDataList = filteredData.flat()
519
- if (filteredDataList.length <= 1) {
520
- return 1
521
- }
522
-
523
- const maxValueDatum = filteredDataList.reduce((max, current) => {
524
- return current.value > max.value ? current : max;
525
- }, filteredDataList[0])
526
- const maxValueGroupIndex = maxValueDatum.groupIndex
527
- const maxValueGroupSum = filteredDataList
528
- .filter(d => d.groupIndex === maxValueGroupIndex)
529
- .reduce((sum, current) => {
530
- return sum + current.value
531
- }, 0)
532
- return maxValueDatum.value / maxValueGroupSum
533
- })
534
- )
535
-
536
- const stackedData$ = combineLatest({
537
- computedLayoutData: computedLayoutData$,
538
- yRatio: yRatio$,
539
- zeroY: zeroY$
540
- }).pipe(
541
- takeUntil(destroy$),
542
- map(data => {
543
- let accYArr: number[] = data.computedLayoutData[0]
544
- ? data.computedLayoutData[0].map(() => data.zeroY)
545
- : []
546
- return data.computedLayoutData.map((series, seriesIndex) => {
547
- return series.map((datum, groupIndex) => {
548
- const _barStartY = accYArr[groupIndex] // 前一次的累加高度
549
- let _barHeight = 0
550
- if (datum.visible) {
551
- _barHeight = datum.axisYFromZero * data.yRatio
552
- accYArr[groupIndex] = accYArr[groupIndex] + _barHeight // 累加高度
553
- }
554
- return <GraphicDatum>{
555
- ...datum,
556
- _barStartY,
557
- _barHeight
558
- }
559
- })
560
- })
561
- // return data.computedData.map(d => {
562
- // let accY = data.zeroY
563
- // return d.map(_d => {
564
- // const _barStartY = accY
565
- // const _barHeight = _d.axisYFromZero * data.yRatio
566
- // accY = accY + _barHeight
567
- // return <GraphicDatum>{
568
- // ..._d,
569
- // _barStartY,
570
- // _barHeight
571
- // }
572
- // })
573
- // })
574
- })
575
- )
576
-
577
- const noneStackedData$ = combineLatest({
578
- computedLayoutData: computedLayoutData$,
579
- // yRatio: yRatio$,
580
- zeroY: zeroY$
581
- }).pipe(
582
- takeUntil(destroy$),
583
- map(data => {
584
- return data.computedLayoutData.map((series, seriesIndex) => {
585
- return series.map((datum, groupIndex) => {
586
- return <GraphicDatum>{
587
- ...datum,
588
- _barStartY: data.zeroY,
589
- _barHeight: datum.axisYFromZero
590
- }
591
- })
592
- })
593
- })
594
- )
595
-
596
- const graphicData$ = isSeriesSeprate$.pipe(
597
- switchMap(isSeriesSeprate => {
598
- return iif(() => isSeriesSeprate, noneStackedData$, stackedData$)
599
- })
600
- )
601
-
602
- combineLatest({
603
- defsSelection: defsSelection$,
604
- gridAxesSize: gridAxesSize$,
605
- }).pipe(
606
- takeUntil(destroy$),
607
- switchMap(async d => d)
608
- ).subscribe(data => {
609
- const clipPathData = [{
610
- id: clipPathID,
611
- width: data.gridAxesSize.width,
612
- height: data.gridAxesSize.height
613
- }]
614
- renderClipPath({
615
- defsSelection: data.defsSelection,
616
- clipPathData
617
- })
618
- })
619
-
620
- const highlightTarget$ = fullChartParams$.pipe(
621
- takeUntil(destroy$),
622
- map(d => d.highlightTarget),
623
- distinctUntilChanged()
624
- )
625
-
626
- const barSelection$ = combineLatest({
627
- graphicGSelection: graphicGSelection$,
628
- graphicData: graphicData$,
629
- zeroY: zeroY$,
630
- groupLabels: groupLabels$,
631
- // barScale: barScale$,
632
- params: fullParams$,
633
- chartParams: fullChartParams$,
634
- highlightTarget: highlightTarget$,
635
- barWidth: barWidth$,
636
- transformedBarRadius: transformedBarRadius$,
637
- delayGroup: delayGroup$,
638
- transitionItem: transitionItem$,
639
- isSeriesSeprate: isSeriesSeprate$
640
- }).pipe(
641
- takeUntil(destroy$),
642
- switchMap(async (d) => d),
643
- map(data => {
644
- return renderRectBars({
645
- graphicGSelection: data.graphicGSelection,
646
- rectClassName,
647
- barData: data.graphicData,
648
- zeroY: data.zeroY,
649
- groupLabels: data.groupLabels,
650
- // barScale: data.barScale,
651
- params: data.params,
652
- chartParams: data.chartParams,
653
- barWidth: data.barWidth,
654
- transformedBarRadius: data.transformedBarRadius,
655
- delayGroup: data.delayGroup,
656
- transitionItem: data.transitionItem,
657
- isSeriesSeprate: data.isSeriesSeprate
658
- })
659
- })
660
- )
661
-
662
- combineLatest({
663
- barSelection: barSelection$,
664
- computedData: computedData$,
665
- highlightTarget: highlightTarget$,
666
- SeriesDataMap: SeriesDataMap$,
667
- GroupDataMap: GroupDataMap$,
668
- }).subscribe(data => {
669
-
670
- data.barSelection!
671
- .on('mouseover', (event, datum) => {
672
- event.stopPropagation()
673
-
674
- event$.next({
675
- type: 'grid',
676
- eventName: 'mouseover',
677
- pluginName,
678
- highlightTarget: data.highlightTarget,
679
- datum,
680
- gridIndex: datum.gridIndex,
681
- series: data.SeriesDataMap.get(datum.seriesLabel)!,
682
- seriesIndex: datum.seriesIndex,
683
- seriesLabel: datum.seriesLabel,
684
- groups: data.GroupDataMap.get(datum.groupLabel)!,
685
- groupIndex: datum.groupIndex,
686
- groupLabel: datum.groupLabel,
687
- event,
688
- data: data.computedData
689
- })
690
- })
691
- .on('mousemove', (event, datum) => {
692
- event.stopPropagation()
693
-
694
- event$.next({
695
- type: 'grid',
696
- eventName: 'mousemove',
697
- pluginName,
698
- highlightTarget: data.highlightTarget,
699
- datum,
700
- gridIndex: datum.gridIndex,
701
- series: data.SeriesDataMap.get(datum.seriesLabel)!,
702
- seriesIndex: datum.seriesIndex,
703
- seriesLabel: datum.seriesLabel,
704
- groups: data.GroupDataMap.get(datum.groupLabel)!,
705
- groupIndex: datum.groupIndex,
706
- groupLabel: datum.groupLabel,
707
- event,
708
- data: data.computedData
709
- })
710
- })
711
- .on('mouseout', (event, datum) => {
712
- event.stopPropagation()
713
-
714
- event$.next({
715
- type: 'grid',
716
- eventName: 'mouseout',
717
- pluginName,
718
- highlightTarget: data.highlightTarget,
719
- datum,
720
- gridIndex: datum.gridIndex,
721
- series: data.SeriesDataMap.get(datum.seriesLabel)!,
722
- seriesIndex: datum.seriesIndex,
723
- seriesLabel: datum.seriesLabel,
724
- groups: data.GroupDataMap.get(datum.groupLabel)!,
725
- groupIndex: datum.groupIndex,
726
- groupLabel: datum.groupLabel,
727
- event,
728
- data: data.computedData
729
- })
730
- })
731
- .on('click', (event, datum) => {
732
- event.stopPropagation()
733
-
734
- event$.next({
735
- type: 'grid',
736
- eventName: 'click',
737
- pluginName,
738
- highlightTarget: data.highlightTarget,
739
- datum,
740
- gridIndex: datum.gridIndex,
741
- series: data.SeriesDataMap.get(datum.seriesLabel)!,
742
- seriesIndex: datum.seriesIndex,
743
- seriesLabel: datum.seriesLabel,
744
- groups: data.GroupDataMap.get(datum.groupLabel)!,
745
- groupIndex: datum.groupIndex,
746
- groupLabel: datum.groupLabel,
747
- event,
748
- data: data.computedData
749
- })
750
- })
751
-
752
- })
753
-
754
- // const datumList$ = computedData$.pipe(
755
- // takeUntil(destroy$),
756
- // map(d => d.flat())
757
- // )
758
- // const highlight$ = highlightObservable({ datumList$, chartParams$, event$: store.event$ })
759
-
760
- combineLatest({
761
- barSelection: barSelection$,
762
- highlight: gridHighlight$.pipe(
763
- map(data => data.map(d => d.id))
764
- ),
765
- fullChartParams: fullChartParams$
766
- }).pipe(
767
- takeUntil(destroy$),
768
- switchMap(async d => d)
769
- ).subscribe(data => {
770
- highlight({
771
- selection: data.barSelection,
772
- ids: data.highlight,
773
- fullChartParams: data.fullChartParams
774
- })
775
- })
776
-
777
- return () => {
778
- destroy$.next(undefined)
779
- }
1
+ import * as d3 from 'd3'
2
+ import {
3
+ iif,
4
+ combineLatest,
5
+ map,
6
+ switchMap,
7
+ takeUntil,
8
+ distinctUntilChanged,
9
+ Observable,
10
+ Subject } from 'rxjs'
11
+ import type { BasePluginFn } from './types'
12
+ import type {
13
+ ComputedDatumGrid,
14
+ ComputedDataGrid,
15
+ ComputedLayoutDatumGrid,
16
+ ComputedLayoutDataGrid,
17
+ DataFormatterGrid,
18
+ EventGrid,
19
+ ChartParams,
20
+ GridContainerPosition,
21
+ Layout,
22
+ TransformData } from '../../lib/core-types'
23
+ import type { BaseBarStackParams } from '../../lib/plugins-basic-types'
24
+ import { getD3TransitionEase } from '../utils/d3Utils'
25
+ import { getClassName, getUniID } from '../utils/orbchartsUtils'
26
+ import { gridSelectionsObservable } from '../grid/gridObservables'
27
+ import { shareReplay } from 'rxjs/operators'
28
+
29
+ // export interface BaseBarStackParams {
30
+ // barWidth: number
31
+ // barGroupPadding: number
32
+ // barRadius: number | boolean
33
+ // }
34
+
35
+ interface BaseBarStackContext {
36
+ selection: d3.Selection<any, unknown, any, unknown>
37
+ computedData$: Observable<ComputedDataGrid>
38
+ computedLayoutData$: Observable<ComputedLayoutDataGrid>
39
+ visibleComputedData$: Observable<ComputedDatumGrid[][]>
40
+ visibleComputedLayoutData$: Observable<ComputedLayoutDataGrid>
41
+ seriesLabels$: Observable<string[]>
42
+ SeriesDataMap$: Observable<Map<string, ComputedDatumGrid[]>>
43
+ GroupDataMap$: Observable<Map<string, ComputedDatumGrid[]>>
44
+ fullParams$: Observable<BaseBarStackParams>
45
+ fullDataFormatter$: Observable<DataFormatterGrid>
46
+ fullChartParams$: Observable<ChartParams>
47
+ gridAxesTransform$: Observable<TransformData>
48
+ gridGraphicTransform$: Observable<TransformData>
49
+ gridGraphicReverseScale$: Observable<[number, number][]>
50
+ gridAxesSize$: Observable<{
51
+ width: number;
52
+ height: number;
53
+ }>
54
+ gridHighlight$: Observable<ComputedDatumGrid[]>
55
+ gridContainerPosition$: Observable<GridContainerPosition[]>
56
+ isSeriesSeprate$: Observable<boolean>
57
+ event$: Subject<EventGrid>
58
+ }
59
+
60
+
61
+ interface GraphicDatum extends ComputedLayoutDatumGrid {
62
+ _barStartY: number // bar的起點y座標
63
+ _barHeight: number // bar的高度
64
+ }
65
+
66
+ interface RenderBarParams {
67
+ graphicGSelection: d3.Selection<SVGGElement, unknown, any, any>
68
+ rectClassName: string
69
+ barData: GraphicDatum[][]
70
+ zeroY: number
71
+ groupLabels: string[]
72
+ // barScale: d3.ScalePoint<string>
73
+ params: BaseBarStackParams
74
+ chartParams: ChartParams
75
+ barWidth: number
76
+ transformedBarRadius: [number, number][]
77
+ delayGroup: number
78
+ transitionItem: number
79
+ isSeriesSeprate: boolean
80
+ }
81
+
82
+ type ClipPathDatum = {
83
+ id: string;
84
+ // x: number;
85
+ // y: number;
86
+ width: number;
87
+ height: number;
88
+ }
89
+
90
+ // const pluginName = 'BarStack'
91
+ // const rectClassName = getClassName(pluginName, 'rect')
92
+ // group的delay在動畫中的佔比(剩餘部份的時間為圖形本身的動畫時間,因為delay時間和最後一個group的動畫時間加總為1)
93
+ const groupDelayProportionOfDuration = 0.3
94
+
95
+ function calcBarWidth ({ axisWidth, groupAmount, barGroupPadding = 0 }: {
96
+ axisWidth: number
97
+ groupAmount: number
98
+ barGroupPadding: number
99
+ }) {
100
+ const eachGroupWidth = axisWidth / (groupAmount - 1)
101
+ const width = eachGroupWidth - barGroupPadding
102
+ return width > 1 ? width : 1
103
+
104
+ }
105
+
106
+ // function makeBarScale (barWidth: number, seriesLabels: string[], params: BarStackParams) {
107
+ // const barHalfWidth = barWidth! / 2
108
+ // const barGroupWidth = barWidth * seriesLabels.length + params.barPadding! * seriesLabels.length
109
+ // return d3.scalePoint()
110
+ // .domain(seriesLabels)
111
+ // .range([-barGroupWidth / 2 + barHalfWidth, barGroupWidth / 2 - barHalfWidth])
112
+ // }
113
+
114
+ function calcDelayGroup (barGroupAmount: number, totalDuration: number) {
115
+ if (barGroupAmount <= 1) {
116
+ // 一筆內計算會出錯所以不算
117
+ return 0
118
+ }
119
+ return totalDuration / (barGroupAmount - 1) * groupDelayProportionOfDuration // 依group數量計算
120
+ }
121
+
122
+ function calctransitionItem (barGroupAmount: number, totalDuration: number) {
123
+ if (barGroupAmount <= 1) {
124
+ // 一筆內不會有delay
125
+ return totalDuration
126
+ }
127
+ return totalDuration * (1 - groupDelayProportionOfDuration) // delay後剩餘的時間
128
+ }
129
+
130
+ function renderRectBars ({ graphicGSelection, rectClassName, barData, zeroY, groupLabels, params, chartParams, barWidth, transformedBarRadius, delayGroup, transitionItem, isSeriesSeprate }: RenderBarParams) {
131
+
132
+ const barHalfWidth = barWidth! / 2
133
+
134
+ graphicGSelection
135
+ .each((seriesData, seriesIndex, g) => {
136
+ d3.select(g[seriesIndex])
137
+ .selectAll<SVGGElement, ComputedDatumGrid>(`rect.${rectClassName}`)
138
+ .data(barData[seriesIndex] ?? [], d => d.id)
139
+ .join(
140
+ enter => {
141
+ // console.log('enter')
142
+ return enter
143
+ .append('rect')
144
+ .classed(rectClassName, true)
145
+ .attr('cursor', 'pointer')
146
+ .attr('height', d => 0)
147
+ },
148
+ update => update,
149
+ exit => exit.remove()
150
+ )
151
+ .attr('transform', (d, i) => `translate(${(d ? d.axisX : 0) - barHalfWidth}, ${0})`)
152
+ .attr('fill', d => d.color)
153
+ .attr('y', d => zeroY)
154
+ .attr('x', d =>0)
155
+ .attr('width', barWidth!)
156
+ .attr('rx', transformedBarRadius[seriesIndex][0] ?? 1)
157
+ .attr('ry', transformedBarRadius[seriesIndex][1] ?? 1)
158
+ .transition()
159
+ .duration(transitionItem)
160
+ .ease(getD3TransitionEase(chartParams.transitionEase))
161
+ .delay((d, i) => d.groupIndex * delayGroup)
162
+ .attr('y', d => d._barStartY)
163
+ .attr('height', d => Math.abs(d._barHeight))
164
+ })
165
+
166
+ // const barGroup = graphicGSelection
167
+ // .selectAll<SVGGElement, ComputedDatumGrid[]>(`g.${gClassName}`)
168
+ // .data(data, (d, i) => groupLabels[i])
169
+ // .join(
170
+ // enter => {
171
+ // return enter
172
+ // .append('g')
173
+ // .classed(gClassName, true)
174
+ // .attr('cursor', 'pointer')
175
+ // },
176
+ // update => update,
177
+ // exit => exit.remove()
178
+ // )
179
+ // .attr('transform', (d, i) => `translate(${d[0] ? d[0].axisX : 0}, ${0})`)
180
+ // .each((d, i, g) => {
181
+ // const bars = d3.select(g[i])
182
+ // .selectAll<SVGGElement, ComputedDatumGrid>('g')
183
+ // .data(d, _d => _d.id)
184
+ // .join(
185
+ // enter => {
186
+ // return enter
187
+ // .append('g')
188
+ // .classed(gContentClassName, true)
189
+ // },
190
+ // update => update,
191
+ // exit => exit.remove()
192
+ // )
193
+ // .each((_d, _i, _g) => {
194
+ // const rect = d3.select(_g[_i])
195
+ // .selectAll<SVGRectElement, ComputedDatumGrid>('rect')
196
+ // .data([_d], _d => _d.id)
197
+ // .join(
198
+ // enter => {
199
+ // return enter
200
+ // .append('rect')
201
+ // .attr('y', d => zeroY)
202
+ // .attr('height', d => 0)
203
+ // },
204
+ // update => update,
205
+ // exit => exit.remove()
206
+ // )
207
+ // .attr('rx', transformedBarRadius[0])
208
+ // .attr('ry', transformedBarRadius[1])
209
+ // .attr('fill', d => d.color)
210
+ // .attr('transform', `translate(${-barHalfWidth}, 0)`)
211
+ // .attr('x', d => 0)
212
+ // .attr('width', barWidth!)
213
+ // .transition()
214
+ // .duration(transitionItem)
215
+ // .ease(getD3TransitionEase(chartParams.transitionEase))
216
+ // .delay((d, i) => d.groupIndex * delayGroup)
217
+ // .attr('y', d => d._barStartY)
218
+ // .attr('height', d => Math.abs(d._barHeight))
219
+ // })
220
+
221
+ // })
222
+
223
+ const graphicBarSelection: d3.Selection<SVGRectElement, ComputedDatumGrid, SVGGElement, unknown> = graphicGSelection.selectAll(`rect.${rectClassName}`)
224
+
225
+
226
+ return graphicBarSelection
227
+ }
228
+
229
+ function renderClipPath ({ defsSelection, clipPathData }: {
230
+ defsSelection: d3.Selection<SVGDefsElement, any, any, any>
231
+ clipPathData: ClipPathDatum[]
232
+ }) {
233
+ const clipPath = defsSelection
234
+ .selectAll<SVGClipPathElement, Layout>('clipPath')
235
+ .data(clipPathData)
236
+ .join(
237
+ enter => {
238
+ return enter
239
+ .append('clipPath')
240
+ },
241
+ update => update,
242
+ exit => exit.remove()
243
+ )
244
+ .attr('id', d => d.id)
245
+ .each((d, i, g) => {
246
+ const rect = d3.select(g[i])
247
+ .selectAll<SVGRectElement, typeof d>('rect')
248
+ .data([d])
249
+ .join(
250
+ enter => {
251
+ return enter
252
+ .append('rect')
253
+ },
254
+ update => update,
255
+ exit => exit.remove()
256
+ )
257
+ .attr('x', 0)
258
+ .attr('y', 0)
259
+ .attr('width', _d => _d.width)
260
+ .attr('height', _d => _d.height)
261
+ })
262
+ }
263
+
264
+ function highlight ({ selection, ids, fullChartParams }: {
265
+ selection: d3.Selection<any, ComputedDatumGrid, any, any>
266
+ ids: string[]
267
+ fullChartParams: ChartParams
268
+ }) {
269
+ selection.interrupt('highlight')
270
+
271
+ if (!ids.length) {
272
+ // remove highlight
273
+ selection
274
+ .transition('highlight')
275
+ .duration(200)
276
+ .style('opacity', 1)
277
+ return
278
+ }
279
+
280
+ selection
281
+ .each((d, i, n) => {
282
+ if (ids.includes(d.id)) {
283
+ d3.select(n[i])
284
+ .style('opacity', 1)
285
+ } else {
286
+ d3.select(n[i])
287
+ .style('opacity', fullChartParams.styles.unhighlightedOpacity)
288
+ }
289
+ })
290
+ }
291
+
292
+
293
+ export const createBaseBarStack: BasePluginFn<BaseBarStackContext> = (pluginName: string, {
294
+ selection,
295
+ computedData$,
296
+ computedLayoutData$,
297
+ visibleComputedData$,
298
+ visibleComputedLayoutData$,
299
+ seriesLabels$,
300
+ SeriesDataMap$,
301
+ GroupDataMap$,
302
+ fullParams$,
303
+ fullDataFormatter$,
304
+ fullChartParams$,
305
+ gridAxesTransform$,
306
+ gridGraphicTransform$,
307
+ gridGraphicReverseScale$,
308
+ gridAxesSize$,
309
+ gridHighlight$,
310
+ gridContainerPosition$,
311
+ isSeriesSeprate$,
312
+ event$
313
+ }) => {
314
+
315
+ const destroy$ = new Subject()
316
+
317
+ const clipPathID = getUniID(pluginName, 'clipPath-box')
318
+ const rectClassName = getClassName(pluginName, 'rect')
319
+
320
+ const {
321
+ seriesSelection$,
322
+ axesSelection$,
323
+ defsSelection$,
324
+ graphicGSelection$
325
+ } = gridSelectionsObservable({
326
+ selection,
327
+ pluginName,
328
+ clipPathID,
329
+ seriesLabels$,
330
+ gridContainerPosition$,
331
+ gridAxesTransform$,
332
+ gridGraphicTransform$
333
+ })
334
+
335
+
336
+ const zeroY$ = visibleComputedLayoutData$.pipe(
337
+ takeUntil(destroy$),
338
+ map(d => d[0] && d[0][0]
339
+ ? d[0][0].axisY - d[0][0].axisYFromZero
340
+ : 0),
341
+ distinctUntilChanged()
342
+ )
343
+
344
+ const barWidth$ = combineLatest({
345
+ computedData: computedData$,
346
+ // visibleComputedData: visibleComputedData$,
347
+ params: fullParams$,
348
+ axisSize: gridAxesSize$,
349
+ isSeriesSeprate: isSeriesSeprate$
350
+ }).pipe(
351
+ takeUntil(destroy$),
352
+ switchMap(async d => d),
353
+ map(data => {
354
+ const barWidth = data.params.barWidth
355
+ ? data.params.barWidth
356
+ : calcBarWidth({
357
+ axisWidth: data.axisSize.width,
358
+ groupAmount: data.computedData[0] ? data.computedData[0].length : 0,
359
+ barGroupPadding: data.params.barGroupPadding
360
+ })
361
+ return barWidth
362
+ }),
363
+ distinctUntilChanged()
364
+ )
365
+
366
+ // 圓角的值 [rx, ry]
367
+ const transformedBarRadius$: Observable<[number, number][]> = combineLatest({
368
+ computedData: computedData$,
369
+ // gridGraphicTransform: gridGraphicTransform$,
370
+ barWidth: barWidth$,
371
+ params: fullParams$,
372
+ gridGraphicReverseScale: gridGraphicReverseScale$
373
+ }).pipe(
374
+ takeUntil(destroy$),
375
+ switchMap(async data => data),
376
+ map(data => {
377
+ const barHalfWidth = data.barWidth! / 2
378
+ const radius = data.params.barRadius === true ? barHalfWidth
379
+ : data.params.barRadius === false ? 0
380
+ : typeof data.params.barRadius == 'number' ? data.params.barRadius
381
+ : 0
382
+
383
+ return data.computedData.map((series, seriesIndex) => {
384
+ const gridGraphicReverseScale = data.gridGraphicReverseScale[seriesIndex] ?? data.gridGraphicReverseScale[0]
385
+
386
+ const transformedRx = radius * gridGraphicReverseScale[0]
387
+ const transformedRy = radius * gridGraphicReverseScale[1]
388
+
389
+ return [transformedRx, transformedRy]
390
+ })
391
+ })
392
+ )
393
+
394
+ // const seriesLabels$ = visibleComputedData$.pipe(
395
+ // takeUntil(destroy$),
396
+ // map(data => {
397
+ // const SeriesLabelSet: Set<string> = new Set()
398
+ // data.forEach(d => {
399
+ // d.forEach(_d => {
400
+ // SeriesLabelSet.add(_d.seriesLabel)
401
+ // })
402
+ // })
403
+ // return Array.from(SeriesLabelSet)
404
+ // })
405
+ // )
406
+
407
+ const groupLabels$ = visibleComputedData$.pipe(
408
+ takeUntil(destroy$),
409
+ map(data => {
410
+ const GroupLabelSet: Set<string> = new Set()
411
+ data.forEach(d => {
412
+ d.forEach(_d => {
413
+ GroupLabelSet.add(_d.groupLabel)
414
+ })
415
+ })
416
+ return Array.from(GroupLabelSet)
417
+ }),
418
+ shareReplay(1)
419
+ )
420
+
421
+ const transitionDuration$ = fullChartParams$.pipe(
422
+ takeUntil(destroy$),
423
+ map(d => d.transitionDuration),
424
+ distinctUntilChanged()
425
+ )
426
+
427
+ const delayGroup$ = new Observable<number>(subscriber => {
428
+ combineLatest({
429
+ groupLabels: groupLabels$,
430
+ transitionDuration: transitionDuration$,
431
+ }).pipe(
432
+ switchMap(async d => d)
433
+ ).subscribe(data => {
434
+ const delay = calcDelayGroup(data.groupLabels.length, data.transitionDuration)
435
+ subscriber.next(delay)
436
+ })
437
+ }).pipe(
438
+ takeUntil(destroy$),
439
+ distinctUntilChanged()
440
+ )
441
+
442
+ const transitionItem$ = new Observable<number>(subscriber => {
443
+ combineLatest({
444
+ groupLabels: groupLabels$,
445
+ transitionDuration: transitionDuration$
446
+ }).pipe(
447
+ switchMap(async d => d)
448
+ ).subscribe(data => {
449
+ const transition = calctransitionItem(data.groupLabels.length, data.transitionDuration)
450
+ subscriber.next(transition)
451
+ })
452
+ }).pipe(
453
+ takeUntil(destroy$),
454
+ distinctUntilChanged()
455
+ )
456
+
457
+ // const transposedVisibleData$: Observable<ComputedDataGrid> = visibleComputedData$.pipe(
458
+ // takeUntil(destroy$),
459
+ // map(data => {
460
+ // console.log('visibleComputedData', data)
461
+ // // 取得原始陣列的維度
462
+ // const rows = data.length;
463
+ // const cols = data.reduce((prev, current) => {
464
+ // return Math.max(prev, current.length)
465
+ // }, 0)
466
+
467
+ // // 初始化轉換後的陣列
468
+ // const transposedArray = new Array(cols).fill(null).map(() => new Array(rows).fill(null))
469
+
470
+ // // 遍歷原始陣列,進行轉換
471
+ // for (let i = 0; i < rows; i++) {
472
+ // for (let j = 0; j < cols; j++) {
473
+ // transposedArray[j][i] = data[i][j]
474
+ // }
475
+ // }
476
+ // return transposedArray
477
+ // })
478
+ // )
479
+
480
+ const groupScaleDomain$ = combineLatest({
481
+ computedData: computedData$,
482
+ dataFormatter: fullDataFormatter$,
483
+ }).pipe(
484
+ takeUntil(destroy$),
485
+ switchMap(async d => d),
486
+ map(data => {
487
+ const groupMin = 0
488
+ const groupMax = data.computedData[0] ? data.computedData[0].length - 1 : 0
489
+ // const groupScaleDomainMin = data.dataFormatter.grid.groupAxis.scaleDomain[0] === 'auto'
490
+ // ? groupMin // - data.dataFormatter.grid.groupAxis.scalePadding
491
+ // : data.dataFormatter.grid.groupAxis.scaleDomain[0] as number // - data.dataFormatter.grid.groupAxis.scalePadding
492
+ const groupScaleDomainMin = data.dataFormatter.grid.groupAxis.scaleDomain[0]
493
+ const groupScaleDomainMax = data.dataFormatter.grid.groupAxis.scaleDomain[1] === 'max'
494
+ ? groupMax // + data.dataFormatter.grid.groupAxis.scalePadding
495
+ : data.dataFormatter.grid.groupAxis.scaleDomain[1] as number // + data.dataFormatter.grid.groupAxis.scalePadding
496
+
497
+ return [groupScaleDomainMin, groupScaleDomainMax]
498
+ })
499
+ )
500
+
501
+ // 堆疊後的高度對應圖軸的比例(最大值/最大值所在group的總和)
502
+ const yRatio$ = combineLatest({
503
+ visibleComputedLayoutData: visibleComputedLayoutData$,
504
+ groupScaleDomain: groupScaleDomain$
505
+ }).pipe(
506
+ takeUntil(destroy$),
507
+ switchMap(async d => d),
508
+ map(data => {
509
+ const groupScaleDomainMin = data.groupScaleDomain[0]
510
+ const groupScaleDomainMax = data.groupScaleDomain[1]
511
+ // 只選取group篩選範圍內的資料
512
+ const filteredData = data.visibleComputedLayoutData
513
+ .map(d => {
514
+ return d.filter((_d, i) => {
515
+ return _d.groupIndex >= groupScaleDomainMin && _d.groupIndex <= groupScaleDomainMax
516
+ })
517
+ })
518
+
519
+ const filteredDataList = filteredData.flat()
520
+ if (filteredDataList.length <= 1) {
521
+ return 1
522
+ }
523
+
524
+ const maxValueDatum = filteredDataList.reduce((max, current) => {
525
+ return current.value > max.value ? current : max;
526
+ }, filteredDataList[0])
527
+ const maxValueGroupIndex = maxValueDatum.groupIndex
528
+ const maxValueGroupSum = filteredDataList
529
+ .filter(d => d.groupIndex === maxValueGroupIndex)
530
+ .reduce((sum, current) => {
531
+ return sum + current.value
532
+ }, 0)
533
+ return maxValueDatum.value / maxValueGroupSum
534
+ })
535
+ )
536
+
537
+ const stackedData$ = combineLatest({
538
+ computedLayoutData: computedLayoutData$,
539
+ yRatio: yRatio$,
540
+ zeroY: zeroY$
541
+ }).pipe(
542
+ takeUntil(destroy$),
543
+ map(data => {
544
+ let accYArr: number[] = data.computedLayoutData[0]
545
+ ? data.computedLayoutData[0].map(() => data.zeroY)
546
+ : []
547
+ return data.computedLayoutData.map((series, seriesIndex) => {
548
+ return series.map((datum, groupIndex) => {
549
+ const _barStartY = accYArr[groupIndex] // 前一次的累加高度
550
+ let _barHeight = 0
551
+ if (datum.visible) {
552
+ _barHeight = datum.axisYFromZero * data.yRatio
553
+ accYArr[groupIndex] = accYArr[groupIndex] + _barHeight // 累加高度
554
+ }
555
+ return <GraphicDatum>{
556
+ ...datum,
557
+ _barStartY,
558
+ _barHeight
559
+ }
560
+ })
561
+ })
562
+ // return data.computedData.map(d => {
563
+ // let accY = data.zeroY
564
+ // return d.map(_d => {
565
+ // const _barStartY = accY
566
+ // const _barHeight = _d.axisYFromZero * data.yRatio
567
+ // accY = accY + _barHeight
568
+ // return <GraphicDatum>{
569
+ // ..._d,
570
+ // _barStartY,
571
+ // _barHeight
572
+ // }
573
+ // })
574
+ // })
575
+ })
576
+ )
577
+
578
+ const noneStackedData$ = combineLatest({
579
+ computedLayoutData: computedLayoutData$,
580
+ // yRatio: yRatio$,
581
+ zeroY: zeroY$
582
+ }).pipe(
583
+ takeUntil(destroy$),
584
+ map(data => {
585
+ return data.computedLayoutData.map((series, seriesIndex) => {
586
+ return series.map((datum, groupIndex) => {
587
+ return <GraphicDatum>{
588
+ ...datum,
589
+ _barStartY: data.zeroY,
590
+ _barHeight: datum.axisYFromZero
591
+ }
592
+ })
593
+ })
594
+ })
595
+ )
596
+
597
+ const graphicData$ = isSeriesSeprate$.pipe(
598
+ switchMap(isSeriesSeprate => {
599
+ return iif(() => isSeriesSeprate, noneStackedData$, stackedData$)
600
+ })
601
+ )
602
+
603
+ combineLatest({
604
+ defsSelection: defsSelection$,
605
+ gridAxesSize: gridAxesSize$,
606
+ }).pipe(
607
+ takeUntil(destroy$),
608
+ switchMap(async d => d)
609
+ ).subscribe(data => {
610
+ const clipPathData = [{
611
+ id: clipPathID,
612
+ width: data.gridAxesSize.width,
613
+ height: data.gridAxesSize.height
614
+ }]
615
+ renderClipPath({
616
+ defsSelection: data.defsSelection,
617
+ clipPathData
618
+ })
619
+ })
620
+
621
+ const highlightTarget$ = fullChartParams$.pipe(
622
+ takeUntil(destroy$),
623
+ map(d => d.highlightTarget),
624
+ distinctUntilChanged()
625
+ )
626
+
627
+ const barSelection$ = combineLatest({
628
+ graphicGSelection: graphicGSelection$,
629
+ graphicData: graphicData$,
630
+ zeroY: zeroY$,
631
+ groupLabels: groupLabels$,
632
+ // barScale: barScale$,
633
+ params: fullParams$,
634
+ chartParams: fullChartParams$,
635
+ highlightTarget: highlightTarget$,
636
+ barWidth: barWidth$,
637
+ transformedBarRadius: transformedBarRadius$,
638
+ delayGroup: delayGroup$,
639
+ transitionItem: transitionItem$,
640
+ isSeriesSeprate: isSeriesSeprate$
641
+ }).pipe(
642
+ takeUntil(destroy$),
643
+ switchMap(async (d) => d),
644
+ map(data => {
645
+ return renderRectBars({
646
+ graphicGSelection: data.graphicGSelection,
647
+ rectClassName,
648
+ barData: data.graphicData,
649
+ zeroY: data.zeroY,
650
+ groupLabels: data.groupLabels,
651
+ // barScale: data.barScale,
652
+ params: data.params,
653
+ chartParams: data.chartParams,
654
+ barWidth: data.barWidth,
655
+ transformedBarRadius: data.transformedBarRadius,
656
+ delayGroup: data.delayGroup,
657
+ transitionItem: data.transitionItem,
658
+ isSeriesSeprate: data.isSeriesSeprate
659
+ })
660
+ })
661
+ )
662
+
663
+ combineLatest({
664
+ barSelection: barSelection$,
665
+ computedData: computedData$,
666
+ highlightTarget: highlightTarget$,
667
+ SeriesDataMap: SeriesDataMap$,
668
+ GroupDataMap: GroupDataMap$,
669
+ }).subscribe(data => {
670
+
671
+ data.barSelection!
672
+ .on('mouseover', (event, datum) => {
673
+ event.stopPropagation()
674
+
675
+ event$.next({
676
+ type: 'grid',
677
+ eventName: 'mouseover',
678
+ pluginName,
679
+ highlightTarget: data.highlightTarget,
680
+ datum,
681
+ gridIndex: datum.gridIndex,
682
+ series: data.SeriesDataMap.get(datum.seriesLabel)!,
683
+ seriesIndex: datum.seriesIndex,
684
+ seriesLabel: datum.seriesLabel,
685
+ groups: data.GroupDataMap.get(datum.groupLabel)!,
686
+ groupIndex: datum.groupIndex,
687
+ groupLabel: datum.groupLabel,
688
+ event,
689
+ data: data.computedData
690
+ })
691
+ })
692
+ .on('mousemove', (event, datum) => {
693
+ event.stopPropagation()
694
+
695
+ event$.next({
696
+ type: 'grid',
697
+ eventName: 'mousemove',
698
+ pluginName,
699
+ highlightTarget: data.highlightTarget,
700
+ datum,
701
+ gridIndex: datum.gridIndex,
702
+ series: data.SeriesDataMap.get(datum.seriesLabel)!,
703
+ seriesIndex: datum.seriesIndex,
704
+ seriesLabel: datum.seriesLabel,
705
+ groups: data.GroupDataMap.get(datum.groupLabel)!,
706
+ groupIndex: datum.groupIndex,
707
+ groupLabel: datum.groupLabel,
708
+ event,
709
+ data: data.computedData
710
+ })
711
+ })
712
+ .on('mouseout', (event, datum) => {
713
+ event.stopPropagation()
714
+
715
+ event$.next({
716
+ type: 'grid',
717
+ eventName: 'mouseout',
718
+ pluginName,
719
+ highlightTarget: data.highlightTarget,
720
+ datum,
721
+ gridIndex: datum.gridIndex,
722
+ series: data.SeriesDataMap.get(datum.seriesLabel)!,
723
+ seriesIndex: datum.seriesIndex,
724
+ seriesLabel: datum.seriesLabel,
725
+ groups: data.GroupDataMap.get(datum.groupLabel)!,
726
+ groupIndex: datum.groupIndex,
727
+ groupLabel: datum.groupLabel,
728
+ event,
729
+ data: data.computedData
730
+ })
731
+ })
732
+ .on('click', (event, datum) => {
733
+ event.stopPropagation()
734
+
735
+ event$.next({
736
+ type: 'grid',
737
+ eventName: 'click',
738
+ pluginName,
739
+ highlightTarget: data.highlightTarget,
740
+ datum,
741
+ gridIndex: datum.gridIndex,
742
+ series: data.SeriesDataMap.get(datum.seriesLabel)!,
743
+ seriesIndex: datum.seriesIndex,
744
+ seriesLabel: datum.seriesLabel,
745
+ groups: data.GroupDataMap.get(datum.groupLabel)!,
746
+ groupIndex: datum.groupIndex,
747
+ groupLabel: datum.groupLabel,
748
+ event,
749
+ data: data.computedData
750
+ })
751
+ })
752
+
753
+ })
754
+
755
+ // const datumList$ = computedData$.pipe(
756
+ // takeUntil(destroy$),
757
+ // map(d => d.flat())
758
+ // )
759
+ // const highlight$ = highlightObservable({ datumList$, chartParams$, event$: store.event$ })
760
+
761
+ combineLatest({
762
+ barSelection: barSelection$,
763
+ highlight: gridHighlight$.pipe(
764
+ map(data => data.map(d => d.id))
765
+ ),
766
+ fullChartParams: fullChartParams$
767
+ }).pipe(
768
+ takeUntil(destroy$),
769
+ switchMap(async d => d)
770
+ ).subscribe(data => {
771
+ highlight({
772
+ selection: data.barSelection,
773
+ ids: data.highlight,
774
+ fullChartParams: data.fullChartParams
775
+ })
776
+ })
777
+
778
+ return () => {
779
+ destroy$.next(undefined)
780
+ }
780
781
  }