@orbcharts/plugins-basic 3.0.0-beta.2 → 3.0.0-beta.20

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