@orbcharts/plugins-basic 3.0.8 → 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 (120) hide show
  1. package/LICENSE +200 -200
  2. package/dist/orbcharts-plugins-basic.es.js +6177 -6004
  3. package/dist/orbcharts-plugins-basic.umd.js +44 -44
  4. package/dist/src/series/defaults.d.ts +2 -1
  5. package/dist/src/series/index.d.ts +1 -0
  6. package/dist/src/series/plugins/Indicator.d.ts +3 -0
  7. package/lib/core-types.ts +7 -7
  8. package/lib/core.ts +6 -6
  9. package/lib/gridObservables.ts +6 -6
  10. package/lib/plugins-basic-types.ts +6 -6
  11. package/package.json +48 -48
  12. package/src/base/BaseBars.ts +765 -765
  13. package/src/base/BaseBarsTriangle.ts +676 -676
  14. package/src/base/BaseDots.ts +464 -464
  15. package/src/base/BaseGroupAxis.ts +691 -691
  16. package/src/base/BaseLegend.ts +684 -684
  17. package/src/base/BaseLineAreas.ts +629 -629
  18. package/src/base/BaseLines.ts +706 -706
  19. package/src/base/BaseOrdinalBubbles.ts +729 -729
  20. package/src/base/BaseRacingBars.ts +582 -582
  21. package/src/base/BaseRacingLabels.ts +404 -404
  22. package/src/base/BaseRacingValueLabels.ts +403 -403
  23. package/src/base/BaseStackedBars.ts +793 -793
  24. package/src/base/BaseTooltip.ts +408 -408
  25. package/src/base/BaseValueAxis.ts +600 -600
  26. package/src/base/BaseXAxis.ts +427 -427
  27. package/src/base/BaseXZoom.ts +241 -241
  28. package/src/base/BaseYAxis.ts +389 -389
  29. package/src/base/types.ts +2 -2
  30. package/src/const.ts +30 -30
  31. package/src/grid/defaults.ts +213 -213
  32. package/src/grid/gridObservables.ts +635 -635
  33. package/src/grid/index.ts +16 -16
  34. package/src/grid/plugins/Bars.ts +69 -69
  35. package/src/grid/plugins/BarsPN.ts +66 -66
  36. package/src/grid/plugins/BarsTriangle.ts +73 -73
  37. package/src/grid/plugins/Dots.ts +68 -68
  38. package/src/grid/plugins/GridLegend.ts +107 -107
  39. package/src/grid/plugins/GridTooltip.ts +66 -66
  40. package/src/grid/plugins/GroupAux.ts +1095 -1095
  41. package/src/grid/plugins/GroupAxis.ts +73 -73
  42. package/src/grid/plugins/GroupZoom.ts +218 -218
  43. package/src/grid/plugins/LineAreas.ts +65 -65
  44. package/src/grid/plugins/Lines.ts +59 -59
  45. package/src/grid/plugins/StackedBars.ts +66 -66
  46. package/src/grid/plugins/StackedValueAxis.ts +97 -97
  47. package/src/grid/plugins/ValueAxis.ts +94 -94
  48. package/src/index.ts +6 -6
  49. package/src/multiGrid/defaults.ts +244 -244
  50. package/src/multiGrid/index.ts +14 -14
  51. package/src/multiGrid/multiGridObservables.ts +50 -50
  52. package/src/multiGrid/plugins/MultiBars.ts +108 -108
  53. package/src/multiGrid/plugins/MultiBarsTriangle.ts +114 -114
  54. package/src/multiGrid/plugins/MultiDots.ts +102 -102
  55. package/src/multiGrid/plugins/MultiGridLegend.ts +169 -169
  56. package/src/multiGrid/plugins/MultiGridTooltip.ts +66 -66
  57. package/src/multiGrid/plugins/MultiGroupAxis.ts +137 -137
  58. package/src/multiGrid/plugins/MultiLineAreas.ts +107 -107
  59. package/src/multiGrid/plugins/MultiLines.ts +101 -101
  60. package/src/multiGrid/plugins/MultiStackedBars.ts +109 -109
  61. package/src/multiGrid/plugins/MultiStackedValueAxis.ts +135 -135
  62. package/src/multiGrid/plugins/MultiValueAxis.ts +134 -134
  63. package/src/multiGrid/plugins/OverlappingStackedValueAxes.ts +300 -300
  64. package/src/multiGrid/plugins/OverlappingValueAxes.ts +300 -300
  65. package/src/multiValue/defaults.ts +523 -523
  66. package/src/multiValue/index.ts +16 -16
  67. package/src/multiValue/multiValueObservables.ts +781 -781
  68. package/src/multiValue/plugins/MultiValueLegend.ts +107 -107
  69. package/src/multiValue/plugins/MultiValueTooltip.ts +66 -66
  70. package/src/multiValue/plugins/OrdinalAux.ts +660 -660
  71. package/src/multiValue/plugins/OrdinalAxis.ts +524 -524
  72. package/src/multiValue/plugins/OrdinalBubbles.ts +226 -226
  73. package/src/multiValue/plugins/OrdinalZoom.ts +57 -57
  74. package/src/multiValue/plugins/RacingBars.ts +375 -375
  75. package/src/multiValue/plugins/RacingCounterTexts.ts +300 -300
  76. package/src/multiValue/plugins/RacingValueAxis.ts +114 -114
  77. package/src/multiValue/plugins/Scatter.ts +486 -486
  78. package/src/multiValue/plugins/ScatterBubbles.ts +635 -635
  79. package/src/multiValue/plugins/XAxis.ts +107 -107
  80. package/src/multiValue/plugins/XYAux.ts +683 -683
  81. package/src/multiValue/plugins/XYAxes.ts +194 -194
  82. package/src/multiValue/plugins/XYAxes_legacy.ts +683 -683
  83. package/src/multiValue/plugins/XZoom.ts +40 -40
  84. package/src/noneData/defaults.ts +102 -102
  85. package/src/noneData/index.ts +3 -3
  86. package/src/noneData/plugins/Container.ts +27 -27
  87. package/src/noneData/plugins/Tooltip.ts +373 -373
  88. package/src/relationship/defaults.ts +221 -221
  89. package/src/relationship/index.ts +5 -5
  90. package/src/relationship/plugins/ForceDirected.ts +1056 -1056
  91. package/src/relationship/plugins/ForceDirectedBubbles.ts +1294 -1294
  92. package/src/relationship/plugins/RelationshipLegend.ts +100 -100
  93. package/src/relationship/plugins/RelationshipTooltip.ts +66 -66
  94. package/src/relationship/relationshipObservables.ts +49 -49
  95. package/src/series/defaults.ts +236 -224
  96. package/src/series/index.ts +10 -9
  97. package/src/series/plugins/Bubbles.ts +784 -784
  98. package/src/series/plugins/Indicator.ts +292 -0
  99. package/src/series/plugins/Pie.ts +622 -622
  100. package/src/series/plugins/PieEventTexts.ts +283 -283
  101. package/src/series/plugins/PieLabels.ts +639 -639
  102. package/src/series/plugins/Rose.ts +515 -515
  103. package/src/series/plugins/RoseLabels.ts +599 -599
  104. package/src/series/plugins/SeriesLegend.ts +107 -107
  105. package/src/series/plugins/SeriesTooltip.ts +66 -66
  106. package/src/series/seriesObservables.ts +168 -168
  107. package/src/series/seriesUtils.ts +51 -51
  108. package/src/tree/defaults.ts +102 -102
  109. package/src/tree/index.ts +4 -4
  110. package/src/tree/plugins/TreeLegend.ts +100 -100
  111. package/src/tree/plugins/TreeMap.ts +341 -341
  112. package/src/tree/plugins/TreeTooltip.ts +66 -66
  113. package/src/utils/commonUtils.ts +31 -31
  114. package/src/utils/d3Graphics.ts +176 -176
  115. package/src/utils/d3Utils.ts +92 -92
  116. package/src/utils/observables.ts +14 -14
  117. package/src/utils/orbchartsUtils.ts +129 -129
  118. package/tsconfig.base.json +13 -13
  119. package/tsconfig.json +2 -2
  120. 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
+ })