@orbcharts/plugins-basic 3.0.8 → 3.0.10

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 +6182 -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 +298 -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,600 +1,600 @@
1
- import * as d3 from 'd3'
2
- import {
3
- combineLatest,
4
- switchMap,
5
- distinctUntilChanged,
6
- first,
7
- map,
8
- takeUntil,
9
- Observable,
10
- Subject } from 'rxjs'
11
- import { createValueToAxisScale } from '../../lib/core'
12
- import type { BasePluginFn } from './types'
13
- import type {
14
- ComputedDataGrid,
15
- DataFormatterGrid,
16
- ChartParams,
17
- ComputedDatumGrid,
18
- ContainerPositionScaled,
19
- ContainerSize,
20
- TransformData,
21
- EventGrid,
22
- ColorType
23
- } from '../../lib/core-types'
24
- import type { BaseValueAxisParams } from '../../lib/plugins-basic-types'
25
- import { parseTickFormatValue } from '../utils/d3Utils'
26
- import { getColor, getMinMaxValue, getClassName, getUniID } from '../utils/orbchartsUtils'
27
- import { gridContainerSelectionsObservable } from '../grid/gridObservables'
28
-
29
- // export interface BaseValueAxisParams {
30
- // labelOffset: [number, number]
31
- // labelColorType: ColorType
32
- // axisLineVisible: boolean
33
- // axisLineColorType: ColorType
34
- // ticks: number
35
- // tickFormat: string | ((text: d3.NumberValue) => string)
36
- // tickLineVisible: boolean
37
- // tickPadding: number
38
- // tickFullLine: boolean
39
- // tickFullLineDasharray: string
40
- // tickColorType: ColorType
41
- // tickTextRotate: number
42
- // tickTextColorType: ColorType
43
- // }
44
-
45
- interface BaseValueAxisContext {
46
- selection: d3.Selection<any, unknown, any, unknown>
47
- computedData$: Observable<ComputedDataGrid>
48
- filteredMinMaxValue$: Observable<[number, number]>
49
- fullParams$: Observable<BaseValueAxisParams>
50
- fullDataFormatter$: Observable<DataFormatterGrid>
51
- fullChartParams$: Observable<ChartParams>
52
- gridAxesTransform$: Observable<TransformData>
53
- gridAxesReverseTransform$: Observable<TransformData>
54
- // gridAxesSize$: Observable<{
55
- // width: number;
56
- // height: number;
57
- // }>
58
- gridContainerPosition$: Observable<ContainerPositionScaled[]>
59
- gridAxesSize$: Observable<ContainerSize>
60
- // gridAxesContainerSize$: Observable<ContainerSize>
61
- isSeriesSeprate$: Observable<boolean>
62
- }
63
-
64
- interface TextAlign {
65
- textAnchor: "start" | "middle" | "end"
66
- dominantBaseline: "middle" | "auto" | "hanging"
67
- }
68
-
69
- // const pluginName = 'ValueAxis'
70
- // const containerClassName = getClassName(pluginName, 'container')
71
- // const yAxisGClassName = getClassName(pluginName, 'yAxisG')
72
- // const yAxisClassName = getClassName(pluginName, 'yAxis')
73
- // const textClassName = getClassName(pluginName, 'text')
74
- const defaultTickSize = 6
75
-
76
- function renderAxisLabel ({ selection, textClassName, fullParams, axisLabelAlign, gridAxesSize, fullDataFormatter, fullChartParams, textReverseTransform }: {
77
- selection: d3.Selection<SVGGElement, any, any, any>,
78
- textClassName: string
79
- fullParams: BaseValueAxisParams
80
- axisLabelAlign: TextAlign
81
- gridAxesSize: ContainerSize
82
- fullDataFormatter: DataFormatterGrid,
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 = 0
89
- let labelY = 0
90
- if (fullDataFormatter.groupAxis.position === 'bottom') {
91
- // labelY = - gridAxesSize.height - offsetY
92
- labelY = - offsetY
93
- if (fullDataFormatter.valueAxis.position === 'left') {
94
- labelX = - offsetX
95
- } else if (fullDataFormatter.valueAxis.position === 'right') {
96
- labelX = offsetX
97
- }
98
- } else if (fullDataFormatter.groupAxis.position === 'top') {
99
- // labelY = gridAxesSize.height + offsetY
100
- labelY = offsetY
101
- if (fullDataFormatter.valueAxis.position === 'left') {
102
- labelX = - offsetX
103
- } else if (fullDataFormatter.valueAxis.position === 'right') {
104
- labelX = offsetX
105
- }
106
- } else if (fullDataFormatter.groupAxis.position === 'left') {
107
- // labelX = gridAxesSize.width + offsetX
108
- labelX = offsetX
109
- if (fullDataFormatter.valueAxis.position === 'bottom') {
110
- labelY = offsetY
111
- } else if (fullDataFormatter.valueAxis.position === 'top') {
112
- labelY = - offsetY
113
- }
114
- } else if (fullDataFormatter.groupAxis.position === 'right') {
115
- labelX = - offsetX
116
- if (fullDataFormatter.valueAxis.position === 'bottom') {
117
- labelY = offsetY
118
- } else if (fullDataFormatter.valueAxis.position === 'top') {
119
- labelY = - offsetY
120
- }
121
- }
122
-
123
- const axisLabelSelection = selection
124
- .selectAll<SVGGElement, BaseValueAxisParams>(`g.${textClassName}`)
125
- .data([fullParams])
126
- .join('g')
127
- .classed(textClassName, true)
128
- .each((d, i, g) => {
129
- const text = d3.select(g[i])
130
- .selectAll<SVGTextElement, BaseValueAxisParams>(`text`)
131
- .data([d])
132
- .join(
133
- enter => {
134
- return enter
135
- .append('text')
136
- .style('font-weight', 'bold')
137
- },
138
- update => update,
139
- exit => exit.remove()
140
- )
141
- .attr('text-anchor', axisLabelAlign.textAnchor)
142
- .attr('dominant-baseline', axisLabelAlign.dominantBaseline)
143
- .attr('font-size', fullChartParams.styles.textSize)
144
- .style('fill', getColor(fullParams.labelColorType, fullChartParams))
145
- .style('transform', textReverseTransform)
146
- // 偏移使用 x, y 而非 transform 才不會受到外層 scale 變形影響
147
- .attr('x', labelX)
148
- .attr('y', labelY)
149
- .text(d => fullDataFormatter.valueAxis.label)
150
- })
151
- .attr('transform', d => `translate(0, ${gridAxesSize.height})`)
152
- // .attr('transform', d => `translate(${- fullParams.tickPadding + fullParams.labelOffset[0]}, ${gridAxesSize.height + fullParams.tickPadding + fullParams.labelOffset[1]})`)
153
-
154
-
155
- }
156
-
157
- function renderAxis ({ selection, yAxisClassName, fullParams, tickTextAlign, gridAxesSize, fullDataFormatter, fullChartParams, valueScale, textReverseTransformWithRotate, filteredMinMaxValue }: {
158
- selection: d3.Selection<SVGGElement, any, any, any>,
159
- yAxisClassName: string
160
- fullParams: BaseValueAxisParams
161
- tickTextAlign: TextAlign
162
- gridAxesSize: ContainerSize
163
- fullDataFormatter: DataFormatterGrid,
164
- fullChartParams: ChartParams
165
- valueScale: d3.ScaleLinear<number, number>
166
- textReverseTransformWithRotate: string,
167
- filteredMinMaxValue: [number, number]
168
- }) {
169
-
170
- const yAxisSelection = selection
171
- .selectAll<SVGGElement, BaseValueAxisParams>(`g.${yAxisClassName}`)
172
- .data([fullParams])
173
- .join('g')
174
- .classed(yAxisClassName, true)
175
-
176
- // const _valueScale = d3.scaleLinear()
177
- // .domain([0, 150])
178
- // .range([416.5, 791.349])
179
-
180
- // 刻度文字偏移
181
- let tickPadding = 0
182
- let textY = 0
183
- if (fullDataFormatter.valueAxis.position === 'left') {
184
- tickPadding = fullParams.tickPadding
185
- textY = 0
186
- } else if (fullDataFormatter.valueAxis.position === 'right') {
187
- tickPadding = - fullParams.tickPadding
188
- textY = 0
189
- } else if (fullDataFormatter.valueAxis.position === 'bottom') {
190
- tickPadding = 0
191
- textY = fullParams.tickPadding
192
- } else if (fullDataFormatter.valueAxis.position === 'top') {
193
- tickPadding = 0
194
- textY = - fullParams.tickPadding
195
- }
196
-
197
- // 設定Y軸刻度
198
- const yAxis = d3.axisLeft(valueScale)
199
- .scale(valueScale)
200
- .ticks(fullParams.ticks) // 刻度分段數量
201
- .tickFormat(d => parseTickFormatValue(d, fullParams.tickFormat))
202
- .tickSize(fullParams.tickFullLine == true
203
- ? -gridAxesSize.width
204
- : defaultTickSize)
205
- .tickPadding(tickPadding)
206
-
207
- const yAxisEl = yAxisSelection
208
- .transition()
209
- .duration(100)
210
- .ease(d3.easeLinear) // 線性的 - 當托曳或快速變動的時候比較滑順
211
- .call(yAxis)
212
-
213
- yAxisEl.selectAll('line')
214
- .style('fill', 'none')
215
- .style('stroke', fullParams.tickLineVisible == true ? getColor(fullParams.tickColorType, fullChartParams) : 'none')
216
- .style('stroke-dasharray', fullParams.tickFullLineDasharray)
217
- .style('vector-effect', 'non-scaling-stroke') // 避免 scale 導致線條變形
218
- .attr('pointer-events', 'none')
219
-
220
- yAxisEl.selectAll('path')
221
- .style('fill', 'none')
222
- // .style('stroke', this.fullParams.axisLineColor!)
223
- .style('stroke', fullParams.axisLineVisible == true ? getColor(fullParams.axisLineColorType, fullChartParams) : 'none')
224
- .style('shape-rendering', 'crispEdges')
225
-
226
- // const yText = yAxisEl.selectAll('text')
227
- const yText = yAxisSelection.selectAll('text')
228
- // .style('font-family', 'sans-serif')
229
- .attr('font-size', fullChartParams.styles.textSize)
230
- .style('color', getColor(fullParams.tickTextColorType, fullChartParams))
231
- .attr('text-anchor', tickTextAlign.textAnchor)
232
- .attr('dominant-baseline', tickTextAlign.dominantBaseline)
233
- // .attr('dy', 0)
234
- .attr('y', textY)
235
- .attr('dy', 0)
236
- yText.style('transform', textReverseTransformWithRotate)
237
-
238
- // 抵消掉預設的偏移
239
- if (fullDataFormatter.valueAxis.position === 'bottom' || fullDataFormatter.valueAxis.position === 'top') {
240
- yText.attr('dy', 0)
241
- }
242
-
243
- return yAxisSelection
244
- }
245
-
246
-
247
-
248
- export const createBaseValueAxis: BasePluginFn<BaseValueAxisContext> = (pluginName: string, {
249
- selection,
250
- computedData$,
251
- filteredMinMaxValue$,
252
- fullParams$,
253
- fullDataFormatter$,
254
- fullChartParams$,
255
- gridAxesSize$,
256
- gridAxesTransform$,
257
- gridAxesReverseTransform$,
258
- // gridAxesSize$,
259
- gridContainerPosition$,
260
- // gridAxesContainerSize$,
261
- isSeriesSeprate$,
262
- }) => {
263
-
264
- const destroy$ = new Subject()
265
-
266
- const containerClassName = getClassName(pluginName, 'container')
267
- const yAxisGClassName = getClassName(pluginName, 'yAxisG')
268
- const yAxisClassName = getClassName(pluginName, 'yAxis')
269
- const textClassName = getClassName(pluginName, 'text')
270
-
271
- const containerSelection$ = gridContainerSelectionsObservable({
272
- selection,
273
- pluginName,
274
- computedData$,
275
- gridContainerPosition$,
276
- isSeriesSeprate$
277
- })
278
-
279
- // const containerSelection$ = combineLatest({
280
- // computedData: computedData$.pipe(
281
- // distinctUntilChanged((a, b) => {
282
- // // 只有當series的數量改變時,才重新計算
283
- // return a.length === b.length
284
- // }),
285
- // ),
286
- // isSeriesSeprate: isSeriesSeprate$
287
- // }).pipe(
288
- // takeUntil(destroy$),
289
- // switchMap(async (d) => d),
290
- // map(data => {
291
- // return data.isSeriesSeprate
292
- // // series分開的時候顯示各別axis
293
- // ? data.computedData
294
- // // series合併的時候只顯示第一個axis
295
- // : [data.computedData[0]]
296
- // }),
297
- // map((computedData, i) => {
298
- // return selection
299
- // .selectAll<SVGGElement, ComputedDatumGrid[]>(`g.${containerClassName}`)
300
- // .data(computedData, d => d[0] ? d[0].seriesIndex : i)
301
- // .join('g')
302
- // .classed(containerClassName, true)
303
- // })
304
- // )
305
-
306
- const axisSelection$ = containerSelection$.pipe(
307
- takeUntil(destroy$),
308
- map((containerSelection, i) => {
309
- return containerSelection
310
- .selectAll<SVGGElement, ComputedDatumGrid[]>(`g.${yAxisGClassName}`)
311
- .data([yAxisGClassName])
312
- .join('g')
313
- .classed(yAxisGClassName, true)
314
- })
315
- )
316
-
317
- // combineLatest({
318
- // containerSelection: containerSelection$,
319
- // gridContainerPosition: gridContainerPosition$
320
- // }).pipe(
321
- // takeUntil(destroy$),
322
- // switchMap(async d => d)
323
- // ).subscribe(data => {
324
- // data.containerSelection
325
- // .attr('transform', (d, i) => {
326
- // const gridContainerPosition = data.gridContainerPosition[i] ?? data.gridContainerPosition[0]
327
- // const translate = gridContainerPosition.translate
328
- // const scale = gridContainerPosition.scale
329
- // // return `translate(${translate[0]}, ${translate[1]}) scale(${scale[0]}, ${scale[1]})`
330
- // return `translate(${translate[0]}, ${translate[1]})`
331
- // })
332
- // // .attr('opacity', 0)
333
- // // .transition()
334
- // // .attr('opacity', 1)
335
- // })
336
-
337
- combineLatest({
338
- axisSelection: axisSelection$,
339
- gridAxesTransform: gridAxesTransform$,
340
- }).pipe(
341
- takeUntil(destroy$),
342
- switchMap(async d => d)
343
- ).subscribe(data => {
344
- data.axisSelection
345
- .style('transform', data.gridAxesTransform.value)
346
- // .attr('opacity', 0)
347
- // .transition()
348
- // .attr('opacity', 1)
349
-
350
- })
351
-
352
- // const gridAxesSize$ = gridAxisSizeObservable({
353
- // fullDataFormatter$,
354
- // layout$
355
- // })
356
-
357
- // const textReverseTransform$: Observable<string> = new Observable(subscriber => {
358
- // combineLatest({
359
- // fullParams: fullParams$,
360
- // layout: layout$
361
- // }).pipe(
362
- // takeUntil(destroy$),
363
- // // 轉換後會退訂前一個未完成的訂閱事件,因此可以取到「同時間」最後一次的訂閱事件
364
- // switchMap(async (d) => d),
365
- // ).subscribe(data => {
366
-
367
- // const transformData = Object.assign({}, data.layout.content.axesTransformData)
368
-
369
- // const value = getAxesTransformValue({
370
- // translate: [0, 0],
371
- // scale: [transformData.scale[0] * -1, transformData.scale[1] * -1],
372
- // rotate: transformData.rotate * -1 + data.fullParams.tickTextRotate,
373
- // rotateX: transformData.rotateX * -1,
374
- // rotateY: transformData.rotateY * -1
375
- // })
376
-
377
- // subscriber.next(value)
378
- // })
379
- // })
380
- // const reverseTransform$: Observable<TransformData> = gridAxesTransform$.pipe(
381
- // takeUntil(destroy$),
382
- // map(d => {
383
- // const translate: [number, number] = [d.translate[0] * -1, d.translate[1] * -1]
384
- // const scale: [number, number] = [d.scale[0] * -1, d.scale[1] * -1]
385
- // const rotate = d.rotate * -1
386
- // const rotateX = d.rotateX * -1
387
- // const rotateY = d.rotateY * -1
388
- // return {
389
- // translate,
390
- // scale,
391
- // rotate,
392
- // rotateX,
393
- // rotateY,
394
- // value: ''
395
- // }
396
- // }),
397
- // )
398
- const textReverseTransform$ = combineLatest({
399
- gridAxesReverseTransform: gridAxesReverseTransform$,
400
- gridContainerPosition: gridContainerPosition$
401
- }).pipe(
402
- takeUntil(destroy$),
403
- switchMap(async (d) => d),
404
- map(data => {
405
- // const axisReverseTranslateValue = `translate(${data.gridAxesReverseTransform.translate[0]}px, ${data.gridAxesReverseTransform.translate[1]}px)`
406
- const axesRotateXYReverseValue = `rotateX(${data.gridAxesReverseTransform.rotateX}deg) rotateY(${data.gridAxesReverseTransform.rotateY}deg)`
407
- const axesRotateReverseValue = `rotate(${data.gridAxesReverseTransform.rotate}deg)`
408
- const containerScaleReverseValue = `scale(${1 / data.gridContainerPosition[0].scale[0]}, ${1 / data.gridContainerPosition[0].scale[1]})`
409
- // 必須按照順序(先抵消外層rotate,再抵消最外層scale)
410
- return `${axesRotateXYReverseValue} ${axesRotateReverseValue} ${containerScaleReverseValue}`
411
- }),
412
- distinctUntilChanged()
413
- )
414
-
415
- const textReverseTransformWithRotate$ = combineLatest({
416
- textReverseTransform: textReverseTransform$,
417
- fullParams: fullParams$,
418
- }).pipe(
419
- takeUntil(destroy$),
420
- switchMap(async (d) => d),
421
- map(data => {
422
- // 必須按照順序(先抵消外層rotate,再抵消最外層scale,最後再做本身的rotate)
423
- return `${data.textReverseTransform} rotate(${data.fullParams.tickTextRotate}deg)`
424
- })
425
- )
426
-
427
- // const minMax$: Observable<[number, number]> = new Observable(subscriber => {
428
- // combineLatest({
429
- // fullDataFormatter: fullDataFormatter$,
430
- // computedData: computedData$
431
- // }).pipe(
432
- // takeUntil(destroy$),
433
- // switchMap(async (d) => d),
434
- // ).subscribe(data => {
435
- // const groupMin = 0
436
- // const groupMax = data.computedData[0] ? data.computedData[0].length - 1 : 0
437
- // // const groupScaleDomainMin = data.fullDataFormatter.groupAxis.scaleDomain[0] === 'auto'
438
- // // ? groupMin - data.fullDataFormatter.groupAxis.scalePadding
439
- // // : data.fullDataFormatter.groupAxis.scaleDomain[0] as number - data.fullDataFormatter.groupAxis.scalePadding
440
- // const groupScaleDomainMin = data.fullDataFormatter.groupAxis.scaleDomain[0] - data.fullDataFormatter.groupAxis.scalePadding
441
- // const groupScaleDomainMax = data.fullDataFormatter.groupAxis.scaleDomain[1] === 'max'
442
- // ? groupMax + data.fullDataFormatter.groupAxis.scalePadding
443
- // : data.fullDataFormatter.groupAxis.scaleDomain[1] as number + data.fullDataFormatter.groupAxis.scalePadding
444
-
445
- // const filteredData = data.computedData.map((d, i) => {
446
- // return d.filter((_d, _i) => {
447
- // return _i >= groupScaleDomainMin && _i <= groupScaleDomainMax
448
- // })
449
- // })
450
-
451
- // const filteredMinMax = getMinMaxValue(filteredData.flat())
452
- // if (filteredMinMax[0] === filteredMinMax[1]) {
453
- // filteredMinMax[0] = filteredMinMax[1] - 1 // 避免最大及最小值相同造成無法計算scale
454
- // }
455
- // subscriber.next(filteredMinMax)
456
- // })
457
- // })
458
-
459
- const valueScale$: Observable<d3.ScaleLinear<number, number>> = new Observable(subscriber => {
460
- combineLatest({
461
- fullDataFormatter: fullDataFormatter$,
462
- gridAxesSize: gridAxesSize$,
463
- // minMax: minMax$
464
- filteredMinMaxValue: filteredMinMaxValue$
465
- }).pipe(
466
- takeUntil(destroy$),
467
- switchMap(async (d) => d),
468
- ).subscribe(data => {
469
- let maxValue = data.filteredMinMaxValue[1]
470
- let minValue = data.filteredMinMaxValue[0]
471
- if (maxValue === minValue && maxValue === 0) {
472
- // 避免最大及最小值同等於 0 造成無法計算scale
473
- maxValue = 1
474
- }
475
-
476
- const valueScale: d3.ScaleLinear<number, number> = createValueToAxisScale({
477
- maxValue,
478
- minValue,
479
- axisWidth: data.gridAxesSize.height,
480
- scaleDomain: data.fullDataFormatter.valueAxis.scaleDomain,
481
- scaleRange: data.fullDataFormatter.valueAxis.scaleRange
482
- })
483
-
484
- subscriber.next(valueScale)
485
- })
486
- })
487
-
488
- const tickTextAlign$: Observable<TextAlign> = combineLatest({
489
- fullDataFormatter: fullDataFormatter$,
490
- fullParams: fullParams$
491
- }).pipe(
492
- takeUntil(destroy$),
493
- switchMap(async (d) => d),
494
- map(data => {
495
- let textAnchor: 'start' | 'middle' | 'end' = 'start'
496
- let dominantBaseline: 'auto' | 'middle' | 'hanging' = 'hanging'
497
-
498
- if (data.fullDataFormatter.valueAxis.position === 'left') {
499
- textAnchor = 'end'
500
- dominantBaseline = 'middle'
501
- } else if (data.fullDataFormatter.valueAxis.position === 'right') {
502
- textAnchor = 'start'
503
- dominantBaseline = 'middle'
504
- } else if (data.fullDataFormatter.valueAxis.position === 'bottom') {
505
- textAnchor = data.fullParams.tickTextRotate
506
- ? 'end'
507
- : 'middle'
508
- dominantBaseline = 'hanging'
509
- } else if (data.fullDataFormatter.valueAxis.position === 'top') {
510
- textAnchor = data.fullParams.tickTextRotate
511
- ? 'start'
512
- : 'middle'
513
- dominantBaseline = 'auto'
514
- }
515
- return {
516
- textAnchor,
517
- dominantBaseline
518
- }
519
- })
520
- )
521
-
522
- const axisLabelAlign$: Observable<TextAlign> = fullDataFormatter$.pipe(
523
- takeUntil(destroy$),
524
- map(d => {
525
- let textAnchor: 'start' | 'middle' | 'end' = 'start'
526
- let dominantBaseline: 'auto' | 'middle' | 'hanging' = 'hanging'
527
-
528
- if (d.groupAxis.position === 'bottom') {
529
- dominantBaseline = 'auto'
530
- } else if (d.groupAxis.position === 'top') {
531
- dominantBaseline = 'hanging'
532
- } else if (d.groupAxis.position === 'left') {
533
- textAnchor = 'start'
534
- } else if (d.groupAxis.position === 'right') {
535
- textAnchor = 'end'
536
- }
537
- if (d.valueAxis.position === 'left') {
538
- textAnchor = 'end'
539
- } else if (d.valueAxis.position === 'right') {
540
- textAnchor = 'start'
541
- } else if (d.valueAxis.position === 'bottom') {
542
- dominantBaseline = 'hanging'
543
- } else if (d.valueAxis.position === 'top') {
544
- dominantBaseline = 'auto'
545
- }
546
- return {
547
- textAnchor,
548
- dominantBaseline
549
- }
550
- })
551
- )
552
-
553
-
554
- combineLatest({
555
- axisSelection: axisSelection$,
556
- fullParams: fullParams$,
557
- tickTextAlign: tickTextAlign$,
558
- axisLabelAlign: axisLabelAlign$,
559
- computedData: computedData$,
560
- gridAxesSize: gridAxesSize$,
561
- fullDataFormatter: fullDataFormatter$,
562
- fullChartParams: fullChartParams$,
563
- valueScale: valueScale$,
564
- textReverseTransform: textReverseTransform$,
565
- textReverseTransformWithRotate: textReverseTransformWithRotate$,
566
- filteredMinMaxValue: filteredMinMaxValue$
567
- }).pipe(
568
- takeUntil(destroy$),
569
- switchMap(async (d) => d),
570
- ).subscribe(data => {
571
-
572
- renderAxis({
573
- selection: data.axisSelection,
574
- yAxisClassName,
575
- fullParams: data.fullParams,
576
- tickTextAlign: data.tickTextAlign,
577
- gridAxesSize: data.gridAxesSize,
578
- fullDataFormatter: data.fullDataFormatter,
579
- fullChartParams: data.fullChartParams,
580
- valueScale: data.valueScale,
581
- textReverseTransformWithRotate: data.textReverseTransformWithRotate,
582
- filteredMinMaxValue: data.filteredMinMaxValue
583
- })
584
-
585
- renderAxisLabel({
586
- selection: data.axisSelection,
587
- textClassName,
588
- fullParams: data.fullParams,
589
- axisLabelAlign: data.axisLabelAlign,
590
- gridAxesSize: data.gridAxesSize,
591
- fullDataFormatter: data.fullDataFormatter,
592
- fullChartParams: data.fullChartParams,
593
- textReverseTransform: data.textReverseTransform,
594
- })
595
- })
596
-
597
- return () => {
598
- destroy$.next(undefined)
599
- }
600
- }
1
+ import * as d3 from 'd3'
2
+ import {
3
+ combineLatest,
4
+ switchMap,
5
+ distinctUntilChanged,
6
+ first,
7
+ map,
8
+ takeUntil,
9
+ Observable,
10
+ Subject } from 'rxjs'
11
+ import { createValueToAxisScale } from '../../lib/core'
12
+ import type { BasePluginFn } from './types'
13
+ import type {
14
+ ComputedDataGrid,
15
+ DataFormatterGrid,
16
+ ChartParams,
17
+ ComputedDatumGrid,
18
+ ContainerPositionScaled,
19
+ ContainerSize,
20
+ TransformData,
21
+ EventGrid,
22
+ ColorType
23
+ } from '../../lib/core-types'
24
+ import type { BaseValueAxisParams } from '../../lib/plugins-basic-types'
25
+ import { parseTickFormatValue } from '../utils/d3Utils'
26
+ import { getColor, getMinMaxValue, getClassName, getUniID } from '../utils/orbchartsUtils'
27
+ import { gridContainerSelectionsObservable } from '../grid/gridObservables'
28
+
29
+ // export interface BaseValueAxisParams {
30
+ // labelOffset: [number, number]
31
+ // labelColorType: ColorType
32
+ // axisLineVisible: boolean
33
+ // axisLineColorType: ColorType
34
+ // ticks: number
35
+ // tickFormat: string | ((text: d3.NumberValue) => string)
36
+ // tickLineVisible: boolean
37
+ // tickPadding: number
38
+ // tickFullLine: boolean
39
+ // tickFullLineDasharray: string
40
+ // tickColorType: ColorType
41
+ // tickTextRotate: number
42
+ // tickTextColorType: ColorType
43
+ // }
44
+
45
+ interface BaseValueAxisContext {
46
+ selection: d3.Selection<any, unknown, any, unknown>
47
+ computedData$: Observable<ComputedDataGrid>
48
+ filteredMinMaxValue$: Observable<[number, number]>
49
+ fullParams$: Observable<BaseValueAxisParams>
50
+ fullDataFormatter$: Observable<DataFormatterGrid>
51
+ fullChartParams$: Observable<ChartParams>
52
+ gridAxesTransform$: Observable<TransformData>
53
+ gridAxesReverseTransform$: Observable<TransformData>
54
+ // gridAxesSize$: Observable<{
55
+ // width: number;
56
+ // height: number;
57
+ // }>
58
+ gridContainerPosition$: Observable<ContainerPositionScaled[]>
59
+ gridAxesSize$: Observable<ContainerSize>
60
+ // gridAxesContainerSize$: Observable<ContainerSize>
61
+ isSeriesSeprate$: Observable<boolean>
62
+ }
63
+
64
+ interface TextAlign {
65
+ textAnchor: "start" | "middle" | "end"
66
+ dominantBaseline: "middle" | "auto" | "hanging"
67
+ }
68
+
69
+ // const pluginName = 'ValueAxis'
70
+ // const containerClassName = getClassName(pluginName, 'container')
71
+ // const yAxisGClassName = getClassName(pluginName, 'yAxisG')
72
+ // const yAxisClassName = getClassName(pluginName, 'yAxis')
73
+ // const textClassName = getClassName(pluginName, 'text')
74
+ const defaultTickSize = 6
75
+
76
+ function renderAxisLabel ({ selection, textClassName, fullParams, axisLabelAlign, gridAxesSize, fullDataFormatter, fullChartParams, textReverseTransform }: {
77
+ selection: d3.Selection<SVGGElement, any, any, any>,
78
+ textClassName: string
79
+ fullParams: BaseValueAxisParams
80
+ axisLabelAlign: TextAlign
81
+ gridAxesSize: ContainerSize
82
+ fullDataFormatter: DataFormatterGrid,
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 = 0
89
+ let labelY = 0
90
+ if (fullDataFormatter.groupAxis.position === 'bottom') {
91
+ // labelY = - gridAxesSize.height - offsetY
92
+ labelY = - offsetY
93
+ if (fullDataFormatter.valueAxis.position === 'left') {
94
+ labelX = - offsetX
95
+ } else if (fullDataFormatter.valueAxis.position === 'right') {
96
+ labelX = offsetX
97
+ }
98
+ } else if (fullDataFormatter.groupAxis.position === 'top') {
99
+ // labelY = gridAxesSize.height + offsetY
100
+ labelY = offsetY
101
+ if (fullDataFormatter.valueAxis.position === 'left') {
102
+ labelX = - offsetX
103
+ } else if (fullDataFormatter.valueAxis.position === 'right') {
104
+ labelX = offsetX
105
+ }
106
+ } else if (fullDataFormatter.groupAxis.position === 'left') {
107
+ // labelX = gridAxesSize.width + offsetX
108
+ labelX = offsetX
109
+ if (fullDataFormatter.valueAxis.position === 'bottom') {
110
+ labelY = offsetY
111
+ } else if (fullDataFormatter.valueAxis.position === 'top') {
112
+ labelY = - offsetY
113
+ }
114
+ } else if (fullDataFormatter.groupAxis.position === 'right') {
115
+ labelX = - offsetX
116
+ if (fullDataFormatter.valueAxis.position === 'bottom') {
117
+ labelY = offsetY
118
+ } else if (fullDataFormatter.valueAxis.position === 'top') {
119
+ labelY = - offsetY
120
+ }
121
+ }
122
+
123
+ const axisLabelSelection = selection
124
+ .selectAll<SVGGElement, BaseValueAxisParams>(`g.${textClassName}`)
125
+ .data([fullParams])
126
+ .join('g')
127
+ .classed(textClassName, true)
128
+ .each((d, i, g) => {
129
+ const text = d3.select(g[i])
130
+ .selectAll<SVGTextElement, BaseValueAxisParams>(`text`)
131
+ .data([d])
132
+ .join(
133
+ enter => {
134
+ return enter
135
+ .append('text')
136
+ .style('font-weight', 'bold')
137
+ },
138
+ update => update,
139
+ exit => exit.remove()
140
+ )
141
+ .attr('text-anchor', axisLabelAlign.textAnchor)
142
+ .attr('dominant-baseline', axisLabelAlign.dominantBaseline)
143
+ .attr('font-size', fullChartParams.styles.textSize)
144
+ .style('fill', getColor(fullParams.labelColorType, fullChartParams))
145
+ .style('transform', textReverseTransform)
146
+ // 偏移使用 x, y 而非 transform 才不會受到外層 scale 變形影響
147
+ .attr('x', labelX)
148
+ .attr('y', labelY)
149
+ .text(d => fullDataFormatter.valueAxis.label)
150
+ })
151
+ .attr('transform', d => `translate(0, ${gridAxesSize.height})`)
152
+ // .attr('transform', d => `translate(${- fullParams.tickPadding + fullParams.labelOffset[0]}, ${gridAxesSize.height + fullParams.tickPadding + fullParams.labelOffset[1]})`)
153
+
154
+
155
+ }
156
+
157
+ function renderAxis ({ selection, yAxisClassName, fullParams, tickTextAlign, gridAxesSize, fullDataFormatter, fullChartParams, valueScale, textReverseTransformWithRotate, filteredMinMaxValue }: {
158
+ selection: d3.Selection<SVGGElement, any, any, any>,
159
+ yAxisClassName: string
160
+ fullParams: BaseValueAxisParams
161
+ tickTextAlign: TextAlign
162
+ gridAxesSize: ContainerSize
163
+ fullDataFormatter: DataFormatterGrid,
164
+ fullChartParams: ChartParams
165
+ valueScale: d3.ScaleLinear<number, number>
166
+ textReverseTransformWithRotate: string,
167
+ filteredMinMaxValue: [number, number]
168
+ }) {
169
+
170
+ const yAxisSelection = selection
171
+ .selectAll<SVGGElement, BaseValueAxisParams>(`g.${yAxisClassName}`)
172
+ .data([fullParams])
173
+ .join('g')
174
+ .classed(yAxisClassName, true)
175
+
176
+ // const _valueScale = d3.scaleLinear()
177
+ // .domain([0, 150])
178
+ // .range([416.5, 791.349])
179
+
180
+ // 刻度文字偏移
181
+ let tickPadding = 0
182
+ let textY = 0
183
+ if (fullDataFormatter.valueAxis.position === 'left') {
184
+ tickPadding = fullParams.tickPadding
185
+ textY = 0
186
+ } else if (fullDataFormatter.valueAxis.position === 'right') {
187
+ tickPadding = - fullParams.tickPadding
188
+ textY = 0
189
+ } else if (fullDataFormatter.valueAxis.position === 'bottom') {
190
+ tickPadding = 0
191
+ textY = fullParams.tickPadding
192
+ } else if (fullDataFormatter.valueAxis.position === 'top') {
193
+ tickPadding = 0
194
+ textY = - fullParams.tickPadding
195
+ }
196
+
197
+ // 設定Y軸刻度
198
+ const yAxis = d3.axisLeft(valueScale)
199
+ .scale(valueScale)
200
+ .ticks(fullParams.ticks) // 刻度分段數量
201
+ .tickFormat(d => parseTickFormatValue(d, fullParams.tickFormat))
202
+ .tickSize(fullParams.tickFullLine == true
203
+ ? -gridAxesSize.width
204
+ : defaultTickSize)
205
+ .tickPadding(tickPadding)
206
+
207
+ const yAxisEl = yAxisSelection
208
+ .transition()
209
+ .duration(100)
210
+ .ease(d3.easeLinear) // 線性的 - 當托曳或快速變動的時候比較滑順
211
+ .call(yAxis)
212
+
213
+ yAxisEl.selectAll('line')
214
+ .style('fill', 'none')
215
+ .style('stroke', fullParams.tickLineVisible == true ? getColor(fullParams.tickColorType, fullChartParams) : 'none')
216
+ .style('stroke-dasharray', fullParams.tickFullLineDasharray)
217
+ .style('vector-effect', 'non-scaling-stroke') // 避免 scale 導致線條變形
218
+ .attr('pointer-events', 'none')
219
+
220
+ yAxisEl.selectAll('path')
221
+ .style('fill', 'none')
222
+ // .style('stroke', this.fullParams.axisLineColor!)
223
+ .style('stroke', fullParams.axisLineVisible == true ? getColor(fullParams.axisLineColorType, fullChartParams) : 'none')
224
+ .style('shape-rendering', 'crispEdges')
225
+
226
+ // const yText = yAxisEl.selectAll('text')
227
+ const yText = yAxisSelection.selectAll('text')
228
+ // .style('font-family', 'sans-serif')
229
+ .attr('font-size', fullChartParams.styles.textSize)
230
+ .style('color', getColor(fullParams.tickTextColorType, fullChartParams))
231
+ .attr('text-anchor', tickTextAlign.textAnchor)
232
+ .attr('dominant-baseline', tickTextAlign.dominantBaseline)
233
+ // .attr('dy', 0)
234
+ .attr('y', textY)
235
+ .attr('dy', 0)
236
+ yText.style('transform', textReverseTransformWithRotate)
237
+
238
+ // 抵消掉預設的偏移
239
+ if (fullDataFormatter.valueAxis.position === 'bottom' || fullDataFormatter.valueAxis.position === 'top') {
240
+ yText.attr('dy', 0)
241
+ }
242
+
243
+ return yAxisSelection
244
+ }
245
+
246
+
247
+
248
+ export const createBaseValueAxis: BasePluginFn<BaseValueAxisContext> = (pluginName: string, {
249
+ selection,
250
+ computedData$,
251
+ filteredMinMaxValue$,
252
+ fullParams$,
253
+ fullDataFormatter$,
254
+ fullChartParams$,
255
+ gridAxesSize$,
256
+ gridAxesTransform$,
257
+ gridAxesReverseTransform$,
258
+ // gridAxesSize$,
259
+ gridContainerPosition$,
260
+ // gridAxesContainerSize$,
261
+ isSeriesSeprate$,
262
+ }) => {
263
+
264
+ const destroy$ = new Subject()
265
+
266
+ const containerClassName = getClassName(pluginName, 'container')
267
+ const yAxisGClassName = getClassName(pluginName, 'yAxisG')
268
+ const yAxisClassName = getClassName(pluginName, 'yAxis')
269
+ const textClassName = getClassName(pluginName, 'text')
270
+
271
+ const containerSelection$ = gridContainerSelectionsObservable({
272
+ selection,
273
+ pluginName,
274
+ computedData$,
275
+ gridContainerPosition$,
276
+ isSeriesSeprate$
277
+ })
278
+
279
+ // const containerSelection$ = combineLatest({
280
+ // computedData: computedData$.pipe(
281
+ // distinctUntilChanged((a, b) => {
282
+ // // 只有當series的數量改變時,才重新計算
283
+ // return a.length === b.length
284
+ // }),
285
+ // ),
286
+ // isSeriesSeprate: isSeriesSeprate$
287
+ // }).pipe(
288
+ // takeUntil(destroy$),
289
+ // switchMap(async (d) => d),
290
+ // map(data => {
291
+ // return data.isSeriesSeprate
292
+ // // series分開的時候顯示各別axis
293
+ // ? data.computedData
294
+ // // series合併的時候只顯示第一個axis
295
+ // : [data.computedData[0]]
296
+ // }),
297
+ // map((computedData, i) => {
298
+ // return selection
299
+ // .selectAll<SVGGElement, ComputedDatumGrid[]>(`g.${containerClassName}`)
300
+ // .data(computedData, d => d[0] ? d[0].seriesIndex : i)
301
+ // .join('g')
302
+ // .classed(containerClassName, true)
303
+ // })
304
+ // )
305
+
306
+ const axisSelection$ = containerSelection$.pipe(
307
+ takeUntil(destroy$),
308
+ map((containerSelection, i) => {
309
+ return containerSelection
310
+ .selectAll<SVGGElement, ComputedDatumGrid[]>(`g.${yAxisGClassName}`)
311
+ .data([yAxisGClassName])
312
+ .join('g')
313
+ .classed(yAxisGClassName, true)
314
+ })
315
+ )
316
+
317
+ // combineLatest({
318
+ // containerSelection: containerSelection$,
319
+ // gridContainerPosition: gridContainerPosition$
320
+ // }).pipe(
321
+ // takeUntil(destroy$),
322
+ // switchMap(async d => d)
323
+ // ).subscribe(data => {
324
+ // data.containerSelection
325
+ // .attr('transform', (d, i) => {
326
+ // const gridContainerPosition = data.gridContainerPosition[i] ?? data.gridContainerPosition[0]
327
+ // const translate = gridContainerPosition.translate
328
+ // const scale = gridContainerPosition.scale
329
+ // // return `translate(${translate[0]}, ${translate[1]}) scale(${scale[0]}, ${scale[1]})`
330
+ // return `translate(${translate[0]}, ${translate[1]})`
331
+ // })
332
+ // // .attr('opacity', 0)
333
+ // // .transition()
334
+ // // .attr('opacity', 1)
335
+ // })
336
+
337
+ combineLatest({
338
+ axisSelection: axisSelection$,
339
+ gridAxesTransform: gridAxesTransform$,
340
+ }).pipe(
341
+ takeUntil(destroy$),
342
+ switchMap(async d => d)
343
+ ).subscribe(data => {
344
+ data.axisSelection
345
+ .style('transform', data.gridAxesTransform.value)
346
+ // .attr('opacity', 0)
347
+ // .transition()
348
+ // .attr('opacity', 1)
349
+
350
+ })
351
+
352
+ // const gridAxesSize$ = gridAxisSizeObservable({
353
+ // fullDataFormatter$,
354
+ // layout$
355
+ // })
356
+
357
+ // const textReverseTransform$: Observable<string> = new Observable(subscriber => {
358
+ // combineLatest({
359
+ // fullParams: fullParams$,
360
+ // layout: layout$
361
+ // }).pipe(
362
+ // takeUntil(destroy$),
363
+ // // 轉換後會退訂前一個未完成的訂閱事件,因此可以取到「同時間」最後一次的訂閱事件
364
+ // switchMap(async (d) => d),
365
+ // ).subscribe(data => {
366
+
367
+ // const transformData = Object.assign({}, data.layout.content.axesTransformData)
368
+
369
+ // const value = getAxesTransformValue({
370
+ // translate: [0, 0],
371
+ // scale: [transformData.scale[0] * -1, transformData.scale[1] * -1],
372
+ // rotate: transformData.rotate * -1 + data.fullParams.tickTextRotate,
373
+ // rotateX: transformData.rotateX * -1,
374
+ // rotateY: transformData.rotateY * -1
375
+ // })
376
+
377
+ // subscriber.next(value)
378
+ // })
379
+ // })
380
+ // const reverseTransform$: Observable<TransformData> = gridAxesTransform$.pipe(
381
+ // takeUntil(destroy$),
382
+ // map(d => {
383
+ // const translate: [number, number] = [d.translate[0] * -1, d.translate[1] * -1]
384
+ // const scale: [number, number] = [d.scale[0] * -1, d.scale[1] * -1]
385
+ // const rotate = d.rotate * -1
386
+ // const rotateX = d.rotateX * -1
387
+ // const rotateY = d.rotateY * -1
388
+ // return {
389
+ // translate,
390
+ // scale,
391
+ // rotate,
392
+ // rotateX,
393
+ // rotateY,
394
+ // value: ''
395
+ // }
396
+ // }),
397
+ // )
398
+ const textReverseTransform$ = combineLatest({
399
+ gridAxesReverseTransform: gridAxesReverseTransform$,
400
+ gridContainerPosition: gridContainerPosition$
401
+ }).pipe(
402
+ takeUntil(destroy$),
403
+ switchMap(async (d) => d),
404
+ map(data => {
405
+ // const axisReverseTranslateValue = `translate(${data.gridAxesReverseTransform.translate[0]}px, ${data.gridAxesReverseTransform.translate[1]}px)`
406
+ const axesRotateXYReverseValue = `rotateX(${data.gridAxesReverseTransform.rotateX}deg) rotateY(${data.gridAxesReverseTransform.rotateY}deg)`
407
+ const axesRotateReverseValue = `rotate(${data.gridAxesReverseTransform.rotate}deg)`
408
+ const containerScaleReverseValue = `scale(${1 / data.gridContainerPosition[0].scale[0]}, ${1 / data.gridContainerPosition[0].scale[1]})`
409
+ // 必須按照順序(先抵消外層rotate,再抵消最外層scale)
410
+ return `${axesRotateXYReverseValue} ${axesRotateReverseValue} ${containerScaleReverseValue}`
411
+ }),
412
+ distinctUntilChanged()
413
+ )
414
+
415
+ const textReverseTransformWithRotate$ = combineLatest({
416
+ textReverseTransform: textReverseTransform$,
417
+ fullParams: fullParams$,
418
+ }).pipe(
419
+ takeUntil(destroy$),
420
+ switchMap(async (d) => d),
421
+ map(data => {
422
+ // 必須按照順序(先抵消外層rotate,再抵消最外層scale,最後再做本身的rotate)
423
+ return `${data.textReverseTransform} rotate(${data.fullParams.tickTextRotate}deg)`
424
+ })
425
+ )
426
+
427
+ // const minMax$: Observable<[number, number]> = new Observable(subscriber => {
428
+ // combineLatest({
429
+ // fullDataFormatter: fullDataFormatter$,
430
+ // computedData: computedData$
431
+ // }).pipe(
432
+ // takeUntil(destroy$),
433
+ // switchMap(async (d) => d),
434
+ // ).subscribe(data => {
435
+ // const groupMin = 0
436
+ // const groupMax = data.computedData[0] ? data.computedData[0].length - 1 : 0
437
+ // // const groupScaleDomainMin = data.fullDataFormatter.groupAxis.scaleDomain[0] === 'auto'
438
+ // // ? groupMin - data.fullDataFormatter.groupAxis.scalePadding
439
+ // // : data.fullDataFormatter.groupAxis.scaleDomain[0] as number - data.fullDataFormatter.groupAxis.scalePadding
440
+ // const groupScaleDomainMin = data.fullDataFormatter.groupAxis.scaleDomain[0] - data.fullDataFormatter.groupAxis.scalePadding
441
+ // const groupScaleDomainMax = data.fullDataFormatter.groupAxis.scaleDomain[1] === 'max'
442
+ // ? groupMax + data.fullDataFormatter.groupAxis.scalePadding
443
+ // : data.fullDataFormatter.groupAxis.scaleDomain[1] as number + data.fullDataFormatter.groupAxis.scalePadding
444
+
445
+ // const filteredData = data.computedData.map((d, i) => {
446
+ // return d.filter((_d, _i) => {
447
+ // return _i >= groupScaleDomainMin && _i <= groupScaleDomainMax
448
+ // })
449
+ // })
450
+
451
+ // const filteredMinMax = getMinMaxValue(filteredData.flat())
452
+ // if (filteredMinMax[0] === filteredMinMax[1]) {
453
+ // filteredMinMax[0] = filteredMinMax[1] - 1 // 避免最大及最小值相同造成無法計算scale
454
+ // }
455
+ // subscriber.next(filteredMinMax)
456
+ // })
457
+ // })
458
+
459
+ const valueScale$: Observable<d3.ScaleLinear<number, number>> = new Observable(subscriber => {
460
+ combineLatest({
461
+ fullDataFormatter: fullDataFormatter$,
462
+ gridAxesSize: gridAxesSize$,
463
+ // minMax: minMax$
464
+ filteredMinMaxValue: filteredMinMaxValue$
465
+ }).pipe(
466
+ takeUntil(destroy$),
467
+ switchMap(async (d) => d),
468
+ ).subscribe(data => {
469
+ let maxValue = data.filteredMinMaxValue[1]
470
+ let minValue = data.filteredMinMaxValue[0]
471
+ if (maxValue === minValue && maxValue === 0) {
472
+ // 避免最大及最小值同等於 0 造成無法計算scale
473
+ maxValue = 1
474
+ }
475
+
476
+ const valueScale: d3.ScaleLinear<number, number> = createValueToAxisScale({
477
+ maxValue,
478
+ minValue,
479
+ axisWidth: data.gridAxesSize.height,
480
+ scaleDomain: data.fullDataFormatter.valueAxis.scaleDomain,
481
+ scaleRange: data.fullDataFormatter.valueAxis.scaleRange
482
+ })
483
+
484
+ subscriber.next(valueScale)
485
+ })
486
+ })
487
+
488
+ const tickTextAlign$: Observable<TextAlign> = combineLatest({
489
+ fullDataFormatter: fullDataFormatter$,
490
+ fullParams: fullParams$
491
+ }).pipe(
492
+ takeUntil(destroy$),
493
+ switchMap(async (d) => d),
494
+ map(data => {
495
+ let textAnchor: 'start' | 'middle' | 'end' = 'start'
496
+ let dominantBaseline: 'auto' | 'middle' | 'hanging' = 'hanging'
497
+
498
+ if (data.fullDataFormatter.valueAxis.position === 'left') {
499
+ textAnchor = 'end'
500
+ dominantBaseline = 'middle'
501
+ } else if (data.fullDataFormatter.valueAxis.position === 'right') {
502
+ textAnchor = 'start'
503
+ dominantBaseline = 'middle'
504
+ } else if (data.fullDataFormatter.valueAxis.position === 'bottom') {
505
+ textAnchor = data.fullParams.tickTextRotate
506
+ ? 'end'
507
+ : 'middle'
508
+ dominantBaseline = 'hanging'
509
+ } else if (data.fullDataFormatter.valueAxis.position === 'top') {
510
+ textAnchor = data.fullParams.tickTextRotate
511
+ ? 'start'
512
+ : 'middle'
513
+ dominantBaseline = 'auto'
514
+ }
515
+ return {
516
+ textAnchor,
517
+ dominantBaseline
518
+ }
519
+ })
520
+ )
521
+
522
+ const axisLabelAlign$: Observable<TextAlign> = fullDataFormatter$.pipe(
523
+ takeUntil(destroy$),
524
+ map(d => {
525
+ let textAnchor: 'start' | 'middle' | 'end' = 'start'
526
+ let dominantBaseline: 'auto' | 'middle' | 'hanging' = 'hanging'
527
+
528
+ if (d.groupAxis.position === 'bottom') {
529
+ dominantBaseline = 'auto'
530
+ } else if (d.groupAxis.position === 'top') {
531
+ dominantBaseline = 'hanging'
532
+ } else if (d.groupAxis.position === 'left') {
533
+ textAnchor = 'start'
534
+ } else if (d.groupAxis.position === 'right') {
535
+ textAnchor = 'end'
536
+ }
537
+ if (d.valueAxis.position === 'left') {
538
+ textAnchor = 'end'
539
+ } else if (d.valueAxis.position === 'right') {
540
+ textAnchor = 'start'
541
+ } else if (d.valueAxis.position === 'bottom') {
542
+ dominantBaseline = 'hanging'
543
+ } else if (d.valueAxis.position === 'top') {
544
+ dominantBaseline = 'auto'
545
+ }
546
+ return {
547
+ textAnchor,
548
+ dominantBaseline
549
+ }
550
+ })
551
+ )
552
+
553
+
554
+ combineLatest({
555
+ axisSelection: axisSelection$,
556
+ fullParams: fullParams$,
557
+ tickTextAlign: tickTextAlign$,
558
+ axisLabelAlign: axisLabelAlign$,
559
+ computedData: computedData$,
560
+ gridAxesSize: gridAxesSize$,
561
+ fullDataFormatter: fullDataFormatter$,
562
+ fullChartParams: fullChartParams$,
563
+ valueScale: valueScale$,
564
+ textReverseTransform: textReverseTransform$,
565
+ textReverseTransformWithRotate: textReverseTransformWithRotate$,
566
+ filteredMinMaxValue: filteredMinMaxValue$
567
+ }).pipe(
568
+ takeUntil(destroy$),
569
+ switchMap(async (d) => d),
570
+ ).subscribe(data => {
571
+
572
+ renderAxis({
573
+ selection: data.axisSelection,
574
+ yAxisClassName,
575
+ fullParams: data.fullParams,
576
+ tickTextAlign: data.tickTextAlign,
577
+ gridAxesSize: data.gridAxesSize,
578
+ fullDataFormatter: data.fullDataFormatter,
579
+ fullChartParams: data.fullChartParams,
580
+ valueScale: data.valueScale,
581
+ textReverseTransformWithRotate: data.textReverseTransformWithRotate,
582
+ filteredMinMaxValue: data.filteredMinMaxValue
583
+ })
584
+
585
+ renderAxisLabel({
586
+ selection: data.axisSelection,
587
+ textClassName,
588
+ fullParams: data.fullParams,
589
+ axisLabelAlign: data.axisLabelAlign,
590
+ gridAxesSize: data.gridAxesSize,
591
+ fullDataFormatter: data.fullDataFormatter,
592
+ fullChartParams: data.fullChartParams,
593
+ textReverseTransform: data.textReverseTransform,
594
+ })
595
+ })
596
+
597
+ return () => {
598
+ destroy$.next(undefined)
599
+ }
600
+ }