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

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