@orbcharts/plugins-basic 3.0.7 → 3.0.9

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 (121) hide show
  1. package/LICENSE +200 -200
  2. package/dist/orbcharts-plugins-basic.es.js +6837 -6656
  3. package/dist/orbcharts-plugins-basic.umd.js +45 -45
  4. package/dist/src/base/BaseStackedBars.d.ts +2 -0
  5. package/dist/src/series/defaults.d.ts +2 -1
  6. package/dist/src/series/index.d.ts +1 -0
  7. package/dist/src/series/plugins/Indicator.d.ts +3 -0
  8. package/lib/core-types.ts +7 -7
  9. package/lib/core.ts +6 -6
  10. package/lib/gridObservables.ts +6 -6
  11. package/lib/plugins-basic-types.ts +6 -6
  12. package/package.json +48 -48
  13. package/src/base/BaseBars.ts +765 -765
  14. package/src/base/BaseBarsTriangle.ts +676 -676
  15. package/src/base/BaseDots.ts +464 -464
  16. package/src/base/BaseGroupAxis.ts +691 -691
  17. package/src/base/BaseLegend.ts +684 -684
  18. package/src/base/BaseLineAreas.ts +629 -629
  19. package/src/base/BaseLines.ts +706 -706
  20. package/src/base/BaseOrdinalBubbles.ts +729 -729
  21. package/src/base/BaseRacingBars.ts +582 -582
  22. package/src/base/BaseRacingLabels.ts +404 -404
  23. package/src/base/BaseRacingValueLabels.ts +403 -403
  24. package/src/base/BaseStackedBars.ts +793 -782
  25. package/src/base/BaseTooltip.ts +408 -408
  26. package/src/base/BaseValueAxis.ts +600 -600
  27. package/src/base/BaseXAxis.ts +427 -427
  28. package/src/base/BaseXZoom.ts +241 -241
  29. package/src/base/BaseYAxis.ts +389 -389
  30. package/src/base/types.ts +2 -2
  31. package/src/const.ts +30 -30
  32. package/src/grid/defaults.ts +213 -213
  33. package/src/grid/gridObservables.ts +635 -635
  34. package/src/grid/index.ts +16 -16
  35. package/src/grid/plugins/Bars.ts +69 -69
  36. package/src/grid/plugins/BarsPN.ts +66 -66
  37. package/src/grid/plugins/BarsTriangle.ts +73 -73
  38. package/src/grid/plugins/Dots.ts +68 -68
  39. package/src/grid/plugins/GridLegend.ts +107 -107
  40. package/src/grid/plugins/GridTooltip.ts +66 -66
  41. package/src/grid/plugins/GroupAux.ts +1095 -1095
  42. package/src/grid/plugins/GroupAxis.ts +73 -73
  43. package/src/grid/plugins/GroupZoom.ts +218 -218
  44. package/src/grid/plugins/LineAreas.ts +65 -65
  45. package/src/grid/plugins/Lines.ts +59 -59
  46. package/src/grid/plugins/StackedBars.ts +66 -64
  47. package/src/grid/plugins/StackedValueAxis.ts +97 -96
  48. package/src/grid/plugins/ValueAxis.ts +94 -94
  49. package/src/index.ts +6 -6
  50. package/src/multiGrid/defaults.ts +244 -244
  51. package/src/multiGrid/index.ts +14 -14
  52. package/src/multiGrid/multiGridObservables.ts +50 -50
  53. package/src/multiGrid/plugins/MultiBars.ts +108 -108
  54. package/src/multiGrid/plugins/MultiBarsTriangle.ts +114 -114
  55. package/src/multiGrid/plugins/MultiDots.ts +102 -102
  56. package/src/multiGrid/plugins/MultiGridLegend.ts +169 -169
  57. package/src/multiGrid/plugins/MultiGridTooltip.ts +66 -66
  58. package/src/multiGrid/plugins/MultiGroupAxis.ts +137 -137
  59. package/src/multiGrid/plugins/MultiLineAreas.ts +107 -107
  60. package/src/multiGrid/plugins/MultiLines.ts +101 -101
  61. package/src/multiGrid/plugins/MultiStackedBars.ts +109 -106
  62. package/src/multiGrid/plugins/MultiStackedValueAxis.ts +135 -134
  63. package/src/multiGrid/plugins/MultiValueAxis.ts +134 -134
  64. package/src/multiGrid/plugins/OverlappingStackedValueAxes.ts +300 -300
  65. package/src/multiGrid/plugins/OverlappingValueAxes.ts +300 -300
  66. package/src/multiValue/defaults.ts +523 -523
  67. package/src/multiValue/index.ts +16 -16
  68. package/src/multiValue/multiValueObservables.ts +781 -781
  69. package/src/multiValue/plugins/MultiValueLegend.ts +107 -107
  70. package/src/multiValue/plugins/MultiValueTooltip.ts +66 -66
  71. package/src/multiValue/plugins/OrdinalAux.ts +660 -660
  72. package/src/multiValue/plugins/OrdinalAxis.ts +524 -524
  73. package/src/multiValue/plugins/OrdinalBubbles.ts +226 -226
  74. package/src/multiValue/plugins/OrdinalZoom.ts +57 -57
  75. package/src/multiValue/plugins/RacingBars.ts +375 -375
  76. package/src/multiValue/plugins/RacingCounterTexts.ts +300 -300
  77. package/src/multiValue/plugins/RacingValueAxis.ts +114 -114
  78. package/src/multiValue/plugins/Scatter.ts +486 -486
  79. package/src/multiValue/plugins/ScatterBubbles.ts +635 -635
  80. package/src/multiValue/plugins/XAxis.ts +107 -107
  81. package/src/multiValue/plugins/XYAux.ts +683 -683
  82. package/src/multiValue/plugins/XYAxes.ts +194 -194
  83. package/src/multiValue/plugins/XYAxes_legacy.ts +683 -683
  84. package/src/multiValue/plugins/XZoom.ts +40 -40
  85. package/src/noneData/defaults.ts +102 -102
  86. package/src/noneData/index.ts +3 -3
  87. package/src/noneData/plugins/Container.ts +27 -27
  88. package/src/noneData/plugins/Tooltip.ts +373 -373
  89. package/src/relationship/defaults.ts +221 -221
  90. package/src/relationship/index.ts +5 -5
  91. package/src/relationship/plugins/ForceDirected.ts +1056 -1056
  92. package/src/relationship/plugins/ForceDirectedBubbles.ts +1294 -1294
  93. package/src/relationship/plugins/RelationshipLegend.ts +100 -100
  94. package/src/relationship/plugins/RelationshipTooltip.ts +66 -66
  95. package/src/relationship/relationshipObservables.ts +49 -49
  96. package/src/series/defaults.ts +236 -224
  97. package/src/series/index.ts +10 -9
  98. package/src/series/plugins/Bubbles.ts +784 -784
  99. package/src/series/plugins/Indicator.ts +292 -0
  100. package/src/series/plugins/Pie.ts +622 -622
  101. package/src/series/plugins/PieEventTexts.ts +283 -283
  102. package/src/series/plugins/PieLabels.ts +639 -639
  103. package/src/series/plugins/Rose.ts +515 -515
  104. package/src/series/plugins/RoseLabels.ts +599 -599
  105. package/src/series/plugins/SeriesLegend.ts +107 -107
  106. package/src/series/plugins/SeriesTooltip.ts +66 -66
  107. package/src/series/seriesObservables.ts +168 -168
  108. package/src/series/seriesUtils.ts +51 -51
  109. package/src/tree/defaults.ts +102 -102
  110. package/src/tree/index.ts +4 -4
  111. package/src/tree/plugins/TreeLegend.ts +100 -100
  112. package/src/tree/plugins/TreeMap.ts +341 -341
  113. package/src/tree/plugins/TreeTooltip.ts +66 -66
  114. package/src/utils/commonUtils.ts +31 -31
  115. package/src/utils/d3Graphics.ts +176 -176
  116. package/src/utils/d3Utils.ts +92 -92
  117. package/src/utils/observables.ts +14 -14
  118. package/src/utils/orbchartsUtils.ts +129 -129
  119. package/tsconfig.base.json +13 -13
  120. package/tsconfig.json +2 -2
  121. package/vite.config.js +22 -22
@@ -0,0 +1,292 @@
1
+ import * as d3 from 'd3'
2
+ import {
3
+ combineLatest,
4
+ switchMap,
5
+ first,
6
+ map,
7
+ takeUntil,
8
+ Observable,
9
+ distinctUntilChanged,
10
+ Subject,
11
+ BehaviorSubject } from 'rxjs'
12
+ import type { DefinePluginConfig } from '../../../lib/core-types'
13
+ import {
14
+ defineSeriesPlugin } from '../../../lib/core'
15
+ import type {
16
+ ComputedDatumSeries,
17
+ ContainerPosition,
18
+ EventSeries,
19
+ ChartParams,
20
+ ComputedDatumBase,
21
+ ComputedDatumBaseSeries } from '../../../lib/core-types'
22
+ import type { IndicatorParams } from '../../../lib/plugins-basic-types'
23
+ import { DEFAULT_INDICATOR_PARAMS } from '../defaults'
24
+ import { getDatumColor, getClassName } from '../../utils/orbchartsUtils'
25
+ import { seriesCenterSelectionObservable } from '../seriesObservables'
26
+ import { LAYER_INDEX_OF_LABEL } from '../../const'
27
+
28
+ const pluginName = 'Indicator'
29
+ const indicatorGClassName = getClassName(pluginName, 'indicator-g')
30
+ const triangleGClassName = getClassName(pluginName, 'triangle-g')
31
+
32
+ const pluginConfig: DefinePluginConfig<typeof pluginName, typeof DEFAULT_INDICATOR_PARAMS> = {
33
+ name: pluginName,
34
+ defaultParams: DEFAULT_INDICATOR_PARAMS,
35
+ layerIndex: LAYER_INDEX_OF_LABEL,
36
+ validator: (params, { validateColumns }) => {
37
+ const result = validateColumns(params, {
38
+ startAngle: {
39
+ toBeTypes: ['number'],
40
+ },
41
+ endAngle: {
42
+ toBeTypes: ['number'],
43
+ },
44
+ radius: {
45
+ toBeTypes: ['number'],
46
+ },
47
+ size: {
48
+ toBeTypes: ['number'],
49
+ },
50
+ colorType: {
51
+ toBeOption: 'ColorType'
52
+ },
53
+ value: {
54
+ toBeTypes: ['number'],
55
+ },
56
+ })
57
+ return result
58
+ }
59
+ }
60
+
61
+ function renderIndicatorTriangle ({ containerSelection, angle, pointerDistance, fullParams, fullChartParams, graphicColor }: {
62
+ containerSelection: d3.Selection<SVGGElement, any, any, unknown>
63
+ angle: number
64
+ pointerDistance: number
65
+ fullParams: IndicatorParams
66
+ fullChartParams: ChartParams
67
+ graphicColor: string
68
+ }) {
69
+
70
+ const indicatorG = containerSelection.selectAll(`g.${indicatorGClassName}`)
71
+ .data([angle])
72
+ .join(
73
+ enter => enter.append('g')
74
+ .attr('class', indicatorGClassName)
75
+ .attr('transform', `rotate(${fullParams.startAngle / Math.PI * 180})`),
76
+ update => update,
77
+ exit => exit.remove()
78
+ )
79
+
80
+ indicatorG
81
+ .transition()
82
+ .duration(fullChartParams.transitionDuration)
83
+ .attr('transform', `rotate(${angle})`)
84
+
85
+ indicatorG
86
+ .selectAll(`g.${triangleGClassName}`)
87
+ .data([pointerDistance])
88
+ .join('g')
89
+ .attr('class', triangleGClassName)
90
+ .attr('transform', `translate(0, -${pointerDistance})`)
91
+ .selectAll('path')
92
+ .data([fullParams.size])
93
+ .join('path')
94
+ .attr('d', d3.symbol().type(d3.symbolTriangle).size(d => {
95
+ const area = (Math.sqrt(3) / 4) * Math.pow(fullParams.size, 2) // size參數為三角形寬度(邊長),以此計算面積
96
+ return area
97
+ }))
98
+ .attr('fill', graphicColor)
99
+ }
100
+
101
+ function createEachGraphic (pluginName: string, context: {
102
+ containerSelection: d3.Selection<SVGGElement, any, any, unknown>
103
+ // computedData$: Observable<ComputedDatumSeries[][]>
104
+ containerVisibleComputedSortedData$: Observable<ComputedDatumSeries[]>
105
+ // SeriesDataMap$: Observable<Map<string, ComputedDatumSeries[]>>
106
+ fullParams$: Observable<IndicatorParams>
107
+ fullChartParams$: Observable<ChartParams>
108
+ // textSizePx$: Observable<number>
109
+ // seriesHighlight$: Observable<ComputedDatumSeries[]>
110
+ seriesContainerPosition$: Observable<ContainerPosition>
111
+ event$: Subject<EventSeries>
112
+ }) {
113
+ const destroy$ = new Subject()
114
+
115
+ context.containerSelection.selectAll('g').remove()
116
+
117
+ const containerValueSum$ = context.containerVisibleComputedSortedData$.pipe(
118
+ map(data => data.reduce((sum, d) => sum + (d.value ?? 0), 0)),
119
+ distinctUntilChanged()
120
+ )
121
+
122
+ const valueToAngle$ = combineLatest({
123
+ fullParams: context.fullParams$,
124
+ containerValueSum: containerValueSum$,
125
+ }).pipe(
126
+ switchMap(async d => d),
127
+ map(({ fullParams, containerValueSum }) => {
128
+ return d3.scaleLinear()
129
+ .domain([0, containerValueSum])
130
+ .range([fullParams.startAngle / Math.PI * 180, fullParams.endAngle / Math.PI * 180])
131
+ }),
132
+ )
133
+
134
+ const value$ = context.fullParams$.pipe(
135
+ map(params => params.value),
136
+ distinctUntilChanged()
137
+ )
138
+
139
+ const angle$ = combineLatest({
140
+ value: value$,
141
+ valueToAngle: valueToAngle$,
142
+ }).pipe(
143
+ switchMap(async d => d),
144
+ map(({ value, valueToAngle }) => {
145
+ return valueToAngle(value)
146
+ }),
147
+ distinctUntilChanged()
148
+ )
149
+
150
+ const pointerDistance$ = combineLatest({
151
+ fullParams: context.fullParams$,
152
+ seriesContainerPosition: context.seriesContainerPosition$,
153
+ }).pipe(
154
+ switchMap(async d => d),
155
+ map(({fullParams, seriesContainerPosition}) => {
156
+ const { radius } = fullParams
157
+ const { width, height } = seriesContainerPosition
158
+
159
+ return Math.min(width, height) * radius / 2
160
+ }),
161
+ distinctUntilChanged()
162
+ )
163
+
164
+ // indicator 的 value 對應到 data 區間
165
+ const valueStackedIndex$ = combineLatest({
166
+ value: value$,
167
+ containerVisibleComputedSortedData: context.containerVisibleComputedSortedData$,
168
+ }).pipe(
169
+ switchMap(async d => d),
170
+ map(({ value, containerVisibleComputedSortedData }) => {
171
+ let valueIndex = 0
172
+ let stackedValue = 0
173
+ for (let i = 0; i < containerVisibleComputedSortedData.length; i++) {
174
+ const datumValue = containerVisibleComputedSortedData[i].value ?? 0
175
+ stackedValue += datumValue
176
+ if (stackedValue >= value) {
177
+ valueIndex = i
178
+ break
179
+ }
180
+ }
181
+ return valueIndex
182
+ }),
183
+ distinctUntilChanged()
184
+ )
185
+
186
+ const graphicColor$ = combineLatest({
187
+ value: value$,
188
+ valueStackedIndex: valueStackedIndex$,
189
+ // containerVisibleComputedSortedData: context.containerVisibleComputedSortedData$,
190
+ fullParams: context.fullParams$,
191
+ fullChartParams: context.fullChartParams$,
192
+ }).pipe(
193
+ switchMap(async d => d),
194
+ map(data => {
195
+ const labelColor = data.fullParams.colorType === 'label'
196
+ ? data.fullChartParams.colors[data.fullChartParams.colorScheme].label[data.valueStackedIndex]
197
+ : '' // 忽略
198
+
199
+ const datum: ComputedDatumBaseSeries = {
200
+ color: labelColor,
201
+ seriesIndex: 0, // 忽略
202
+ seriesLabel: '', // 忽略
203
+ seq: 0 // 忽略
204
+ }
205
+ return getDatumColor({
206
+ datum: datum as any as ComputedDatumBase,
207
+ colorType: data.fullParams.colorType,
208
+ fullChartParams: data.fullChartParams
209
+ })
210
+ }),
211
+ distinctUntilChanged()
212
+ )
213
+
214
+ combineLatest({
215
+ fullParams: context.fullParams$,
216
+ fullChartParams: context.fullChartParams$,
217
+ angle: angle$,
218
+ pointerDistance: pointerDistance$,
219
+ graphicColor: graphicColor$,
220
+ }).subscribe(data => {
221
+ renderIndicatorTriangle({
222
+ containerSelection: context.containerSelection,
223
+ angle: data.angle,
224
+ pointerDistance: data.pointerDistance,
225
+ fullParams: data.fullParams,
226
+ fullChartParams: data.fullChartParams,
227
+ graphicColor: data.graphicColor,
228
+ })
229
+ })
230
+
231
+
232
+ return () => {
233
+ destroy$.next(undefined)
234
+ }
235
+ }
236
+
237
+
238
+ export const Indicator = defineSeriesPlugin(pluginConfig)(({ selection, observer, subject }) => {
239
+
240
+ const destroy$ = new Subject()
241
+
242
+ const { seriesCenterSelection$ } = seriesCenterSelectionObservable({
243
+ selection: selection,
244
+ pluginName,
245
+ visibleComputedSortedData$: observer.visibleComputedSortedData$,
246
+ seriesContainerPosition$: observer.seriesContainerPosition$
247
+ })
248
+
249
+ const unsubscribeFnArr: (() => void)[] = []
250
+
251
+ seriesCenterSelection$
252
+ .pipe(
253
+ takeUntil(destroy$)
254
+ )
255
+ .subscribe(seriesCenterSelection => {
256
+ // 每次重新計算時,清除之前的訂閱
257
+ unsubscribeFnArr.forEach(fn => fn())
258
+
259
+ seriesCenterSelection.each((d, containerIndex, g) => {
260
+
261
+ const containerSelection = d3.select(g[containerIndex])
262
+
263
+ const containerVisibleComputedSortedData$ = observer.visibleComputedSortedData$.pipe(
264
+ takeUntil(destroy$),
265
+ map(data => data[containerIndex] ?? data[0])
266
+ )
267
+
268
+ const containerPosition$ = observer.seriesContainerPosition$.pipe(
269
+ takeUntil(destroy$),
270
+ map(data => data[containerIndex] ?? data[0])
271
+ )
272
+
273
+ unsubscribeFnArr[containerIndex] = createEachGraphic(pluginName, {
274
+ containerSelection: containerSelection,
275
+ // computedData$: observer.computedData$,
276
+ containerVisibleComputedSortedData$: containerVisibleComputedSortedData$,
277
+ // SeriesDataMap$: observer.SeriesDataMap$,
278
+ fullParams$: observer.fullParams$,
279
+ fullChartParams$: observer.fullChartParams$,
280
+ // textSizePx$: observer.textSizePx$,
281
+ // seriesHighlight$: observer.seriesHighlight$,
282
+ seriesContainerPosition$: containerPosition$,
283
+ event$: subject.event$,
284
+ })
285
+
286
+ })
287
+ })
288
+
289
+ return () => {
290
+ destroy$.next(undefined)
291
+ }
292
+ })