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