@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
@@ -1,389 +1,389 @@
1
- import * as d3 from 'd3'
2
- import {
3
- Observable,
4
- Subject,
5
- combineLatest,
6
- takeUntil,
7
- map,
8
- distinctUntilChanged,
9
- switchMap,
10
- shareReplay
11
- } from 'rxjs'
12
- import type {
13
- ColorType,
14
- ChartParams,
15
- ComputedDatumMultiValue,
16
- ComputedDataMultiValue,
17
- ComputedXYDatumMultiValue,
18
- ContainerPositionScaled,
19
- ContainerSize,
20
- DataFormatterMultiValue,
21
- DefinePluginConfig,
22
- TransformData,
23
- Layout
24
- } from '../../lib/core-types'
25
- import {
26
- createValueToAxisScale,
27
- } from '../../lib/core'
28
- import type { BaseYAxisParams
29
- } from '../../lib/plugins-basic-types'
30
- import type { BasePluginFn } from './types'
31
- // import { DEFAULT_X_Y_AXES_PARAMS } from '../defaults'
32
- // import { LAYER_INDEX_OF_AXIS } from '../const'
33
- import { getColor, getDatumColor, getClassName, getUniID } from '../utils/orbchartsUtils'
34
- import { parseTickFormatValue } from '../utils/d3Utils'
35
- import { multiValueContainerSelectionsObservable } from '../multiValue/multiValueObservables'
36
-
37
- interface BaseYAxisContext {
38
- selection: d3.Selection<any, unknown, any, unknown>
39
- computedData$: Observable<ComputedDataMultiValue>
40
- // filteredMinMaxValue$: Observable<[number, number]>
41
- fullParams$: Observable<BaseYAxisParams>
42
- fullDataFormatter$: Observable<DataFormatterMultiValue>
43
- fullChartParams$: Observable<ChartParams>
44
- isCategorySeprate$: Observable<boolean>
45
- containerPosition$: Observable<ContainerPositionScaled[]>
46
- containerSize$: Observable<ContainerSize>
47
- // layout$: Observable<Layout>
48
- // filteredXYMinMaxData$: Observable<{
49
- // datumList: ComputedXYDatumMultiValue[];
50
- // minXDatum: ComputedXYDatumMultiValue | null;
51
- // maxXDatum: ComputedXYDatumMultiValue | null;
52
- // minYDatum: ComputedXYDatumMultiValue | null;
53
- // maxYDatum: ComputedXYDatumMultiValue | null;
54
- // }>
55
- yScale$: Observable<d3.ScaleLinear<number, number>>
56
- // xyMinMax$: Observable<{
57
- // minX: number;
58
- // maxX: number;
59
- // minY: number;
60
- // maxY: number;
61
- // }>
62
- }
63
-
64
- // interface TextAlign {
65
- // textAnchor: "start" | "middle" | "end"
66
- // dominantBaseline: "middle" | "auto" | "hanging"
67
- // }
68
-
69
- const defaultTickSize = 6
70
-
71
- const yTickTextAnchor = 'end'
72
- const yTickDominantBaseline = 'middle'
73
- const yAxisLabelAnchor = 'end'
74
- const yAxisLabelDominantBaseline = 'auto'
75
-
76
- function renderYAxisLabel ({ selection, yLabelClassName, fullParams, containerSize, fullDataFormatter, fullChartParams }: {
77
- selection: d3.Selection<SVGGElement, any, any, any>,
78
- yLabelClassName: string
79
- fullParams: BaseYAxisParams
80
- // axisLabelAlign: TextAlign
81
- containerSize: { width: number, height: number }
82
- fullDataFormatter: DataFormatterMultiValue,
83
- fullChartParams: ChartParams
84
- // textReverseTransform: string,
85
- }) {
86
- const offsetX = fullParams.tickPadding - fullParams.labelOffset[0]
87
- const offsetY = fullParams.tickPadding + fullParams.labelOffset[1]
88
- let labelX = - offsetX
89
- let labelY = - offsetY
90
-
91
- const axisLabelSelection = selection
92
- .selectAll<SVGGElement, BaseYAxisParams>(`g.${yLabelClassName}`)
93
- .data([fullParams])
94
- .join('g')
95
- .classed(yLabelClassName, true)
96
- .each((d, i, g) => {
97
- const text = d3.select(g[i])
98
- .selectAll<SVGTextElement, BaseYAxisParams>(`text`)
99
- .data([d])
100
- .join(
101
- enter => {
102
- return enter
103
- .append('text')
104
- .style('font-weight', 'bold')
105
- },
106
- update => update,
107
- exit => exit.remove()
108
- )
109
- .attr('text-anchor', yAxisLabelAnchor)
110
- .attr('dominant-baseline', yAxisLabelDominantBaseline)
111
- .attr('font-size', fullChartParams.styles.textSize)
112
- .style('fill', getColor(fullParams.labelColorType, fullChartParams))
113
- // .style('transform', textReverseTransform)
114
- // 偏移使用 x, y 而非 transform 才不會受到外層 scale 變形影響
115
- .attr('x', labelX)
116
- .attr('y', labelY)
117
- .text(d => fullDataFormatter.yAxis.label)
118
- })
119
- // .attr('transform', d => `translate(0, ${layout.height})`)
120
- }
121
-
122
- function renderYAxis ({ selection, yAxisClassName, fullParams, containerSize, fullDataFormatter, fullChartParams, yScale }: {
123
- selection: d3.Selection<SVGGElement, any, any, any>,
124
- yAxisClassName: string
125
- fullParams: BaseYAxisParams
126
- // tickTextAlign: TextAlign
127
- containerSize: ContainerSize
128
- fullDataFormatter: DataFormatterMultiValue,
129
- fullChartParams: ChartParams
130
- yScale: d3.ScaleLinear<number, number>
131
- // textReverseTransform: string,
132
- // xyMinMax: {
133
- // minX: number;
134
- // maxX: number;
135
- // minY: number;
136
- // maxY: number;
137
- // }
138
- }) {
139
-
140
- const yAxisSelection = selection
141
- .selectAll<SVGGElement, BaseYAxisParams>(`g.${yAxisClassName}`)
142
- .data([fullParams])
143
- .join('g')
144
- .classed(yAxisClassName, true)
145
-
146
- // const _yScale = d3.scaleLinear()
147
- // .domain([0, 150])
148
- // .range([416.5, 791.349])
149
-
150
- // 刻度文字偏移
151
- let tickPadding = fullParams.tickPadding
152
-
153
- // 設定Y軸刻度
154
- const yAxis = d3.axisLeft(yScale)
155
- .scale(yScale)
156
- .ticks(fullParams.ticks) // 刻度分段數量
157
- .tickFormat(d => parseTickFormatValue(d, fullParams.tickFormat))
158
- .tickSize(fullParams.tickFullLine == true
159
- ? -containerSize.width
160
- : defaultTickSize)
161
- .tickPadding(tickPadding)
162
-
163
- const yAxisEl = yAxisSelection
164
- .transition()
165
- .duration(100)
166
- .ease(d3.easeLinear) // 線性的 - 當托曳或快速變動的時候比較滑順
167
- .call(yAxis)
168
-
169
- yAxisEl.selectAll('line')
170
- .style('fill', 'none')
171
- .style('stroke', fullParams.tickLineVisible == true ? getColor(fullParams.tickColorType, fullChartParams) : 'none')
172
- .style('stroke-dasharray', fullParams.tickFullLineDasharray)
173
- .attr('pointer-events', 'none')
174
-
175
- yAxisEl.selectAll('path')
176
- .style('fill', 'none')
177
- // .style('stroke', this.fullParams.axisLineColor!)
178
- .style('stroke', fullParams.axisLineVisible == true ? getColor(fullParams.axisLineColorType, fullChartParams) : 'none')
179
- .style('shape-rendering', 'crispEdges')
180
-
181
- // const yText = yAxisEl.selectAll('text')
182
- const yText = yAxisSelection.selectAll('text')
183
- // .style('font-family', 'sans-serif')
184
- .attr('font-size', fullChartParams.styles.textSize)
185
- .style('color', getColor(fullParams.tickTextColorType, fullChartParams))
186
- .attr('text-anchor', yTickTextAnchor)
187
- .attr('dominant-baseline', yTickDominantBaseline)
188
- // .attr('dy', 0)
189
- .attr('x', - tickPadding)
190
- .attr('dy', 0)
191
- // yText.style('transform', textReverseTransform)
192
-
193
- // // 抵消掉預設的偏移
194
- // if (fullDataFormatter.grid.valueAxis.position === 'bottom' || fullDataFormatter.grid.valueAxis.position === 'top') {
195
- // yText.attr('dy', 0)
196
- // }
197
-
198
- return yAxisSelection
199
- }
200
-
201
- export const createBaseYAxis: BasePluginFn<BaseYAxisContext> = (pluginName: string, {
202
- selection,
203
- computedData$,
204
- fullParams$,
205
- fullDataFormatter$,
206
- fullChartParams$,
207
- isCategorySeprate$,
208
- containerPosition$,
209
- containerSize$,
210
- // layout$,
211
- // filteredXYMinMaxData$,
212
- yScale$,
213
- // xyMinMax$
214
- }) => {
215
-
216
- const destroy$ = new Subject()
217
-
218
- // const containerClassName = getClassName(pluginName, 'container')
219
- const yAxisGClassName = getClassName(pluginName, 'yAxisG')
220
- const yAxisClassName = getClassName(pluginName, 'yAxis')
221
- const yLabelClassName = getClassName(pluginName, 'yLabel')
222
-
223
- const containerSelection$ = multiValueContainerSelectionsObservable({
224
- selection,
225
- pluginName,
226
- clipPathID: null,
227
- computedData$,
228
- containerPosition$,
229
- isCategorySeprate$,
230
- }).pipe(
231
- takeUntil(destroy$),
232
- )
233
-
234
- // const containerSelection$ = combineLatest({
235
- // computedData: computedData$.pipe(
236
- // distinctUntilChanged((a, b) => {
237
- // // 只有當series的數量改變時,才重新計算
238
- // return a.length === b.length
239
- // }),
240
- // ),
241
- // isCategorySeprate: isCategorySeprate$
242
- // }).pipe(
243
- // takeUntil(destroy$),
244
- // switchMap(async (d) => d),
245
- // map(data => {
246
- // return data.isCategorySeprate
247
- // // category分開的時候顯示各別axis
248
- // ? data.computedData
249
- // // category合併的時候只顯示第一個axis
250
- // : [data.computedData[0]]
251
- // }),
252
- // map((computedData, i) => {
253
- // return selection
254
- // .selectAll<SVGGElement, ComputedDatumMultiValue[]>(`g.${containerClassName}`)
255
- // .data(computedData, d => d[0] ? d[0].categoryIndex : i)
256
- // .join('g')
257
- // .classed(containerClassName, true)
258
- // })
259
- // )
260
-
261
- const axisSelection$ = containerSelection$.pipe(
262
- takeUntil(destroy$),
263
- map((containerSelection, i) => {
264
- return containerSelection
265
- .selectAll<SVGGElement, ComputedDatumMultiValue[]>(`g.${yAxisGClassName}`)
266
- .data([yAxisGClassName])
267
- .join('g')
268
- .classed(yAxisGClassName, true)
269
- })
270
- )
271
-
272
- // combineLatest({
273
- // containerSelection: containerSelection$,
274
- // gridContainerPosition: containerPosition$
275
- // }).pipe(
276
- // takeUntil(destroy$),
277
- // switchMap(async d => d)
278
- // ).subscribe(data => {
279
- // data.containerSelection
280
- // .attr('transform', (d, i) => {
281
- // const gridContainerPosition = data.gridContainerPosition[i] ?? data.gridContainerPosition[0]
282
- // const translate = gridContainerPosition.translate
283
- // const scale = gridContainerPosition.scale
284
- // // return `translate(${translate[0]}, ${translate[1]}) scale(${scale[0]}, ${scale[1]})`
285
- // return `translate(${translate[0]}, ${translate[1]})`
286
- // })
287
- // // .attr('opacity', 0)
288
- // // .transition()
289
- // // .attr('opacity', 1)
290
- // })
291
-
292
- // const textReverseTransform$ = containerPosition$.pipe(
293
- // takeUntil(destroy$),
294
- // switchMap(async (d) => d),
295
- // map(containerPosition => {
296
- // // const axesRotateXYReverseValue = `rotateX(${data.gridAxesReverseTransform.rotateX}deg) rotateY(${data.gridAxesReverseTransform.rotateY}deg)`
297
- // // const axesRotateReverseValue = `rotate(${data.gridAxesReverseTransform.rotate}deg)`
298
- // const containerScaleReverseValue = `scale(${1 / containerPosition[0].scale[0]}, ${1 / containerPosition[0].scale[1]})`
299
- // // 抵消最外層scale
300
- // return `${containerScaleReverseValue}`
301
- // }),
302
- // distinctUntilChanged()
303
- // )
304
-
305
- // const yScale$: Observable<d3.ScaleLinear<number, number>> = new Observable(subscriber => {
306
- // combineLatest({
307
- // fullDataFormatter: fullDataFormatter$,
308
- // layout: layout$,
309
- // // xyMinMax: observer.xyMinMax$
310
- // filteredXYMinMaxData: filteredXYMinMaxData$
311
- // }).pipe(
312
- // takeUntil(destroy$),
313
- // switchMap(async (d) => d),
314
- // ).subscribe(data => {
315
- // const valueIndex = data.fullDataFormatter.xAxis.valueIndex
316
- // if (!data.filteredXYMinMaxData.minYDatum || !data.filteredXYMinMaxData.maxYDatum
317
- // || data.filteredXYMinMaxData.minYDatum.value[valueIndex] == null || data.filteredXYMinMaxData.maxYDatum.value[valueIndex] == null
318
- // ) {
319
- // return
320
- // }
321
- // let maxValue = data.filteredXYMinMaxData.maxYDatum.value[valueIndex]
322
- // let minValue = data.filteredXYMinMaxData.minYDatum.value[valueIndex]
323
- // if (maxValue === minValue && maxValue === 0) {
324
- // // 避免最大及最小值同等於 0 造成無法計算scale
325
- // maxValue = 1
326
- // }
327
-
328
- // const yScale: d3.ScaleLinear<number, number> = createValueToAxisScale({
329
- // maxValue,
330
- // minValue,
331
- // axisWidth: data.layout.height,
332
- // scaleDomain: data.fullDataFormatter.yAxis.scaleDomain,
333
- // scaleRange: data.fullDataFormatter.yAxis.scaleRange,
334
- // reverse: true
335
- // })
336
-
337
- // subscriber.next(yScale)
338
- // })
339
- // })
340
-
341
-
342
- combineLatest({
343
- axisSelection: axisSelection$,
344
- fullParams: fullParams$,
345
- // tickTextAlign: tickTextAlign$,
346
- // axisLabelAlign: axisLabelAlign$,
347
- computedData: computedData$,
348
- containerSize: containerSize$,
349
- fullDataFormatter: fullDataFormatter$,
350
- fullChartParams: fullChartParams$,
351
- yScale: yScale$,
352
- // textReverseTransform: textReverseTransform$,
353
- // xyMinMax: xyMinMax$
354
- }).pipe(
355
- takeUntil(destroy$),
356
- switchMap(async (d) => d),
357
- ).subscribe(data => {
358
-
359
- renderYAxis({
360
- selection: data.axisSelection,
361
- yAxisClassName,
362
- fullParams: data.fullParams,
363
- // tickTextAlign: data.tickTextAlign,
364
- containerSize: data.containerSize,
365
- fullDataFormatter: data.fullDataFormatter,
366
- fullChartParams: data.fullChartParams,
367
- yScale: data.yScale,
368
- // textReverseTransform: data.textReverseTransform,
369
- // xyMinMax: data.xyMinMax
370
- })
371
-
372
- renderYAxisLabel({
373
- selection: data.axisSelection,
374
- yLabelClassName,
375
- fullParams: data.fullParams,
376
- // axisLabelAlign: data.axisLabelAlign,
377
- containerSize: data.containerSize,
378
- fullDataFormatter: data.fullDataFormatter,
379
- fullChartParams: data.fullChartParams,
380
- // textReverseTransform: data.textReverseTransform,
381
- })
382
-
383
- })
384
-
385
-
386
- return () => {
387
- destroy$.next(undefined)
388
- }
389
- }
1
+ import * as d3 from 'd3'
2
+ import {
3
+ Observable,
4
+ Subject,
5
+ combineLatest,
6
+ takeUntil,
7
+ map,
8
+ distinctUntilChanged,
9
+ switchMap,
10
+ shareReplay
11
+ } from 'rxjs'
12
+ import type {
13
+ ColorType,
14
+ ChartParams,
15
+ ComputedDatumMultiValue,
16
+ ComputedDataMultiValue,
17
+ ComputedXYDatumMultiValue,
18
+ ContainerPositionScaled,
19
+ ContainerSize,
20
+ DataFormatterMultiValue,
21
+ DefinePluginConfig,
22
+ TransformData,
23
+ Layout
24
+ } from '../../lib/core-types'
25
+ import {
26
+ createValueToAxisScale,
27
+ } from '../../lib/core'
28
+ import type { BaseYAxisParams
29
+ } from '../../lib/plugins-basic-types'
30
+ import type { BasePluginFn } from './types'
31
+ // import { DEFAULT_X_Y_AXES_PARAMS } from '../defaults'
32
+ // import { LAYER_INDEX_OF_AXIS } from '../const'
33
+ import { getColor, getDatumColor, getClassName, getUniID } from '../utils/orbchartsUtils'
34
+ import { parseTickFormatValue } from '../utils/d3Utils'
35
+ import { multiValueContainerSelectionsObservable } from '../multiValue/multiValueObservables'
36
+
37
+ interface BaseYAxisContext {
38
+ selection: d3.Selection<any, unknown, any, unknown>
39
+ computedData$: Observable<ComputedDataMultiValue>
40
+ // filteredMinMaxValue$: Observable<[number, number]>
41
+ fullParams$: Observable<BaseYAxisParams>
42
+ fullDataFormatter$: Observable<DataFormatterMultiValue>
43
+ fullChartParams$: Observable<ChartParams>
44
+ isCategorySeprate$: Observable<boolean>
45
+ containerPosition$: Observable<ContainerPositionScaled[]>
46
+ containerSize$: Observable<ContainerSize>
47
+ // layout$: Observable<Layout>
48
+ // filteredXYMinMaxData$: Observable<{
49
+ // datumList: ComputedXYDatumMultiValue[];
50
+ // minXDatum: ComputedXYDatumMultiValue | null;
51
+ // maxXDatum: ComputedXYDatumMultiValue | null;
52
+ // minYDatum: ComputedXYDatumMultiValue | null;
53
+ // maxYDatum: ComputedXYDatumMultiValue | null;
54
+ // }>
55
+ yScale$: Observable<d3.ScaleLinear<number, number>>
56
+ // xyMinMax$: Observable<{
57
+ // minX: number;
58
+ // maxX: number;
59
+ // minY: number;
60
+ // maxY: number;
61
+ // }>
62
+ }
63
+
64
+ // interface TextAlign {
65
+ // textAnchor: "start" | "middle" | "end"
66
+ // dominantBaseline: "middle" | "auto" | "hanging"
67
+ // }
68
+
69
+ const defaultTickSize = 6
70
+
71
+ const yTickTextAnchor = 'end'
72
+ const yTickDominantBaseline = 'middle'
73
+ const yAxisLabelAnchor = 'end'
74
+ const yAxisLabelDominantBaseline = 'auto'
75
+
76
+ function renderYAxisLabel ({ selection, yLabelClassName, fullParams, containerSize, fullDataFormatter, fullChartParams }: {
77
+ selection: d3.Selection<SVGGElement, any, any, any>,
78
+ yLabelClassName: string
79
+ fullParams: BaseYAxisParams
80
+ // axisLabelAlign: TextAlign
81
+ containerSize: { width: number, height: number }
82
+ fullDataFormatter: DataFormatterMultiValue,
83
+ fullChartParams: ChartParams
84
+ // textReverseTransform: string,
85
+ }) {
86
+ const offsetX = fullParams.tickPadding - fullParams.labelOffset[0]
87
+ const offsetY = fullParams.tickPadding + fullParams.labelOffset[1]
88
+ let labelX = - offsetX
89
+ let labelY = - offsetY
90
+
91
+ const axisLabelSelection = selection
92
+ .selectAll<SVGGElement, BaseYAxisParams>(`g.${yLabelClassName}`)
93
+ .data([fullParams])
94
+ .join('g')
95
+ .classed(yLabelClassName, true)
96
+ .each((d, i, g) => {
97
+ const text = d3.select(g[i])
98
+ .selectAll<SVGTextElement, BaseYAxisParams>(`text`)
99
+ .data([d])
100
+ .join(
101
+ enter => {
102
+ return enter
103
+ .append('text')
104
+ .style('font-weight', 'bold')
105
+ },
106
+ update => update,
107
+ exit => exit.remove()
108
+ )
109
+ .attr('text-anchor', yAxisLabelAnchor)
110
+ .attr('dominant-baseline', yAxisLabelDominantBaseline)
111
+ .attr('font-size', fullChartParams.styles.textSize)
112
+ .style('fill', getColor(fullParams.labelColorType, fullChartParams))
113
+ // .style('transform', textReverseTransform)
114
+ // 偏移使用 x, y 而非 transform 才不會受到外層 scale 變形影響
115
+ .attr('x', labelX)
116
+ .attr('y', labelY)
117
+ .text(d => fullDataFormatter.yAxis.label)
118
+ })
119
+ // .attr('transform', d => `translate(0, ${layout.height})`)
120
+ }
121
+
122
+ function renderYAxis ({ selection, yAxisClassName, fullParams, containerSize, fullDataFormatter, fullChartParams, yScale }: {
123
+ selection: d3.Selection<SVGGElement, any, any, any>,
124
+ yAxisClassName: string
125
+ fullParams: BaseYAxisParams
126
+ // tickTextAlign: TextAlign
127
+ containerSize: ContainerSize
128
+ fullDataFormatter: DataFormatterMultiValue,
129
+ fullChartParams: ChartParams
130
+ yScale: d3.ScaleLinear<number, number>
131
+ // textReverseTransform: string,
132
+ // xyMinMax: {
133
+ // minX: number;
134
+ // maxX: number;
135
+ // minY: number;
136
+ // maxY: number;
137
+ // }
138
+ }) {
139
+
140
+ const yAxisSelection = selection
141
+ .selectAll<SVGGElement, BaseYAxisParams>(`g.${yAxisClassName}`)
142
+ .data([fullParams])
143
+ .join('g')
144
+ .classed(yAxisClassName, true)
145
+
146
+ // const _yScale = d3.scaleLinear()
147
+ // .domain([0, 150])
148
+ // .range([416.5, 791.349])
149
+
150
+ // 刻度文字偏移
151
+ let tickPadding = fullParams.tickPadding
152
+
153
+ // 設定Y軸刻度
154
+ const yAxis = d3.axisLeft(yScale)
155
+ .scale(yScale)
156
+ .ticks(fullParams.ticks) // 刻度分段數量
157
+ .tickFormat(d => parseTickFormatValue(d, fullParams.tickFormat))
158
+ .tickSize(fullParams.tickFullLine == true
159
+ ? -containerSize.width
160
+ : defaultTickSize)
161
+ .tickPadding(tickPadding)
162
+
163
+ const yAxisEl = yAxisSelection
164
+ .transition()
165
+ .duration(100)
166
+ .ease(d3.easeLinear) // 線性的 - 當托曳或快速變動的時候比較滑順
167
+ .call(yAxis)
168
+
169
+ yAxisEl.selectAll('line')
170
+ .style('fill', 'none')
171
+ .style('stroke', fullParams.tickLineVisible == true ? getColor(fullParams.tickColorType, fullChartParams) : 'none')
172
+ .style('stroke-dasharray', fullParams.tickFullLineDasharray)
173
+ .attr('pointer-events', 'none')
174
+
175
+ yAxisEl.selectAll('path')
176
+ .style('fill', 'none')
177
+ // .style('stroke', this.fullParams.axisLineColor!)
178
+ .style('stroke', fullParams.axisLineVisible == true ? getColor(fullParams.axisLineColorType, fullChartParams) : 'none')
179
+ .style('shape-rendering', 'crispEdges')
180
+
181
+ // const yText = yAxisEl.selectAll('text')
182
+ const yText = yAxisSelection.selectAll('text')
183
+ // .style('font-family', 'sans-serif')
184
+ .attr('font-size', fullChartParams.styles.textSize)
185
+ .style('color', getColor(fullParams.tickTextColorType, fullChartParams))
186
+ .attr('text-anchor', yTickTextAnchor)
187
+ .attr('dominant-baseline', yTickDominantBaseline)
188
+ // .attr('dy', 0)
189
+ .attr('x', - tickPadding)
190
+ .attr('dy', 0)
191
+ // yText.style('transform', textReverseTransform)
192
+
193
+ // // 抵消掉預設的偏移
194
+ // if (fullDataFormatter.grid.valueAxis.position === 'bottom' || fullDataFormatter.grid.valueAxis.position === 'top') {
195
+ // yText.attr('dy', 0)
196
+ // }
197
+
198
+ return yAxisSelection
199
+ }
200
+
201
+ export const createBaseYAxis: BasePluginFn<BaseYAxisContext> = (pluginName: string, {
202
+ selection,
203
+ computedData$,
204
+ fullParams$,
205
+ fullDataFormatter$,
206
+ fullChartParams$,
207
+ isCategorySeprate$,
208
+ containerPosition$,
209
+ containerSize$,
210
+ // layout$,
211
+ // filteredXYMinMaxData$,
212
+ yScale$,
213
+ // xyMinMax$
214
+ }) => {
215
+
216
+ const destroy$ = new Subject()
217
+
218
+ // const containerClassName = getClassName(pluginName, 'container')
219
+ const yAxisGClassName = getClassName(pluginName, 'yAxisG')
220
+ const yAxisClassName = getClassName(pluginName, 'yAxis')
221
+ const yLabelClassName = getClassName(pluginName, 'yLabel')
222
+
223
+ const containerSelection$ = multiValueContainerSelectionsObservable({
224
+ selection,
225
+ pluginName,
226
+ clipPathID: null,
227
+ computedData$,
228
+ containerPosition$,
229
+ isCategorySeprate$,
230
+ }).pipe(
231
+ takeUntil(destroy$),
232
+ )
233
+
234
+ // const containerSelection$ = combineLatest({
235
+ // computedData: computedData$.pipe(
236
+ // distinctUntilChanged((a, b) => {
237
+ // // 只有當series的數量改變時,才重新計算
238
+ // return a.length === b.length
239
+ // }),
240
+ // ),
241
+ // isCategorySeprate: isCategorySeprate$
242
+ // }).pipe(
243
+ // takeUntil(destroy$),
244
+ // switchMap(async (d) => d),
245
+ // map(data => {
246
+ // return data.isCategorySeprate
247
+ // // category分開的時候顯示各別axis
248
+ // ? data.computedData
249
+ // // category合併的時候只顯示第一個axis
250
+ // : [data.computedData[0]]
251
+ // }),
252
+ // map((computedData, i) => {
253
+ // return selection
254
+ // .selectAll<SVGGElement, ComputedDatumMultiValue[]>(`g.${containerClassName}`)
255
+ // .data(computedData, d => d[0] ? d[0].categoryIndex : i)
256
+ // .join('g')
257
+ // .classed(containerClassName, true)
258
+ // })
259
+ // )
260
+
261
+ const axisSelection$ = containerSelection$.pipe(
262
+ takeUntil(destroy$),
263
+ map((containerSelection, i) => {
264
+ return containerSelection
265
+ .selectAll<SVGGElement, ComputedDatumMultiValue[]>(`g.${yAxisGClassName}`)
266
+ .data([yAxisGClassName])
267
+ .join('g')
268
+ .classed(yAxisGClassName, true)
269
+ })
270
+ )
271
+
272
+ // combineLatest({
273
+ // containerSelection: containerSelection$,
274
+ // gridContainerPosition: containerPosition$
275
+ // }).pipe(
276
+ // takeUntil(destroy$),
277
+ // switchMap(async d => d)
278
+ // ).subscribe(data => {
279
+ // data.containerSelection
280
+ // .attr('transform', (d, i) => {
281
+ // const gridContainerPosition = data.gridContainerPosition[i] ?? data.gridContainerPosition[0]
282
+ // const translate = gridContainerPosition.translate
283
+ // const scale = gridContainerPosition.scale
284
+ // // return `translate(${translate[0]}, ${translate[1]}) scale(${scale[0]}, ${scale[1]})`
285
+ // return `translate(${translate[0]}, ${translate[1]})`
286
+ // })
287
+ // // .attr('opacity', 0)
288
+ // // .transition()
289
+ // // .attr('opacity', 1)
290
+ // })
291
+
292
+ // const textReverseTransform$ = containerPosition$.pipe(
293
+ // takeUntil(destroy$),
294
+ // switchMap(async (d) => d),
295
+ // map(containerPosition => {
296
+ // // const axesRotateXYReverseValue = `rotateX(${data.gridAxesReverseTransform.rotateX}deg) rotateY(${data.gridAxesReverseTransform.rotateY}deg)`
297
+ // // const axesRotateReverseValue = `rotate(${data.gridAxesReverseTransform.rotate}deg)`
298
+ // const containerScaleReverseValue = `scale(${1 / containerPosition[0].scale[0]}, ${1 / containerPosition[0].scale[1]})`
299
+ // // 抵消最外層scale
300
+ // return `${containerScaleReverseValue}`
301
+ // }),
302
+ // distinctUntilChanged()
303
+ // )
304
+
305
+ // const yScale$: Observable<d3.ScaleLinear<number, number>> = new Observable(subscriber => {
306
+ // combineLatest({
307
+ // fullDataFormatter: fullDataFormatter$,
308
+ // layout: layout$,
309
+ // // xyMinMax: observer.xyMinMax$
310
+ // filteredXYMinMaxData: filteredXYMinMaxData$
311
+ // }).pipe(
312
+ // takeUntil(destroy$),
313
+ // switchMap(async (d) => d),
314
+ // ).subscribe(data => {
315
+ // const valueIndex = data.fullDataFormatter.xAxis.valueIndex
316
+ // if (!data.filteredXYMinMaxData.minYDatum || !data.filteredXYMinMaxData.maxYDatum
317
+ // || data.filteredXYMinMaxData.minYDatum.value[valueIndex] == null || data.filteredXYMinMaxData.maxYDatum.value[valueIndex] == null
318
+ // ) {
319
+ // return
320
+ // }
321
+ // let maxValue = data.filteredXYMinMaxData.maxYDatum.value[valueIndex]
322
+ // let minValue = data.filteredXYMinMaxData.minYDatum.value[valueIndex]
323
+ // if (maxValue === minValue && maxValue === 0) {
324
+ // // 避免最大及最小值同等於 0 造成無法計算scale
325
+ // maxValue = 1
326
+ // }
327
+
328
+ // const yScale: d3.ScaleLinear<number, number> = createValueToAxisScale({
329
+ // maxValue,
330
+ // minValue,
331
+ // axisWidth: data.layout.height,
332
+ // scaleDomain: data.fullDataFormatter.yAxis.scaleDomain,
333
+ // scaleRange: data.fullDataFormatter.yAxis.scaleRange,
334
+ // reverse: true
335
+ // })
336
+
337
+ // subscriber.next(yScale)
338
+ // })
339
+ // })
340
+
341
+
342
+ combineLatest({
343
+ axisSelection: axisSelection$,
344
+ fullParams: fullParams$,
345
+ // tickTextAlign: tickTextAlign$,
346
+ // axisLabelAlign: axisLabelAlign$,
347
+ computedData: computedData$,
348
+ containerSize: containerSize$,
349
+ fullDataFormatter: fullDataFormatter$,
350
+ fullChartParams: fullChartParams$,
351
+ yScale: yScale$,
352
+ // textReverseTransform: textReverseTransform$,
353
+ // xyMinMax: xyMinMax$
354
+ }).pipe(
355
+ takeUntil(destroy$),
356
+ switchMap(async (d) => d),
357
+ ).subscribe(data => {
358
+
359
+ renderYAxis({
360
+ selection: data.axisSelection,
361
+ yAxisClassName,
362
+ fullParams: data.fullParams,
363
+ // tickTextAlign: data.tickTextAlign,
364
+ containerSize: data.containerSize,
365
+ fullDataFormatter: data.fullDataFormatter,
366
+ fullChartParams: data.fullChartParams,
367
+ yScale: data.yScale,
368
+ // textReverseTransform: data.textReverseTransform,
369
+ // xyMinMax: data.xyMinMax
370
+ })
371
+
372
+ renderYAxisLabel({
373
+ selection: data.axisSelection,
374
+ yLabelClassName,
375
+ fullParams: data.fullParams,
376
+ // axisLabelAlign: data.axisLabelAlign,
377
+ containerSize: data.containerSize,
378
+ fullDataFormatter: data.fullDataFormatter,
379
+ fullChartParams: data.fullChartParams,
380
+ // textReverseTransform: data.textReverseTransform,
381
+ })
382
+
383
+ })
384
+
385
+
386
+ return () => {
387
+ destroy$.next(undefined)
388
+ }
389
+ }