@orbcharts/plugins-basic 3.0.8 → 3.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (120) hide show
  1. package/LICENSE +200 -200
  2. package/dist/orbcharts-plugins-basic.es.js +6182 -6004
  3. package/dist/orbcharts-plugins-basic.umd.js +44 -44
  4. package/dist/src/series/defaults.d.ts +2 -1
  5. package/dist/src/series/index.d.ts +1 -0
  6. package/dist/src/series/plugins/Indicator.d.ts +3 -0
  7. package/lib/core-types.ts +7 -7
  8. package/lib/core.ts +6 -6
  9. package/lib/gridObservables.ts +6 -6
  10. package/lib/plugins-basic-types.ts +6 -6
  11. package/package.json +48 -48
  12. package/src/base/BaseBars.ts +765 -765
  13. package/src/base/BaseBarsTriangle.ts +676 -676
  14. package/src/base/BaseDots.ts +464 -464
  15. package/src/base/BaseGroupAxis.ts +691 -691
  16. package/src/base/BaseLegend.ts +684 -684
  17. package/src/base/BaseLineAreas.ts +629 -629
  18. package/src/base/BaseLines.ts +706 -706
  19. package/src/base/BaseOrdinalBubbles.ts +729 -729
  20. package/src/base/BaseRacingBars.ts +582 -582
  21. package/src/base/BaseRacingLabels.ts +404 -404
  22. package/src/base/BaseRacingValueLabels.ts +403 -403
  23. package/src/base/BaseStackedBars.ts +793 -793
  24. package/src/base/BaseTooltip.ts +408 -408
  25. package/src/base/BaseValueAxis.ts +600 -600
  26. package/src/base/BaseXAxis.ts +427 -427
  27. package/src/base/BaseXZoom.ts +241 -241
  28. package/src/base/BaseYAxis.ts +389 -389
  29. package/src/base/types.ts +2 -2
  30. package/src/const.ts +30 -30
  31. package/src/grid/defaults.ts +213 -213
  32. package/src/grid/gridObservables.ts +635 -635
  33. package/src/grid/index.ts +16 -16
  34. package/src/grid/plugins/Bars.ts +69 -69
  35. package/src/grid/plugins/BarsPN.ts +66 -66
  36. package/src/grid/plugins/BarsTriangle.ts +73 -73
  37. package/src/grid/plugins/Dots.ts +68 -68
  38. package/src/grid/plugins/GridLegend.ts +107 -107
  39. package/src/grid/plugins/GridTooltip.ts +66 -66
  40. package/src/grid/plugins/GroupAux.ts +1095 -1095
  41. package/src/grid/plugins/GroupAxis.ts +73 -73
  42. package/src/grid/plugins/GroupZoom.ts +218 -218
  43. package/src/grid/plugins/LineAreas.ts +65 -65
  44. package/src/grid/plugins/Lines.ts +59 -59
  45. package/src/grid/plugins/StackedBars.ts +66 -66
  46. package/src/grid/plugins/StackedValueAxis.ts +97 -97
  47. package/src/grid/plugins/ValueAxis.ts +94 -94
  48. package/src/index.ts +6 -6
  49. package/src/multiGrid/defaults.ts +244 -244
  50. package/src/multiGrid/index.ts +14 -14
  51. package/src/multiGrid/multiGridObservables.ts +50 -50
  52. package/src/multiGrid/plugins/MultiBars.ts +108 -108
  53. package/src/multiGrid/plugins/MultiBarsTriangle.ts +114 -114
  54. package/src/multiGrid/plugins/MultiDots.ts +102 -102
  55. package/src/multiGrid/plugins/MultiGridLegend.ts +169 -169
  56. package/src/multiGrid/plugins/MultiGridTooltip.ts +66 -66
  57. package/src/multiGrid/plugins/MultiGroupAxis.ts +137 -137
  58. package/src/multiGrid/plugins/MultiLineAreas.ts +107 -107
  59. package/src/multiGrid/plugins/MultiLines.ts +101 -101
  60. package/src/multiGrid/plugins/MultiStackedBars.ts +109 -109
  61. package/src/multiGrid/plugins/MultiStackedValueAxis.ts +135 -135
  62. package/src/multiGrid/plugins/MultiValueAxis.ts +134 -134
  63. package/src/multiGrid/plugins/OverlappingStackedValueAxes.ts +300 -300
  64. package/src/multiGrid/plugins/OverlappingValueAxes.ts +300 -300
  65. package/src/multiValue/defaults.ts +523 -523
  66. package/src/multiValue/index.ts +16 -16
  67. package/src/multiValue/multiValueObservables.ts +781 -781
  68. package/src/multiValue/plugins/MultiValueLegend.ts +107 -107
  69. package/src/multiValue/plugins/MultiValueTooltip.ts +66 -66
  70. package/src/multiValue/plugins/OrdinalAux.ts +660 -660
  71. package/src/multiValue/plugins/OrdinalAxis.ts +524 -524
  72. package/src/multiValue/plugins/OrdinalBubbles.ts +226 -226
  73. package/src/multiValue/plugins/OrdinalZoom.ts +57 -57
  74. package/src/multiValue/plugins/RacingBars.ts +375 -375
  75. package/src/multiValue/plugins/RacingCounterTexts.ts +300 -300
  76. package/src/multiValue/plugins/RacingValueAxis.ts +114 -114
  77. package/src/multiValue/plugins/Scatter.ts +486 -486
  78. package/src/multiValue/plugins/ScatterBubbles.ts +635 -635
  79. package/src/multiValue/plugins/XAxis.ts +107 -107
  80. package/src/multiValue/plugins/XYAux.ts +683 -683
  81. package/src/multiValue/plugins/XYAxes.ts +194 -194
  82. package/src/multiValue/plugins/XYAxes_legacy.ts +683 -683
  83. package/src/multiValue/plugins/XZoom.ts +40 -40
  84. package/src/noneData/defaults.ts +102 -102
  85. package/src/noneData/index.ts +3 -3
  86. package/src/noneData/plugins/Container.ts +27 -27
  87. package/src/noneData/plugins/Tooltip.ts +373 -373
  88. package/src/relationship/defaults.ts +221 -221
  89. package/src/relationship/index.ts +5 -5
  90. package/src/relationship/plugins/ForceDirected.ts +1056 -1056
  91. package/src/relationship/plugins/ForceDirectedBubbles.ts +1294 -1294
  92. package/src/relationship/plugins/RelationshipLegend.ts +100 -100
  93. package/src/relationship/plugins/RelationshipTooltip.ts +66 -66
  94. package/src/relationship/relationshipObservables.ts +49 -49
  95. package/src/series/defaults.ts +236 -224
  96. package/src/series/index.ts +10 -9
  97. package/src/series/plugins/Bubbles.ts +784 -784
  98. package/src/series/plugins/Indicator.ts +298 -0
  99. package/src/series/plugins/Pie.ts +622 -622
  100. package/src/series/plugins/PieEventTexts.ts +283 -283
  101. package/src/series/plugins/PieLabels.ts +639 -639
  102. package/src/series/plugins/Rose.ts +515 -515
  103. package/src/series/plugins/RoseLabels.ts +599 -599
  104. package/src/series/plugins/SeriesLegend.ts +107 -107
  105. package/src/series/plugins/SeriesTooltip.ts +66 -66
  106. package/src/series/seriesObservables.ts +168 -168
  107. package/src/series/seriesUtils.ts +51 -51
  108. package/src/tree/defaults.ts +102 -102
  109. package/src/tree/index.ts +4 -4
  110. package/src/tree/plugins/TreeLegend.ts +100 -100
  111. package/src/tree/plugins/TreeMap.ts +341 -341
  112. package/src/tree/plugins/TreeTooltip.ts +66 -66
  113. package/src/utils/commonUtils.ts +31 -31
  114. package/src/utils/d3Graphics.ts +176 -176
  115. package/src/utils/d3Utils.ts +92 -92
  116. package/src/utils/observables.ts +14 -14
  117. package/src/utils/orbchartsUtils.ts +129 -129
  118. package/tsconfig.base.json +13 -13
  119. package/tsconfig.json +2 -2
  120. package/vite.config.js +22 -22
@@ -1,677 +1,677 @@
1
- import * as d3 from 'd3'
2
- import {
3
- combineLatest,
4
- map,
5
- switchMap,
6
- takeUntil,
7
- distinctUntilChanged,
8
- shareReplay,
9
- Observable,
10
- Subject } from 'rxjs'
11
- import type { BasePluginFn } from './types'
12
- import type {
13
- ComputedDatumGrid,
14
- ComputedDataGrid,
15
- ComputedLayoutDatumGrid,
16
- ComputedAxesDataGrid,
17
- DataFormatterTypeMap,
18
- EventGrid,
19
- ChartParams,
20
- ContainerPositionScaled,
21
- Layout,
22
- TransformData
23
- } from '../../lib/core-types'
24
- import type { BaseBarsTriangleParams } from '../../lib/plugins-basic-types'
25
- import { getD3TransitionEase } from '../utils/d3Utils'
26
- import { getClassName, getUniID } from '../utils/orbchartsUtils'
27
- import { gridSelectionsObservable } from '../grid/gridObservables'
28
-
29
- // export interface BaseBarsTriangleParams {
30
- // barWidth: number
31
- // barPadding: number
32
- // barGroupPadding: number // 群組和群組間的間隔
33
- // linearGradientOpacity: [number, number]
34
- // }
35
-
36
- interface BaseBarsContext {
37
- selection: d3.Selection<any, unknown, any, unknown>
38
- computedData$: Observable<ComputedDataGrid>
39
- computedAxesData$: Observable<ComputedAxesDataGrid>
40
- visibleComputedData$: Observable<ComputedDatumGrid[][]>
41
- visibleComputedAxesData$: Observable<ComputedAxesDataGrid>
42
- fullDataFormatter$: Observable<DataFormatterTypeMap<'grid'>>
43
- seriesLabels$: Observable<string[]>
44
- SeriesDataMap$: Observable<Map<string, ComputedDatumGrid[]>>
45
- GroupDataMap$: Observable<Map<string, ComputedDatumGrid[]>>
46
- fullParams$: Observable<BaseBarsTriangleParams>
47
- fullChartParams$: Observable<ChartParams>
48
- gridAxesTransform$: Observable<TransformData>
49
- gridGraphicTransform$: Observable<TransformData>
50
- gridAxesSize$: Observable<{
51
- width: number;
52
- height: number;
53
- }>
54
- gridHighlight$: Observable<ComputedDatumGrid[]>
55
- gridContainerPosition$: Observable<ContainerPositionScaled[]>
56
- isSeriesSeprate$: Observable<boolean>
57
- event$: Subject<EventGrid>
58
- }
59
-
60
-
61
- interface RenderBarParams {
62
- graphicGSelection: d3.Selection<SVGGElement, unknown, any, any>
63
- pathGClassName: string
64
- pathClassName: string
65
- visibleComputedAxesData: ComputedAxesDataGrid
66
- linearGradientIds: string[]
67
- zeroYArr: number[]
68
- groupLabels: string[]
69
- barScale: d3.ScalePoint<string>
70
- params: BaseBarsTriangleParams
71
- chartParams: ChartParams
72
- barWidth: number
73
- delayGroup: number
74
- transitionItem: number
75
- isSeriesSeprate: boolean
76
- }
77
-
78
- // interface BarDatumGrid extends ComputedDatumGrid {
79
- // linearGradientId: string
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 = 'BaseBarsTriangle'
91
- // const pathGClassName = getClassName(pluginName, 'pathG')
92
- // const pathClassName = getClassName(pluginName, 'path')
93
- // group的delay在動畫中的佔比(剩餘部份的時間為圖形本身的動畫時間,因為delay時間和最後一個group的動畫時間加總為1)
94
- const groupDelayProportionOfDuration = 0.3
95
-
96
- function calcBarWidth ({ axisWidth, groupAmount, barAmountOfGroup, barPadding = 0, barGroupPadding = 0 }: {
97
- axisWidth: number
98
- groupAmount: number
99
- barAmountOfGroup: number
100
- barPadding: number
101
- barGroupPadding: number
102
- }) {
103
- const eachGroupWidth = groupAmount > 1 // 等於 1 時會算出 Infinity
104
- ? axisWidth / (groupAmount - 1)
105
- : axisWidth
106
- const width = (eachGroupWidth - barGroupPadding) / barAmountOfGroup - barPadding
107
- return width > 1 ? width : 1
108
- }
109
-
110
- function makeBarScale (barWidth: number, seriesLabels: string[], params: BaseBarsTriangleParams) {
111
- const barHalfWidth = barWidth! / 2
112
- const barGroupWidth = barWidth * seriesLabels.length + params.barPadding! * seriesLabels.length
113
- return d3.scalePoint()
114
- .domain(seriesLabels)
115
- .range([-barGroupWidth / 2 + barHalfWidth, barGroupWidth / 2 - barHalfWidth])
116
- }
117
-
118
- function calcDelayGroup (barGroupAmount: number, totalDuration: number) {
119
- if (barGroupAmount <= 1) {
120
- // 一筆內計算會出錯所以不算
121
- return 0
122
- }
123
- return totalDuration / (barGroupAmount - 1) * groupDelayProportionOfDuration // 依group數量計算
124
- }
125
-
126
- function calctransitionItem (barGroupAmount: number, totalDuration: number) {
127
- if (barGroupAmount <= 1) {
128
- // 一筆內不會有delay
129
- return totalDuration
130
- }
131
- return totalDuration * (1 - groupDelayProportionOfDuration) // delay後剩餘的時間
132
- }
133
-
134
- function renderTriangleBars ({ graphicGSelection, pathGClassName, pathClassName, visibleComputedAxesData, linearGradientIds, zeroYArr, groupLabels, barScale, params, chartParams, barWidth, delayGroup, transitionItem, isSeriesSeprate }: RenderBarParams) {
135
-
136
- const barHalfWidth = barWidth! / 2
137
-
138
- graphicGSelection
139
- .each((d, seriesIndex, g) => {
140
- // g
141
- const gSelection = d3.select(g[seriesIndex])
142
- .selectAll<SVGGElement, ComputedDatumGrid>(`g.${pathGClassName}`)
143
- .data(visibleComputedAxesData[seriesIndex] ?? [])
144
- .join(
145
- enter => {
146
- const enterSelection = enter
147
- .append('g')
148
- .classed(pathGClassName, true)
149
- .attr('cursor', 'pointer')
150
- enterSelection
151
- .append('path')
152
- .classed(pathClassName, true)
153
- .style('vector-effect', 'non-scaling-stroke')
154
- .attr('d', (d) => {
155
- const x = -barHalfWidth
156
- const y1 = zeroYArr[seriesIndex]
157
- const y2 = zeroYArr[seriesIndex]
158
- return `M${x},${y1} L${x + (barWidth! / 2)},${y2} ${x + barWidth!},${y1}`
159
- })
160
- return enterSelection
161
- },
162
- update => update,
163
- exit => exit.remove()
164
- )
165
- .attr('transform', d => `translate(${isSeriesSeprate ? 0 : barScale(d.seriesLabel)!}, 0)`)
166
-
167
- // path
168
- gSelection.select(`path.${pathClassName}`)
169
- .attr('height', d => Math.abs(d.axisYFromZero) || 1) // 無值還是給一個 1 的高度
170
- .attr('y', d => d.axisY < zeroYArr[seriesIndex] ? d.axisY : zeroYArr[seriesIndex])
171
- .attr('x', d => isSeriesSeprate ? 0 : barScale(d.seriesLabel)!)
172
- // .style('fill', d => `url(#${d.linearGradientId})`)
173
- .style('fill', d => `url(#${linearGradientIds[d.seriesIndex]})`)
174
- .attr('stroke', d => d.color)
175
- .attr('transform', d => `translate(${(d ? d.axisX : 0)}, ${0})`)
176
- .transition()
177
- .duration(transitionItem)
178
- .ease(getD3TransitionEase(chartParams.transitionEase))
179
- .delay((d, i) => d.groupIndex * delayGroup)
180
- // .attr('transform', `translate(${-barHalfWidth}, 0)`)
181
- // .attr('x', d => itemScale(d.itemLabel)!)
182
- // .attr('y', d => -d.y)
183
- .attr('d', (d) => {
184
- const x = -barHalfWidth
185
- const y1 = zeroYArr[seriesIndex]
186
- const y2 = d.axisY
187
- return `M${x},${y1} L${x + (barWidth! / 2)},${y2} ${x + barWidth!},${y1}`
188
- })
189
- })
190
-
191
- const graphicBarSelection: d3.Selection<SVGPathElement, ComputedDatumGrid, any, any> = graphicGSelection.selectAll(`path.${pathClassName}`)
192
-
193
- return graphicBarSelection
194
- }
195
-
196
- function renderLinearGradient ({ defsSelection, computedData, linearGradientIds, params }: {
197
- defsSelection: d3.Selection<SVGDefsElement, any, any, any>
198
- computedData: ComputedDataGrid
199
- linearGradientIds: string[]
200
- params: BaseBarsTriangleParams
201
- }) {
202
- defsSelection!
203
- .selectAll<SVGLinearGradientElement, ComputedDatumGrid>('linearGradient')
204
- .data(computedData ?? [])
205
- .join(
206
- enter => {
207
- return enter
208
- .append('linearGradient')
209
- .attr('x1', '0%')
210
- .attr('x2', '0%')
211
- .attr('y1', '100%')
212
- .attr('y2', '0%')
213
- .attr('spreadMethod', 'pad')
214
- },
215
- update => update,
216
- exit => exit.remove()
217
- )
218
- .attr('id', (d, i) => {
219
- return d[0] ? linearGradientIds[d[0].seriesIndex] : ''
220
- })
221
- .html((d, i) => {
222
- const color = d[0] ? d[0].color : ''
223
- return `
224
- <stop offset="0%" stop-color="${color}" stop-opacity="${params.linearGradientOpacity[0]}"/>
225
- <stop offset="100%" stop-color="${color}" stop-opacity="${params.linearGradientOpacity[1]}"/>
226
- `
227
- })
228
-
229
- }
230
-
231
-
232
- function renderClipPath ({ defsSelection, clipPathData }: {
233
- defsSelection: d3.Selection<SVGDefsElement, any, any, any>
234
- clipPathData: ClipPathDatum[]
235
- }) {
236
- const clipPath = defsSelection
237
- .selectAll<SVGClipPathElement, Layout>('clipPath')
238
- .data(clipPathData)
239
- .join(
240
- enter => {
241
- return enter
242
- .append('clipPath')
243
- },
244
- update => update,
245
- exit => exit.remove()
246
- )
247
- .attr('id', d => d.id)
248
- .each((d, i, g) => {
249
- const rect = d3.select(g[i])
250
- .selectAll<SVGRectElement, typeof d>('rect')
251
- .data([d])
252
- .join(
253
- enter => {
254
- return enter
255
- .append('rect')
256
- },
257
- update => update,
258
- exit => exit.remove()
259
- )
260
- .attr('x', 0)
261
- .attr('y', 0)
262
- .attr('width', _d => _d.width)
263
- .attr('height', _d => _d.height)
264
- })
265
- }
266
-
267
- function highlight ({ selection, ids, fullChartParams }: {
268
- selection: d3.Selection<SVGPathElement, ComputedDatumGrid, any, any>
269
- ids: string[]
270
- fullChartParams: ChartParams
271
- }) {
272
- selection.interrupt('highlight')
273
-
274
- const removeHighlight = () => {
275
- selection
276
- .transition('highlight')
277
- .duration(200)
278
- .style('opacity', 1)
279
- }
280
-
281
- if (!ids.length) {
282
- removeHighlight()
283
- return
284
- }
285
-
286
- selection
287
- .each((d, i, n) => {
288
- if (ids.includes(d.id)) {
289
- d3.select(n[i])
290
- .style('opacity', 1)
291
- } else {
292
- d3.select(n[i])
293
- .style('opacity', fullChartParams.styles.unhighlightedOpacity)
294
- }
295
- })
296
- }
297
-
298
-
299
- export const createBaseBarsTriangle: BasePluginFn<BaseBarsContext> = (pluginName: string, {
300
- selection,
301
- computedData$,
302
- computedAxesData$,
303
- visibleComputedData$,
304
- visibleComputedAxesData$,
305
- fullDataFormatter$,
306
- seriesLabels$,
307
- SeriesDataMap$,
308
- GroupDataMap$,
309
- fullParams$,
310
- fullChartParams$,
311
- gridAxesTransform$,
312
- gridGraphicTransform$,
313
- gridAxesSize$,
314
- gridHighlight$,
315
- gridContainerPosition$,
316
- isSeriesSeprate$,
317
- event$
318
- }) => {
319
- const destroy$ = new Subject()
320
-
321
- const clipPathID = getUniID(pluginName, 'clipPath-box')
322
- const pathGClassName = getClassName(pluginName, 'pathG')
323
- const pathClassName = getClassName(pluginName, 'path')
324
-
325
- const {
326
- seriesSelection$,
327
- axesSelection$,
328
- defsSelection$,
329
- graphicGSelection$
330
- } = gridSelectionsObservable({
331
- selection,
332
- pluginName,
333
- clipPathID,
334
- seriesLabels$,
335
- gridContainerPosition$,
336
- gridAxesTransform$,
337
- gridGraphicTransform$
338
- })
339
-
340
- // valueAxis 的起始座標
341
- const valueAxisStart$: Observable<number> = gridGraphicTransform$.pipe(
342
- takeUntil(destroy$),
343
- map(data => {
344
- // 抵消掉外層的變型
345
- return - data.translate[1] / data.scale[1]
346
- })
347
- )
348
-
349
- const zeroYArr$ = visibleComputedAxesData$.pipe(
350
- // map(d => d[0] && d[0][0]
351
- // ? d[0][0].axisY - d[0][0].axisYFromZero
352
- // : 0),
353
- map(data => {
354
- return data.map(d => {
355
- return d[0] ? d[0].axisY - d[0].axisYFromZero : 0
356
- })
357
- }),
358
- distinctUntilChanged()
359
- )
360
-
361
- const barWidth$ = combineLatest({
362
- computedData: computedData$,
363
- visibleComputedData: visibleComputedData$,
364
- params: fullParams$,
365
- gridAxesSize: gridAxesSize$,
366
- isSeriesSeprate: isSeriesSeprate$
367
- }).pipe(
368
- takeUntil(destroy$),
369
- switchMap(async d => d),
370
- map(data => {
371
- if (data.params.barWidth) {
372
- return data.params.barWidth
373
- } else if (data.isSeriesSeprate) {
374
- return calcBarWidth({
375
- axisWidth: data.gridAxesSize.width,
376
- groupAmount: data.computedData[0] ? data.computedData[0].length : 0,
377
- barAmountOfGroup: 1,
378
- barPadding: data.params.barPadding,
379
- barGroupPadding: data.params.barGroupPadding
380
- })
381
- } else {
382
- return calcBarWidth({
383
- axisWidth: data.gridAxesSize.width,
384
- groupAmount: data.computedData[0] ? data.computedData[0].length : 0,
385
- barAmountOfGroup: data.visibleComputedData.length,
386
- barPadding: data.params.barPadding,
387
- barGroupPadding: data.params.barGroupPadding
388
- })
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
- )
418
-
419
- const barScale$: Observable<d3.ScalePoint<string>> = new Observable(subscriber => {
420
- combineLatest({
421
- seriesLabels: seriesLabels$,
422
- barWidth: barWidth$,
423
- params: fullParams$,
424
- }).pipe(
425
- takeUntil(destroy$),
426
- switchMap(async d => d)
427
- ).subscribe(data => {
428
- const barScale = makeBarScale(data.barWidth, data.seriesLabels, data.params)
429
- subscriber.next(barScale)
430
- })
431
- })
432
-
433
- const transitionDuration$ = fullChartParams$.pipe(
434
- takeUntil(destroy$),
435
- map(d => d.transitionDuration),
436
- distinctUntilChanged()
437
- )
438
-
439
- const delayGroup$ = new Observable<number>(subscriber => {
440
- combineLatest({
441
- groupLabels: groupLabels$,
442
- transitionDuration: transitionDuration$,
443
- }).pipe(
444
- switchMap(async d => d)
445
- ).subscribe(data => {
446
- const delay = calcDelayGroup(data.groupLabels.length, data.transitionDuration)
447
- subscriber.next(delay)
448
- })
449
- }).pipe(
450
- takeUntil(destroy$),
451
- distinctUntilChanged()
452
- )
453
-
454
- const transitionItem$ = new Observable<number>(subscriber => {
455
- combineLatest({
456
- groupLabels: groupLabels$,
457
- transitionDuration: transitionDuration$
458
- }).pipe(
459
- switchMap(async d => d)
460
- ).subscribe(data => {
461
- const transition = calctransitionItem(data.groupLabels.length, data.transitionDuration)
462
- subscriber.next(transition)
463
- })
464
- }).pipe(
465
- takeUntil(destroy$),
466
- distinctUntilChanged()
467
- )
468
-
469
- //
470
-
471
- combineLatest({
472
- defsSelection: defsSelection$,
473
- gridAxesSize: gridAxesSize$,
474
- }).pipe(
475
- takeUntil(destroy$),
476
- switchMap(async d => d)
477
- ).subscribe(data => {
478
- const clipPathData = [{
479
- id: clipPathID,
480
- width: data.gridAxesSize.width,
481
- height: data.gridAxesSize.height
482
- }]
483
- renderClipPath({
484
- defsSelection: data.defsSelection,
485
- clipPathData
486
- })
487
- })
488
-
489
-
490
- const highlightTarget$ = fullChartParams$.pipe(
491
- takeUntil(destroy$),
492
- map(d => d.highlightTarget),
493
- distinctUntilChanged()
494
- )
495
-
496
- const linearGradientIds$ = seriesLabels$.pipe(
497
- takeUntil(destroy$),
498
- map(d => d.map((d, i) => {
499
- return getUniID(pluginName, `lineargradient-${d}`)
500
- }))
501
- )
502
-
503
- // const barData$ = combineLatest({
504
- // linearGradientIds: linearGradientIds$,
505
- // computedData: computedData$
506
- // }).pipe(
507
- // takeUntil(destroy$),
508
- // switchMap(async d => d),
509
- // map(data => {
510
- // return data.computedData.map((series, seriesIndex) => {
511
- // return series.map((_d, _i) => {
512
- // return <BarDatumGrid>{
513
- // linearGradientId: data.linearGradientIds[seriesIndex],
514
- // ..._d
515
- // }
516
- // })
517
- // })
518
- // })
519
- // )
520
-
521
- const barSelection$ = combineLatest({
522
- graphicGSelection: graphicGSelection$,
523
- defsSelection: defsSelection$,
524
- computedData: computedData$,
525
- visibleComputedAxesData: visibleComputedAxesData$,
526
- linearGradientIds: linearGradientIds$,
527
- zeroYArr: zeroYArr$,
528
- groupLabels: groupLabels$,
529
- barScale: barScale$,
530
- params: fullParams$,
531
- chartParams: fullChartParams$,
532
- barWidth: barWidth$,
533
- delayGroup: delayGroup$,
534
- transitionItem: transitionItem$,
535
- isSeriesSeprate: isSeriesSeprate$
536
- }).pipe(
537
- takeUntil(destroy$),
538
- switchMap(async (d) => d),
539
- map(data => {
540
- renderLinearGradient({
541
- defsSelection: data.defsSelection,
542
- computedData: data.computedData,
543
- linearGradientIds: data.linearGradientIds,
544
- params: data.params
545
- })
546
-
547
- return renderTriangleBars({
548
- graphicGSelection: data.graphicGSelection,
549
- pathGClassName,
550
- pathClassName,
551
- visibleComputedAxesData: data.visibleComputedAxesData,
552
- linearGradientIds: data.linearGradientIds,
553
- zeroYArr: data.zeroYArr,
554
- groupLabels: data.groupLabels,
555
- barScale: data.barScale,
556
- params: data.params,
557
- chartParams: data.chartParams,
558
- barWidth: data.barWidth,
559
- delayGroup: data.delayGroup,
560
- transitionItem: data.transitionItem,
561
- isSeriesSeprate: data.isSeriesSeprate
562
- })
563
- })
564
- )
565
-
566
- combineLatest({
567
- barSelection: barSelection$,
568
- computedData: computedData$,
569
- highlightTarget: highlightTarget$,
570
- SeriesDataMap: SeriesDataMap$,
571
- GroupDataMap: GroupDataMap$,
572
- }).subscribe(data => {
573
-
574
- data.barSelection!
575
- .on('mouseover', (event, datum) => {
576
- event.stopPropagation()
577
-
578
- event$.next({
579
- type: 'grid',
580
- eventName: 'mouseover',
581
- pluginName,
582
- highlightTarget: data.highlightTarget,
583
- datum,
584
- gridIndex: datum.gridIndex,
585
- series: data.SeriesDataMap.get(datum.seriesLabel)!,
586
- seriesIndex: datum.seriesIndex,
587
- seriesLabel: datum.seriesLabel,
588
- group: data.GroupDataMap.get(datum.groupLabel)!,
589
- groupIndex: datum.groupIndex,
590
- groupLabel: datum.groupLabel,
591
- event,
592
- data: data.computedData
593
- })
594
- })
595
- .on('mousemove', (event, datum) => {
596
- event.stopPropagation()
597
-
598
- event$.next({
599
- type: 'grid',
600
- eventName: 'mousemove',
601
- pluginName,
602
- highlightTarget: data.highlightTarget,
603
- datum,
604
- gridIndex: datum.gridIndex,
605
- series: data.SeriesDataMap.get(datum.seriesLabel)!,
606
- seriesIndex: datum.seriesIndex,
607
- seriesLabel: datum.seriesLabel,
608
- group: data.GroupDataMap.get(datum.groupLabel)!,
609
- groupIndex: datum.groupIndex,
610
- groupLabel: datum.groupLabel,
611
- event,
612
- data: data.computedData
613
- })
614
- })
615
- .on('mouseout', (event, datum) => {
616
- event.stopPropagation()
617
-
618
- event$.next({
619
- type: 'grid',
620
- eventName: 'mouseout',
621
- pluginName,
622
- highlightTarget: data.highlightTarget,
623
- datum,
624
- gridIndex: datum.gridIndex,
625
- series: data.SeriesDataMap.get(datum.seriesLabel)!,
626
- seriesIndex: datum.seriesIndex,
627
- seriesLabel: datum.seriesLabel,
628
- group: data.GroupDataMap.get(datum.groupLabel)!,
629
- groupIndex: datum.groupIndex,
630
- groupLabel: datum.groupLabel,
631
- event,
632
- data: data.computedData
633
- })
634
- })
635
- .on('click', (event, datum) => {
636
- event.stopPropagation()
637
-
638
- event$.next({
639
- type: 'grid',
640
- eventName: 'click',
641
- pluginName,
642
- highlightTarget: data.highlightTarget,
643
- datum,
644
- gridIndex: datum.gridIndex,
645
- series: data.SeriesDataMap.get(datum.seriesLabel)!,
646
- seriesIndex: datum.seriesIndex,
647
- seriesLabel: datum.seriesLabel,
648
- group: data.GroupDataMap.get(datum.groupLabel)!,
649
- groupIndex: datum.groupIndex,
650
- groupLabel: datum.groupLabel,
651
- event,
652
- data: data.computedData
653
- })
654
- })
655
- })
656
-
657
- combineLatest({
658
- barSelection: barSelection$,
659
- highlight: gridHighlight$.pipe(
660
- map(data => data.map(d => d.id))
661
- ),
662
- fullChartParams: fullChartParams$
663
- }).pipe(
664
- takeUntil(destroy$),
665
- switchMap(async d => d)
666
- ).subscribe(data => {
667
- highlight({
668
- selection: data.barSelection,
669
- ids: data.highlight,
670
- fullChartParams: data.fullChartParams
671
- })
672
- })
673
-
674
- return () => {
675
- destroy$.next(undefined)
676
- }
1
+ import * as d3 from 'd3'
2
+ import {
3
+ combineLatest,
4
+ map,
5
+ switchMap,
6
+ takeUntil,
7
+ distinctUntilChanged,
8
+ shareReplay,
9
+ Observable,
10
+ Subject } from 'rxjs'
11
+ import type { BasePluginFn } from './types'
12
+ import type {
13
+ ComputedDatumGrid,
14
+ ComputedDataGrid,
15
+ ComputedLayoutDatumGrid,
16
+ ComputedAxesDataGrid,
17
+ DataFormatterTypeMap,
18
+ EventGrid,
19
+ ChartParams,
20
+ ContainerPositionScaled,
21
+ Layout,
22
+ TransformData
23
+ } from '../../lib/core-types'
24
+ import type { BaseBarsTriangleParams } from '../../lib/plugins-basic-types'
25
+ import { getD3TransitionEase } from '../utils/d3Utils'
26
+ import { getClassName, getUniID } from '../utils/orbchartsUtils'
27
+ import { gridSelectionsObservable } from '../grid/gridObservables'
28
+
29
+ // export interface BaseBarsTriangleParams {
30
+ // barWidth: number
31
+ // barPadding: number
32
+ // barGroupPadding: number // 群組和群組間的間隔
33
+ // linearGradientOpacity: [number, number]
34
+ // }
35
+
36
+ interface BaseBarsContext {
37
+ selection: d3.Selection<any, unknown, any, unknown>
38
+ computedData$: Observable<ComputedDataGrid>
39
+ computedAxesData$: Observable<ComputedAxesDataGrid>
40
+ visibleComputedData$: Observable<ComputedDatumGrid[][]>
41
+ visibleComputedAxesData$: Observable<ComputedAxesDataGrid>
42
+ fullDataFormatter$: Observable<DataFormatterTypeMap<'grid'>>
43
+ seriesLabels$: Observable<string[]>
44
+ SeriesDataMap$: Observable<Map<string, ComputedDatumGrid[]>>
45
+ GroupDataMap$: Observable<Map<string, ComputedDatumGrid[]>>
46
+ fullParams$: Observable<BaseBarsTriangleParams>
47
+ fullChartParams$: Observable<ChartParams>
48
+ gridAxesTransform$: Observable<TransformData>
49
+ gridGraphicTransform$: Observable<TransformData>
50
+ gridAxesSize$: Observable<{
51
+ width: number;
52
+ height: number;
53
+ }>
54
+ gridHighlight$: Observable<ComputedDatumGrid[]>
55
+ gridContainerPosition$: Observable<ContainerPositionScaled[]>
56
+ isSeriesSeprate$: Observable<boolean>
57
+ event$: Subject<EventGrid>
58
+ }
59
+
60
+
61
+ interface RenderBarParams {
62
+ graphicGSelection: d3.Selection<SVGGElement, unknown, any, any>
63
+ pathGClassName: string
64
+ pathClassName: string
65
+ visibleComputedAxesData: ComputedAxesDataGrid
66
+ linearGradientIds: string[]
67
+ zeroYArr: number[]
68
+ groupLabels: string[]
69
+ barScale: d3.ScalePoint<string>
70
+ params: BaseBarsTriangleParams
71
+ chartParams: ChartParams
72
+ barWidth: number
73
+ delayGroup: number
74
+ transitionItem: number
75
+ isSeriesSeprate: boolean
76
+ }
77
+
78
+ // interface BarDatumGrid extends ComputedDatumGrid {
79
+ // linearGradientId: string
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 = 'BaseBarsTriangle'
91
+ // const pathGClassName = getClassName(pluginName, 'pathG')
92
+ // const pathClassName = getClassName(pluginName, 'path')
93
+ // group的delay在動畫中的佔比(剩餘部份的時間為圖形本身的動畫時間,因為delay時間和最後一個group的動畫時間加總為1)
94
+ const groupDelayProportionOfDuration = 0.3
95
+
96
+ function calcBarWidth ({ axisWidth, groupAmount, barAmountOfGroup, barPadding = 0, barGroupPadding = 0 }: {
97
+ axisWidth: number
98
+ groupAmount: number
99
+ barAmountOfGroup: number
100
+ barPadding: number
101
+ barGroupPadding: number
102
+ }) {
103
+ const eachGroupWidth = groupAmount > 1 // 等於 1 時會算出 Infinity
104
+ ? axisWidth / (groupAmount - 1)
105
+ : axisWidth
106
+ const width = (eachGroupWidth - barGroupPadding) / barAmountOfGroup - barPadding
107
+ return width > 1 ? width : 1
108
+ }
109
+
110
+ function makeBarScale (barWidth: number, seriesLabels: string[], params: BaseBarsTriangleParams) {
111
+ const barHalfWidth = barWidth! / 2
112
+ const barGroupWidth = barWidth * seriesLabels.length + params.barPadding! * seriesLabels.length
113
+ return d3.scalePoint()
114
+ .domain(seriesLabels)
115
+ .range([-barGroupWidth / 2 + barHalfWidth, barGroupWidth / 2 - barHalfWidth])
116
+ }
117
+
118
+ function calcDelayGroup (barGroupAmount: number, totalDuration: number) {
119
+ if (barGroupAmount <= 1) {
120
+ // 一筆內計算會出錯所以不算
121
+ return 0
122
+ }
123
+ return totalDuration / (barGroupAmount - 1) * groupDelayProportionOfDuration // 依group數量計算
124
+ }
125
+
126
+ function calctransitionItem (barGroupAmount: number, totalDuration: number) {
127
+ if (barGroupAmount <= 1) {
128
+ // 一筆內不會有delay
129
+ return totalDuration
130
+ }
131
+ return totalDuration * (1 - groupDelayProportionOfDuration) // delay後剩餘的時間
132
+ }
133
+
134
+ function renderTriangleBars ({ graphicGSelection, pathGClassName, pathClassName, visibleComputedAxesData, linearGradientIds, zeroYArr, groupLabels, barScale, params, chartParams, barWidth, delayGroup, transitionItem, isSeriesSeprate }: RenderBarParams) {
135
+
136
+ const barHalfWidth = barWidth! / 2
137
+
138
+ graphicGSelection
139
+ .each((d, seriesIndex, g) => {
140
+ // g
141
+ const gSelection = d3.select(g[seriesIndex])
142
+ .selectAll<SVGGElement, ComputedDatumGrid>(`g.${pathGClassName}`)
143
+ .data(visibleComputedAxesData[seriesIndex] ?? [])
144
+ .join(
145
+ enter => {
146
+ const enterSelection = enter
147
+ .append('g')
148
+ .classed(pathGClassName, true)
149
+ .attr('cursor', 'pointer')
150
+ enterSelection
151
+ .append('path')
152
+ .classed(pathClassName, true)
153
+ .style('vector-effect', 'non-scaling-stroke')
154
+ .attr('d', (d) => {
155
+ const x = -barHalfWidth
156
+ const y1 = zeroYArr[seriesIndex]
157
+ const y2 = zeroYArr[seriesIndex]
158
+ return `M${x},${y1} L${x + (barWidth! / 2)},${y2} ${x + barWidth!},${y1}`
159
+ })
160
+ return enterSelection
161
+ },
162
+ update => update,
163
+ exit => exit.remove()
164
+ )
165
+ .attr('transform', d => `translate(${isSeriesSeprate ? 0 : barScale(d.seriesLabel)!}, 0)`)
166
+
167
+ // path
168
+ gSelection.select(`path.${pathClassName}`)
169
+ .attr('height', d => Math.abs(d.axisYFromZero) || 1) // 無值還是給一個 1 的高度
170
+ .attr('y', d => d.axisY < zeroYArr[seriesIndex] ? d.axisY : zeroYArr[seriesIndex])
171
+ .attr('x', d => isSeriesSeprate ? 0 : barScale(d.seriesLabel)!)
172
+ // .style('fill', d => `url(#${d.linearGradientId})`)
173
+ .style('fill', d => `url(#${linearGradientIds[d.seriesIndex]})`)
174
+ .attr('stroke', d => d.color)
175
+ .attr('transform', d => `translate(${(d ? d.axisX : 0)}, ${0})`)
176
+ .transition()
177
+ .duration(transitionItem)
178
+ .ease(getD3TransitionEase(chartParams.transitionEase))
179
+ .delay((d, i) => d.groupIndex * delayGroup)
180
+ // .attr('transform', `translate(${-barHalfWidth}, 0)`)
181
+ // .attr('x', d => itemScale(d.itemLabel)!)
182
+ // .attr('y', d => -d.y)
183
+ .attr('d', (d) => {
184
+ const x = -barHalfWidth
185
+ const y1 = zeroYArr[seriesIndex]
186
+ const y2 = d.axisY
187
+ return `M${x},${y1} L${x + (barWidth! / 2)},${y2} ${x + barWidth!},${y1}`
188
+ })
189
+ })
190
+
191
+ const graphicBarSelection: d3.Selection<SVGPathElement, ComputedDatumGrid, any, any> = graphicGSelection.selectAll(`path.${pathClassName}`)
192
+
193
+ return graphicBarSelection
194
+ }
195
+
196
+ function renderLinearGradient ({ defsSelection, computedData, linearGradientIds, params }: {
197
+ defsSelection: d3.Selection<SVGDefsElement, any, any, any>
198
+ computedData: ComputedDataGrid
199
+ linearGradientIds: string[]
200
+ params: BaseBarsTriangleParams
201
+ }) {
202
+ defsSelection!
203
+ .selectAll<SVGLinearGradientElement, ComputedDatumGrid>('linearGradient')
204
+ .data(computedData ?? [])
205
+ .join(
206
+ enter => {
207
+ return enter
208
+ .append('linearGradient')
209
+ .attr('x1', '0%')
210
+ .attr('x2', '0%')
211
+ .attr('y1', '100%')
212
+ .attr('y2', '0%')
213
+ .attr('spreadMethod', 'pad')
214
+ },
215
+ update => update,
216
+ exit => exit.remove()
217
+ )
218
+ .attr('id', (d, i) => {
219
+ return d[0] ? linearGradientIds[d[0].seriesIndex] : ''
220
+ })
221
+ .html((d, i) => {
222
+ const color = d[0] ? d[0].color : ''
223
+ return `
224
+ <stop offset="0%" stop-color="${color}" stop-opacity="${params.linearGradientOpacity[0]}"/>
225
+ <stop offset="100%" stop-color="${color}" stop-opacity="${params.linearGradientOpacity[1]}"/>
226
+ `
227
+ })
228
+
229
+ }
230
+
231
+
232
+ function renderClipPath ({ defsSelection, clipPathData }: {
233
+ defsSelection: d3.Selection<SVGDefsElement, any, any, any>
234
+ clipPathData: ClipPathDatum[]
235
+ }) {
236
+ const clipPath = defsSelection
237
+ .selectAll<SVGClipPathElement, Layout>('clipPath')
238
+ .data(clipPathData)
239
+ .join(
240
+ enter => {
241
+ return enter
242
+ .append('clipPath')
243
+ },
244
+ update => update,
245
+ exit => exit.remove()
246
+ )
247
+ .attr('id', d => d.id)
248
+ .each((d, i, g) => {
249
+ const rect = d3.select(g[i])
250
+ .selectAll<SVGRectElement, typeof d>('rect')
251
+ .data([d])
252
+ .join(
253
+ enter => {
254
+ return enter
255
+ .append('rect')
256
+ },
257
+ update => update,
258
+ exit => exit.remove()
259
+ )
260
+ .attr('x', 0)
261
+ .attr('y', 0)
262
+ .attr('width', _d => _d.width)
263
+ .attr('height', _d => _d.height)
264
+ })
265
+ }
266
+
267
+ function highlight ({ selection, ids, fullChartParams }: {
268
+ selection: d3.Selection<SVGPathElement, ComputedDatumGrid, any, any>
269
+ ids: string[]
270
+ fullChartParams: ChartParams
271
+ }) {
272
+ selection.interrupt('highlight')
273
+
274
+ const removeHighlight = () => {
275
+ selection
276
+ .transition('highlight')
277
+ .duration(200)
278
+ .style('opacity', 1)
279
+ }
280
+
281
+ if (!ids.length) {
282
+ removeHighlight()
283
+ return
284
+ }
285
+
286
+ selection
287
+ .each((d, i, n) => {
288
+ if (ids.includes(d.id)) {
289
+ d3.select(n[i])
290
+ .style('opacity', 1)
291
+ } else {
292
+ d3.select(n[i])
293
+ .style('opacity', fullChartParams.styles.unhighlightedOpacity)
294
+ }
295
+ })
296
+ }
297
+
298
+
299
+ export const createBaseBarsTriangle: BasePluginFn<BaseBarsContext> = (pluginName: string, {
300
+ selection,
301
+ computedData$,
302
+ computedAxesData$,
303
+ visibleComputedData$,
304
+ visibleComputedAxesData$,
305
+ fullDataFormatter$,
306
+ seriesLabels$,
307
+ SeriesDataMap$,
308
+ GroupDataMap$,
309
+ fullParams$,
310
+ fullChartParams$,
311
+ gridAxesTransform$,
312
+ gridGraphicTransform$,
313
+ gridAxesSize$,
314
+ gridHighlight$,
315
+ gridContainerPosition$,
316
+ isSeriesSeprate$,
317
+ event$
318
+ }) => {
319
+ const destroy$ = new Subject()
320
+
321
+ const clipPathID = getUniID(pluginName, 'clipPath-box')
322
+ const pathGClassName = getClassName(pluginName, 'pathG')
323
+ const pathClassName = getClassName(pluginName, 'path')
324
+
325
+ const {
326
+ seriesSelection$,
327
+ axesSelection$,
328
+ defsSelection$,
329
+ graphicGSelection$
330
+ } = gridSelectionsObservable({
331
+ selection,
332
+ pluginName,
333
+ clipPathID,
334
+ seriesLabels$,
335
+ gridContainerPosition$,
336
+ gridAxesTransform$,
337
+ gridGraphicTransform$
338
+ })
339
+
340
+ // valueAxis 的起始座標
341
+ const valueAxisStart$: Observable<number> = gridGraphicTransform$.pipe(
342
+ takeUntil(destroy$),
343
+ map(data => {
344
+ // 抵消掉外層的變型
345
+ return - data.translate[1] / data.scale[1]
346
+ })
347
+ )
348
+
349
+ const zeroYArr$ = visibleComputedAxesData$.pipe(
350
+ // map(d => d[0] && d[0][0]
351
+ // ? d[0][0].axisY - d[0][0].axisYFromZero
352
+ // : 0),
353
+ map(data => {
354
+ return data.map(d => {
355
+ return d[0] ? d[0].axisY - d[0].axisYFromZero : 0
356
+ })
357
+ }),
358
+ distinctUntilChanged()
359
+ )
360
+
361
+ const barWidth$ = combineLatest({
362
+ computedData: computedData$,
363
+ visibleComputedData: visibleComputedData$,
364
+ params: fullParams$,
365
+ gridAxesSize: gridAxesSize$,
366
+ isSeriesSeprate: isSeriesSeprate$
367
+ }).pipe(
368
+ takeUntil(destroy$),
369
+ switchMap(async d => d),
370
+ map(data => {
371
+ if (data.params.barWidth) {
372
+ return data.params.barWidth
373
+ } else if (data.isSeriesSeprate) {
374
+ return calcBarWidth({
375
+ axisWidth: data.gridAxesSize.width,
376
+ groupAmount: data.computedData[0] ? data.computedData[0].length : 0,
377
+ barAmountOfGroup: 1,
378
+ barPadding: data.params.barPadding,
379
+ barGroupPadding: data.params.barGroupPadding
380
+ })
381
+ } else {
382
+ return calcBarWidth({
383
+ axisWidth: data.gridAxesSize.width,
384
+ groupAmount: data.computedData[0] ? data.computedData[0].length : 0,
385
+ barAmountOfGroup: data.visibleComputedData.length,
386
+ barPadding: data.params.barPadding,
387
+ barGroupPadding: data.params.barGroupPadding
388
+ })
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
+ )
418
+
419
+ const barScale$: Observable<d3.ScalePoint<string>> = new Observable(subscriber => {
420
+ combineLatest({
421
+ seriesLabels: seriesLabels$,
422
+ barWidth: barWidth$,
423
+ params: fullParams$,
424
+ }).pipe(
425
+ takeUntil(destroy$),
426
+ switchMap(async d => d)
427
+ ).subscribe(data => {
428
+ const barScale = makeBarScale(data.barWidth, data.seriesLabels, data.params)
429
+ subscriber.next(barScale)
430
+ })
431
+ })
432
+
433
+ const transitionDuration$ = fullChartParams$.pipe(
434
+ takeUntil(destroy$),
435
+ map(d => d.transitionDuration),
436
+ distinctUntilChanged()
437
+ )
438
+
439
+ const delayGroup$ = new Observable<number>(subscriber => {
440
+ combineLatest({
441
+ groupLabels: groupLabels$,
442
+ transitionDuration: transitionDuration$,
443
+ }).pipe(
444
+ switchMap(async d => d)
445
+ ).subscribe(data => {
446
+ const delay = calcDelayGroup(data.groupLabels.length, data.transitionDuration)
447
+ subscriber.next(delay)
448
+ })
449
+ }).pipe(
450
+ takeUntil(destroy$),
451
+ distinctUntilChanged()
452
+ )
453
+
454
+ const transitionItem$ = new Observable<number>(subscriber => {
455
+ combineLatest({
456
+ groupLabels: groupLabels$,
457
+ transitionDuration: transitionDuration$
458
+ }).pipe(
459
+ switchMap(async d => d)
460
+ ).subscribe(data => {
461
+ const transition = calctransitionItem(data.groupLabels.length, data.transitionDuration)
462
+ subscriber.next(transition)
463
+ })
464
+ }).pipe(
465
+ takeUntil(destroy$),
466
+ distinctUntilChanged()
467
+ )
468
+
469
+ //
470
+
471
+ combineLatest({
472
+ defsSelection: defsSelection$,
473
+ gridAxesSize: gridAxesSize$,
474
+ }).pipe(
475
+ takeUntil(destroy$),
476
+ switchMap(async d => d)
477
+ ).subscribe(data => {
478
+ const clipPathData = [{
479
+ id: clipPathID,
480
+ width: data.gridAxesSize.width,
481
+ height: data.gridAxesSize.height
482
+ }]
483
+ renderClipPath({
484
+ defsSelection: data.defsSelection,
485
+ clipPathData
486
+ })
487
+ })
488
+
489
+
490
+ const highlightTarget$ = fullChartParams$.pipe(
491
+ takeUntil(destroy$),
492
+ map(d => d.highlightTarget),
493
+ distinctUntilChanged()
494
+ )
495
+
496
+ const linearGradientIds$ = seriesLabels$.pipe(
497
+ takeUntil(destroy$),
498
+ map(d => d.map((d, i) => {
499
+ return getUniID(pluginName, `lineargradient-${d}`)
500
+ }))
501
+ )
502
+
503
+ // const barData$ = combineLatest({
504
+ // linearGradientIds: linearGradientIds$,
505
+ // computedData: computedData$
506
+ // }).pipe(
507
+ // takeUntil(destroy$),
508
+ // switchMap(async d => d),
509
+ // map(data => {
510
+ // return data.computedData.map((series, seriesIndex) => {
511
+ // return series.map((_d, _i) => {
512
+ // return <BarDatumGrid>{
513
+ // linearGradientId: data.linearGradientIds[seriesIndex],
514
+ // ..._d
515
+ // }
516
+ // })
517
+ // })
518
+ // })
519
+ // )
520
+
521
+ const barSelection$ = combineLatest({
522
+ graphicGSelection: graphicGSelection$,
523
+ defsSelection: defsSelection$,
524
+ computedData: computedData$,
525
+ visibleComputedAxesData: visibleComputedAxesData$,
526
+ linearGradientIds: linearGradientIds$,
527
+ zeroYArr: zeroYArr$,
528
+ groupLabels: groupLabels$,
529
+ barScale: barScale$,
530
+ params: fullParams$,
531
+ chartParams: fullChartParams$,
532
+ barWidth: barWidth$,
533
+ delayGroup: delayGroup$,
534
+ transitionItem: transitionItem$,
535
+ isSeriesSeprate: isSeriesSeprate$
536
+ }).pipe(
537
+ takeUntil(destroy$),
538
+ switchMap(async (d) => d),
539
+ map(data => {
540
+ renderLinearGradient({
541
+ defsSelection: data.defsSelection,
542
+ computedData: data.computedData,
543
+ linearGradientIds: data.linearGradientIds,
544
+ params: data.params
545
+ })
546
+
547
+ return renderTriangleBars({
548
+ graphicGSelection: data.graphicGSelection,
549
+ pathGClassName,
550
+ pathClassName,
551
+ visibleComputedAxesData: data.visibleComputedAxesData,
552
+ linearGradientIds: data.linearGradientIds,
553
+ zeroYArr: data.zeroYArr,
554
+ groupLabels: data.groupLabels,
555
+ barScale: data.barScale,
556
+ params: data.params,
557
+ chartParams: data.chartParams,
558
+ barWidth: data.barWidth,
559
+ delayGroup: data.delayGroup,
560
+ transitionItem: data.transitionItem,
561
+ isSeriesSeprate: data.isSeriesSeprate
562
+ })
563
+ })
564
+ )
565
+
566
+ combineLatest({
567
+ barSelection: barSelection$,
568
+ computedData: computedData$,
569
+ highlightTarget: highlightTarget$,
570
+ SeriesDataMap: SeriesDataMap$,
571
+ GroupDataMap: GroupDataMap$,
572
+ }).subscribe(data => {
573
+
574
+ data.barSelection!
575
+ .on('mouseover', (event, datum) => {
576
+ event.stopPropagation()
577
+
578
+ event$.next({
579
+ type: 'grid',
580
+ eventName: 'mouseover',
581
+ pluginName,
582
+ highlightTarget: data.highlightTarget,
583
+ datum,
584
+ gridIndex: datum.gridIndex,
585
+ series: data.SeriesDataMap.get(datum.seriesLabel)!,
586
+ seriesIndex: datum.seriesIndex,
587
+ seriesLabel: datum.seriesLabel,
588
+ group: data.GroupDataMap.get(datum.groupLabel)!,
589
+ groupIndex: datum.groupIndex,
590
+ groupLabel: datum.groupLabel,
591
+ event,
592
+ data: data.computedData
593
+ })
594
+ })
595
+ .on('mousemove', (event, datum) => {
596
+ event.stopPropagation()
597
+
598
+ event$.next({
599
+ type: 'grid',
600
+ eventName: 'mousemove',
601
+ pluginName,
602
+ highlightTarget: data.highlightTarget,
603
+ datum,
604
+ gridIndex: datum.gridIndex,
605
+ series: data.SeriesDataMap.get(datum.seriesLabel)!,
606
+ seriesIndex: datum.seriesIndex,
607
+ seriesLabel: datum.seriesLabel,
608
+ group: data.GroupDataMap.get(datum.groupLabel)!,
609
+ groupIndex: datum.groupIndex,
610
+ groupLabel: datum.groupLabel,
611
+ event,
612
+ data: data.computedData
613
+ })
614
+ })
615
+ .on('mouseout', (event, datum) => {
616
+ event.stopPropagation()
617
+
618
+ event$.next({
619
+ type: 'grid',
620
+ eventName: 'mouseout',
621
+ pluginName,
622
+ highlightTarget: data.highlightTarget,
623
+ datum,
624
+ gridIndex: datum.gridIndex,
625
+ series: data.SeriesDataMap.get(datum.seriesLabel)!,
626
+ seriesIndex: datum.seriesIndex,
627
+ seriesLabel: datum.seriesLabel,
628
+ group: data.GroupDataMap.get(datum.groupLabel)!,
629
+ groupIndex: datum.groupIndex,
630
+ groupLabel: datum.groupLabel,
631
+ event,
632
+ data: data.computedData
633
+ })
634
+ })
635
+ .on('click', (event, datum) => {
636
+ event.stopPropagation()
637
+
638
+ event$.next({
639
+ type: 'grid',
640
+ eventName: 'click',
641
+ pluginName,
642
+ highlightTarget: data.highlightTarget,
643
+ datum,
644
+ gridIndex: datum.gridIndex,
645
+ series: data.SeriesDataMap.get(datum.seriesLabel)!,
646
+ seriesIndex: datum.seriesIndex,
647
+ seriesLabel: datum.seriesLabel,
648
+ group: data.GroupDataMap.get(datum.groupLabel)!,
649
+ groupIndex: datum.groupIndex,
650
+ groupLabel: datum.groupLabel,
651
+ event,
652
+ data: data.computedData
653
+ })
654
+ })
655
+ })
656
+
657
+ combineLatest({
658
+ barSelection: barSelection$,
659
+ highlight: gridHighlight$.pipe(
660
+ map(data => data.map(d => d.id))
661
+ ),
662
+ fullChartParams: fullChartParams$
663
+ }).pipe(
664
+ takeUntil(destroy$),
665
+ switchMap(async d => d)
666
+ ).subscribe(data => {
667
+ highlight({
668
+ selection: data.barSelection,
669
+ ids: data.highlight,
670
+ fullChartParams: data.fullChartParams
671
+ })
672
+ })
673
+
674
+ return () => {
675
+ destroy$.next(undefined)
676
+ }
677
677
  }