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