@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,684 +1,684 @@
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
- DataFormatterMultiValue,
17
- DefinePluginConfig,
18
- } from '../../../lib/core-types'
19
- import {
20
- defineMultiValuePlugin,
21
- createValueToAxisScale,
22
- getMinMax
23
- } from '../../../lib/core'
24
- import type { XYAxesParams
25
- } from '../../../lib/plugins-basic-types'
26
- import { DEFAULT_X_Y_AXES_PARAMS } from '../defaults'
27
- import { LAYER_INDEX_OF_AXIS } from '../../const'
28
- import { getColor, getDatumColor, getClassName, getUniID } from '../../utils/orbchartsUtils'
29
- import { parseTickFormatValue } from '../../utils/d3Utils'
30
- // import { filteredXYMinMaxDataObservable } from '../../../../orbcharts-core/src/utils/multiValueObservables'
31
- // import { multiValueSelectionsObservable } from '../multiValueObservables'
32
-
33
- // interface TextAlign {
34
- // textAnchor: "start" | "middle" | "end"
35
- // dominantBaseline: "middle" | "auto" | "hanging"
36
- // }
37
-
38
- // interface Axis {
39
- // labelOffset: [number, number]
40
- // labelColorType: ColorType
41
- // axisLineVisible: boolean
42
- // axisLineColorType: ColorType
43
- // ticks: number | null
44
- // tickFormat: string | ((text: d3.NumberValue) => string)
45
- // tickLineVisible: boolean
46
- // tickPadding: number
47
- // tickFullLine: boolean
48
- // tickFullLineDasharray: string
49
- // tickColorType: ColorType
50
- // tickTextColorType: ColorType
51
- // }
52
-
53
- const pluginName = 'XYAxes'
54
-
55
- const defaultTickSize = 6
56
-
57
- const xTickTextAnchor = 'middle'
58
- const xTickDominantBaseline = 'hanging'
59
- const xAxisLabelAnchor = 'start'
60
- const xAxisLabelDominantBaseline = 'hanging'
61
- const yTickTextAnchor = 'end'
62
- const yTickDominantBaseline = 'middle'
63
- const yAxisLabelAnchor = 'end'
64
- const yAxisLabelDominantBaseline = 'auto'
65
-
66
- const pluginConfig: DefinePluginConfig<typeof pluginName, typeof DEFAULT_X_Y_AXES_PARAMS> = {
67
- name: pluginName,
68
- defaultParams: DEFAULT_X_Y_AXES_PARAMS,
69
- layerIndex: LAYER_INDEX_OF_AXIS,
70
- validator: (params, { validateColumns }) => {
71
- const result = validateColumns(params, {
72
- xAxis: {
73
- toBeTypes: ['object']
74
- },
75
- yAxis: {
76
- toBeTypes: ['object']
77
- }
78
- })
79
- if (params.xAxis) {
80
- const forceResult = validateColumns(params.xAxis, {
81
- labelOffset: {
82
- toBe: '[number, number]',
83
- test: (value: any) => {
84
- return Array.isArray(value)
85
- && value.length === 2
86
- && typeof value[0] === 'number'
87
- && typeof value[1] === 'number'
88
- }
89
- },
90
- labelColorType: {
91
- toBeOption: 'ColorType',
92
- },
93
- axisLineVisible: {
94
- toBeTypes: ['boolean']
95
- },
96
- axisLineColorType: {
97
- toBeOption: 'ColorType',
98
- },
99
- ticks: {
100
- toBeTypes: ['number', 'null']
101
- },
102
- tickFormat: {
103
- toBeTypes: ['string', 'Function']
104
- },
105
- tickLineVisible: {
106
- toBeTypes: ['boolean']
107
- },
108
- tickPadding: {
109
- toBeTypes: ['number']
110
- },
111
- tickFullLine: {
112
- toBeTypes: ['boolean']
113
- },
114
- tickFullLineDasharray: {
115
- toBeTypes: ['string']
116
- },
117
- tickColorType: {
118
- toBeOption: 'ColorType',
119
- },
120
- tickTextColorType: {
121
- toBeOption: 'ColorType',
122
- }
123
- })
124
- if (forceResult.status === 'error') {
125
- return forceResult
126
- }
127
- }
128
- if (params.yAxis) {
129
- const forceResult = validateColumns(params.yAxis, {
130
- labelOffset: {
131
- toBe: '[number, number]',
132
- test: (value: any) => {
133
- return Array.isArray(value)
134
- && value.length === 2
135
- && typeof value[0] === 'number'
136
- && typeof value[1] === 'number'
137
- }
138
- },
139
- labelColorType: {
140
- toBeOption: 'ColorType',
141
- },
142
- axisLineVisible: {
143
- toBeTypes: ['boolean']
144
- },
145
- axisLineColorType: {
146
- toBeOption: 'ColorType',
147
- },
148
- ticks: {
149
- toBeTypes: ['number', 'null']
150
- },
151
- tickFormat: {
152
- toBeTypes: ['string', 'Function']
153
- },
154
- tickLineVisible: {
155
- toBeTypes: ['boolean']
156
- },
157
- tickPadding: {
158
- toBeTypes: ['number']
159
- },
160
- tickFullLine: {
161
- toBeTypes: ['boolean']
162
- },
163
- tickFullLineDasharray: {
164
- toBeTypes: ['string']
165
- },
166
- tickColorType: {
167
- toBeOption: 'ColorType',
168
- },
169
- tickTextColorType: {
170
- toBeOption: 'ColorType',
171
- }
172
- })
173
- if (forceResult.status === 'error') {
174
- return forceResult
175
- }
176
- }
177
- return result
178
- }
179
- }
180
-
181
- function renderXAxisLabel ({ selection, xLabelClassName, fullParams, layout, fullDataFormatter, fullChartParams, textReverseTransform }: {
182
- selection: d3.Selection<SVGGElement, any, any, any>,
183
- xLabelClassName: string
184
- fullParams: XYAxesParams
185
- // axisLabelAlign: TextAlign
186
- layout: { width: number, height: number }
187
- fullDataFormatter: DataFormatterMultiValue,
188
- fullChartParams: ChartParams
189
- textReverseTransform: string,
190
- }) {
191
- const offsetX = fullParams.xAxis.tickPadding + fullParams.xAxis.labelOffset[0]
192
- const offsetY = fullParams.xAxis.tickPadding + fullParams.xAxis.labelOffset[1]
193
- let labelX = offsetX
194
- let labelY = offsetY
195
-
196
- const axisLabelSelection = selection
197
- .selectAll<SVGGElement, XYAxesParams>(`g.${xLabelClassName}`)
198
- .data([fullParams])
199
- .join('g')
200
- .classed(xLabelClassName, true)
201
- .each((d, i, g) => {
202
- const text = d3.select(g[i])
203
- .selectAll<SVGTextElement, XYAxesParams>(`text`)
204
- .data([d])
205
- .join(
206
- enter => {
207
- return enter
208
- .append('text')
209
- .style('font-weight', 'bold')
210
- },
211
- update => update,
212
- exit => exit.remove()
213
- )
214
- .attr('text-anchor', xAxisLabelAnchor)
215
- .attr('dominant-baseline', xAxisLabelDominantBaseline)
216
- .attr('font-size', fullChartParams.styles.textSize)
217
- .style('fill', getColor(fullParams.xAxis.labelColorType, fullChartParams))
218
- .style('transform', textReverseTransform)
219
- // 偏移使用 x, y 而非 transform 才不會受到外層 scale 變形影響
220
- .attr('x', labelX)
221
- .attr('y', labelY)
222
- .text(d => fullDataFormatter.xAxis.label)
223
- })
224
- .attr('transform', d => `translate(${layout.width}, ${layout.height})`)
225
- }
226
-
227
- function renderYAxisLabel ({ selection, yLabelClassName, fullParams, layout, fullDataFormatter, fullChartParams, textReverseTransform }: {
228
- selection: d3.Selection<SVGGElement, any, any, any>,
229
- yLabelClassName: string
230
- fullParams: XYAxesParams
231
- // axisLabelAlign: TextAlign
232
- layout: { width: number, height: number }
233
- fullDataFormatter: DataFormatterMultiValue,
234
- fullChartParams: ChartParams
235
- textReverseTransform: string,
236
- }) {
237
- const offsetX = fullParams.yAxis.tickPadding - fullParams.yAxis.labelOffset[0]
238
- const offsetY = fullParams.yAxis.tickPadding + fullParams.yAxis.labelOffset[1]
239
- let labelX = - offsetX
240
- let labelY = - offsetY
241
-
242
- const axisLabelSelection = selection
243
- .selectAll<SVGGElement, XYAxesParams>(`g.${yLabelClassName}`)
244
- .data([fullParams])
245
- .join('g')
246
- .classed(yLabelClassName, true)
247
- .each((d, i, g) => {
248
- const text = d3.select(g[i])
249
- .selectAll<SVGTextElement, XYAxesParams>(`text`)
250
- .data([d])
251
- .join(
252
- enter => {
253
- return enter
254
- .append('text')
255
- .style('font-weight', 'bold')
256
- },
257
- update => update,
258
- exit => exit.remove()
259
- )
260
- .attr('text-anchor', yAxisLabelAnchor)
261
- .attr('dominant-baseline', yAxisLabelDominantBaseline)
262
- .attr('font-size', fullChartParams.styles.textSize)
263
- .style('fill', getColor(fullParams.yAxis.labelColorType, fullChartParams))
264
- .style('transform', textReverseTransform)
265
- // 偏移使用 x, y 而非 transform 才不會受到外層 scale 變形影響
266
- .attr('x', labelX)
267
- .attr('y', labelY)
268
- .text(d => fullDataFormatter.yAxis.label)
269
- })
270
- // .attr('transform', d => `translate(0, ${layout.height})`)
271
- }
272
-
273
- function renderXAxis ({ selection, xAxisClassName, fullParams, layout, fullDataFormatter, fullChartParams, xScale, textReverseTransform, xyMinMax }: {
274
- selection: d3.Selection<SVGGElement, any, any, any>,
275
- xAxisClassName: string
276
- fullParams: XYAxesParams
277
- // tickTextAlign: TextAlign
278
- layout: { width: number, height: number }
279
- fullDataFormatter: DataFormatterMultiValue,
280
- fullChartParams: ChartParams
281
- xScale: d3.ScaleLinear<number, number>
282
- textReverseTransform: string,
283
- xyMinMax: {
284
- minX: number;
285
- maxX: number;
286
- minY: number;
287
- maxY: number;
288
- }
289
- }) {
290
-
291
- const xAxisSelection = selection
292
- .selectAll<SVGGElement, XYAxesParams>(`g.${xAxisClassName}`)
293
- .data([fullParams])
294
- .join('g')
295
- .classed(xAxisClassName, true)
296
- .attr('transform', `translate(0, ${layout.height})`)
297
-
298
- // const _xScale = d3.scaleLinear()
299
- // .domain([0, 150])
300
- // .range([416.5, 791.349])
301
-
302
- // 刻度文字偏移
303
- let tickPadding = fullParams.xAxis.tickPadding
304
-
305
- // 設定Y軸刻度
306
- const xAxis = d3.axisBottom(xScale)
307
- .scale(xScale)
308
- .ticks(fullParams.xAxis.ticks) // 刻度分段數量
309
- .tickFormat(d => parseTickFormatValue(d, fullParams.xAxis.tickFormat))
310
- .tickSize(fullParams.xAxis.tickFullLine == true
311
- ? -layout.height
312
- : defaultTickSize)
313
- .tickSizeOuter(-layout.height)
314
- .tickPadding(tickPadding)
315
-
316
- const xAxisEl = xAxisSelection
317
- .transition()
318
- .duration(100)
319
- .call(xAxis)
320
-
321
- xAxisEl.selectAll('line')
322
- .style('fill', 'none')
323
- .style('stroke', fullParams.xAxis.tickLineVisible == true ? getColor(fullParams.xAxis.tickColorType, fullChartParams) : 'none')
324
- .style('stroke-dasharray', fullParams.xAxis.tickFullLineDasharray)
325
- .attr('pointer-events', 'none')
326
-
327
- xAxisEl.selectAll('path')
328
- .style('fill', 'none')
329
- // .style('stroke', this.fullParams.axisLineColor!)
330
- .style('stroke', fullParams.xAxis.axisLineVisible == true ? getColor(fullParams.xAxis.axisLineColorType, fullChartParams) : 'none')
331
- .style('shape-rendering', 'crispEdges')
332
-
333
- // const xText = xAxisEl.selectAll('text')
334
- const xText = xAxisSelection.selectAll('text')
335
- // .style('font-family', 'sans-serif')
336
- .attr('font-size', fullChartParams.styles.textSize)
337
- .style('color', getColor(fullParams.xAxis.tickTextColorType, fullChartParams))
338
- .attr('text-anchor', xTickTextAnchor)
339
- .attr('dominant-baseline', xTickDominantBaseline)
340
- .attr('dy', 0)
341
- .attr('y', tickPadding)
342
- xText.style('transform', textReverseTransform)
343
-
344
- // // 抵消掉預設的偏移
345
- // if (fullDataFormatter.grid.valueAxis.position === 'bottom' || fullDataFormatter.grid.valueAxis.position === 'top') {
346
- // xText.attr('dy', 0)
347
- // }
348
-
349
- return xAxisSelection
350
- }
351
-
352
- function renderYAxis ({ selection, yAxisClassName, fullParams, layout, fullDataFormatter, fullChartParams, yScale, textReverseTransform, xyMinMax }: {
353
- selection: d3.Selection<SVGGElement, any, any, any>,
354
- yAxisClassName: string
355
- fullParams: XYAxesParams
356
- // tickTextAlign: TextAlign
357
- layout: { width: number, height: number }
358
- fullDataFormatter: DataFormatterMultiValue,
359
- fullChartParams: ChartParams
360
- yScale: d3.ScaleLinear<number, number>
361
- textReverseTransform: string,
362
- xyMinMax: {
363
- minX: number;
364
- maxX: number;
365
- minY: number;
366
- maxY: number;
367
- }
368
- }) {
369
-
370
- const yAxisSelection = selection
371
- .selectAll<SVGGElement, XYAxesParams>(`g.${yAxisClassName}`)
372
- .data([fullParams])
373
- .join('g')
374
- .classed(yAxisClassName, true)
375
-
376
- // const _yScale = d3.scaleLinear()
377
- // .domain([0, 150])
378
- // .range([416.5, 791.349])
379
-
380
- // 刻度文字偏移
381
- let tickPadding = fullParams.yAxis.tickPadding
382
-
383
- // 設定Y軸刻度
384
- const yAxis = d3.axisLeft(yScale)
385
- .scale(yScale)
386
- .ticks(fullParams.yAxis.ticks) // 刻度分段數量
387
- .tickFormat(d => parseTickFormatValue(d, fullParams.yAxis.tickFormat))
388
- .tickSize(fullParams.yAxis.tickFullLine == true
389
- ? -layout.width
390
- : defaultTickSize)
391
- .tickPadding(tickPadding)
392
-
393
- const yAxisEl = yAxisSelection
394
- .transition()
395
- .duration(100)
396
- .call(yAxis)
397
-
398
- yAxisEl.selectAll('line')
399
- .style('fill', 'none')
400
- .style('stroke', fullParams.yAxis.tickLineVisible == true ? getColor(fullParams.yAxis.tickColorType, fullChartParams) : 'none')
401
- .style('stroke-dasharray', fullParams.yAxis.tickFullLineDasharray)
402
- .attr('pointer-events', 'none')
403
-
404
- yAxisEl.selectAll('path')
405
- .style('fill', 'none')
406
- // .style('stroke', this.fullParams.axisLineColor!)
407
- .style('stroke', fullParams.yAxis.axisLineVisible == true ? getColor(fullParams.yAxis.axisLineColorType, fullChartParams) : 'none')
408
- .style('shape-rendering', 'crispEdges')
409
-
410
- // const yText = yAxisEl.selectAll('text')
411
- const yText = yAxisSelection.selectAll('text')
412
- // .style('font-family', 'sans-serif')
413
- .attr('font-size', fullChartParams.styles.textSize)
414
- .style('color', getColor(fullParams.yAxis.tickTextColorType, fullChartParams))
415
- .attr('text-anchor', yTickTextAnchor)
416
- .attr('dominant-baseline', yTickDominantBaseline)
417
- // .attr('dy', 0)
418
- .attr('x', - tickPadding)
419
- .attr('dy', 0)
420
- yText.style('transform', textReverseTransform)
421
-
422
- // // 抵消掉預設的偏移
423
- // if (fullDataFormatter.grid.valueAxis.position === 'bottom' || fullDataFormatter.grid.valueAxis.position === 'top') {
424
- // yText.attr('dy', 0)
425
- // }
426
-
427
- return yAxisSelection
428
- }
429
-
430
-
431
- export const XYAxes = defineMultiValuePlugin(pluginConfig)(({ selection, name, observer, subject }) => {
432
-
433
- const destroy$ = new Subject()
434
-
435
- const containerClassName = getClassName(pluginName, 'container')
436
- const xyAxisGClassName = getClassName(pluginName, 'xyAxisG')
437
- const xAxisClassName = getClassName(pluginName, 'xAxis')
438
- const yAxisClassName = getClassName(pluginName, 'yAxis')
439
- const xLabelClassName = getClassName(pluginName, 'xLabel')
440
- const yLabelClassName = getClassName(pluginName, 'yLabel')
441
-
442
- const containerSelection$ = combineLatest({
443
- computedData: observer.computedData$.pipe(
444
- distinctUntilChanged((a, b) => {
445
- // 只有當series的數量改變時,才重新計算
446
- return a.length === b.length
447
- }),
448
- ),
449
- isCategorySeprate: observer.isCategorySeprate$
450
- }).pipe(
451
- takeUntil(destroy$),
452
- switchMap(async (d) => d),
453
- map(data => {
454
- return data.isCategorySeprate
455
- // category分開的時候顯示各別axis
456
- ? data.computedData
457
- // category合併的時候只顯示第一個axis
458
- : [data.computedData[0]]
459
- }),
460
- map((computedData, i) => {
461
- return selection
462
- .selectAll<SVGGElement, ComputedDatumMultiValue[]>(`g.${containerClassName}`)
463
- .data(computedData, d => (d && d[0]) ? d[0].categoryIndex : i)
464
- .join('g')
465
- .classed(containerClassName, true)
466
- })
467
- )
468
-
469
- const axisSelection$ = containerSelection$.pipe(
470
- takeUntil(destroy$),
471
- map((containerSelection, i) => {
472
- return containerSelection
473
- .selectAll<SVGGElement, ComputedDatumMultiValue[]>(`g.${xyAxisGClassName}`)
474
- .data([xyAxisGClassName])
475
- .join('g')
476
- .classed(xyAxisGClassName, true)
477
- })
478
- )
479
-
480
- combineLatest({
481
- containerSelection: containerSelection$,
482
- gridContainerPosition: observer.containerPosition$
483
- }).pipe(
484
- takeUntil(destroy$),
485
- switchMap(async d => d)
486
- ).subscribe(data => {
487
- data.containerSelection
488
- .attr('transform', (d, i) => {
489
- const gridContainerPosition = data.gridContainerPosition[i] ?? data.gridContainerPosition[0]
490
- const translate = gridContainerPosition.translate
491
- const scale = gridContainerPosition.scale
492
- return `translate(${translate[0]}, ${translate[1]}) scale(${scale[0]}, ${scale[1]})`
493
- })
494
- // .attr('opacity', 0)
495
- // .transition()
496
- // .attr('opacity', 1)
497
- })
498
-
499
- const textReverseTransform$ = observer.containerPosition$.pipe(
500
- takeUntil(destroy$),
501
- switchMap(async (d) => d),
502
- map(containerPosition => {
503
- // const axesRotateXYReverseValue = `rotateX(${data.gridAxesReverseTransform.rotateX}deg) rotateY(${data.gridAxesReverseTransform.rotateY}deg)`
504
- // const axesRotateReverseValue = `rotate(${data.gridAxesReverseTransform.rotate}deg)`
505
- const containerScaleReverseValue = `scale(${1 / containerPosition[0].scale[0]}, ${1 / containerPosition[0].scale[1]})`
506
- // 抵消最外層scale
507
- return `${containerScaleReverseValue}`
508
- }),
509
- distinctUntilChanged()
510
- )
511
-
512
- // const minMax$: Observable<[number, number]> = new Observable(subscriber => {
513
- // combineLatest({
514
- // fullDataFormatter: observer.fullDataFormatter$,
515
- // computedData: observer.computedData$
516
- // }).pipe(
517
- // takeUntil(destroy$),
518
- // switchMap(async (d) => d),
519
- // ).subscribe(data => {
520
- // const groupMin = 0
521
- // const groupMax = data.computedData[0] ? data.computedData[0].length - 1 : 0
522
- // // const groupScaleDomainMin = data.fullDataFormatter.grid.groupAxis.scaleDomain[0] === 'auto'
523
- // // ? groupMin - data.fullDataFormatter.grid.groupAxis.scalePadding
524
- // // : data.fullDataFormatter.grid.groupAxis.scaleDomain[0] as number - data.fullDataFormatter.grid.groupAxis.scalePadding
525
- // const groupScaleDomainMin = data.fullDataFormatter.grid.groupAxis.scaleDomain[0] - data.fullDataFormatter.grid.groupAxis.scalePadding
526
- // const groupScaleDomainMax = data.fullDataFormatter.grid.groupAxis.scaleDomain[1] === 'max'
527
- // ? groupMax + data.fullDataFormatter.grid.groupAxis.scalePadding
528
- // : data.fullDataFormatter.grid.groupAxis.scaleDomain[1] as number + data.fullDataFormatter.grid.groupAxis.scalePadding
529
-
530
- // const filteredData = data.computedData.map((d, i) => {
531
- // return d.filter((_d, _i) => {
532
- // return _i >= groupScaleDomainMin && _i <= groupScaleDomainMax
533
- // })
534
- // })
535
-
536
- // const filteredMinMax = getMinMax(filteredData.flat().map(d => d.value[1]))
537
- // if (filteredMinMax[0] === filteredMinMax[1]) {
538
- // filteredMinMax[0] = filteredMinMax[1] - 1 // 避免最大及最小值相同造成無法計算scale
539
- // }
540
- // subscriber.next(filteredMinMax)
541
- // })
542
- // })
543
-
544
- const xScale$: Observable<d3.ScaleLinear<number, number>> = new Observable(subscriber => {
545
- combineLatest({
546
- fullDataFormatter: observer.fullDataFormatter$,
547
- layout: observer.layout$,
548
- // xyMinMax: observer.xyMinMax$
549
- filteredXYMinMaxData: observer.filteredXYMinMaxData$
550
- }).pipe(
551
- takeUntil(destroy$),
552
- switchMap(async (d) => d),
553
- ).subscribe(data => {
554
- if (!data.filteredXYMinMaxData.minXDatum || !data.filteredXYMinMaxData.maxXDatum
555
- || data.filteredXYMinMaxData.minXDatum.value[0] == null || data.filteredXYMinMaxData.maxXDatum.value[0] == null
556
- ) {
557
- return
558
- }
559
- let maxValue = data.filteredXYMinMaxData.maxXDatum.value[0]
560
- let minValue = data.filteredXYMinMaxData.minXDatum.value[0]
561
- if (maxValue === minValue && maxValue === 0) {
562
- // 避免最大及最小值同等於 0 造成無法計算scale
563
- maxValue = 1
564
- }
565
-
566
- const xScale: d3.ScaleLinear<number, number> = createValueToAxisScale({
567
- maxValue,
568
- minValue,
569
- axisWidth: data.layout.width,
570
- scaleDomain: data.fullDataFormatter.xAxis.scaleDomain,
571
- scaleRange: data.fullDataFormatter.xAxis.scaleRange,
572
- })
573
-
574
- subscriber.next(xScale)
575
- })
576
- })
577
-
578
- const yScale$: Observable<d3.ScaleLinear<number, number>> = new Observable(subscriber => {
579
- combineLatest({
580
- fullDataFormatter: observer.fullDataFormatter$,
581
- layout: observer.layout$,
582
- // xyMinMax: observer.xyMinMax$
583
- filteredXYMinMaxData: observer.filteredXYMinMaxData$
584
- }).pipe(
585
- takeUntil(destroy$),
586
- switchMap(async (d) => d),
587
- ).subscribe(data => {
588
- if (!data.filteredXYMinMaxData.minYDatum || !data.filteredXYMinMaxData.maxYDatum
589
- || data.filteredXYMinMaxData.minYDatum.value[1] == null || data.filteredXYMinMaxData.maxYDatum.value[1] == null
590
- ) {
591
- return
592
- }
593
- let maxValue = data.filteredXYMinMaxData.maxYDatum.value[1]
594
- let minValue = data.filteredXYMinMaxData.minYDatum.value[1]
595
- if (maxValue === minValue && maxValue === 0) {
596
- // 避免最大及最小值同等於 0 造成無法計算scale
597
- maxValue = 1
598
- }
599
-
600
- const yScale: d3.ScaleLinear<number, number> = createValueToAxisScale({
601
- maxValue,
602
- minValue,
603
- axisWidth: data.layout.height,
604
- scaleDomain: data.fullDataFormatter.yAxis.scaleDomain,
605
- scaleRange: data.fullDataFormatter.yAxis.scaleRange,
606
- reverse: true
607
- })
608
-
609
- subscriber.next(yScale)
610
- })
611
- })
612
-
613
-
614
- combineLatest({
615
- axisSelection: axisSelection$,
616
- fullParams: observer.fullParams$,
617
- // tickTextAlign: tickTextAlign$,
618
- // axisLabelAlign: axisLabelAlign$,
619
- computedData: observer.computedData$,
620
- layout: observer.layout$,
621
- fullDataFormatter: observer.fullDataFormatter$,
622
- fullChartParams: observer.fullChartParams$,
623
- xScale: xScale$,
624
- yScale: yScale$,
625
- textReverseTransform: textReverseTransform$,
626
- xyMinMax: observer.xyMinMax$
627
- }).pipe(
628
- takeUntil(destroy$),
629
- switchMap(async (d) => d),
630
- ).subscribe(data => {
631
-
632
- renderXAxis({
633
- selection: data.axisSelection,
634
- xAxisClassName,
635
- fullParams: data.fullParams,
636
- // tickTextAlign: data.tickTextAlign,
637
- layout: data.layout,
638
- fullDataFormatter: data.fullDataFormatter,
639
- fullChartParams: data.fullChartParams,
640
- xScale: data.xScale,
641
- textReverseTransform: data.textReverseTransform,
642
- xyMinMax: data.xyMinMax
643
- })
644
-
645
- renderYAxis({
646
- selection: data.axisSelection,
647
- yAxisClassName,
648
- fullParams: data.fullParams,
649
- // tickTextAlign: data.tickTextAlign,
650
- layout: data.layout,
651
- fullDataFormatter: data.fullDataFormatter,
652
- fullChartParams: data.fullChartParams,
653
- yScale: data.yScale,
654
- textReverseTransform: data.textReverseTransform,
655
- xyMinMax: data.xyMinMax
656
- })
657
-
658
- renderXAxisLabel({
659
- selection: data.axisSelection,
660
- xLabelClassName,
661
- fullParams: data.fullParams,
662
- // axisLabelAlign: data.axisLabelAlign,
663
- layout: data.layout,
664
- fullDataFormatter: data.fullDataFormatter,
665
- fullChartParams: data.fullChartParams,
666
- textReverseTransform: data.textReverseTransform,
667
- })
668
-
669
- renderYAxisLabel({
670
- selection: data.axisSelection,
671
- yLabelClassName,
672
- fullParams: data.fullParams,
673
- // axisLabelAlign: data.axisLabelAlign,
674
- layout: data.layout,
675
- fullDataFormatter: data.fullDataFormatter,
676
- fullChartParams: data.fullChartParams,
677
- textReverseTransform: data.textReverseTransform,
678
- })
679
- })
680
-
681
- return () => {
682
- destroy$.next(undefined)
683
- }
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
+ DataFormatterMultiValue,
17
+ DefinePluginConfig,
18
+ } from '../../../lib/core-types'
19
+ import {
20
+ defineMultiValuePlugin,
21
+ createValueToAxisScale,
22
+ getMinMax
23
+ } from '../../../lib/core'
24
+ import type { XYAxesParams
25
+ } from '../../../lib/plugins-basic-types'
26
+ import { DEFAULT_X_Y_AXES_PARAMS } from '../defaults'
27
+ import { LAYER_INDEX_OF_AXIS } from '../../const'
28
+ import { getColor, getDatumColor, getClassName, getUniID } from '../../utils/orbchartsUtils'
29
+ import { parseTickFormatValue } from '../../utils/d3Utils'
30
+ // import { filteredXYMinMaxDataObservable } from '../../../../orbcharts-core/src/utils/multiValueObservables'
31
+ // import { multiValueSelectionsObservable } from '../multiValueObservables'
32
+
33
+ // interface TextAlign {
34
+ // textAnchor: "start" | "middle" | "end"
35
+ // dominantBaseline: "middle" | "auto" | "hanging"
36
+ // }
37
+
38
+ // interface Axis {
39
+ // labelOffset: [number, number]
40
+ // labelColorType: ColorType
41
+ // axisLineVisible: boolean
42
+ // axisLineColorType: ColorType
43
+ // ticks: number | null
44
+ // tickFormat: string | ((text: d3.NumberValue) => string)
45
+ // tickLineVisible: boolean
46
+ // tickPadding: number
47
+ // tickFullLine: boolean
48
+ // tickFullLineDasharray: string
49
+ // tickColorType: ColorType
50
+ // tickTextColorType: ColorType
51
+ // }
52
+
53
+ const pluginName = 'XYAxes'
54
+
55
+ const defaultTickSize = 6
56
+
57
+ const xTickTextAnchor = 'middle'
58
+ const xTickDominantBaseline = 'hanging'
59
+ const xAxisLabelAnchor = 'start'
60
+ const xAxisLabelDominantBaseline = 'hanging'
61
+ const yTickTextAnchor = 'end'
62
+ const yTickDominantBaseline = 'middle'
63
+ const yAxisLabelAnchor = 'end'
64
+ const yAxisLabelDominantBaseline = 'auto'
65
+
66
+ const pluginConfig: DefinePluginConfig<typeof pluginName, typeof DEFAULT_X_Y_AXES_PARAMS> = {
67
+ name: pluginName,
68
+ defaultParams: DEFAULT_X_Y_AXES_PARAMS,
69
+ layerIndex: LAYER_INDEX_OF_AXIS,
70
+ validator: (params, { validateColumns }) => {
71
+ const result = validateColumns(params, {
72
+ xAxis: {
73
+ toBeTypes: ['object']
74
+ },
75
+ yAxis: {
76
+ toBeTypes: ['object']
77
+ }
78
+ })
79
+ if (params.xAxis) {
80
+ const forceResult = validateColumns(params.xAxis, {
81
+ labelOffset: {
82
+ toBe: '[number, number]',
83
+ test: (value: any) => {
84
+ return Array.isArray(value)
85
+ && value.length === 2
86
+ && typeof value[0] === 'number'
87
+ && typeof value[1] === 'number'
88
+ }
89
+ },
90
+ labelColorType: {
91
+ toBeOption: 'ColorType',
92
+ },
93
+ axisLineVisible: {
94
+ toBeTypes: ['boolean']
95
+ },
96
+ axisLineColorType: {
97
+ toBeOption: 'ColorType',
98
+ },
99
+ ticks: {
100
+ toBeTypes: ['number', 'null']
101
+ },
102
+ tickFormat: {
103
+ toBeTypes: ['string', 'Function']
104
+ },
105
+ tickLineVisible: {
106
+ toBeTypes: ['boolean']
107
+ },
108
+ tickPadding: {
109
+ toBeTypes: ['number']
110
+ },
111
+ tickFullLine: {
112
+ toBeTypes: ['boolean']
113
+ },
114
+ tickFullLineDasharray: {
115
+ toBeTypes: ['string']
116
+ },
117
+ tickColorType: {
118
+ toBeOption: 'ColorType',
119
+ },
120
+ tickTextColorType: {
121
+ toBeOption: 'ColorType',
122
+ }
123
+ })
124
+ if (forceResult.status === 'error') {
125
+ return forceResult
126
+ }
127
+ }
128
+ if (params.yAxis) {
129
+ const forceResult = validateColumns(params.yAxis, {
130
+ labelOffset: {
131
+ toBe: '[number, number]',
132
+ test: (value: any) => {
133
+ return Array.isArray(value)
134
+ && value.length === 2
135
+ && typeof value[0] === 'number'
136
+ && typeof value[1] === 'number'
137
+ }
138
+ },
139
+ labelColorType: {
140
+ toBeOption: 'ColorType',
141
+ },
142
+ axisLineVisible: {
143
+ toBeTypes: ['boolean']
144
+ },
145
+ axisLineColorType: {
146
+ toBeOption: 'ColorType',
147
+ },
148
+ ticks: {
149
+ toBeTypes: ['number', 'null']
150
+ },
151
+ tickFormat: {
152
+ toBeTypes: ['string', 'Function']
153
+ },
154
+ tickLineVisible: {
155
+ toBeTypes: ['boolean']
156
+ },
157
+ tickPadding: {
158
+ toBeTypes: ['number']
159
+ },
160
+ tickFullLine: {
161
+ toBeTypes: ['boolean']
162
+ },
163
+ tickFullLineDasharray: {
164
+ toBeTypes: ['string']
165
+ },
166
+ tickColorType: {
167
+ toBeOption: 'ColorType',
168
+ },
169
+ tickTextColorType: {
170
+ toBeOption: 'ColorType',
171
+ }
172
+ })
173
+ if (forceResult.status === 'error') {
174
+ return forceResult
175
+ }
176
+ }
177
+ return result
178
+ }
179
+ }
180
+
181
+ function renderXAxisLabel ({ selection, xLabelClassName, fullParams, layout, fullDataFormatter, fullChartParams, textReverseTransform }: {
182
+ selection: d3.Selection<SVGGElement, any, any, any>,
183
+ xLabelClassName: string
184
+ fullParams: XYAxesParams
185
+ // axisLabelAlign: TextAlign
186
+ layout: { width: number, height: number }
187
+ fullDataFormatter: DataFormatterMultiValue,
188
+ fullChartParams: ChartParams
189
+ textReverseTransform: string,
190
+ }) {
191
+ const offsetX = fullParams.xAxis.tickPadding + fullParams.xAxis.labelOffset[0]
192
+ const offsetY = fullParams.xAxis.tickPadding + fullParams.xAxis.labelOffset[1]
193
+ let labelX = offsetX
194
+ let labelY = offsetY
195
+
196
+ const axisLabelSelection = selection
197
+ .selectAll<SVGGElement, XYAxesParams>(`g.${xLabelClassName}`)
198
+ .data([fullParams])
199
+ .join('g')
200
+ .classed(xLabelClassName, true)
201
+ .each((d, i, g) => {
202
+ const text = d3.select(g[i])
203
+ .selectAll<SVGTextElement, XYAxesParams>(`text`)
204
+ .data([d])
205
+ .join(
206
+ enter => {
207
+ return enter
208
+ .append('text')
209
+ .style('font-weight', 'bold')
210
+ },
211
+ update => update,
212
+ exit => exit.remove()
213
+ )
214
+ .attr('text-anchor', xAxisLabelAnchor)
215
+ .attr('dominant-baseline', xAxisLabelDominantBaseline)
216
+ .attr('font-size', fullChartParams.styles.textSize)
217
+ .style('fill', getColor(fullParams.xAxis.labelColorType, fullChartParams))
218
+ .style('transform', textReverseTransform)
219
+ // 偏移使用 x, y 而非 transform 才不會受到外層 scale 變形影響
220
+ .attr('x', labelX)
221
+ .attr('y', labelY)
222
+ .text(d => fullDataFormatter.xAxis.label)
223
+ })
224
+ .attr('transform', d => `translate(${layout.width}, ${layout.height})`)
225
+ }
226
+
227
+ function renderYAxisLabel ({ selection, yLabelClassName, fullParams, layout, fullDataFormatter, fullChartParams, textReverseTransform }: {
228
+ selection: d3.Selection<SVGGElement, any, any, any>,
229
+ yLabelClassName: string
230
+ fullParams: XYAxesParams
231
+ // axisLabelAlign: TextAlign
232
+ layout: { width: number, height: number }
233
+ fullDataFormatter: DataFormatterMultiValue,
234
+ fullChartParams: ChartParams
235
+ textReverseTransform: string,
236
+ }) {
237
+ const offsetX = fullParams.yAxis.tickPadding - fullParams.yAxis.labelOffset[0]
238
+ const offsetY = fullParams.yAxis.tickPadding + fullParams.yAxis.labelOffset[1]
239
+ let labelX = - offsetX
240
+ let labelY = - offsetY
241
+
242
+ const axisLabelSelection = selection
243
+ .selectAll<SVGGElement, XYAxesParams>(`g.${yLabelClassName}`)
244
+ .data([fullParams])
245
+ .join('g')
246
+ .classed(yLabelClassName, true)
247
+ .each((d, i, g) => {
248
+ const text = d3.select(g[i])
249
+ .selectAll<SVGTextElement, XYAxesParams>(`text`)
250
+ .data([d])
251
+ .join(
252
+ enter => {
253
+ return enter
254
+ .append('text')
255
+ .style('font-weight', 'bold')
256
+ },
257
+ update => update,
258
+ exit => exit.remove()
259
+ )
260
+ .attr('text-anchor', yAxisLabelAnchor)
261
+ .attr('dominant-baseline', yAxisLabelDominantBaseline)
262
+ .attr('font-size', fullChartParams.styles.textSize)
263
+ .style('fill', getColor(fullParams.yAxis.labelColorType, fullChartParams))
264
+ .style('transform', textReverseTransform)
265
+ // 偏移使用 x, y 而非 transform 才不會受到外層 scale 變形影響
266
+ .attr('x', labelX)
267
+ .attr('y', labelY)
268
+ .text(d => fullDataFormatter.yAxis.label)
269
+ })
270
+ // .attr('transform', d => `translate(0, ${layout.height})`)
271
+ }
272
+
273
+ function renderXAxis ({ selection, xAxisClassName, fullParams, layout, fullDataFormatter, fullChartParams, xScale, textReverseTransform, xyMinMax }: {
274
+ selection: d3.Selection<SVGGElement, any, any, any>,
275
+ xAxisClassName: string
276
+ fullParams: XYAxesParams
277
+ // tickTextAlign: TextAlign
278
+ layout: { width: number, height: number }
279
+ fullDataFormatter: DataFormatterMultiValue,
280
+ fullChartParams: ChartParams
281
+ xScale: d3.ScaleLinear<number, number>
282
+ textReverseTransform: string,
283
+ xyMinMax: {
284
+ minX: number;
285
+ maxX: number;
286
+ minY: number;
287
+ maxY: number;
288
+ }
289
+ }) {
290
+
291
+ const xAxisSelection = selection
292
+ .selectAll<SVGGElement, XYAxesParams>(`g.${xAxisClassName}`)
293
+ .data([fullParams])
294
+ .join('g')
295
+ .classed(xAxisClassName, true)
296
+ .attr('transform', `translate(0, ${layout.height})`)
297
+
298
+ // const _xScale = d3.scaleLinear()
299
+ // .domain([0, 150])
300
+ // .range([416.5, 791.349])
301
+
302
+ // 刻度文字偏移
303
+ let tickPadding = fullParams.xAxis.tickPadding
304
+
305
+ // 設定Y軸刻度
306
+ const xAxis = d3.axisBottom(xScale)
307
+ .scale(xScale)
308
+ .ticks(fullParams.xAxis.ticks) // 刻度分段數量
309
+ .tickFormat(d => parseTickFormatValue(d, fullParams.xAxis.tickFormat))
310
+ .tickSize(fullParams.xAxis.tickFullLine == true
311
+ ? -layout.height
312
+ : defaultTickSize)
313
+ .tickSizeOuter(-layout.height)
314
+ .tickPadding(tickPadding)
315
+
316
+ const xAxisEl = xAxisSelection
317
+ .transition()
318
+ .duration(100)
319
+ .call(xAxis)
320
+
321
+ xAxisEl.selectAll('line')
322
+ .style('fill', 'none')
323
+ .style('stroke', fullParams.xAxis.tickLineVisible == true ? getColor(fullParams.xAxis.tickColorType, fullChartParams) : 'none')
324
+ .style('stroke-dasharray', fullParams.xAxis.tickFullLineDasharray)
325
+ .attr('pointer-events', 'none')
326
+
327
+ xAxisEl.selectAll('path')
328
+ .style('fill', 'none')
329
+ // .style('stroke', this.fullParams.axisLineColor!)
330
+ .style('stroke', fullParams.xAxis.axisLineVisible == true ? getColor(fullParams.xAxis.axisLineColorType, fullChartParams) : 'none')
331
+ .style('shape-rendering', 'crispEdges')
332
+
333
+ // const xText = xAxisEl.selectAll('text')
334
+ const xText = xAxisSelection.selectAll('text')
335
+ // .style('font-family', 'sans-serif')
336
+ .attr('font-size', fullChartParams.styles.textSize)
337
+ .style('color', getColor(fullParams.xAxis.tickTextColorType, fullChartParams))
338
+ .attr('text-anchor', xTickTextAnchor)
339
+ .attr('dominant-baseline', xTickDominantBaseline)
340
+ .attr('dy', 0)
341
+ .attr('y', tickPadding)
342
+ xText.style('transform', textReverseTransform)
343
+
344
+ // // 抵消掉預設的偏移
345
+ // if (fullDataFormatter.grid.valueAxis.position === 'bottom' || fullDataFormatter.grid.valueAxis.position === 'top') {
346
+ // xText.attr('dy', 0)
347
+ // }
348
+
349
+ return xAxisSelection
350
+ }
351
+
352
+ function renderYAxis ({ selection, yAxisClassName, fullParams, layout, fullDataFormatter, fullChartParams, yScale, textReverseTransform, xyMinMax }: {
353
+ selection: d3.Selection<SVGGElement, any, any, any>,
354
+ yAxisClassName: string
355
+ fullParams: XYAxesParams
356
+ // tickTextAlign: TextAlign
357
+ layout: { width: number, height: number }
358
+ fullDataFormatter: DataFormatterMultiValue,
359
+ fullChartParams: ChartParams
360
+ yScale: d3.ScaleLinear<number, number>
361
+ textReverseTransform: string,
362
+ xyMinMax: {
363
+ minX: number;
364
+ maxX: number;
365
+ minY: number;
366
+ maxY: number;
367
+ }
368
+ }) {
369
+
370
+ const yAxisSelection = selection
371
+ .selectAll<SVGGElement, XYAxesParams>(`g.${yAxisClassName}`)
372
+ .data([fullParams])
373
+ .join('g')
374
+ .classed(yAxisClassName, true)
375
+
376
+ // const _yScale = d3.scaleLinear()
377
+ // .domain([0, 150])
378
+ // .range([416.5, 791.349])
379
+
380
+ // 刻度文字偏移
381
+ let tickPadding = fullParams.yAxis.tickPadding
382
+
383
+ // 設定Y軸刻度
384
+ const yAxis = d3.axisLeft(yScale)
385
+ .scale(yScale)
386
+ .ticks(fullParams.yAxis.ticks) // 刻度分段數量
387
+ .tickFormat(d => parseTickFormatValue(d, fullParams.yAxis.tickFormat))
388
+ .tickSize(fullParams.yAxis.tickFullLine == true
389
+ ? -layout.width
390
+ : defaultTickSize)
391
+ .tickPadding(tickPadding)
392
+
393
+ const yAxisEl = yAxisSelection
394
+ .transition()
395
+ .duration(100)
396
+ .call(yAxis)
397
+
398
+ yAxisEl.selectAll('line')
399
+ .style('fill', 'none')
400
+ .style('stroke', fullParams.yAxis.tickLineVisible == true ? getColor(fullParams.yAxis.tickColorType, fullChartParams) : 'none')
401
+ .style('stroke-dasharray', fullParams.yAxis.tickFullLineDasharray)
402
+ .attr('pointer-events', 'none')
403
+
404
+ yAxisEl.selectAll('path')
405
+ .style('fill', 'none')
406
+ // .style('stroke', this.fullParams.axisLineColor!)
407
+ .style('stroke', fullParams.yAxis.axisLineVisible == true ? getColor(fullParams.yAxis.axisLineColorType, fullChartParams) : 'none')
408
+ .style('shape-rendering', 'crispEdges')
409
+
410
+ // const yText = yAxisEl.selectAll('text')
411
+ const yText = yAxisSelection.selectAll('text')
412
+ // .style('font-family', 'sans-serif')
413
+ .attr('font-size', fullChartParams.styles.textSize)
414
+ .style('color', getColor(fullParams.yAxis.tickTextColorType, fullChartParams))
415
+ .attr('text-anchor', yTickTextAnchor)
416
+ .attr('dominant-baseline', yTickDominantBaseline)
417
+ // .attr('dy', 0)
418
+ .attr('x', - tickPadding)
419
+ .attr('dy', 0)
420
+ yText.style('transform', textReverseTransform)
421
+
422
+ // // 抵消掉預設的偏移
423
+ // if (fullDataFormatter.grid.valueAxis.position === 'bottom' || fullDataFormatter.grid.valueAxis.position === 'top') {
424
+ // yText.attr('dy', 0)
425
+ // }
426
+
427
+ return yAxisSelection
428
+ }
429
+
430
+
431
+ export const XYAxes = defineMultiValuePlugin(pluginConfig)(({ selection, name, observer, subject }) => {
432
+
433
+ const destroy$ = new Subject()
434
+
435
+ const containerClassName = getClassName(pluginName, 'container')
436
+ const xyAxisGClassName = getClassName(pluginName, 'xyAxisG')
437
+ const xAxisClassName = getClassName(pluginName, 'xAxis')
438
+ const yAxisClassName = getClassName(pluginName, 'yAxis')
439
+ const xLabelClassName = getClassName(pluginName, 'xLabel')
440
+ const yLabelClassName = getClassName(pluginName, 'yLabel')
441
+
442
+ const containerSelection$ = combineLatest({
443
+ computedData: observer.computedData$.pipe(
444
+ distinctUntilChanged((a, b) => {
445
+ // 只有當series的數量改變時,才重新計算
446
+ return a.length === b.length
447
+ }),
448
+ ),
449
+ isCategorySeprate: observer.isCategorySeprate$
450
+ }).pipe(
451
+ takeUntil(destroy$),
452
+ switchMap(async (d) => d),
453
+ map(data => {
454
+ return data.isCategorySeprate
455
+ // category分開的時候顯示各別axis
456
+ ? data.computedData
457
+ // category合併的時候只顯示第一個axis
458
+ : [data.computedData[0]]
459
+ }),
460
+ map((computedData, i) => {
461
+ return selection
462
+ .selectAll<SVGGElement, ComputedDatumMultiValue[]>(`g.${containerClassName}`)
463
+ .data(computedData, d => (d && d[0]) ? d[0].categoryIndex : i)
464
+ .join('g')
465
+ .classed(containerClassName, true)
466
+ })
467
+ )
468
+
469
+ const axisSelection$ = containerSelection$.pipe(
470
+ takeUntil(destroy$),
471
+ map((containerSelection, i) => {
472
+ return containerSelection
473
+ .selectAll<SVGGElement, ComputedDatumMultiValue[]>(`g.${xyAxisGClassName}`)
474
+ .data([xyAxisGClassName])
475
+ .join('g')
476
+ .classed(xyAxisGClassName, true)
477
+ })
478
+ )
479
+
480
+ combineLatest({
481
+ containerSelection: containerSelection$,
482
+ gridContainerPosition: observer.containerPosition$
483
+ }).pipe(
484
+ takeUntil(destroy$),
485
+ switchMap(async d => d)
486
+ ).subscribe(data => {
487
+ data.containerSelection
488
+ .attr('transform', (d, i) => {
489
+ const gridContainerPosition = data.gridContainerPosition[i] ?? data.gridContainerPosition[0]
490
+ const translate = gridContainerPosition.translate
491
+ const scale = gridContainerPosition.scale
492
+ return `translate(${translate[0]}, ${translate[1]}) scale(${scale[0]}, ${scale[1]})`
493
+ })
494
+ // .attr('opacity', 0)
495
+ // .transition()
496
+ // .attr('opacity', 1)
497
+ })
498
+
499
+ const textReverseTransform$ = observer.containerPosition$.pipe(
500
+ takeUntil(destroy$),
501
+ switchMap(async (d) => d),
502
+ map(containerPosition => {
503
+ // const axesRotateXYReverseValue = `rotateX(${data.gridAxesReverseTransform.rotateX}deg) rotateY(${data.gridAxesReverseTransform.rotateY}deg)`
504
+ // const axesRotateReverseValue = `rotate(${data.gridAxesReverseTransform.rotate}deg)`
505
+ const containerScaleReverseValue = `scale(${1 / containerPosition[0].scale[0]}, ${1 / containerPosition[0].scale[1]})`
506
+ // 抵消最外層scale
507
+ return `${containerScaleReverseValue}`
508
+ }),
509
+ distinctUntilChanged()
510
+ )
511
+
512
+ // const minMax$: Observable<[number, number]> = new Observable(subscriber => {
513
+ // combineLatest({
514
+ // fullDataFormatter: observer.fullDataFormatter$,
515
+ // computedData: observer.computedData$
516
+ // }).pipe(
517
+ // takeUntil(destroy$),
518
+ // switchMap(async (d) => d),
519
+ // ).subscribe(data => {
520
+ // const groupMin = 0
521
+ // const groupMax = data.computedData[0] ? data.computedData[0].length - 1 : 0
522
+ // // const groupScaleDomainMin = data.fullDataFormatter.grid.groupAxis.scaleDomain[0] === 'auto'
523
+ // // ? groupMin - data.fullDataFormatter.grid.groupAxis.scalePadding
524
+ // // : data.fullDataFormatter.grid.groupAxis.scaleDomain[0] as number - data.fullDataFormatter.grid.groupAxis.scalePadding
525
+ // const groupScaleDomainMin = data.fullDataFormatter.grid.groupAxis.scaleDomain[0] - data.fullDataFormatter.grid.groupAxis.scalePadding
526
+ // const groupScaleDomainMax = data.fullDataFormatter.grid.groupAxis.scaleDomain[1] === 'max'
527
+ // ? groupMax + data.fullDataFormatter.grid.groupAxis.scalePadding
528
+ // : data.fullDataFormatter.grid.groupAxis.scaleDomain[1] as number + data.fullDataFormatter.grid.groupAxis.scalePadding
529
+
530
+ // const filteredData = data.computedData.map((d, i) => {
531
+ // return d.filter((_d, _i) => {
532
+ // return _i >= groupScaleDomainMin && _i <= groupScaleDomainMax
533
+ // })
534
+ // })
535
+
536
+ // const filteredMinMax = getMinMax(filteredData.flat().map(d => d.value[1]))
537
+ // if (filteredMinMax[0] === filteredMinMax[1]) {
538
+ // filteredMinMax[0] = filteredMinMax[1] - 1 // 避免最大及最小值相同造成無法計算scale
539
+ // }
540
+ // subscriber.next(filteredMinMax)
541
+ // })
542
+ // })
543
+
544
+ const xScale$: Observable<d3.ScaleLinear<number, number>> = new Observable(subscriber => {
545
+ combineLatest({
546
+ fullDataFormatter: observer.fullDataFormatter$,
547
+ layout: observer.layout$,
548
+ // xyMinMax: observer.xyMinMax$
549
+ filteredXYMinMaxData: observer.filteredXYMinMaxData$
550
+ }).pipe(
551
+ takeUntil(destroy$),
552
+ switchMap(async (d) => d),
553
+ ).subscribe(data => {
554
+ if (!data.filteredXYMinMaxData.minXDatum || !data.filteredXYMinMaxData.maxXDatum
555
+ || data.filteredXYMinMaxData.minXDatum.value[0] == null || data.filteredXYMinMaxData.maxXDatum.value[0] == null
556
+ ) {
557
+ return
558
+ }
559
+ let maxValue = data.filteredXYMinMaxData.maxXDatum.value[0]
560
+ let minValue = data.filteredXYMinMaxData.minXDatum.value[0]
561
+ if (maxValue === minValue && maxValue === 0) {
562
+ // 避免最大及最小值同等於 0 造成無法計算scale
563
+ maxValue = 1
564
+ }
565
+
566
+ const xScale: d3.ScaleLinear<number, number> = createValueToAxisScale({
567
+ maxValue,
568
+ minValue,
569
+ axisWidth: data.layout.width,
570
+ scaleDomain: data.fullDataFormatter.xAxis.scaleDomain,
571
+ scaleRange: data.fullDataFormatter.xAxis.scaleRange,
572
+ })
573
+
574
+ subscriber.next(xScale)
575
+ })
576
+ })
577
+
578
+ const yScale$: Observable<d3.ScaleLinear<number, number>> = new Observable(subscriber => {
579
+ combineLatest({
580
+ fullDataFormatter: observer.fullDataFormatter$,
581
+ layout: observer.layout$,
582
+ // xyMinMax: observer.xyMinMax$
583
+ filteredXYMinMaxData: observer.filteredXYMinMaxData$
584
+ }).pipe(
585
+ takeUntil(destroy$),
586
+ switchMap(async (d) => d),
587
+ ).subscribe(data => {
588
+ if (!data.filteredXYMinMaxData.minYDatum || !data.filteredXYMinMaxData.maxYDatum
589
+ || data.filteredXYMinMaxData.minYDatum.value[1] == null || data.filteredXYMinMaxData.maxYDatum.value[1] == null
590
+ ) {
591
+ return
592
+ }
593
+ let maxValue = data.filteredXYMinMaxData.maxYDatum.value[1]
594
+ let minValue = data.filteredXYMinMaxData.minYDatum.value[1]
595
+ if (maxValue === minValue && maxValue === 0) {
596
+ // 避免最大及最小值同等於 0 造成無法計算scale
597
+ maxValue = 1
598
+ }
599
+
600
+ const yScale: d3.ScaleLinear<number, number> = createValueToAxisScale({
601
+ maxValue,
602
+ minValue,
603
+ axisWidth: data.layout.height,
604
+ scaleDomain: data.fullDataFormatter.yAxis.scaleDomain,
605
+ scaleRange: data.fullDataFormatter.yAxis.scaleRange,
606
+ reverse: true
607
+ })
608
+
609
+ subscriber.next(yScale)
610
+ })
611
+ })
612
+
613
+
614
+ combineLatest({
615
+ axisSelection: axisSelection$,
616
+ fullParams: observer.fullParams$,
617
+ // tickTextAlign: tickTextAlign$,
618
+ // axisLabelAlign: axisLabelAlign$,
619
+ computedData: observer.computedData$,
620
+ layout: observer.layout$,
621
+ fullDataFormatter: observer.fullDataFormatter$,
622
+ fullChartParams: observer.fullChartParams$,
623
+ xScale: xScale$,
624
+ yScale: yScale$,
625
+ textReverseTransform: textReverseTransform$,
626
+ xyMinMax: observer.xyMinMax$
627
+ }).pipe(
628
+ takeUntil(destroy$),
629
+ switchMap(async (d) => d),
630
+ ).subscribe(data => {
631
+
632
+ renderXAxis({
633
+ selection: data.axisSelection,
634
+ xAxisClassName,
635
+ fullParams: data.fullParams,
636
+ // tickTextAlign: data.tickTextAlign,
637
+ layout: data.layout,
638
+ fullDataFormatter: data.fullDataFormatter,
639
+ fullChartParams: data.fullChartParams,
640
+ xScale: data.xScale,
641
+ textReverseTransform: data.textReverseTransform,
642
+ xyMinMax: data.xyMinMax
643
+ })
644
+
645
+ renderYAxis({
646
+ selection: data.axisSelection,
647
+ yAxisClassName,
648
+ fullParams: data.fullParams,
649
+ // tickTextAlign: data.tickTextAlign,
650
+ layout: data.layout,
651
+ fullDataFormatter: data.fullDataFormatter,
652
+ fullChartParams: data.fullChartParams,
653
+ yScale: data.yScale,
654
+ textReverseTransform: data.textReverseTransform,
655
+ xyMinMax: data.xyMinMax
656
+ })
657
+
658
+ renderXAxisLabel({
659
+ selection: data.axisSelection,
660
+ xLabelClassName,
661
+ fullParams: data.fullParams,
662
+ // axisLabelAlign: data.axisLabelAlign,
663
+ layout: data.layout,
664
+ fullDataFormatter: data.fullDataFormatter,
665
+ fullChartParams: data.fullChartParams,
666
+ textReverseTransform: data.textReverseTransform,
667
+ })
668
+
669
+ renderYAxisLabel({
670
+ selection: data.axisSelection,
671
+ yLabelClassName,
672
+ fullParams: data.fullParams,
673
+ // axisLabelAlign: data.axisLabelAlign,
674
+ layout: data.layout,
675
+ fullDataFormatter: data.fullDataFormatter,
676
+ fullChartParams: data.fullChartParams,
677
+ textReverseTransform: data.textReverseTransform,
678
+ })
679
+ })
680
+
681
+ return () => {
682
+ destroy$.next(undefined)
683
+ }
684
684
  })