@orbcharts/plugins-basic 3.0.0-beta.6 → 3.0.0-beta.7

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