@orbcharts/plugins-basic 3.0.0-alpha.45 → 3.0.0-alpha.46

Sign up to get free protection for your applications and to get access to all the features.
Files changed (153) hide show
  1. package/LICENSE +200 -200
  2. package/dist/orbcharts-plugins-basic.es.js +182 -159
  3. package/dist/orbcharts-plugins-basic.umd.js +7 -7
  4. package/dist/src/index.d.ts +5 -1
  5. package/dist/{orbcharts-plugins-basic/src → src}/series/seriesObservables.d.ts +4 -4
  6. package/package.json +42 -42
  7. package/src/base/BaseBarStack.ts +778 -778
  8. package/src/base/BaseBars.ts +764 -764
  9. package/src/base/BaseBarsTriangle.ts +672 -672
  10. package/src/base/BaseDots.ts +502 -502
  11. package/src/base/BaseGroupAxis.ts +496 -496
  12. package/src/base/BaseLegend.ts +641 -641
  13. package/src/base/BaseLineAreas.ts +625 -625
  14. package/src/base/BaseLines.ts +699 -699
  15. package/src/base/BaseValueAxis.ts +478 -478
  16. package/src/base/types.ts +2 -2
  17. package/src/grid/defaults.ts +121 -121
  18. package/src/grid/gridObservables.ts +247 -247
  19. package/src/grid/index.ts +15 -15
  20. package/src/grid/plugins/BarStack.ts +50 -50
  21. package/src/grid/plugins/Bars.ts +51 -51
  22. package/src/grid/plugins/BarsDiverging.ts +41 -41
  23. package/src/grid/plugins/BarsTriangle.ts +50 -50
  24. package/src/grid/plugins/Dots.ts +37 -37
  25. package/src/grid/plugins/GridLegend.ts +59 -59
  26. package/src/grid/plugins/GroupAux.ts +645 -645
  27. package/src/grid/plugins/GroupAxis.ts +42 -42
  28. package/src/grid/plugins/LineAreas.ts +39 -39
  29. package/src/grid/plugins/Lines.ts +38 -38
  30. package/src/grid/plugins/ScalingArea.ts +173 -173
  31. package/src/grid/plugins/ValueAxis.ts +43 -43
  32. package/src/grid/plugins/ValueStackAxis.ts +79 -79
  33. package/src/grid/types.ts +120 -120
  34. package/src/index.ts +9 -9
  35. package/src/multiGrid/defaults.ts +147 -147
  36. package/src/multiGrid/index.ts +11 -11
  37. package/src/multiGrid/multiGridObservables.ts +42 -42
  38. package/src/multiGrid/plugins/MultiBarStack.ts +74 -74
  39. package/src/multiGrid/plugins/MultiBars.ts +73 -73
  40. package/src/multiGrid/plugins/MultiBarsTriangle.ts +73 -73
  41. package/src/multiGrid/plugins/MultiDots.ts +60 -60
  42. package/src/multiGrid/plugins/MultiGridLegend.ts +89 -89
  43. package/src/multiGrid/plugins/MultiGroupAxis.ts +65 -65
  44. package/src/multiGrid/plugins/MultiLineAreas.ts +62 -62
  45. package/src/multiGrid/plugins/MultiLines.ts +61 -61
  46. package/src/multiGrid/plugins/MultiValueAxis.ts +65 -65
  47. package/src/multiGrid/plugins/OverlappingValueAxes.ts +169 -169
  48. package/src/multiGrid/types.ts +67 -67
  49. package/src/noneData/defaults.ts +64 -64
  50. package/src/noneData/index.ts +3 -3
  51. package/src/noneData/plugins/Container.ts +10 -10
  52. package/src/noneData/plugins/Tooltip.ts +310 -310
  53. package/src/noneData/types.ts +26 -26
  54. package/src/series/defaults.ts +109 -109
  55. package/src/series/index.ts +6 -6
  56. package/src/series/plugins/Bubbles.ts +602 -571
  57. package/src/series/plugins/Pie.ts +555 -548
  58. package/src/series/plugins/PieEventTexts.ts +258 -258
  59. package/src/series/plugins/PieLabels.ts +335 -335
  60. package/src/series/plugins/SeriesLegend.ts +59 -59
  61. package/src/series/seriesObservables.ts +145 -145
  62. package/src/series/seriesUtils.ts +50 -50
  63. package/src/series/types.ts +67 -67
  64. package/src/tree/defaults.ts +22 -22
  65. package/src/tree/index.ts +3 -3
  66. package/src/tree/plugins/TreeLegend.ts +59 -59
  67. package/src/tree/plugins/TreeMap.ts +305 -305
  68. package/src/tree/types.ts +23 -23
  69. package/src/utils/commonUtils.ts +21 -21
  70. package/src/utils/d3Graphics.ts +124 -124
  71. package/src/utils/d3Utils.ts +73 -73
  72. package/src/utils/observables.ts +14 -14
  73. package/src/utils/orbchartsUtils.ts +100 -100
  74. package/tsconfig.dev.json +16 -16
  75. package/tsconfig.json +13 -16
  76. package/tsconfig.prod.json +13 -13
  77. package/vite.config.js +49 -49
  78. package/dist/orbcharts-plugins-basic/src/index.d.ts +0 -5
  79. /package/dist/{orbcharts-plugins-basic/src → src}/base/BaseBarStack.d.ts +0 -0
  80. /package/dist/{orbcharts-plugins-basic/src → src}/base/BaseBars.d.ts +0 -0
  81. /package/dist/{orbcharts-plugins-basic/src → src}/base/BaseBarsTriangle.d.ts +0 -0
  82. /package/dist/{orbcharts-plugins-basic/src → src}/base/BaseDots.d.ts +0 -0
  83. /package/dist/{orbcharts-plugins-basic/src → src}/base/BaseGroupArea.d.ts +0 -0
  84. /package/dist/{orbcharts-plugins-basic/src → src}/base/BaseGroupAxis.d.ts +0 -0
  85. /package/dist/{orbcharts-plugins-basic/src → src}/base/BaseLegend.d.ts +0 -0
  86. /package/dist/{orbcharts-plugins-basic/src → src}/base/BaseLineAreas.d.ts +0 -0
  87. /package/dist/{orbcharts-plugins-basic/src → src}/base/BaseLines.d.ts +0 -0
  88. /package/dist/{orbcharts-plugins-basic/src → src}/base/BaseValueAxis.d.ts +0 -0
  89. /package/dist/{orbcharts-plugins-basic/src → src}/base/types.d.ts +0 -0
  90. /package/dist/{orbcharts-plugins-basic/src → src}/grid/defaults.d.ts +0 -0
  91. /package/dist/{orbcharts-plugins-basic/src → src}/grid/gridObservables.d.ts +0 -0
  92. /package/dist/{orbcharts-plugins-basic/src → src}/grid/index.d.ts +0 -0
  93. /package/dist/{orbcharts-plugins-basic/src → src}/grid/plugins/BarStack.d.ts +0 -0
  94. /package/dist/{orbcharts-plugins-basic/src → src}/grid/plugins/Bars.d.ts +0 -0
  95. /package/dist/{orbcharts-plugins-basic/src → src}/grid/plugins/BarsDiverging.d.ts +0 -0
  96. /package/dist/{orbcharts-plugins-basic/src → src}/grid/plugins/BarsTriangle.d.ts +0 -0
  97. /package/dist/{orbcharts-plugins-basic/src → src}/grid/plugins/Dots.d.ts +0 -0
  98. /package/dist/{orbcharts-plugins-basic/src → src}/grid/plugins/GridLegend.d.ts +0 -0
  99. /package/dist/{orbcharts-plugins-basic/src → src}/grid/plugins/GroupAux.d.ts +0 -0
  100. /package/dist/{orbcharts-plugins-basic/src → src}/grid/plugins/GroupAxis.d.ts +0 -0
  101. /package/dist/{orbcharts-plugins-basic/src → src}/grid/plugins/LineAreas.d.ts +0 -0
  102. /package/dist/{orbcharts-plugins-basic/src → src}/grid/plugins/Lines.d.ts +0 -0
  103. /package/dist/{orbcharts-plugins-basic/src → src}/grid/plugins/Ranking.d.ts +0 -0
  104. /package/dist/{orbcharts-plugins-basic/src → src}/grid/plugins/RankingAxis.d.ts +0 -0
  105. /package/dist/{orbcharts-plugins-basic/src → src}/grid/plugins/ScalingArea.d.ts +0 -0
  106. /package/dist/{orbcharts-plugins-basic/src → src}/grid/plugins/ValueAxis.d.ts +0 -0
  107. /package/dist/{orbcharts-plugins-basic/src → src}/grid/plugins/ValueStackAxis.d.ts +0 -0
  108. /package/dist/{orbcharts-plugins-basic/src → src}/grid/types.d.ts +0 -0
  109. /package/dist/{orbcharts-plugins-basic/src → src}/multiGrid/defaults.d.ts +0 -0
  110. /package/dist/{orbcharts-plugins-basic/src → src}/multiGrid/index.d.ts +0 -0
  111. /package/dist/{orbcharts-plugins-basic/src → src}/multiGrid/multiGridObservables.d.ts +0 -0
  112. /package/dist/{orbcharts-plugins-basic/src → src}/multiGrid/plugins/MultiBarStack.d.ts +0 -0
  113. /package/dist/{orbcharts-plugins-basic/src → src}/multiGrid/plugins/MultiBars.d.ts +0 -0
  114. /package/dist/{orbcharts-plugins-basic/src → src}/multiGrid/plugins/MultiBarsTriangle.d.ts +0 -0
  115. /package/dist/{orbcharts-plugins-basic/src → src}/multiGrid/plugins/MultiDots.d.ts +0 -0
  116. /package/dist/{orbcharts-plugins-basic/src → src}/multiGrid/plugins/MultiGridLegend.d.ts +0 -0
  117. /package/dist/{orbcharts-plugins-basic/src → src}/multiGrid/plugins/MultiGroupAxis.d.ts +0 -0
  118. /package/dist/{orbcharts-plugins-basic/src → src}/multiGrid/plugins/MultiLineAreas.d.ts +0 -0
  119. /package/dist/{orbcharts-plugins-basic/src → src}/multiGrid/plugins/MultiLines.d.ts +0 -0
  120. /package/dist/{orbcharts-plugins-basic/src → src}/multiGrid/plugins/MultiValueAxis.d.ts +0 -0
  121. /package/dist/{orbcharts-plugins-basic/src → src}/multiGrid/plugins/OverlappingValueAxes.d.ts +0 -0
  122. /package/dist/{orbcharts-plugins-basic/src → src}/multiGrid/types.d.ts +0 -0
  123. /package/dist/{orbcharts-plugins-basic/src → src}/multiValue/index.d.ts +0 -0
  124. /package/dist/{orbcharts-plugins-basic/src → src}/multiValue/plugins/Scatter.d.ts +0 -0
  125. /package/dist/{orbcharts-plugins-basic/src → src}/multiValue/plugins/ScatterAxes.d.ts +0 -0
  126. /package/dist/{orbcharts-plugins-basic/src → src}/noneData/defaults.d.ts +0 -0
  127. /package/dist/{orbcharts-plugins-basic/src → src}/noneData/index.d.ts +0 -0
  128. /package/dist/{orbcharts-plugins-basic/src → src}/noneData/plugins/Container.d.ts +0 -0
  129. /package/dist/{orbcharts-plugins-basic/src → src}/noneData/plugins/Tooltip.d.ts +0 -0
  130. /package/dist/{orbcharts-plugins-basic/src → src}/noneData/types.d.ts +0 -0
  131. /package/dist/{orbcharts-plugins-basic/src → src}/relationship/index.d.ts +0 -0
  132. /package/dist/{orbcharts-plugins-basic/src → src}/relationship/plugins/Relationship.d.ts +0 -0
  133. /package/dist/{orbcharts-plugins-basic/src → src}/series/defaults.d.ts +0 -0
  134. /package/dist/{orbcharts-plugins-basic/src → src}/series/index.d.ts +0 -0
  135. /package/dist/{orbcharts-plugins-basic/src → src}/series/plugins/Bubbles.d.ts +0 -0
  136. /package/dist/{orbcharts-plugins-basic/src → src}/series/plugins/Pie.d.ts +0 -0
  137. /package/dist/{orbcharts-plugins-basic/src → src}/series/plugins/PieEventTexts.d.ts +0 -0
  138. /package/dist/{orbcharts-plugins-basic/src → src}/series/plugins/PieLabels.d.ts +0 -0
  139. /package/dist/{orbcharts-plugins-basic/src → src}/series/plugins/SeriesLegend.d.ts +0 -0
  140. /package/dist/{orbcharts-plugins-basic/src → src}/series/plugins/Waffle.d.ts +0 -0
  141. /package/dist/{orbcharts-plugins-basic/src → src}/series/seriesUtils.d.ts +0 -0
  142. /package/dist/{orbcharts-plugins-basic/src → src}/series/types.d.ts +0 -0
  143. /package/dist/{orbcharts-plugins-basic/src → src}/tree/defaults.d.ts +0 -0
  144. /package/dist/{orbcharts-plugins-basic/src → src}/tree/index.d.ts +0 -0
  145. /package/dist/{orbcharts-plugins-basic/src → src}/tree/plugins/TreeLegend.d.ts +0 -0
  146. /package/dist/{orbcharts-plugins-basic/src → src}/tree/plugins/TreeMap.d.ts +0 -0
  147. /package/dist/{orbcharts-plugins-basic/src → src}/tree/types.d.ts +0 -0
  148. /package/dist/{orbcharts-plugins-basic/src → src}/utils/commonUtils.d.ts +0 -0
  149. /package/dist/{orbcharts-plugins-basic/src → src}/utils/d3Graphics.d.ts +0 -0
  150. /package/dist/{orbcharts-plugins-basic/src → src}/utils/d3Utils.d.ts +0 -0
  151. /package/dist/{orbcharts-plugins-basic/src → src}/utils/observables.d.ts +0 -0
  152. /package/dist/{orbcharts-plugins-basic/src → src}/utils/orbchartsUtils.d.ts +0 -0
  153. /package/dist/{orbcharts-plugins-basic/vite.config.d.ts → vite.config.d.ts} +0 -0
@@ -1,626 +1,626 @@
1
- import * as d3 from 'd3'
2
- import {
3
- combineLatest,
4
- map,
5
- filter,
6
- switchMap,
7
- takeUntil,
8
- distinctUntilChanged,
9
- Observable,
10
- Subject } from 'rxjs'
11
- import type { BasePluginFn } from './types'
12
- import type {
13
- ComputedDatumGrid,
14
- ComputedDataGrid,
15
- ComputedLayoutDatumGrid,
16
- ComputedLayoutDataGrid,
17
- DataFormatterGrid,
18
- EventGrid,
19
- GridContainerPosition,
20
- ChartParams,
21
- Layout,
22
- TransformData } from '@orbcharts/core'
23
- import { createAxisLinearScale } from '@orbcharts/core'
24
- import { getD3TransitionEase } from '../utils/d3Utils'
25
- import { getClassName, getUniID, getMinAndMaxValue } from '../utils/orbchartsUtils'
26
- import { gridGroupPositionFnObservable } from '../grid/gridObservables'
27
- import { gridSelectionsObservable } from '../grid/gridObservables'
28
-
29
- export interface BaseLineAreasParams {
30
- lineCurve: string
31
- // lineWidth: number
32
- linearGradientOpacity: [number, number]
33
- }
34
-
35
- interface BaseLineAreasContext {
36
- selection: d3.Selection<any, unknown, any, unknown>
37
- computedData$: Observable<ComputedDataGrid>
38
- computedLayoutData$: Observable<ComputedLayoutDataGrid>
39
- visibleComputedData$: Observable<ComputedDatumGrid[][]>
40
- visibleComputedLayoutData$: Observable<ComputedLayoutDataGrid>
41
- seriesLabels$: Observable<string[]>
42
- SeriesDataMap$: Observable<Map<string, ComputedDatumGrid[]>>
43
- GroupDataMap$: Observable<Map<string, ComputedDatumGrid[]>>
44
- fullDataFormatter$: Observable<DataFormatterGrid>
45
- fullParams$: Observable<BaseLineAreasParams>
46
- fullChartParams$: Observable<ChartParams>
47
- gridAxesTransform$: Observable<TransformData>
48
- gridGraphicTransform$: Observable<TransformData>
49
- gridAxesSize$: Observable<{
50
- width: number;
51
- height: number;
52
- }>
53
- gridHighlight$: Observable<ComputedDatumGrid[]>
54
- gridContainerPosition$: Observable<GridContainerPosition[]>
55
- layout$: Observable<Layout>
56
- event$: Subject<EventGrid>
57
- }
58
-
59
- type ClipPathDatum = {
60
- id: string;
61
- // x: number;
62
- // y: number;
63
- width: number;
64
- height: number;
65
- }
66
-
67
- // const pluginName = 'Lines'
68
- // const pathClassName = getClassName(pluginName, 'path')
69
-
70
-
71
- function createAreaPath (lineCurve: string = 'curveLinear', valueAxisStart: number): d3.Line<ComputedLayoutDatumGrid> {
72
- return d3.area<ComputedLayoutDatumGrid>()
73
- .x((d) => d.axisX)
74
- .y0(d => valueAxisStart)
75
- .y1((d) => d.axisY)
76
- .curve((d3 as any)[lineCurve])
77
-
78
- }
79
-
80
- // 依無值的資料分段
81
- function makeSegmentData (data: ComputedLayoutDatumGrid[]): ComputedLayoutDatumGrid[][] {
82
- let segmentData: ComputedLayoutDatumGrid[][] = [[]]
83
-
84
- let currentIndex = 0
85
- for (let i in data) {
86
- if (data[i].visible == false || data[i].value === undefined || data[i].value === null) {
87
- // 換下一段的 index
88
- if (segmentData[currentIndex].length) {
89
- currentIndex ++
90
- segmentData[currentIndex] = []
91
- }
92
- continue
93
- }
94
- segmentData[currentIndex].push(data[i])
95
- }
96
-
97
- return segmentData
98
- }
99
-
100
-
101
- function renderLineAreas ({ selection, pathClassName, segmentData, areaPath, linearGradientIds, params }: {
102
- selection: d3.Selection<SVGGElement, unknown, any, unknown>
103
- pathClassName: string
104
- segmentData: ComputedLayoutDatumGrid[][]
105
- areaPath: d3.Line<ComputedLayoutDatumGrid>
106
- linearGradientIds: string[]
107
- params: BaseLineAreasParams
108
- }): d3.Selection<SVGPathElement, ComputedLayoutDatumGrid[], any, any> {
109
- // if (!data[0]) {
110
- // return undefined
111
- // }
112
-
113
- const lineAreas = selection
114
- .selectAll<SVGPathElement, ComputedLayoutDatumGrid[]>('path')
115
- .data(segmentData, (d, i) => d.length ? `${d[0].id}_${d[d.length - 1].id}` : i) // 以線段起迄id結合為線段id
116
- .join(
117
- enter => {
118
- return enter
119
- .append<SVGPathElement>('path')
120
- .classed(pathClassName, true)
121
- .attr("fill","none")
122
- // .attr('pointer-events', 'visibleStroke') // 只對線條產生事件
123
- .style('vector-effect', 'non-scaling-stroke')
124
- .style('cursor', 'pointer')
125
- },
126
- update => update,
127
- exit => exit.remove()
128
- )
129
- // .attr("stroke-width", params.lineWidth)
130
- // .attr("stroke", (d, i) => d[0] && d[0].color)
131
- .attr("fill", (d, i) => d[0] ? `url(#${linearGradientIds[d[0].seriesIndex]})` : '')
132
- .attr("d", (d) => {
133
- return areaPath(d)
134
- })
135
-
136
- return lineAreas
137
- }
138
-
139
- function highlightLineAreas ({ selection, seriesLabel, fullChartParams }: {
140
- selection: d3.Selection<any, string, any, any>
141
- seriesLabel: string | null
142
- fullChartParams: ChartParams
143
- }) {
144
- selection.interrupt('highlight')
145
- if (!seriesLabel) {
146
- // remove highlight
147
- selection
148
- .transition('highlight')
149
- .duration(200)
150
- .style('opacity', 1)
151
- return
152
- }
153
-
154
- selection
155
- .each((currentSeriesLabel, i, n) => {
156
- // const currentSeriesLabel = d[0] ? d[0].seriesLabel : ''
157
-
158
- if (currentSeriesLabel === seriesLabel) {
159
- d3.select(n[i])
160
- .style('opacity', 1)
161
- } else {
162
- d3.select(n[i])
163
- .style('opacity', fullChartParams.styles.unhighlightedOpacity)
164
- }
165
- })
166
- }
167
-
168
- function renderLinearGradient ({ defsSelection, computedData, linearGradientIds, params }: {
169
- defsSelection: d3.Selection<SVGDefsElement, any, any, any>
170
- computedData: ComputedDataGrid
171
- linearGradientIds: string[]
172
- params: BaseLineAreasParams
173
- }) {
174
- defsSelection!
175
- .selectAll<SVGLinearGradientElement, ComputedDatumGrid>('linearGradient')
176
- .data(computedData ?? [])
177
- .join(
178
- enter => {
179
- return enter
180
- .append('linearGradient')
181
- .attr('x1', '0%')
182
- .attr('x2', '0%')
183
- .attr('y1', '100%')
184
- .attr('y2', '0%')
185
- .attr('spreadMethod', 'pad')
186
- },
187
- update => update,
188
- exit => exit.remove()
189
- )
190
- .attr('id', (d, i) => {
191
- return d[0] ? linearGradientIds[d[0].seriesIndex] : ''
192
- })
193
- .html((d, i) => {
194
- const color = d[0] ? d[0].color : ''
195
- return `
196
- <stop offset="0%" stop-color="${color}" stop-opacity="${params.linearGradientOpacity[0]}"/>
197
- <stop offset="100%" stop-color="${color}" stop-opacity="${params.linearGradientOpacity[1]}"/>
198
- `
199
- })
200
-
201
- }
202
-
203
- function renderClipPath ({ defsSelection, clipPathData, transitionDuration, transitionEase }: {
204
- defsSelection: d3.Selection<SVGDefsElement, any, any, any>
205
- clipPathData: ClipPathDatum[]
206
- transitionDuration: number
207
- transitionEase: string
208
- }) {
209
- const clipPath = defsSelection
210
- .selectAll<SVGClipPathElement, Layout>('clipPath')
211
- .data(clipPathData)
212
- .join(
213
- enter => {
214
- return enter
215
- .append('clipPath')
216
- },
217
- update => update,
218
- exit => exit.remove()
219
- )
220
- .attr('id', d => d.id)
221
- .each((d, i, g) => {
222
- const rect = d3.select(g[i])
223
- .selectAll<SVGRectElement, typeof d>('rect')
224
- .data([d])
225
- .join(
226
- enter => {
227
- const enterSelection = enter
228
- .append('rect')
229
- enterSelection
230
- .transition()
231
- .duration(transitionDuration)
232
- .ease(getD3TransitionEase(transitionEase))
233
- // .delay(100) // @Q@ 不知為何如果沒加 delay位置會有點跑掉
234
- .tween('tween', (_d, _i, _g) => {
235
- return (t) => {
236
- const transitionWidth = _d.width * t
237
-
238
- enterSelection
239
- .attr('x', 0)
240
- .attr('y', 0)
241
- .attr('width', _d => transitionWidth)
242
- .attr('height', _d => _d.height)
243
- }
244
- })
245
- return enterSelection
246
- },
247
- update => {
248
- return update
249
- .attr('x', 0)
250
- .attr('y', 0)
251
- .attr('width', _d => _d.width)
252
- .attr('height', _d => _d.height)
253
- },
254
- exit => exit.remove()
255
- )
256
- })
257
-
258
- }
259
-
260
- export const createBaseLineAreas: BasePluginFn<BaseLineAreasContext> = (pluginName: string, {
261
- selection,
262
- computedData$,
263
- computedLayoutData$,
264
- visibleComputedData$,
265
- visibleComputedLayoutData$,
266
- seriesLabels$,
267
- SeriesDataMap$,
268
- GroupDataMap$,
269
- fullParams$,
270
- fullDataFormatter$,
271
- fullChartParams$,
272
- gridAxesTransform$,
273
- gridGraphicTransform$,
274
- gridAxesSize$,
275
- gridHighlight$,
276
- gridContainerPosition$,
277
- layout$,
278
- event$
279
- }) => {
280
-
281
- const destroy$ = new Subject()
282
-
283
- const clipPathID = getUniID(pluginName, 'clipPath-box')
284
- const pathClassName = getClassName(pluginName, 'path')
285
-
286
- const {
287
- seriesSelection$,
288
- axesSelection$,
289
- defsSelection$,
290
- graphicGSelection$
291
- } = gridSelectionsObservable({
292
- selection,
293
- pluginName,
294
- clipPathID,
295
- seriesLabels$,
296
- gridContainerPosition$,
297
- gridAxesTransform$,
298
- gridGraphicTransform$
299
- })
300
-
301
- // valueAxis 的起始座標
302
- const valueAxisStart$: Observable<number> = gridGraphicTransform$.pipe(
303
- takeUntil(destroy$),
304
- map(data => {
305
- // 抵消掉外層的變型
306
- return - data.translate[1] / data.scale[1]
307
- })
308
- )
309
-
310
- const areaPath$: Observable<d3.Line<ComputedLayoutDatumGrid>> = new Observable(subscriber => {
311
- const paramsSubscription = combineLatest({
312
- fullParams: fullParams$,
313
- valueAxisStart: valueAxisStart$
314
- }).pipe(
315
- takeUntil(destroy$)
316
- )
317
- .subscribe(d => {
318
- const areaPath = createAreaPath(d.fullParams.lineCurve, d.valueAxisStart)
319
- subscriber.next(areaPath)
320
- })
321
- return () => {
322
- paramsSubscription.unsubscribe()
323
- }
324
- })
325
-
326
- // // 顯示範圍內的series labels
327
- // const seriesLabels$: Observable<string[]> = new Observable(subscriber => {
328
- // computedData$.pipe(
329
- // takeUntil(destroy$),
330
- // switchMap(async (d) => d),
331
- // ).subscribe(data => {
332
- // const labels = data[0] && data[0][0]
333
- // ? data.map(d => d[0].seriesLabel)
334
- // : []
335
- // subscriber.next(labels)
336
- // })
337
- // })
338
-
339
- // const axisSize$ = gridAxisSizeObservable({
340
- // fullDataFormatter$,
341
- // computedLayout$
342
- // })
343
-
344
- const transitionDuration$ = fullChartParams$
345
- .pipe(
346
- map(d => d.transitionDuration),
347
- distinctUntilChanged()
348
- )
349
-
350
- const transitionEase$ = fullChartParams$
351
- .pipe(
352
- map(d => d.transitionEase),
353
- distinctUntilChanged()
354
- )
355
-
356
- const clipPathSubscription = combineLatest({
357
- defsSelection: defsSelection$,
358
- seriesLabels: seriesLabels$,
359
- axisSize: gridAxesSize$,
360
- transitionDuration: transitionDuration$,
361
- transitionEase: transitionEase$
362
- }).pipe(
363
- takeUntil(destroy$),
364
- switchMap(async (d) => d),
365
- ).subscribe(data => {
366
- // 外層的遮罩
367
- const clipPathBox = [{
368
- id: clipPathID,
369
- width: data.axisSize.width,
370
- height: data.axisSize.height
371
- }]
372
- // 各別線條的遮罩(各別動畫)
373
- const clipPathData = clipPathBox.concat(
374
- data.seriesLabels.map(d => {
375
- return {
376
- id: `orbcharts__clipPath_${d}`,
377
- width: data.axisSize.width,
378
- height: data.axisSize.height
379
- }
380
- })
381
- )
382
- renderClipPath({
383
- defsSelection: data.defsSelection,
384
- clipPathData,
385
- transitionDuration: data.transitionDuration,
386
- transitionEase: data.transitionEase
387
- })
388
- })
389
-
390
- // const SeriesDataMap$ = computedData$.pipe(
391
- // map(d => makeGridSeriesDataMap(d))
392
- // )
393
-
394
- // const GroupDataMap$ = computedData$.pipe(
395
- // map(d => makeGridGroupDataMap(d))
396
- // )
397
-
398
- const DataMap$ = computedData$.pipe(
399
- map(d => {
400
- const DataMap: Map<string, ComputedDatumGrid> = new Map()
401
- d.flat().forEach(_d => DataMap.set(_d.id, _d))
402
- return DataMap
403
- })
404
- )
405
-
406
- // 取得事件座標的group資料
407
- const gridGroupPositionFn$ = gridGroupPositionFnObservable({
408
- fullDataFormatter$,
409
- gridAxesSize$: gridAxesSize$,
410
- computedData$: computedData$,
411
- fullChartParams$: fullChartParams$
412
- })
413
-
414
- const highlightTarget$ = fullChartParams$.pipe(
415
- takeUntil(destroy$),
416
- map(d => d.highlightTarget),
417
- distinctUntilChanged()
418
- )
419
-
420
- const linearGradientIds$ = seriesLabels$.pipe(
421
- takeUntil(destroy$),
422
- map(d => d.map((d, i) => {
423
- return getUniID(pluginName, `lineargradient-${d}`)
424
- }))
425
- )
426
-
427
- const pathSelectionArr$ = combineLatest({
428
- graphicGSelection: graphicGSelection$,
429
- defsSelection: defsSelection$,
430
- visibleComputedLayoutData: visibleComputedLayoutData$,
431
- linearGradientIds: linearGradientIds$,
432
- areaPath: areaPath$,
433
- params: fullParams$,
434
- }).pipe(
435
- takeUntil(destroy$),
436
- switchMap(async (d) => d),
437
- map(data => {
438
- // const updateGraphic = data.graphicGSelection
439
- // .selectAll<SVGGElement, number>('g')
440
- // .data(data.seriesLabels, (d, i) => d)
441
- // const enterGraphic = updateGraphic.enter()
442
- // .append('g')
443
- // .classed(graphicClassName, true)
444
- // updateGraphic.exit().remove()
445
- // const graphicSelection = updateGraphic.merge(enterGraphic)
446
- // .attr('clip-path', (d, i) => `url(#orbcharts__clipPath_${d})`)
447
- let pathSelectionArr: d3.Selection<SVGPathElement, ComputedLayoutDatumGrid[], any, any>[] = []
448
-
449
- // 繪圖
450
- data.graphicGSelection.each((d, i, all) => {
451
- // 將資料分段
452
- const segmentData = makeSegmentData(data.visibleComputedLayoutData[i] ?? [])
453
-
454
- pathSelectionArr[i] = renderLineAreas({
455
- selection: d3.select(all[i]),
456
- pathClassName,
457
- areaPath: data.areaPath,
458
- segmentData: segmentData,
459
- linearGradientIds: data.linearGradientIds,
460
- params: data.params
461
- })
462
- renderLinearGradient({
463
- defsSelection: data.defsSelection,
464
- computedData: data.visibleComputedLayoutData,
465
- linearGradientIds: data.linearGradientIds,
466
- params: data.params
467
- })
468
- })
469
-
470
- return pathSelectionArr
471
- })
472
- )
473
-
474
- combineLatest({
475
- pathSelectionArr: pathSelectionArr$,
476
- computedData: computedData$,
477
- SeriesDataMap: SeriesDataMap$,
478
- GroupDataMap: GroupDataMap$,
479
- highlightTarget: highlightTarget$,
480
- gridGroupPositionFn: gridGroupPositionFn$,
481
- }).pipe(
482
- takeUntil(destroy$),
483
- switchMap(async (d) => d),
484
- ).subscribe(data => {
485
- data.pathSelectionArr.forEach(pathSelection => {
486
- pathSelection
487
- .on('mouseover', (event, datum) => {
488
- event.stopPropagation()
489
-
490
- const seriesLabel = datum[0] ? datum[0].seriesLabel : ''
491
- const { groupIndex, groupLabel } = data.gridGroupPositionFn(event)
492
- const groupData = data.GroupDataMap.get(groupLabel)!
493
- const targetDatum = groupData.find(d => d.seriesLabel === seriesLabel)
494
- const _datum = targetDatum ?? datum[0]
495
-
496
- event$.next({
497
- type: 'grid',
498
- eventName: 'mouseover',
499
- pluginName,
500
- highlightTarget: data.highlightTarget,
501
- datum: _datum,
502
- gridIndex: _datum.gridIndex,
503
- series: data.SeriesDataMap.get(_datum.seriesLabel)!,
504
- seriesIndex: _datum.seriesIndex,
505
- seriesLabel: _datum.seriesLabel,
506
- groups: data.GroupDataMap.get(_datum.groupLabel)!,
507
- groupIndex: _datum.groupIndex,
508
- groupLabel: _datum.groupLabel,
509
- event,
510
- data: data.computedData
511
- })
512
- })
513
- .on('mousemove', (event, datum) => {
514
- event.stopPropagation()
515
-
516
- const seriesLabel = datum[0] ? datum[0].seriesLabel : ''
517
- const { groupIndex, groupLabel } = data.gridGroupPositionFn(event)
518
- const groupData = data.GroupDataMap.get(groupLabel)!
519
- const targetDatum = groupData.find(d => d.seriesLabel === seriesLabel)
520
- const _datum = targetDatum ?? datum[0]
521
-
522
- event$.next({
523
- type: 'grid',
524
- eventName: 'mousemove',
525
- pluginName,
526
- highlightTarget: data.highlightTarget,
527
- datum: _datum,
528
- gridIndex: _datum.gridIndex,
529
- series: data.SeriesDataMap.get(_datum.seriesLabel)!,
530
- seriesIndex: _datum.seriesIndex,
531
- seriesLabel: _datum.seriesLabel,
532
- groups: data.GroupDataMap.get(_datum.groupLabel)!,
533
- groupIndex: _datum.groupIndex,
534
- groupLabel: _datum.groupLabel,
535
- event,
536
- data: data.computedData
537
- })
538
- })
539
- .on('mouseout', (event, datum) => {
540
- event.stopPropagation()
541
-
542
- const seriesLabel = datum[0] ? datum[0].seriesLabel : ''
543
- const { groupIndex, groupLabel } = data.gridGroupPositionFn(event)
544
- const groupData = data.GroupDataMap.get(groupLabel)!
545
- const targetDatum = groupData.find(d => d.seriesLabel === seriesLabel)
546
- const _datum = targetDatum ?? datum[0]
547
-
548
- event$.next({
549
- type: 'grid',
550
- eventName: 'mouseout',
551
- pluginName,
552
- highlightTarget: data.highlightTarget,
553
- datum: _datum,
554
- gridIndex: _datum.gridIndex,
555
- series: data.SeriesDataMap.get(_datum.seriesLabel)!,
556
- seriesIndex: _datum.seriesIndex,
557
- seriesLabel: _datum.seriesLabel,
558
- groups: data.GroupDataMap.get(_datum.groupLabel)!,
559
- groupIndex: _datum.groupIndex,
560
- groupLabel: _datum.groupLabel,
561
- event,
562
- data: data.computedData
563
- })
564
- })
565
- .on('click', (event, datum) => {
566
- event.stopPropagation()
567
-
568
- const seriesLabel = datum[0] ? datum[0].seriesLabel : ''
569
- const { groupIndex, groupLabel } = data.gridGroupPositionFn(event)
570
- const groupData = data.GroupDataMap.get(groupLabel)!
571
- const targetDatum = groupData.find(d => d.seriesLabel === seriesLabel)
572
- const _datum = targetDatum ?? datum[0]
573
-
574
- event$.next({
575
- type: 'grid',
576
- eventName: 'click',
577
- pluginName,
578
- highlightTarget: data.highlightTarget,
579
- datum: _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
- })
592
- })
593
-
594
- // const datumList$ = computedData$.pipe(
595
- // takeUntil(destroy$),
596
- // map(d => d.flat())
597
- // )
598
- // const highlight$ = highlightObservable({ datumList$, fullChartParams$, event$: store.event$ })
599
- // const highlightSubscription = gridHighlight$.subscribe()
600
-
601
- fullChartParams$.pipe(
602
- takeUntil(destroy$),
603
- filter(d => d.highlightTarget === 'series'),
604
- switchMap(d => combineLatest({
605
- graphicGSelection: graphicGSelection$,
606
- gridHighlight: gridHighlight$,
607
- DataMap: DataMap$,
608
- fullChartParams: fullChartParams$
609
- }).pipe(
610
- takeUntil(destroy$),
611
- switchMap(async d => d)
612
- ))
613
- ).subscribe(data => {
614
- const seriesLabel = data.gridHighlight[0] ? data.gridHighlight[0].seriesLabel : null
615
- highlightLineAreas({
616
- selection: data.graphicGSelection,
617
- seriesLabel,
618
- fullChartParams: data.fullChartParams
619
- })
620
- })
621
-
622
- return () => {
623
- destroy$.next(undefined)
624
- // highlightSubscription.unsubscribe()
625
- }
1
+ import * as d3 from 'd3'
2
+ import {
3
+ combineLatest,
4
+ map,
5
+ filter,
6
+ switchMap,
7
+ takeUntil,
8
+ distinctUntilChanged,
9
+ Observable,
10
+ Subject } from 'rxjs'
11
+ import type { BasePluginFn } from './types'
12
+ import type {
13
+ ComputedDatumGrid,
14
+ ComputedDataGrid,
15
+ ComputedLayoutDatumGrid,
16
+ ComputedLayoutDataGrid,
17
+ DataFormatterGrid,
18
+ EventGrid,
19
+ GridContainerPosition,
20
+ ChartParams,
21
+ Layout,
22
+ TransformData } from '@orbcharts/core'
23
+ import { createAxisLinearScale } from '@orbcharts/core'
24
+ import { getD3TransitionEase } from '../utils/d3Utils'
25
+ import { getClassName, getUniID, getMinAndMaxValue } from '../utils/orbchartsUtils'
26
+ import { gridGroupPositionFnObservable } from '../grid/gridObservables'
27
+ import { gridSelectionsObservable } from '../grid/gridObservables'
28
+
29
+ export interface BaseLineAreasParams {
30
+ lineCurve: string
31
+ // lineWidth: number
32
+ linearGradientOpacity: [number, number]
33
+ }
34
+
35
+ interface BaseLineAreasContext {
36
+ selection: d3.Selection<any, unknown, any, unknown>
37
+ computedData$: Observable<ComputedDataGrid>
38
+ computedLayoutData$: Observable<ComputedLayoutDataGrid>
39
+ visibleComputedData$: Observable<ComputedDatumGrid[][]>
40
+ visibleComputedLayoutData$: Observable<ComputedLayoutDataGrid>
41
+ seriesLabels$: Observable<string[]>
42
+ SeriesDataMap$: Observable<Map<string, ComputedDatumGrid[]>>
43
+ GroupDataMap$: Observable<Map<string, ComputedDatumGrid[]>>
44
+ fullDataFormatter$: Observable<DataFormatterGrid>
45
+ fullParams$: Observable<BaseLineAreasParams>
46
+ fullChartParams$: Observable<ChartParams>
47
+ gridAxesTransform$: Observable<TransformData>
48
+ gridGraphicTransform$: Observable<TransformData>
49
+ gridAxesSize$: Observable<{
50
+ width: number;
51
+ height: number;
52
+ }>
53
+ gridHighlight$: Observable<ComputedDatumGrid[]>
54
+ gridContainerPosition$: Observable<GridContainerPosition[]>
55
+ layout$: Observable<Layout>
56
+ event$: Subject<EventGrid>
57
+ }
58
+
59
+ type ClipPathDatum = {
60
+ id: string;
61
+ // x: number;
62
+ // y: number;
63
+ width: number;
64
+ height: number;
65
+ }
66
+
67
+ // const pluginName = 'Lines'
68
+ // const pathClassName = getClassName(pluginName, 'path')
69
+
70
+
71
+ function createAreaPath (lineCurve: string = 'curveLinear', valueAxisStart: number): d3.Line<ComputedLayoutDatumGrid> {
72
+ return d3.area<ComputedLayoutDatumGrid>()
73
+ .x((d) => d.axisX)
74
+ .y0(d => valueAxisStart)
75
+ .y1((d) => d.axisY)
76
+ .curve((d3 as any)[lineCurve])
77
+
78
+ }
79
+
80
+ // 依無值的資料分段
81
+ function makeSegmentData (data: ComputedLayoutDatumGrid[]): ComputedLayoutDatumGrid[][] {
82
+ let segmentData: ComputedLayoutDatumGrid[][] = [[]]
83
+
84
+ let currentIndex = 0
85
+ for (let i in data) {
86
+ if (data[i].visible == false || data[i].value === undefined || data[i].value === null) {
87
+ // 換下一段的 index
88
+ if (segmentData[currentIndex].length) {
89
+ currentIndex ++
90
+ segmentData[currentIndex] = []
91
+ }
92
+ continue
93
+ }
94
+ segmentData[currentIndex].push(data[i])
95
+ }
96
+
97
+ return segmentData
98
+ }
99
+
100
+
101
+ function renderLineAreas ({ selection, pathClassName, segmentData, areaPath, linearGradientIds, params }: {
102
+ selection: d3.Selection<SVGGElement, unknown, any, unknown>
103
+ pathClassName: string
104
+ segmentData: ComputedLayoutDatumGrid[][]
105
+ areaPath: d3.Line<ComputedLayoutDatumGrid>
106
+ linearGradientIds: string[]
107
+ params: BaseLineAreasParams
108
+ }): d3.Selection<SVGPathElement, ComputedLayoutDatumGrid[], any, any> {
109
+ // if (!data[0]) {
110
+ // return undefined
111
+ // }
112
+
113
+ const lineAreas = selection
114
+ .selectAll<SVGPathElement, ComputedLayoutDatumGrid[]>('path')
115
+ .data(segmentData, (d, i) => d.length ? `${d[0].id}_${d[d.length - 1].id}` : i) // 以線段起迄id結合為線段id
116
+ .join(
117
+ enter => {
118
+ return enter
119
+ .append<SVGPathElement>('path')
120
+ .classed(pathClassName, true)
121
+ .attr("fill","none")
122
+ // .attr('pointer-events', 'visibleStroke') // 只對線條產生事件
123
+ .style('vector-effect', 'non-scaling-stroke')
124
+ .style('cursor', 'pointer')
125
+ },
126
+ update => update,
127
+ exit => exit.remove()
128
+ )
129
+ // .attr("stroke-width", params.lineWidth)
130
+ // .attr("stroke", (d, i) => d[0] && d[0].color)
131
+ .attr("fill", (d, i) => d[0] ? `url(#${linearGradientIds[d[0].seriesIndex]})` : '')
132
+ .attr("d", (d) => {
133
+ return areaPath(d)
134
+ })
135
+
136
+ return lineAreas
137
+ }
138
+
139
+ function highlightLineAreas ({ selection, seriesLabel, fullChartParams }: {
140
+ selection: d3.Selection<any, string, any, any>
141
+ seriesLabel: string | null
142
+ fullChartParams: ChartParams
143
+ }) {
144
+ selection.interrupt('highlight')
145
+ if (!seriesLabel) {
146
+ // remove highlight
147
+ selection
148
+ .transition('highlight')
149
+ .duration(200)
150
+ .style('opacity', 1)
151
+ return
152
+ }
153
+
154
+ selection
155
+ .each((currentSeriesLabel, i, n) => {
156
+ // const currentSeriesLabel = d[0] ? d[0].seriesLabel : ''
157
+
158
+ if (currentSeriesLabel === seriesLabel) {
159
+ d3.select(n[i])
160
+ .style('opacity', 1)
161
+ } else {
162
+ d3.select(n[i])
163
+ .style('opacity', fullChartParams.styles.unhighlightedOpacity)
164
+ }
165
+ })
166
+ }
167
+
168
+ function renderLinearGradient ({ defsSelection, computedData, linearGradientIds, params }: {
169
+ defsSelection: d3.Selection<SVGDefsElement, any, any, any>
170
+ computedData: ComputedDataGrid
171
+ linearGradientIds: string[]
172
+ params: BaseLineAreasParams
173
+ }) {
174
+ defsSelection!
175
+ .selectAll<SVGLinearGradientElement, ComputedDatumGrid>('linearGradient')
176
+ .data(computedData ?? [])
177
+ .join(
178
+ enter => {
179
+ return enter
180
+ .append('linearGradient')
181
+ .attr('x1', '0%')
182
+ .attr('x2', '0%')
183
+ .attr('y1', '100%')
184
+ .attr('y2', '0%')
185
+ .attr('spreadMethod', 'pad')
186
+ },
187
+ update => update,
188
+ exit => exit.remove()
189
+ )
190
+ .attr('id', (d, i) => {
191
+ return d[0] ? linearGradientIds[d[0].seriesIndex] : ''
192
+ })
193
+ .html((d, i) => {
194
+ const color = d[0] ? d[0].color : ''
195
+ return `
196
+ <stop offset="0%" stop-color="${color}" stop-opacity="${params.linearGradientOpacity[0]}"/>
197
+ <stop offset="100%" stop-color="${color}" stop-opacity="${params.linearGradientOpacity[1]}"/>
198
+ `
199
+ })
200
+
201
+ }
202
+
203
+ function renderClipPath ({ defsSelection, clipPathData, transitionDuration, transitionEase }: {
204
+ defsSelection: d3.Selection<SVGDefsElement, any, any, any>
205
+ clipPathData: ClipPathDatum[]
206
+ transitionDuration: number
207
+ transitionEase: string
208
+ }) {
209
+ const clipPath = defsSelection
210
+ .selectAll<SVGClipPathElement, Layout>('clipPath')
211
+ .data(clipPathData)
212
+ .join(
213
+ enter => {
214
+ return enter
215
+ .append('clipPath')
216
+ },
217
+ update => update,
218
+ exit => exit.remove()
219
+ )
220
+ .attr('id', d => d.id)
221
+ .each((d, i, g) => {
222
+ const rect = d3.select(g[i])
223
+ .selectAll<SVGRectElement, typeof d>('rect')
224
+ .data([d])
225
+ .join(
226
+ enter => {
227
+ const enterSelection = enter
228
+ .append('rect')
229
+ enterSelection
230
+ .transition()
231
+ .duration(transitionDuration)
232
+ .ease(getD3TransitionEase(transitionEase))
233
+ // .delay(100) // @Q@ 不知為何如果沒加 delay位置會有點跑掉
234
+ .tween('tween', (_d, _i, _g) => {
235
+ return (t) => {
236
+ const transitionWidth = _d.width * t
237
+
238
+ enterSelection
239
+ .attr('x', 0)
240
+ .attr('y', 0)
241
+ .attr('width', _d => transitionWidth)
242
+ .attr('height', _d => _d.height)
243
+ }
244
+ })
245
+ return enterSelection
246
+ },
247
+ update => {
248
+ return update
249
+ .attr('x', 0)
250
+ .attr('y', 0)
251
+ .attr('width', _d => _d.width)
252
+ .attr('height', _d => _d.height)
253
+ },
254
+ exit => exit.remove()
255
+ )
256
+ })
257
+
258
+ }
259
+
260
+ export const createBaseLineAreas: BasePluginFn<BaseLineAreasContext> = (pluginName: string, {
261
+ selection,
262
+ computedData$,
263
+ computedLayoutData$,
264
+ visibleComputedData$,
265
+ visibleComputedLayoutData$,
266
+ seriesLabels$,
267
+ SeriesDataMap$,
268
+ GroupDataMap$,
269
+ fullParams$,
270
+ fullDataFormatter$,
271
+ fullChartParams$,
272
+ gridAxesTransform$,
273
+ gridGraphicTransform$,
274
+ gridAxesSize$,
275
+ gridHighlight$,
276
+ gridContainerPosition$,
277
+ layout$,
278
+ event$
279
+ }) => {
280
+
281
+ const destroy$ = new Subject()
282
+
283
+ const clipPathID = getUniID(pluginName, 'clipPath-box')
284
+ const pathClassName = getClassName(pluginName, 'path')
285
+
286
+ const {
287
+ seriesSelection$,
288
+ axesSelection$,
289
+ defsSelection$,
290
+ graphicGSelection$
291
+ } = gridSelectionsObservable({
292
+ selection,
293
+ pluginName,
294
+ clipPathID,
295
+ seriesLabels$,
296
+ gridContainerPosition$,
297
+ gridAxesTransform$,
298
+ gridGraphicTransform$
299
+ })
300
+
301
+ // valueAxis 的起始座標
302
+ const valueAxisStart$: Observable<number> = gridGraphicTransform$.pipe(
303
+ takeUntil(destroy$),
304
+ map(data => {
305
+ // 抵消掉外層的變型
306
+ return - data.translate[1] / data.scale[1]
307
+ })
308
+ )
309
+
310
+ const areaPath$: Observable<d3.Line<ComputedLayoutDatumGrid>> = new Observable(subscriber => {
311
+ const paramsSubscription = combineLatest({
312
+ fullParams: fullParams$,
313
+ valueAxisStart: valueAxisStart$
314
+ }).pipe(
315
+ takeUntil(destroy$)
316
+ )
317
+ .subscribe(d => {
318
+ const areaPath = createAreaPath(d.fullParams.lineCurve, d.valueAxisStart)
319
+ subscriber.next(areaPath)
320
+ })
321
+ return () => {
322
+ paramsSubscription.unsubscribe()
323
+ }
324
+ })
325
+
326
+ // // 顯示範圍內的series labels
327
+ // const seriesLabels$: Observable<string[]> = new Observable(subscriber => {
328
+ // computedData$.pipe(
329
+ // takeUntil(destroy$),
330
+ // switchMap(async (d) => d),
331
+ // ).subscribe(data => {
332
+ // const labels = data[0] && data[0][0]
333
+ // ? data.map(d => d[0].seriesLabel)
334
+ // : []
335
+ // subscriber.next(labels)
336
+ // })
337
+ // })
338
+
339
+ // const axisSize$ = gridAxisSizeObservable({
340
+ // fullDataFormatter$,
341
+ // computedLayout$
342
+ // })
343
+
344
+ const transitionDuration$ = fullChartParams$
345
+ .pipe(
346
+ map(d => d.transitionDuration),
347
+ distinctUntilChanged()
348
+ )
349
+
350
+ const transitionEase$ = fullChartParams$
351
+ .pipe(
352
+ map(d => d.transitionEase),
353
+ distinctUntilChanged()
354
+ )
355
+
356
+ const clipPathSubscription = combineLatest({
357
+ defsSelection: defsSelection$,
358
+ seriesLabels: seriesLabels$,
359
+ axisSize: gridAxesSize$,
360
+ transitionDuration: transitionDuration$,
361
+ transitionEase: transitionEase$
362
+ }).pipe(
363
+ takeUntil(destroy$),
364
+ switchMap(async (d) => d),
365
+ ).subscribe(data => {
366
+ // 外層的遮罩
367
+ const clipPathBox = [{
368
+ id: clipPathID,
369
+ width: data.axisSize.width,
370
+ height: data.axisSize.height
371
+ }]
372
+ // 各別線條的遮罩(各別動畫)
373
+ const clipPathData = clipPathBox.concat(
374
+ data.seriesLabels.map(d => {
375
+ return {
376
+ id: `orbcharts__clipPath_${d}`,
377
+ width: data.axisSize.width,
378
+ height: data.axisSize.height
379
+ }
380
+ })
381
+ )
382
+ renderClipPath({
383
+ defsSelection: data.defsSelection,
384
+ clipPathData,
385
+ transitionDuration: data.transitionDuration,
386
+ transitionEase: data.transitionEase
387
+ })
388
+ })
389
+
390
+ // const SeriesDataMap$ = computedData$.pipe(
391
+ // map(d => makeGridSeriesDataMap(d))
392
+ // )
393
+
394
+ // const GroupDataMap$ = computedData$.pipe(
395
+ // map(d => makeGridGroupDataMap(d))
396
+ // )
397
+
398
+ const DataMap$ = computedData$.pipe(
399
+ map(d => {
400
+ const DataMap: Map<string, ComputedDatumGrid> = new Map()
401
+ d.flat().forEach(_d => DataMap.set(_d.id, _d))
402
+ return DataMap
403
+ })
404
+ )
405
+
406
+ // 取得事件座標的group資料
407
+ const gridGroupPositionFn$ = gridGroupPositionFnObservable({
408
+ fullDataFormatter$,
409
+ gridAxesSize$: gridAxesSize$,
410
+ computedData$: computedData$,
411
+ fullChartParams$: fullChartParams$
412
+ })
413
+
414
+ const highlightTarget$ = fullChartParams$.pipe(
415
+ takeUntil(destroy$),
416
+ map(d => d.highlightTarget),
417
+ distinctUntilChanged()
418
+ )
419
+
420
+ const linearGradientIds$ = seriesLabels$.pipe(
421
+ takeUntil(destroy$),
422
+ map(d => d.map((d, i) => {
423
+ return getUniID(pluginName, `lineargradient-${d}`)
424
+ }))
425
+ )
426
+
427
+ const pathSelectionArr$ = combineLatest({
428
+ graphicGSelection: graphicGSelection$,
429
+ defsSelection: defsSelection$,
430
+ visibleComputedLayoutData: visibleComputedLayoutData$,
431
+ linearGradientIds: linearGradientIds$,
432
+ areaPath: areaPath$,
433
+ params: fullParams$,
434
+ }).pipe(
435
+ takeUntil(destroy$),
436
+ switchMap(async (d) => d),
437
+ map(data => {
438
+ // const updateGraphic = data.graphicGSelection
439
+ // .selectAll<SVGGElement, number>('g')
440
+ // .data(data.seriesLabels, (d, i) => d)
441
+ // const enterGraphic = updateGraphic.enter()
442
+ // .append('g')
443
+ // .classed(graphicClassName, true)
444
+ // updateGraphic.exit().remove()
445
+ // const graphicSelection = updateGraphic.merge(enterGraphic)
446
+ // .attr('clip-path', (d, i) => `url(#orbcharts__clipPath_${d})`)
447
+ let pathSelectionArr: d3.Selection<SVGPathElement, ComputedLayoutDatumGrid[], any, any>[] = []
448
+
449
+ // 繪圖
450
+ data.graphicGSelection.each((d, i, all) => {
451
+ // 將資料分段
452
+ const segmentData = makeSegmentData(data.visibleComputedLayoutData[i] ?? [])
453
+
454
+ pathSelectionArr[i] = renderLineAreas({
455
+ selection: d3.select(all[i]),
456
+ pathClassName,
457
+ areaPath: data.areaPath,
458
+ segmentData: segmentData,
459
+ linearGradientIds: data.linearGradientIds,
460
+ params: data.params
461
+ })
462
+ renderLinearGradient({
463
+ defsSelection: data.defsSelection,
464
+ computedData: data.visibleComputedLayoutData,
465
+ linearGradientIds: data.linearGradientIds,
466
+ params: data.params
467
+ })
468
+ })
469
+
470
+ return pathSelectionArr
471
+ })
472
+ )
473
+
474
+ combineLatest({
475
+ pathSelectionArr: pathSelectionArr$,
476
+ computedData: computedData$,
477
+ SeriesDataMap: SeriesDataMap$,
478
+ GroupDataMap: GroupDataMap$,
479
+ highlightTarget: highlightTarget$,
480
+ gridGroupPositionFn: gridGroupPositionFn$,
481
+ }).pipe(
482
+ takeUntil(destroy$),
483
+ switchMap(async (d) => d),
484
+ ).subscribe(data => {
485
+ data.pathSelectionArr.forEach(pathSelection => {
486
+ pathSelection
487
+ .on('mouseover', (event, datum) => {
488
+ event.stopPropagation()
489
+
490
+ const seriesLabel = datum[0] ? datum[0].seriesLabel : ''
491
+ const { groupIndex, groupLabel } = data.gridGroupPositionFn(event)
492
+ const groupData = data.GroupDataMap.get(groupLabel)!
493
+ const targetDatum = groupData.find(d => d.seriesLabel === seriesLabel)
494
+ const _datum = targetDatum ?? datum[0]
495
+
496
+ event$.next({
497
+ type: 'grid',
498
+ eventName: 'mouseover',
499
+ pluginName,
500
+ highlightTarget: data.highlightTarget,
501
+ datum: _datum,
502
+ gridIndex: _datum.gridIndex,
503
+ series: data.SeriesDataMap.get(_datum.seriesLabel)!,
504
+ seriesIndex: _datum.seriesIndex,
505
+ seriesLabel: _datum.seriesLabel,
506
+ groups: data.GroupDataMap.get(_datum.groupLabel)!,
507
+ groupIndex: _datum.groupIndex,
508
+ groupLabel: _datum.groupLabel,
509
+ event,
510
+ data: data.computedData
511
+ })
512
+ })
513
+ .on('mousemove', (event, datum) => {
514
+ event.stopPropagation()
515
+
516
+ const seriesLabel = datum[0] ? datum[0].seriesLabel : ''
517
+ const { groupIndex, groupLabel } = data.gridGroupPositionFn(event)
518
+ const groupData = data.GroupDataMap.get(groupLabel)!
519
+ const targetDatum = groupData.find(d => d.seriesLabel === seriesLabel)
520
+ const _datum = targetDatum ?? datum[0]
521
+
522
+ event$.next({
523
+ type: 'grid',
524
+ eventName: 'mousemove',
525
+ pluginName,
526
+ highlightTarget: data.highlightTarget,
527
+ datum: _datum,
528
+ gridIndex: _datum.gridIndex,
529
+ series: data.SeriesDataMap.get(_datum.seriesLabel)!,
530
+ seriesIndex: _datum.seriesIndex,
531
+ seriesLabel: _datum.seriesLabel,
532
+ groups: data.GroupDataMap.get(_datum.groupLabel)!,
533
+ groupIndex: _datum.groupIndex,
534
+ groupLabel: _datum.groupLabel,
535
+ event,
536
+ data: data.computedData
537
+ })
538
+ })
539
+ .on('mouseout', (event, datum) => {
540
+ event.stopPropagation()
541
+
542
+ const seriesLabel = datum[0] ? datum[0].seriesLabel : ''
543
+ const { groupIndex, groupLabel } = data.gridGroupPositionFn(event)
544
+ const groupData = data.GroupDataMap.get(groupLabel)!
545
+ const targetDatum = groupData.find(d => d.seriesLabel === seriesLabel)
546
+ const _datum = targetDatum ?? datum[0]
547
+
548
+ event$.next({
549
+ type: 'grid',
550
+ eventName: 'mouseout',
551
+ pluginName,
552
+ highlightTarget: data.highlightTarget,
553
+ datum: _datum,
554
+ gridIndex: _datum.gridIndex,
555
+ series: data.SeriesDataMap.get(_datum.seriesLabel)!,
556
+ seriesIndex: _datum.seriesIndex,
557
+ seriesLabel: _datum.seriesLabel,
558
+ groups: data.GroupDataMap.get(_datum.groupLabel)!,
559
+ groupIndex: _datum.groupIndex,
560
+ groupLabel: _datum.groupLabel,
561
+ event,
562
+ data: data.computedData
563
+ })
564
+ })
565
+ .on('click', (event, datum) => {
566
+ event.stopPropagation()
567
+
568
+ const seriesLabel = datum[0] ? datum[0].seriesLabel : ''
569
+ const { groupIndex, groupLabel } = data.gridGroupPositionFn(event)
570
+ const groupData = data.GroupDataMap.get(groupLabel)!
571
+ const targetDatum = groupData.find(d => d.seriesLabel === seriesLabel)
572
+ const _datum = targetDatum ?? datum[0]
573
+
574
+ event$.next({
575
+ type: 'grid',
576
+ eventName: 'click',
577
+ pluginName,
578
+ highlightTarget: data.highlightTarget,
579
+ datum: _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
+ })
592
+ })
593
+
594
+ // const datumList$ = computedData$.pipe(
595
+ // takeUntil(destroy$),
596
+ // map(d => d.flat())
597
+ // )
598
+ // const highlight$ = highlightObservable({ datumList$, fullChartParams$, event$: store.event$ })
599
+ // const highlightSubscription = gridHighlight$.subscribe()
600
+
601
+ fullChartParams$.pipe(
602
+ takeUntil(destroy$),
603
+ filter(d => d.highlightTarget === 'series'),
604
+ switchMap(d => combineLatest({
605
+ graphicGSelection: graphicGSelection$,
606
+ gridHighlight: gridHighlight$,
607
+ DataMap: DataMap$,
608
+ fullChartParams: fullChartParams$
609
+ }).pipe(
610
+ takeUntil(destroy$),
611
+ switchMap(async d => d)
612
+ ))
613
+ ).subscribe(data => {
614
+ const seriesLabel = data.gridHighlight[0] ? data.gridHighlight[0].seriesLabel : null
615
+ highlightLineAreas({
616
+ selection: data.graphicGSelection,
617
+ seriesLabel,
618
+ fullChartParams: data.fullChartParams
619
+ })
620
+ })
621
+
622
+ return () => {
623
+ destroy$.next(undefined)
624
+ // highlightSubscription.unsubscribe()
625
+ }
626
626
  }