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

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