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

Sign up to get free protection for your applications and to get access to all the features.
Files changed (155) hide show
  1. package/LICENSE +200 -200
  2. package/dist/{src → orbcharts-plugins-basic/src}/base/BaseBarStack.d.ts +6 -4
  3. package/dist/{src → orbcharts-plugins-basic/src}/base/BaseBars.d.ts +6 -4
  4. package/dist/{src → orbcharts-plugins-basic/src}/base/BaseBarsTriangle.d.ts +7 -4
  5. package/dist/{src → orbcharts-plugins-basic/src}/base/BaseDots.d.ts +5 -3
  6. package/dist/{src → orbcharts-plugins-basic/src}/base/BaseGroupAxis.d.ts +3 -3
  7. package/dist/{src → orbcharts-plugins-basic/src}/base/BaseLineAreas.d.ts +6 -3
  8. package/dist/{src → orbcharts-plugins-basic/src}/base/BaseLines.d.ts +6 -3
  9. package/dist/{src → orbcharts-plugins-basic/src}/base/BaseValueAxis.d.ts +3 -3
  10. package/dist/{src → orbcharts-plugins-basic/src}/grid/gridObservables.d.ts +4 -4
  11. package/dist/orbcharts-plugins-basic/src/index.d.ts +5 -0
  12. package/dist/orbcharts-plugins-basic/src/multiGrid/multiGridObservables.d.ts +8 -0
  13. package/dist/orbcharts-plugins-basic/src/series/plugins/PieEventTexts.d.ts +3 -0
  14. package/dist/orbcharts-plugins-basic/src/series/seriesObservables.d.ts +21 -0
  15. package/dist/{src → orbcharts-plugins-basic/src}/series/seriesUtils.d.ts +3 -3
  16. package/dist/orbcharts-plugins-basic.es.js +7696 -7529
  17. package/dist/orbcharts-plugins-basic.umd.js +8 -8
  18. package/dist/src/index.d.ts +1 -5
  19. package/package.json +42 -42
  20. package/src/base/BaseBarStack.ts +778 -881
  21. package/src/base/BaseBars.ts +764 -750
  22. package/src/base/BaseBarsTriangle.ts +672 -659
  23. package/src/base/BaseDots.ts +502 -639
  24. package/src/base/BaseGroupAxis.ts +496 -496
  25. package/src/base/BaseLegend.ts +641 -641
  26. package/src/base/BaseLineAreas.ts +625 -621
  27. package/src/base/BaseLines.ts +699 -692
  28. package/src/base/BaseValueAxis.ts +478 -479
  29. package/src/base/types.ts +2 -2
  30. package/src/grid/defaults.ts +121 -121
  31. package/src/grid/gridObservables.ts +247 -263
  32. package/src/grid/index.ts +15 -15
  33. package/src/grid/plugins/BarStack.ts +50 -37
  34. package/src/grid/plugins/Bars.ts +51 -37
  35. package/src/grid/plugins/BarsDiverging.ts +41 -39
  36. package/src/grid/plugins/BarsTriangle.ts +50 -34
  37. package/src/grid/plugins/Dots.ts +37 -35
  38. package/src/grid/plugins/GridLegend.ts +59 -59
  39. package/src/grid/plugins/GroupAux.ts +645 -646
  40. package/src/grid/plugins/GroupAxis.ts +42 -30
  41. package/src/grid/plugins/LineAreas.ts +39 -36
  42. package/src/grid/plugins/Lines.ts +38 -35
  43. package/src/grid/plugins/ScalingArea.ts +173 -174
  44. package/src/grid/plugins/ValueAxis.ts +43 -31
  45. package/src/grid/plugins/ValueStackAxis.ts +79 -70
  46. package/src/grid/types.ts +120 -120
  47. package/src/index.ts +9 -9
  48. package/src/multiGrid/defaults.ts +147 -147
  49. package/src/multiGrid/index.ts +11 -11
  50. package/src/multiGrid/multiGridObservables.ts +42 -289
  51. package/src/multiGrid/plugins/MultiBarStack.ts +74 -60
  52. package/src/multiGrid/plugins/MultiBars.ts +73 -59
  53. package/src/multiGrid/plugins/MultiBarsTriangle.ts +73 -58
  54. package/src/multiGrid/plugins/MultiDots.ts +60 -58
  55. package/src/multiGrid/plugins/MultiGridLegend.ts +89 -89
  56. package/src/multiGrid/plugins/MultiGroupAxis.ts +65 -53
  57. package/src/multiGrid/plugins/MultiLineAreas.ts +62 -59
  58. package/src/multiGrid/plugins/MultiLines.ts +61 -58
  59. package/src/multiGrid/plugins/MultiValueAxis.ts +65 -53
  60. package/src/multiGrid/plugins/OverlappingValueAxes.ts +169 -164
  61. package/src/multiGrid/types.ts +67 -67
  62. package/src/noneData/defaults.ts +64 -61
  63. package/src/noneData/index.ts +3 -3
  64. package/src/noneData/plugins/Container.ts +10 -10
  65. package/src/noneData/plugins/Tooltip.ts +310 -310
  66. package/src/noneData/types.ts +26 -26
  67. package/src/series/defaults.ts +109 -99
  68. package/src/series/index.ts +6 -6
  69. package/src/series/plugins/Bubbles.ts +571 -551
  70. package/src/series/plugins/Pie.ts +548 -600
  71. package/src/series/plugins/PieEventTexts.ts +258 -194
  72. package/src/series/plugins/PieLabels.ts +335 -288
  73. package/src/series/plugins/SeriesLegend.ts +59 -59
  74. package/src/series/seriesObservables.ts +145 -0
  75. package/src/series/seriesUtils.ts +50 -50
  76. package/src/series/types.ts +67 -67
  77. package/src/tree/defaults.ts +22 -22
  78. package/src/tree/index.ts +3 -3
  79. package/src/tree/plugins/TreeLegend.ts +59 -59
  80. package/src/tree/plugins/TreeMap.ts +305 -305
  81. package/src/tree/types.ts +23 -23
  82. package/src/utils/commonUtils.ts +21 -21
  83. package/src/utils/d3Graphics.ts +124 -124
  84. package/src/utils/d3Utils.ts +73 -73
  85. package/src/utils/observables.ts +14 -14
  86. package/src/utils/orbchartsUtils.ts +100 -100
  87. package/tsconfig.dev.json +16 -16
  88. package/tsconfig.json +16 -13
  89. package/tsconfig.prod.json +13 -13
  90. package/vite.config.js +49 -49
  91. package/dist/src/multiGrid/multiGridObservables.d.ts +0 -12
  92. package/dist/src/series/plugins/PieEventTexts.d.ts +0 -1
  93. /package/dist/{src → orbcharts-plugins-basic/src}/base/BaseGroupArea.d.ts +0 -0
  94. /package/dist/{src → orbcharts-plugins-basic/src}/base/BaseLegend.d.ts +0 -0
  95. /package/dist/{src → orbcharts-plugins-basic/src}/base/types.d.ts +0 -0
  96. /package/dist/{src → orbcharts-plugins-basic/src}/grid/defaults.d.ts +0 -0
  97. /package/dist/{src → orbcharts-plugins-basic/src}/grid/index.d.ts +0 -0
  98. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/BarStack.d.ts +0 -0
  99. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/Bars.d.ts +0 -0
  100. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/BarsDiverging.d.ts +0 -0
  101. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/BarsTriangle.d.ts +0 -0
  102. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/Dots.d.ts +0 -0
  103. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/GridLegend.d.ts +0 -0
  104. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/GroupAux.d.ts +0 -0
  105. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/GroupAxis.d.ts +0 -0
  106. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/LineAreas.d.ts +0 -0
  107. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/Lines.d.ts +0 -0
  108. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/Ranking.d.ts +0 -0
  109. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/RankingAxis.d.ts +0 -0
  110. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/ScalingArea.d.ts +0 -0
  111. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/ValueAxis.d.ts +0 -0
  112. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/ValueStackAxis.d.ts +0 -0
  113. /package/dist/{src → orbcharts-plugins-basic/src}/grid/types.d.ts +0 -0
  114. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/defaults.d.ts +0 -0
  115. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/index.d.ts +0 -0
  116. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/plugins/MultiBarStack.d.ts +0 -0
  117. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/plugins/MultiBars.d.ts +0 -0
  118. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/plugins/MultiBarsTriangle.d.ts +0 -0
  119. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/plugins/MultiDots.d.ts +0 -0
  120. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/plugins/MultiGridLegend.d.ts +0 -0
  121. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/plugins/MultiGroupAxis.d.ts +0 -0
  122. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/plugins/MultiLineAreas.d.ts +0 -0
  123. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/plugins/MultiLines.d.ts +0 -0
  124. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/plugins/MultiValueAxis.d.ts +0 -0
  125. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/plugins/OverlappingValueAxes.d.ts +0 -0
  126. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/types.d.ts +0 -0
  127. /package/dist/{src → orbcharts-plugins-basic/src}/multiValue/index.d.ts +0 -0
  128. /package/dist/{src → orbcharts-plugins-basic/src}/multiValue/plugins/Scatter.d.ts +0 -0
  129. /package/dist/{src → orbcharts-plugins-basic/src}/multiValue/plugins/ScatterAxes.d.ts +0 -0
  130. /package/dist/{src → orbcharts-plugins-basic/src}/noneData/defaults.d.ts +0 -0
  131. /package/dist/{src → orbcharts-plugins-basic/src}/noneData/index.d.ts +0 -0
  132. /package/dist/{src → orbcharts-plugins-basic/src}/noneData/plugins/Container.d.ts +0 -0
  133. /package/dist/{src → orbcharts-plugins-basic/src}/noneData/plugins/Tooltip.d.ts +0 -0
  134. /package/dist/{src → orbcharts-plugins-basic/src}/noneData/types.d.ts +0 -0
  135. /package/dist/{src → orbcharts-plugins-basic/src}/relationship/index.d.ts +0 -0
  136. /package/dist/{src → orbcharts-plugins-basic/src}/relationship/plugins/Relationship.d.ts +0 -0
  137. /package/dist/{src → orbcharts-plugins-basic/src}/series/defaults.d.ts +0 -0
  138. /package/dist/{src → orbcharts-plugins-basic/src}/series/index.d.ts +0 -0
  139. /package/dist/{src → orbcharts-plugins-basic/src}/series/plugins/Bubbles.d.ts +0 -0
  140. /package/dist/{src → orbcharts-plugins-basic/src}/series/plugins/Pie.d.ts +0 -0
  141. /package/dist/{src → orbcharts-plugins-basic/src}/series/plugins/PieLabels.d.ts +0 -0
  142. /package/dist/{src → orbcharts-plugins-basic/src}/series/plugins/SeriesLegend.d.ts +0 -0
  143. /package/dist/{src → orbcharts-plugins-basic/src}/series/plugins/Waffle.d.ts +0 -0
  144. /package/dist/{src → orbcharts-plugins-basic/src}/series/types.d.ts +0 -0
  145. /package/dist/{src → orbcharts-plugins-basic/src}/tree/defaults.d.ts +0 -0
  146. /package/dist/{src → orbcharts-plugins-basic/src}/tree/index.d.ts +0 -0
  147. /package/dist/{src → orbcharts-plugins-basic/src}/tree/plugins/TreeLegend.d.ts +0 -0
  148. /package/dist/{src → orbcharts-plugins-basic/src}/tree/plugins/TreeMap.d.ts +0 -0
  149. /package/dist/{src → orbcharts-plugins-basic/src}/tree/types.d.ts +0 -0
  150. /package/dist/{src → orbcharts-plugins-basic/src}/utils/commonUtils.d.ts +0 -0
  151. /package/dist/{src → orbcharts-plugins-basic/src}/utils/d3Graphics.d.ts +0 -0
  152. /package/dist/{src → orbcharts-plugins-basic/src}/utils/d3Utils.d.ts +0 -0
  153. /package/dist/{src → orbcharts-plugins-basic/src}/utils/observables.d.ts +0 -0
  154. /package/dist/{src → orbcharts-plugins-basic/src}/utils/orbchartsUtils.d.ts +0 -0
  155. /package/dist/{vite.config.d.ts → orbcharts-plugins-basic/vite.config.d.ts} +0 -0
@@ -1,263 +1,247 @@
1
- import * as d3 from 'd3'
2
- import {
3
- Observable,
4
- Subject,
5
- of,
6
- takeUntil,
7
- filter,
8
- map,
9
- switchMap,
10
- combineLatest,
11
- merge,
12
- shareReplay,
13
- distinctUntilChanged
14
- } from 'rxjs'
15
- import type {
16
- ChartParams,
17
- HighlightTarget,
18
- DataFormatterGrid,
19
- ComputedDataGrid,
20
- ComputedDatumGrid,
21
- TransformData,
22
- ContainerPosition,
23
- Layout } from '@orbcharts/core'
24
- import { createAxisQuantizeScale } from '@orbcharts/core'
25
- import { getClassName, getUniID } from '../utils/orbchartsUtils'
26
-
27
- export const gridSelectionsObservable = ({ selection, pluginName, clipPathID, existSeriesLabels$, gridContainer$, gridAxesTransform$, gridGraphicTransform$ }: {
28
- selection: d3.Selection<any, unknown, any, unknown>
29
- pluginName: string
30
- clipPathID: string
31
- // computedData$: Observable<ComputedDataGrid>
32
- existSeriesLabels$: Observable<string[]>
33
- gridContainer$: Observable<ContainerPosition[]>
34
- gridAxesTransform$: Observable<TransformData>
35
- gridGraphicTransform$: Observable<TransformData>
36
- }) => {
37
- const seriesClassName = getClassName(pluginName, 'series')
38
- const axesClassName = getClassName(pluginName, 'axes')
39
- const graphicClassName = getClassName(pluginName, 'graphic')
40
-
41
- const seriesSelection$ = existSeriesLabels$.pipe(
42
- map((existSeriesLabels, i) => {
43
- return selection
44
- .selectAll<SVGGElement, string>(`g.${seriesClassName}`)
45
- .data(existSeriesLabels, d => d)
46
- .join(
47
- enter => {
48
- return enter
49
- .append('g')
50
- .classed(seriesClassName, true)
51
- .each((d, i, g) => {
52
- const axesSelection = d3.select(g[i])
53
- .selectAll<SVGGElement, ComputedDatumGrid[]>(`g.${axesClassName}`)
54
- .data([i])
55
- .join(
56
- enter => {
57
- return enter
58
- .append('g')
59
- .classed(axesClassName, true)
60
- .attr('clip-path', `url(#${clipPathID})`)
61
- .each((d, i, g) => {
62
- const defsSelection = d3.select(g[i])
63
- .selectAll<SVGDefsElement, any>('defs')
64
- .data([i])
65
- .join('defs')
66
-
67
- const graphicGSelection = d3.select(g[i])
68
- .selectAll<SVGGElement, any>('g')
69
- .data([i])
70
- .join('g')
71
- .classed(graphicClassName, true)
72
- })
73
- },
74
- update => update,
75
- exit => exit.remove()
76
- )
77
- })
78
- },
79
- update => update,
80
- exit => exit.remove()
81
- )
82
- }),
83
- switchMap(selection => combineLatest({
84
- seriesSelection: of(selection),
85
- gridContainer: gridContainer$
86
- })),
87
- map(data => {
88
- data.seriesSelection
89
- .transition()
90
- .attr('transform', (d, i) => {
91
- const gridContainer = data.gridContainer[i] ?? data.gridContainer[0]
92
- const translate = gridContainer.translate
93
- const scale = gridContainer.scale
94
- return `translate(${translate[0]}, ${translate[1]}) scale(${scale[0]}, ${scale[1]})`
95
- })
96
- return data.seriesSelection
97
- }),
98
- shareReplay(1)
99
- )
100
-
101
- // combineLatest({
102
- // seriesSelection: seriesSelection$,
103
- // gridContainer: gridContainer$
104
- // }).pipe(
105
- // switchMap(async d => d)
106
- // ).subscribe(data => {
107
- // data.seriesSelection
108
- // .transition()
109
- // .attr('transform', (d, i) => {
110
- // const translate = data.gridContainer[i].translate
111
- // const scale = data.gridContainer[i].scale
112
- // return `translate(${translate[0]}, ${translate[1]}) scale(${scale[0]}, ${scale[1]})`
113
- // })
114
- // })
115
-
116
-
117
- const axesSelection$ = combineLatest({
118
- seriesSelection: seriesSelection$,
119
- gridAxesTransform: gridAxesTransform$
120
- }).pipe(
121
- switchMap(async d => d),
122
- map(data => {
123
- return data.seriesSelection
124
- .select<SVGGElement>(`g.${axesClassName}`)
125
- .style('transform', data.gridAxesTransform.value)
126
- }),
127
- shareReplay(1)
128
- )
129
- const defsSelection$ = axesSelection$.pipe(
130
- map(axesSelection => {
131
- return axesSelection.select<SVGDefsElement>('defs')
132
- }),
133
- shareReplay(1)
134
- )
135
- const graphicGSelection$ = combineLatest({
136
- axesSelection: axesSelection$,
137
- gridGraphicTransform: gridGraphicTransform$
138
- }).pipe(
139
- switchMap(async d => d),
140
- map(data => {
141
- const graphicGSelection = data.axesSelection
142
- .select<SVGGElement>(`g.${graphicClassName}`)
143
- graphicGSelection
144
- .transition()
145
- .duration(50)
146
- .style('transform', data.gridGraphicTransform.value)
147
- return graphicGSelection
148
- }),
149
- shareReplay(1)
150
- )
151
-
152
- return {
153
- seriesSelection$,
154
- axesSelection$,
155
- defsSelection$,
156
- graphicGSelection$
157
- }
158
- }
159
-
160
- // 由事件取得group data的function
161
- export const gridGroupPositionFnObservable = ({ fullDataFormatter$, gridAxesSize$, computedData$, fullChartParams$ }: {
162
- fullDataFormatter$: Observable<DataFormatterGrid>
163
- gridAxesSize$: Observable<{
164
- width: number;
165
- height: number;
166
- }>
167
- computedData$: Observable<ComputedDataGrid>
168
- // GroupDataMap$: Observable<Map<string, ComputedDatumGrid[]>>
169
- fullChartParams$: Observable<ChartParams>
170
- }): Observable<(event: any) => { groupIndex: number; groupLabel: string }> => {
171
- const destroy$ = new Subject()
172
-
173
- // 顯示範圍內的group labels
174
- const scaleRangeGroupLabels$: Observable<string[]> = new Observable(subscriber => {
175
- combineLatest({
176
- dataFormatter: fullDataFormatter$,
177
- computedData: computedData$
178
- }).pipe(
179
- takeUntil(destroy$),
180
- // 轉換後會退訂前一個未完成的訂閱事件,因此可以取到「同時間」最後一次的訂閱事件
181
- switchMap(async (d) => d),
182
- ).subscribe(data => {
183
- const groupMin = 0
184
- const groupMax = data.computedData[0] ? data.computedData[0].length - 1 : 0
185
- const groupScaleDomainMin = data.dataFormatter.grid.groupAxis.scaleDomain[0] === 'auto'
186
- ? groupMin - data.dataFormatter.grid.groupAxis.scalePadding
187
- : data.dataFormatter.grid.groupAxis.scaleDomain[0] as number - data.dataFormatter.grid.groupAxis.scalePadding
188
- const groupScaleDomainMax = data.dataFormatter.grid.groupAxis.scaleDomain[1] === 'auto'
189
- ? groupMax + data.dataFormatter.grid.groupAxis.scalePadding
190
- : data.dataFormatter.grid.groupAxis.scaleDomain[1] as number + data.dataFormatter.grid.groupAxis.scalePadding
191
-
192
- // const groupingAmount = data.computedData[0]
193
- // ? data.computedData[0].length
194
- // : 0
195
-
196
- let _labels = data.dataFormatter.grid.gridData.seriesDirection === 'row'
197
- ? (data.computedData[0] ?? []).map(d => d.groupLabel)
198
- : data.computedData.map(d => d[0].groupLabel)
199
-
200
- const _axisLabels =
201
- // new Array(groupingAmount).fill(0)
202
- // .map((d, i) => {
203
- // return _labels[i] != null
204
- // ? _labels[i]
205
- // : String(i) // 沒有label則用序列號填充
206
- // })
207
- _labels
208
- .filter((d, i) => {
209
- return i >= groupScaleDomainMin && i <= groupScaleDomainMax
210
- })
211
- subscriber.next(_axisLabels)
212
- })
213
- })
214
-
215
- return new Observable<(event: any) => { groupIndex: number; groupLabel: string }>(subscriber => {
216
- combineLatest({
217
- dataFormatter: fullDataFormatter$,
218
- axisSize: gridAxesSize$,
219
- fullChartParams: fullChartParams$,
220
- scaleRangeGroupLabels: scaleRangeGroupLabels$
221
- }).pipe(
222
- takeUntil(destroy$),
223
- // 轉換後會退訂前一個未完成的訂閱事件,因此可以取到「同時間」最後一次的訂閱事件
224
- switchMap(async (d) => d),
225
- ).subscribe(data => {
226
-
227
- const reverse = data.dataFormatter.grid.valueAxis.position === 'right'
228
- || data.dataFormatter.grid.valueAxis.position === 'bottom'
229
- ? true : false
230
-
231
- // 比例尺座標對應非連續資料索引
232
- const groupIndexScale = createAxisQuantizeScale({
233
- axisLabels: data.scaleRangeGroupLabels,
234
- axisWidth: data.axisSize.width,
235
- reverse
236
- })
237
-
238
- // 依比例尺位置計算座標
239
- const axisValuePredicate = (event: any) => {
240
- return data.dataFormatter.grid.groupAxis.position === 'bottom'
241
- || data.dataFormatter.grid.groupAxis.position === 'top'
242
- ? event.offsetX - data.fullChartParams.padding.left
243
- : event.offsetY - data.fullChartParams.padding.top
244
- }
245
-
246
- // 比例尺座標取得groupData的function
247
- const createEventGroupData: (event: any) => { groupIndex: number; groupLabel: string } = (event: any) => {
248
- const axisValue = axisValuePredicate(event)
249
- const groupIndex = groupIndexScale(axisValue)
250
- return {
251
- groupIndex,
252
- groupLabel: data.scaleRangeGroupLabels[groupIndex] ?? ''
253
- }
254
- }
255
-
256
- subscriber.next(createEventGroupData)
257
-
258
- return function unsubscribe () {
259
- destroy$.next(undefined)
260
- }
261
- })
262
- })
263
- }
1
+ import * as d3 from 'd3'
2
+ import {
3
+ Observable,
4
+ Subject,
5
+ of,
6
+ takeUntil,
7
+ filter,
8
+ map,
9
+ switchMap,
10
+ combineLatest,
11
+ merge,
12
+ shareReplay,
13
+ distinctUntilChanged
14
+ } from 'rxjs'
15
+ import type {
16
+ ChartParams,
17
+ HighlightTarget,
18
+ DataFormatterGrid,
19
+ ComputedDataGrid,
20
+ ComputedDatumGrid,
21
+ TransformData,
22
+ GridContainerPosition,
23
+ Layout } from '@orbcharts/core'
24
+ import { createAxisQuantizeScale } from '@orbcharts/core'
25
+ import { getClassName, getUniID } from '../utils/orbchartsUtils'
26
+
27
+ // grid選取器
28
+ export const gridSelectionsObservable = ({ selection, pluginName, clipPathID, seriesLabels$, gridContainerPosition$, gridAxesTransform$, gridGraphicTransform$ }: {
29
+ selection: d3.Selection<any, unknown, any, unknown>
30
+ pluginName: string
31
+ clipPathID: string
32
+ // computedData$: Observable<ComputedDataGrid>
33
+ seriesLabels$: Observable<string[]>
34
+ gridContainerPosition$: Observable<GridContainerPosition[]>
35
+ gridAxesTransform$: Observable<TransformData>
36
+ gridGraphicTransform$: Observable<TransformData>
37
+ }) => {
38
+ const seriesClassName = getClassName(pluginName, 'series')
39
+ const axesClassName = getClassName(pluginName, 'axes')
40
+ const graphicClassName = getClassName(pluginName, 'graphic')
41
+
42
+ const seriesSelection$ = seriesLabels$.pipe(
43
+ map((existSeriesLabels, i) => {
44
+ return selection
45
+ .selectAll<SVGGElement, string>(`g.${seriesClassName}`)
46
+ .data(existSeriesLabels, d => d)
47
+ .join(
48
+ enter => {
49
+ return enter
50
+ .append('g')
51
+ .classed(seriesClassName, true)
52
+ .each((d, i, g) => {
53
+ const axesSelection = d3.select(g[i])
54
+ .selectAll<SVGGElement, ComputedDatumGrid[]>(`g.${axesClassName}`)
55
+ .data([i])
56
+ .join(
57
+ enter => {
58
+ return enter
59
+ .append('g')
60
+ .classed(axesClassName, true)
61
+ .attr('clip-path', `url(#${clipPathID})`)
62
+ .each((d, i, g) => {
63
+ const defsSelection = d3.select(g[i])
64
+ .selectAll<SVGDefsElement, any>('defs')
65
+ .data([i])
66
+ .join('defs')
67
+
68
+ const graphicGSelection = d3.select(g[i])
69
+ .selectAll<SVGGElement, any>('g')
70
+ .data([i])
71
+ .join('g')
72
+ .classed(graphicClassName, true)
73
+ })
74
+ },
75
+ update => update,
76
+ exit => exit.remove()
77
+ )
78
+ })
79
+ },
80
+ update => update,
81
+ exit => exit.remove()
82
+ )
83
+ }),
84
+ shareReplay(1)
85
+ )
86
+
87
+ combineLatest({
88
+ seriesSelection: seriesSelection$,
89
+ gridContainerPosition: gridContainerPosition$
90
+ }).pipe(
91
+ switchMap(async d => d)
92
+ ).subscribe(data => {
93
+ data.seriesSelection
94
+ .transition()
95
+ .attr('transform', (d, i) => {
96
+ const gridContainerPosition = data.gridContainerPosition[i] ?? data.gridContainerPosition[0]
97
+ const translate = gridContainerPosition.translate
98
+ const scale = gridContainerPosition.scale
99
+ return `translate(${translate[0]}, ${translate[1]}) scale(${scale[0]}, ${scale[1]})`
100
+ })
101
+ })
102
+
103
+ const axesSelection$ = combineLatest({
104
+ seriesSelection: seriesSelection$,
105
+ gridAxesTransform: gridAxesTransform$
106
+ }).pipe(
107
+ switchMap(async d => d),
108
+ map(data => {
109
+ return data.seriesSelection
110
+ .select<SVGGElement>(`g.${axesClassName}`)
111
+ .style('transform', data.gridAxesTransform.value)
112
+ }),
113
+ shareReplay(1)
114
+ )
115
+ const defsSelection$ = axesSelection$.pipe(
116
+ map(axesSelection => {
117
+ return axesSelection.select<SVGDefsElement>('defs')
118
+ }),
119
+ shareReplay(1)
120
+ )
121
+ const graphicGSelection$ = combineLatest({
122
+ axesSelection: axesSelection$,
123
+ gridGraphicTransform: gridGraphicTransform$
124
+ }).pipe(
125
+ switchMap(async d => d),
126
+ map(data => {
127
+ const graphicGSelection = data.axesSelection
128
+ .select<SVGGElement>(`g.${graphicClassName}`)
129
+ graphicGSelection
130
+ .transition()
131
+ .duration(50)
132
+ .style('transform', data.gridGraphicTransform.value)
133
+ return graphicGSelection
134
+ }),
135
+ shareReplay(1)
136
+ )
137
+
138
+ return {
139
+ seriesSelection$,
140
+ axesSelection$,
141
+ defsSelection$,
142
+ graphicGSelection$
143
+ }
144
+ }
145
+
146
+ // 由事件取得group data的function
147
+ export const gridGroupPositionFnObservable = ({ fullDataFormatter$, gridAxesSize$, computedData$, fullChartParams$ }: {
148
+ fullDataFormatter$: Observable<DataFormatterGrid>
149
+ gridAxesSize$: Observable<{
150
+ width: number;
151
+ height: number;
152
+ }>
153
+ computedData$: Observable<ComputedDataGrid>
154
+ // GroupDataMap$: Observable<Map<string, ComputedDatumGrid[]>>
155
+ fullChartParams$: Observable<ChartParams>
156
+ }): Observable<(event: any) => { groupIndex: number; groupLabel: string }> => {
157
+ const destroy$ = new Subject()
158
+
159
+ // 顯示範圍內的group labels
160
+ const scaleRangeGroupLabels$: Observable<string[]> = new Observable(subscriber => {
161
+ combineLatest({
162
+ dataFormatter: fullDataFormatter$,
163
+ computedData: computedData$
164
+ }).pipe(
165
+ takeUntil(destroy$),
166
+ switchMap(async (d) => d),
167
+ ).subscribe(data => {
168
+ const groupMin = 0
169
+ const groupMax = data.computedData[0] ? data.computedData[0].length - 1 : 0
170
+ const groupScaleDomainMin = data.dataFormatter.grid.groupAxis.scaleDomain[0] === 'auto'
171
+ ? groupMin - data.dataFormatter.grid.groupAxis.scalePadding
172
+ : data.dataFormatter.grid.groupAxis.scaleDomain[0] as number - data.dataFormatter.grid.groupAxis.scalePadding
173
+ const groupScaleDomainMax = data.dataFormatter.grid.groupAxis.scaleDomain[1] === 'auto'
174
+ ? groupMax + data.dataFormatter.grid.groupAxis.scalePadding
175
+ : data.dataFormatter.grid.groupAxis.scaleDomain[1] as number + data.dataFormatter.grid.groupAxis.scalePadding
176
+
177
+ // const groupingAmount = data.computedData[0]
178
+ // ? data.computedData[0].length
179
+ // : 0
180
+
181
+ let _labels = data.dataFormatter.grid.seriesDirection === 'row'
182
+ ? (data.computedData[0] ?? []).map(d => d.groupLabel)
183
+ : data.computedData.map(d => d[0].groupLabel)
184
+
185
+ const _axisLabels =
186
+ // new Array(groupingAmount).fill(0)
187
+ // .map((d, i) => {
188
+ // return _labels[i] != null
189
+ // ? _labels[i]
190
+ // : String(i) // 沒有label則用序列號填充
191
+ // })
192
+ _labels
193
+ .filter((d, i) => {
194
+ return i >= groupScaleDomainMin && i <= groupScaleDomainMax
195
+ })
196
+ subscriber.next(_axisLabels)
197
+ })
198
+ })
199
+
200
+ return new Observable<(event: any) => { groupIndex: number; groupLabel: string }>(subscriber => {
201
+ combineLatest({
202
+ dataFormatter: fullDataFormatter$,
203
+ axisSize: gridAxesSize$,
204
+ fullChartParams: fullChartParams$,
205
+ scaleRangeGroupLabels: scaleRangeGroupLabels$
206
+ }).pipe(
207
+ takeUntil(destroy$),
208
+ switchMap(async (d) => d),
209
+ ).subscribe(data => {
210
+
211
+ const reverse = data.dataFormatter.grid.valueAxis.position === 'right'
212
+ || data.dataFormatter.grid.valueAxis.position === 'bottom'
213
+ ? true : false
214
+
215
+ // 比例尺座標對應非連續資料索引
216
+ const groupIndexScale = createAxisQuantizeScale({
217
+ axisLabels: data.scaleRangeGroupLabels,
218
+ axisWidth: data.axisSize.width,
219
+ reverse
220
+ })
221
+
222
+ // 依比例尺位置計算座標
223
+ const axisValuePredicate = (event: any) => {
224
+ return data.dataFormatter.grid.groupAxis.position === 'bottom'
225
+ || data.dataFormatter.grid.groupAxis.position === 'top'
226
+ ? event.offsetX - data.fullChartParams.padding.left
227
+ : event.offsetY - data.fullChartParams.padding.top
228
+ }
229
+
230
+ // 比例尺座標取得groupData的function
231
+ const createEventGroupData: (event: any) => { groupIndex: number; groupLabel: string } = (event: any) => {
232
+ const axisValue = axisValuePredicate(event)
233
+ const groupIndex = groupIndexScale(axisValue)
234
+ return {
235
+ groupIndex,
236
+ groupLabel: data.scaleRangeGroupLabels[groupIndex] ?? ''
237
+ }
238
+ }
239
+
240
+ subscriber.next(createEventGroupData)
241
+
242
+ return function unsubscribe () {
243
+ destroy$.next(undefined)
244
+ }
245
+ })
246
+ })
247
+ }
package/src/grid/index.ts CHANGED
@@ -1,15 +1,15 @@
1
- export * from './defaults'
2
- export * from './types'
3
- export { Lines } from './plugins/Lines'
4
- export { LineAreas } from './plugins/LineAreas'
5
- export { Bars } from './plugins/Bars'
6
- export { BarsDiverging } from './plugins/BarsDiverging'
7
- export { BarStack } from './plugins/BarStack'
8
- export { BarsTriangle } from './plugins/BarsTriangle'
9
- export { Dots } from './plugins/Dots'
10
- export { GridLegend } from './plugins/GridLegend'
11
- export { GroupAxis } from './plugins/GroupAxis'
12
- export { ValueAxis } from './plugins/ValueAxis'
13
- export { ValueStackAxis } from './plugins/ValueStackAxis'
14
- export { ScalingArea } from './plugins/ScalingArea'
15
- export { GroupAux } from './plugins/GroupAux'
1
+ export * from './defaults'
2
+ export * from './types'
3
+ export { Lines } from './plugins/Lines'
4
+ export { LineAreas } from './plugins/LineAreas'
5
+ export { Bars } from './plugins/Bars'
6
+ export { BarsDiverging } from './plugins/BarsDiverging'
7
+ export { BarStack } from './plugins/BarStack'
8
+ export { BarsTriangle } from './plugins/BarsTriangle'
9
+ export { Dots } from './plugins/Dots'
10
+ export { GridLegend } from './plugins/GridLegend'
11
+ export { GroupAxis } from './plugins/GroupAxis'
12
+ export { ValueAxis } from './plugins/ValueAxis'
13
+ export { ValueStackAxis } from './plugins/ValueStackAxis'
14
+ export { ScalingArea } from './plugins/ScalingArea'
15
+ export { GroupAux } from './plugins/GroupAux'
@@ -1,38 +1,51 @@
1
- import {
2
- Subject,
3
- Observable } from 'rxjs'
4
- import { defineGridPlugin } from '@orbcharts/core'
5
- import { DEFAULT_BAR_STACK_PARAMS } from '../defaults'
6
- import { createBaseBarStack } from '../../base/BaseBarStack'
7
-
8
- const pluginName = 'BarStack'
9
-
10
-
11
- export const BarStack = defineGridPlugin(pluginName, DEFAULT_BAR_STACK_PARAMS)(({ selection, name, subject, observer }) => {
12
- const destroy$ = new Subject()
13
-
14
- const unsubscribeBaseBars = createBaseBarStack(pluginName, {
15
- selection,
16
- computedData$: observer.computedData$,
17
- visibleComputedData$: observer.visibleComputedData$,
18
- existSeriesLabels$: observer.existSeriesLabels$,
19
- SeriesDataMap$: observer.SeriesDataMap$,
20
- GroupDataMap$: observer.GroupDataMap$,
21
- fullParams$: observer.fullParams$,
22
- fullDataFormatter$: observer.fullDataFormatter$,
23
- fullChartParams$: observer.fullChartParams$,
24
- gridAxesTransform$: observer.gridAxesTransform$,
25
- gridGraphicTransform$: observer.gridGraphicTransform$,
26
- gridGraphicReverseScale$: observer.gridGraphicReverseScale$,
27
- gridAxesSize$: observer.gridAxesSize$,
28
- gridHighlight$: observer.gridHighlight$,
29
- gridContainer$: observer.gridContainer$,
30
- isSeriesPositionSeprate$: observer.isSeriesPositionSeprate$,
31
- event$: subject.event$,
32
- })
33
-
34
- return () => {
35
- destroy$.next(undefined)
36
- unsubscribeBaseBars()
37
- }
1
+ import {
2
+ map,
3
+ distinctUntilChanged,
4
+ shareReplay,
5
+ takeUntil,
6
+ Subject,
7
+ Observable } from 'rxjs'
8
+ import { defineGridPlugin } from '@orbcharts/core'
9
+ import { DEFAULT_BAR_STACK_PARAMS } from '../defaults'
10
+ import { createBaseBarStack } from '../../base/BaseBarStack'
11
+
12
+ const pluginName = 'BarStack'
13
+
14
+
15
+ export const BarStack = defineGridPlugin(pluginName, DEFAULT_BAR_STACK_PARAMS)(({ selection, name, subject, observer }) => {
16
+ const destroy$ = new Subject()
17
+
18
+ const isSeriesSeprate$ = observer.fullDataFormatter$.pipe(
19
+ takeUntil(destroy$),
20
+ map(d => d.grid.separateSeries),
21
+ distinctUntilChanged(),
22
+ shareReplay(1)
23
+ )
24
+
25
+ const unsubscribeBaseBars = createBaseBarStack(pluginName, {
26
+ selection,
27
+ computedData$: observer.computedData$,
28
+ computedLayoutData$: observer.computedLayoutData$,
29
+ visibleComputedData$: observer.visibleComputedData$,
30
+ visibleComputedLayoutData$: observer.visibleComputedLayoutData$,
31
+ seriesLabels$: observer.seriesLabels$,
32
+ SeriesDataMap$: observer.SeriesDataMap$,
33
+ GroupDataMap$: observer.GroupDataMap$,
34
+ fullParams$: observer.fullParams$,
35
+ fullDataFormatter$: observer.fullDataFormatter$,
36
+ fullChartParams$: observer.fullChartParams$,
37
+ gridAxesTransform$: observer.gridAxesTransform$,
38
+ gridGraphicTransform$: observer.gridGraphicTransform$,
39
+ gridGraphicReverseScale$: observer.gridGraphicReverseScale$,
40
+ gridAxesSize$: observer.gridAxesSize$,
41
+ gridHighlight$: observer.gridHighlight$,
42
+ gridContainerPosition$: observer.gridContainerPosition$,
43
+ isSeriesSeprate$,
44
+ event$: subject.event$,
45
+ })
46
+
47
+ return () => {
48
+ destroy$.next(undefined)
49
+ unsubscribeBaseBars()
50
+ }
38
51
  })