@orbcharts/plugins-basic 3.0.0-beta.2 → 3.0.0-beta.20

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 (182) hide show
  1. package/LICENSE +200 -200
  2. package/dist/orbcharts-plugins-basic.es.js +15169 -10684
  3. package/dist/orbcharts-plugins-basic.umd.js +296 -83
  4. package/dist/src/base/BaseBars.d.ts +4 -4
  5. package/dist/src/base/BaseBarsTriangle.d.ts +4 -4
  6. package/dist/src/base/BaseDots.d.ts +4 -4
  7. package/dist/src/base/BaseGroupAxis.d.ts +2 -2
  8. package/dist/src/base/BaseLegend.d.ts +1 -1
  9. package/dist/src/base/BaseLineAreas.d.ts +5 -5
  10. package/dist/src/base/BaseLines.d.ts +5 -5
  11. package/dist/src/base/BaseRacingBars.d.ts +23 -0
  12. package/dist/src/base/BaseRacingLabels.d.ts +20 -0
  13. package/dist/src/base/BaseRacingValueLabels.d.ts +20 -0
  14. package/dist/src/base/{BaseBarStack.d.ts → BaseStackedBars.d.ts} +8 -8
  15. package/dist/src/base/BaseValueAxis.d.ts +6 -8
  16. package/dist/src/base/BaseXAxis.d.ts +20 -0
  17. package/dist/src/base/BaseYAxis.d.ts +18 -0
  18. package/dist/src/grid/defaults.d.ts +5 -5
  19. package/dist/src/grid/gridObservables.d.ts +12 -5
  20. package/dist/src/grid/index.d.ts +3 -3
  21. package/dist/src/grid/plugins/GroupZoom.d.ts +1 -0
  22. package/dist/src/grid/plugins/StackedBars.d.ts +1 -0
  23. package/dist/src/grid/plugins/StackedValueAxis.d.ts +1 -0
  24. package/dist/src/index.d.ts +2 -0
  25. package/dist/src/multiGrid/defaults.d.ts +4 -4
  26. package/dist/src/multiGrid/index.d.ts +3 -3
  27. package/dist/src/multiGrid/plugins/MultiStackedBars.d.ts +1 -0
  28. package/dist/src/multiGrid/plugins/MultiStackedValueAxis.d.ts +1 -0
  29. package/dist/src/multiGrid/plugins/OverlappingStackedValueAxes.d.ts +1 -0
  30. package/dist/src/multiValue/defaults.d.ts +13 -0
  31. package/dist/src/multiValue/index.d.ts +12 -0
  32. package/dist/src/multiValue/multiValueObservables.d.ts +56 -0
  33. package/dist/src/multiValue/plugins/MultiValueLegend.d.ts +1 -0
  34. package/dist/src/multiValue/plugins/MultiValueTooltip.d.ts +1 -0
  35. package/dist/src/multiValue/plugins/RacingBars.d.ts +1 -0
  36. package/dist/src/multiValue/plugins/RacingCounterTexts.d.ts +3 -0
  37. package/dist/src/multiValue/plugins/RacingValueAxis.d.ts +1 -0
  38. package/dist/src/multiValue/plugins/Scatter.d.ts +3 -0
  39. package/dist/src/multiValue/plugins/ScatterBubbles.d.ts +3 -0
  40. package/dist/src/multiValue/plugins/XAxis.d.ts +1 -0
  41. package/dist/src/multiValue/plugins/XYAux.d.ts +3 -0
  42. package/dist/src/multiValue/plugins/XYAxes.d.ts +1 -0
  43. package/dist/src/multiValue/plugins/XYAxes_legacy.d.ts +3 -0
  44. package/dist/src/multiValue/plugins/XZoom.d.ts +1 -0
  45. package/dist/src/noneData/plugins/Container.d.ts +0 -1
  46. package/dist/src/noneData/plugins/Tooltip.d.ts +0 -3
  47. package/dist/src/relationship/defaults.d.ts +6 -0
  48. package/dist/src/relationship/index.d.ts +5 -0
  49. package/dist/src/relationship/plugins/ForceDirected.d.ts +3 -0
  50. package/dist/src/relationship/plugins/ForceDirectedBubbles.d.ts +3 -0
  51. package/dist/src/relationship/plugins/RelationshipLegend.d.ts +1 -0
  52. package/dist/src/relationship/plugins/RelationshipTooltip.d.ts +1 -0
  53. package/dist/src/relationship/relationshipObservables.d.ts +1 -0
  54. package/dist/src/series/seriesObservables.d.ts +3 -3
  55. package/dist/src/utils/commonUtils.d.ts +2 -1
  56. package/dist/src/utils/d3Graphics.d.ts +2 -1
  57. package/dist/src/utils/d3Utils.d.ts +3 -2
  58. package/dist/src/utils/observables.d.ts +1 -1
  59. package/dist/src/utils/orbchartsUtils.d.ts +3 -3
  60. package/lib/core-types.ts +7 -7
  61. package/lib/core.ts +6 -6
  62. package/lib/plugins-basic-types.ts +6 -6
  63. package/package.json +44 -44
  64. package/src/base/BaseBars.ts +765 -765
  65. package/src/base/BaseBarsTriangle.ts +676 -674
  66. package/src/base/BaseDots.ts +464 -515
  67. package/src/base/BaseGroupAxis.ts +691 -679
  68. package/src/base/BaseLegend.ts +684 -680
  69. package/src/base/BaseLineAreas.ts +629 -629
  70. package/src/base/BaseLines.ts +706 -706
  71. package/src/base/BaseRacingBars.ts +552 -0
  72. package/src/base/BaseRacingLabels.ts +396 -0
  73. package/src/base/BaseRacingValueLabels.ts +403 -0
  74. package/src/base/{BaseBarStack.ts → BaseStackedBars.ts} +782 -780
  75. package/src/base/BaseTooltip.ts +386 -385
  76. package/src/base/BaseValueAxis.ts +600 -580
  77. package/src/base/BaseXAxis.ts +427 -0
  78. package/src/base/BaseYAxis.ts +389 -0
  79. package/src/base/types.ts +2 -2
  80. package/src/const.ts +30 -30
  81. package/src/grid/defaults.ts +213 -244
  82. package/src/grid/gridObservables.ts +612 -545
  83. package/src/grid/index.ts +16 -16
  84. package/src/grid/plugins/Bars.ts +69 -69
  85. package/src/grid/plugins/BarsPN.ts +66 -66
  86. package/src/grid/plugins/BarsTriangle.ts +73 -73
  87. package/src/grid/plugins/Dots.ts +68 -68
  88. package/src/grid/plugins/GridLegend.ts +107 -107
  89. package/src/grid/plugins/GridTooltip.ts +66 -66
  90. package/src/grid/plugins/GroupAux.ts +1120 -1098
  91. package/src/grid/plugins/GroupAxis.ts +73 -97
  92. package/src/grid/plugins/{GridZoom.ts → GroupZoom.ts} +218 -218
  93. package/src/grid/plugins/LineAreas.ts +65 -65
  94. package/src/grid/plugins/Lines.ts +59 -59
  95. package/src/grid/plugins/{BarStack.ts → StackedBars.ts} +64 -64
  96. package/src/grid/plugins/{ValueStackAxis.ts → StackedValueAxis.ts} +96 -95
  97. package/src/grid/plugins/ValueAxis.ts +94 -93
  98. package/src/index.ts +6 -9
  99. package/src/multiGrid/defaults.ts +244 -224
  100. package/src/multiGrid/index.ts +15 -15
  101. package/src/multiGrid/multiGridObservables.ts +50 -49
  102. package/src/multiGrid/plugins/MultiBars.ts +108 -108
  103. package/src/multiGrid/plugins/MultiBarsTriangle.ts +114 -114
  104. package/src/multiGrid/plugins/MultiDots.ts +102 -102
  105. package/src/multiGrid/plugins/MultiGridLegend.ts +169 -159
  106. package/src/multiGrid/plugins/MultiGridTooltip.ts +66 -66
  107. package/src/multiGrid/plugins/MultiGroupAxis.ts +137 -137
  108. package/src/multiGrid/plugins/MultiLineAreas.ts +107 -107
  109. package/src/multiGrid/plugins/MultiLines.ts +101 -101
  110. package/src/multiGrid/plugins/{MultiBarStack.ts → MultiStackedBars.ts} +106 -106
  111. package/src/multiGrid/plugins/{MultiValueStackAxis.ts → MultiStackedValueAxis.ts} +134 -133
  112. package/src/multiGrid/plugins/MultiValueAxis.ts +134 -133
  113. package/src/multiGrid/plugins/{OverlappingValueStackAxes.ts → OverlappingStackedValueAxes.ts} +300 -298
  114. package/src/multiGrid/plugins/OverlappingValueAxes.ts +300 -299
  115. package/src/multiValue/defaults.ts +432 -0
  116. package/src/multiValue/index.ts +13 -0
  117. package/src/multiValue/multiValueObservables.ts +667 -0
  118. package/src/multiValue/plugins/MultiValueLegend.ts +107 -0
  119. package/src/multiValue/plugins/MultiValueTooltip.ts +66 -0
  120. package/src/multiValue/plugins/RacingBars.ts +373 -0
  121. package/src/multiValue/plugins/RacingCounterTexts.ts +300 -0
  122. package/src/multiValue/plugins/RacingValueAxis.ts +115 -0
  123. package/src/multiValue/plugins/RankingAxis_legacy.ts +109 -0
  124. package/src/multiValue/plugins/Scatter.ts +426 -0
  125. package/src/multiValue/plugins/ScatterBubbles.ts +554 -0
  126. package/src/multiValue/plugins/XAxis.ts +108 -0
  127. package/src/multiValue/plugins/XYAux.ts +683 -0
  128. package/src/multiValue/plugins/XYAxes.ts +195 -0
  129. package/src/multiValue/plugins/XYAxes_legacy.ts +684 -0
  130. package/src/multiValue/plugins/XZoom.ts +300 -0
  131. package/src/noneData/defaults.ts +102 -102
  132. package/src/noneData/index.ts +3 -3
  133. package/src/noneData/plugins/Container.ts +28 -28
  134. package/src/noneData/plugins/Tooltip.ts +374 -374
  135. package/src/relationship/defaults.ts +222 -0
  136. package/src/relationship/index.ts +5 -0
  137. package/src/relationship/plugins/ForceDirected.ts +1174 -0
  138. package/src/relationship/plugins/ForceDirectedBubbles.ts +1412 -0
  139. package/src/relationship/plugins/RelationshipLegend.ts +100 -0
  140. package/src/relationship/plugins/RelationshipTooltip.ts +66 -0
  141. package/src/relationship/relationshipObservables.ts +50 -0
  142. package/src/series/defaults.ts +221 -206
  143. package/src/series/index.ts +9 -9
  144. package/src/series/plugins/Bubbles.ts +636 -603
  145. package/src/series/plugins/Pie.ts +623 -623
  146. package/src/series/plugins/PieEventTexts.ts +284 -283
  147. package/src/series/plugins/PieLabels.ts +640 -640
  148. package/src/series/plugins/Rose.ts +516 -516
  149. package/src/series/plugins/RoseLabels.ts +600 -600
  150. package/src/series/plugins/SeriesLegend.ts +107 -107
  151. package/src/series/plugins/SeriesTooltip.ts +66 -66
  152. package/src/series/seriesObservables.ts +145 -145
  153. package/src/series/seriesUtils.ts +51 -51
  154. package/src/tree/defaults.ts +102 -78
  155. package/src/tree/index.ts +4 -4
  156. package/src/tree/plugins/TreeLegend.ts +100 -107
  157. package/src/tree/plugins/TreeMap.ts +341 -333
  158. package/src/tree/plugins/TreeTooltip.ts +66 -66
  159. package/src/utils/commonUtils.ts +31 -22
  160. package/src/utils/d3Graphics.ts +176 -174
  161. package/src/utils/d3Utils.ts +92 -73
  162. package/src/utils/observables.ts +14 -14
  163. package/src/utils/orbchartsUtils.ts +130 -100
  164. package/tsconfig.base.json +13 -13
  165. package/tsconfig.json +2 -2
  166. package/vite.config.js +22 -22
  167. package/dist/src/grid/plugins/BarStack.d.ts +0 -1
  168. package/dist/src/grid/plugins/GridZoom.d.ts +0 -1
  169. package/dist/src/grid/plugins/ValueStackAxis.d.ts +0 -1
  170. package/dist/src/multiGrid/plugins/MultiBarStack.d.ts +0 -1
  171. package/dist/src/multiGrid/plugins/MultiValueStackAxis.d.ts +0 -1
  172. package/dist/src/multiGrid/plugins/OverlappingValueStackAxes.d.ts +0 -1
  173. package/src/grid/plugins/RankingAxis.ts +0 -0
  174. package/src/multiValue/plugins/ScatterAxes.ts +0 -0
  175. package/src/relationship/plugins/Relationship.ts +0 -0
  176. /package/dist/src/{base/BaseGroupArea.d.ts → multiValue/plugins/OrdinalBubbles.d.ts} +0 -0
  177. /package/dist/src/{grid/plugins/Ranking.d.ts → multiValue/plugins/OrdinalXAxis.d.ts} +0 -0
  178. /package/dist/src/{grid/plugins/RankingAxis.d.ts → multiValue/plugins/RacingCounterAxis.d.ts} +0 -0
  179. /package/dist/src/multiValue/plugins/{ScatterAxes.d.ts → RankingAxis_legacy.d.ts} +0 -0
  180. /package/{dist/src/relationship/plugins/Relationship.d.ts → src/multiValue/plugins/OrdinalBubbles.ts} +0 -0
  181. /package/src/{base/BaseGroupArea.ts → multiValue/plugins/OrdinalXAxis.ts} +0 -0
  182. /package/src/{grid/plugins/Ranking.ts → multiValue/plugins/RacingCounterAxis.ts} +0 -0
@@ -0,0 +1,683 @@
1
+ import * as d3 from 'd3'
2
+ import {
3
+ // of,
4
+ iif,
5
+ debounceTime,
6
+ EMPTY,
7
+ combineLatest,
8
+ switchMap,
9
+ map,
10
+ filter,
11
+ first,
12
+ takeUntil,
13
+ distinctUntilChanged,
14
+ shareReplay,
15
+ Subject,
16
+ Observable } from 'rxjs'
17
+ import {
18
+ defineMultiValuePlugin } from '../../../lib/core'
19
+ import type { DefinePluginConfig } from '../../../lib/core-types'
20
+ import type {
21
+ ColorType,
22
+ TransformData,
23
+ DataFormatterMultiValue,
24
+ ChartParams,
25
+ Layout,
26
+ } from '../../../lib/core-types'
27
+ import { DEFAULT_X_Y_AUX_PARAMS } from '../defaults'
28
+ import { parseTickFormatValue } from '../../utils/d3Utils'
29
+ import { measureTextWidth } from '../../utils/commonUtils'
30
+ import { getColor, getClassName, getUniID } from '../../utils/orbchartsUtils'
31
+ import { d3EventObservable } from '../../utils/observables'
32
+ import { multiValueXYPositionObservable } from '../multiValueObservables'
33
+ import type { XYAuxParams } from '../../../lib/plugins-basic-types'
34
+ import { multiValueSelectionsObservable } from '../multiValueObservables'
35
+ import { renderTspansOnAxis } from '../../utils/d3Graphics'
36
+ import { LAYER_INDEX_OF_AUX } from '../../const'
37
+
38
+ interface LineDatum {
39
+ id: string
40
+ x1: number
41
+ x2: number
42
+ y1: number
43
+ y2: number
44
+ dashArray: string
45
+ colorType: ColorType
46
+ }
47
+
48
+ interface LabelDatum {
49
+ id: string
50
+ text: string
51
+ textArr: string[]
52
+ textWidth: number
53
+ textHeight: number
54
+ colorType: ColorType
55
+ textColorType: ColorType
56
+ x: number
57
+ y: number
58
+ rectWidth: number
59
+ rectHeight: number
60
+ rectX: number
61
+ rectY: number
62
+ textX: number
63
+ textY: number
64
+ }
65
+
66
+ const pluginName = 'XYAux'
67
+ const labelClassName = getClassName(pluginName, 'label-box')
68
+
69
+ const pluginConfig: DefinePluginConfig<typeof pluginName, typeof DEFAULT_X_Y_AUX_PARAMS> = {
70
+ name: pluginName,
71
+ defaultParams: DEFAULT_X_Y_AUX_PARAMS,
72
+ layerIndex: LAYER_INDEX_OF_AUX,
73
+ validator: (params, { validateColumns }) => {
74
+ const result = validateColumns(params, {
75
+ xAxis: {
76
+ toBeTypes: ['object']
77
+ },
78
+ yAxis: {
79
+ toBeTypes: ['object']
80
+ }
81
+ })
82
+ if (params.xAxis) {
83
+ const forceResult = validateColumns(params.xAxis, {
84
+ showLine: {
85
+ toBeTypes: ['boolean']
86
+ },
87
+ showLabel: {
88
+ toBeTypes: ['boolean']
89
+ },
90
+ lineDashArray: {
91
+ toBeTypes: ['string']
92
+ },
93
+ lineColorType: {
94
+ toBeOption: 'ColorType'
95
+ },
96
+ labelColorType: {
97
+ toBeOption: 'ColorType'
98
+ },
99
+ labelTextColorType: {
100
+ toBeOption: 'ColorType'
101
+ },
102
+ labelTextFormat: {
103
+ toBeTypes: ['string', 'Function']
104
+ },
105
+ labelPadding: {
106
+ toBeTypes: ['number']
107
+ },
108
+ })
109
+ if (forceResult.status === 'error') {
110
+ return forceResult
111
+ }
112
+ }
113
+ if (params.yAxis) {
114
+ const forceResult = validateColumns(params.yAxis, {
115
+ showLine: {
116
+ toBeTypes: ['boolean']
117
+ },
118
+ showLabel: {
119
+ toBeTypes: ['boolean']
120
+ },
121
+ lineDashArray: {
122
+ toBeTypes: ['string']
123
+ },
124
+ lineColorType: {
125
+ toBeOption: 'ColorType'
126
+ },
127
+ labelColorType: {
128
+ toBeOption: 'ColorType'
129
+ },
130
+ labelTextColorType: {
131
+ toBeOption: 'ColorType'
132
+ },
133
+ labelTextFormat: {
134
+ toBeTypes: ['string', 'Function']
135
+ },
136
+ labelPadding: {
137
+ toBeTypes: ['number']
138
+ },
139
+ })
140
+ if (forceResult.status === 'error') {
141
+ return forceResult
142
+ }
143
+ }
144
+ return result
145
+ }
146
+ }
147
+
148
+ function createLineData ({ axisX, axisY, layout, fullParams }: {
149
+ axisX: number
150
+ axisY: number
151
+ layout: Layout
152
+ fullParams: XYAuxParams
153
+ }): LineDatum[] {
154
+ if ((axisX >= 0 && axisX <= layout.width && axisY >= 0 && axisY <= layout.height) === false) {
155
+ return []
156
+ }
157
+ return [
158
+ {
159
+ id: 'line-x',
160
+ x1: axisX,
161
+ x2: axisX,
162
+ y1: 0,
163
+ y2: layout.height,
164
+ dashArray: fullParams.xAxis.lineDashArray ?? 'none',
165
+ colorType: fullParams.xAxis.lineColorType
166
+ },
167
+ {
168
+ id: 'line-0',
169
+ x1: 0,
170
+ x2: layout.width,
171
+ y1: axisY,
172
+ y2: axisY,
173
+ dashArray: fullParams.yAxis.lineDashArray ?? 'none',
174
+ colorType: fullParams.yAxis.lineColorType
175
+ }
176
+ ]
177
+ }
178
+
179
+ function createLabelData ({ axisX, axisY, xValue, yValue, fullParams, textSizePx, layout, columnAmount, rowAmount }: {
180
+ axisX: number
181
+ axisY: number
182
+ xValue: number
183
+ yValue: number
184
+ fullParams: XYAuxParams
185
+ textSizePx: number
186
+ layout: Layout
187
+ columnAmount: number
188
+ rowAmount: number
189
+ }): LabelDatum[] {
190
+ if ((axisX >= 0 && axisX <= layout.width && axisY >= 0 && axisY <= layout.height) === false) {
191
+ return []
192
+ }
193
+ const rectPaddingWidth = 6
194
+ const rectPaddingHeight = 3
195
+
196
+ // x
197
+ const xX = axisX
198
+ const xY = layout.height + (fullParams.xAxis.labelPadding * rowAmount) // rowAmount 是為了把外部 container 的變形逆轉回來
199
+ const xText = parseTickFormatValue(xValue, fullParams.xAxis.labelTextFormat)
200
+ const xTextArr = xText.split('\n')
201
+ const xMaxLengthText = xTextArr.reduce((acc, current) => current.length > acc.length ? current : acc, '')
202
+ const xTextWidth = measureTextWidth(xMaxLengthText, textSizePx)
203
+ const xTextHeight = textSizePx * xTextArr.length
204
+ const xRectWidth = xTextWidth + (rectPaddingWidth * 2)
205
+ const xRectHeight = xTextHeight + (rectPaddingHeight * 2)
206
+ const xRectX = - xRectWidth / 2
207
+ const xRectY = - rectPaddingHeight
208
+ const xTextX = xRectX + rectPaddingWidth
209
+ const xTextY = xRectY + rectPaddingHeight
210
+ // y
211
+ const yX = - (fullParams.yAxis.labelPadding * columnAmount) // columnAmount 是為了把外部 container 的變形逆轉回來
212
+ const yY = axisY
213
+ const yText = parseTickFormatValue(yValue, fullParams.yAxis.labelTextFormat)
214
+ const yTextArr = yText.split('\n')
215
+ const yMaxLengthText = yTextArr.reduce((acc, current) => current.length > acc.length ? current : acc, '')
216
+ const yTextWidth = measureTextWidth(yMaxLengthText, textSizePx)
217
+ const yTextHeight = textSizePx * yTextArr.length
218
+ const yRectWidth = yTextWidth + (rectPaddingWidth * 2)
219
+ const yRectHeight = yTextHeight + (rectPaddingHeight * 2)
220
+ const yRectX = - yTextWidth - rectPaddingWidth
221
+ const yRectY = - rectPaddingHeight - yTextHeight / 2
222
+ const yTextX = yRectX + rectPaddingWidth
223
+ const yTextY = yRectY + rectPaddingHeight
224
+ return [
225
+ {
226
+ id: 'label-x',
227
+ x: xX,
228
+ y: xY,
229
+ text: xText,
230
+ textArr: xTextArr,
231
+ textWidth: xTextWidth,
232
+ textHeight: xTextHeight,
233
+ colorType: fullParams.xAxis.labelColorType,
234
+ textColorType: fullParams.xAxis.labelTextColorType,
235
+ rectWidth: xRectWidth,
236
+ rectHeight: xRectHeight,
237
+ rectX: xRectX,
238
+ rectY: xRectY,
239
+ textX: xTextX,
240
+ textY: xTextY
241
+ },
242
+ {
243
+ id: 'label-y',
244
+ x: yX,
245
+ y: yY,
246
+ text: yText,
247
+ textArr: yTextArr,
248
+ textWidth: yTextWidth,
249
+ textHeight: yTextHeight,
250
+ colorType: fullParams.yAxis.labelColorType,
251
+ textColorType: fullParams.xAxis.labelTextColorType,
252
+ rectWidth: yRectWidth,
253
+ rectHeight: yRectHeight,
254
+ rectX: yRectX,
255
+ rectY: yRectY,
256
+ textX: yTextX,
257
+ textY: yTextY
258
+ }
259
+ ]
260
+ }
261
+
262
+ function renderLine ({ selection, pluginName, lineData, fullParams, fullChartParams }: {
263
+ selection: d3.Selection<any, string, any, unknown>
264
+ pluginName: string
265
+ lineData: LineDatum[]
266
+ fullParams: XYAuxParams
267
+ fullChartParams: ChartParams
268
+ }) {
269
+ const gClassName = getClassName(pluginName, 'auxline')
270
+ const auxLineSelection = selection
271
+ .selectAll<SVGLineElement, LineDatum>(`line.${gClassName}`)
272
+ .data(lineData)
273
+ .join(
274
+ enter => {
275
+ return enter
276
+ .append('line')
277
+ .classed(gClassName, true)
278
+ .style('stroke-width', 1)
279
+ .style('pointer-events', 'none')
280
+ .style('vector-effect', 'non-scaling-stroke')
281
+ .attr('x1', d => d.x1)
282
+ .attr('y1', d => d.y1)
283
+ .attr('x2', d => d.x2)
284
+ .attr('y2', d => d.y2)
285
+ },
286
+ update => {
287
+ const updateSelection = update
288
+ .transition()
289
+ .duration(50)
290
+ .attr('x1', d => d.x1)
291
+ .attr('y1', d => d.y1)
292
+ .attr('x2', d => d.x2)
293
+ .attr('y2', d => d.y2)
294
+ return updateSelection
295
+ },
296
+ exit => exit.remove()
297
+ )
298
+ .style('stroke', d => getColor(d.colorType, fullChartParams))
299
+ .style('stroke-dasharray', d => d.dashArray)
300
+
301
+ return auxLineSelection
302
+ }
303
+
304
+ function removeLine (selection: d3.Selection<any, string, any, unknown>) {
305
+ const update = selection
306
+ .selectAll<SVGLineElement, LineDatum>('line')
307
+ .data([])
308
+
309
+ update.exit().remove()
310
+ }
311
+
312
+ function renderLabel ({ selection, labelData, fullParams, fullDataFormatter, fullChartParams, textReverseTransform, textSizePx }: {
313
+ selection: d3.Selection<any, string, any, unknown>
314
+ labelData: LabelDatum[]
315
+ fullParams: XYAuxParams
316
+ fullDataFormatter: DataFormatterMultiValue
317
+ fullChartParams: ChartParams
318
+ // gridAxesReverseTransformValue: string
319
+ textReverseTransform: string
320
+ textSizePx: number
321
+ }) {
322
+ // const rectHeight = textSizePx + 6
323
+
324
+ const axisLabelSelection = selection
325
+ .selectAll<SVGGElement, LabelDatum>(`g.${labelClassName}`)
326
+ .data(labelData)
327
+ .join(
328
+ enter => {
329
+ return enter
330
+ .append('g')
331
+ .classed(labelClassName, true)
332
+ .style('cursor', 'pointer')
333
+ .attr("transform", (d, i) => {
334
+ return `translate(${d.x}, ${d.y})`
335
+ })
336
+ },
337
+ update => {
338
+ const updateSelection = update
339
+ .transition()
340
+ .duration(50)
341
+ .attr("transform", (d, i) => {
342
+ return `translate(${d.x}, ${d.y})`
343
+ })
344
+ return updateSelection
345
+ },
346
+ exit => exit.remove()
347
+ )
348
+ .each((datum, i, n) => {
349
+ // // const rectWidth = measureTextWidth(datum.text, textSizePx) + 12
350
+ // const rectWidth = datum.textWidth + 12
351
+ // const rectHeight = datum.textHeight + 6
352
+ // // -- label偏移位置 --
353
+ // let rectX = - rectWidth / 2
354
+ // let rectY = 2
355
+
356
+ // -- rect --
357
+ d3.select(n[i])
358
+ .selectAll<SVGRectElement, LabelDatum>('rect')
359
+ .data([datum])
360
+ .join(
361
+ enter => enter.append('rect')
362
+ .style('cursor', 'pointer')
363
+ .attr('rx', 5)
364
+ .attr('ry', 5),
365
+ update => update,
366
+ exit => exit.remove()
367
+ )
368
+ .attr('width', d => `${d.rectWidth}px`)
369
+ .attr('height', d => `${d.rectHeight}px`)
370
+ .attr('fill', d => getColor(d.colorType, fullChartParams))
371
+ .attr('x', d => d.rectX)
372
+ .attr('y', d => d.rectY)
373
+ .style('transform', textReverseTransform)
374
+
375
+ // -- text --
376
+ d3.select(n[i])
377
+ .selectAll<SVGTextElement, LabelDatum>('text')
378
+ .data([datum])
379
+ .join(
380
+ enter => enter.append('text')
381
+ .style('dominant-baseline', 'hanging')
382
+ .style('cursor', 'pointer')
383
+ .style('pointer-events', 'none'),
384
+ update => update,
385
+ exit => exit.remove()
386
+ )
387
+ .style('transform', textReverseTransform)
388
+ .attr('fill', d => getColor(d.textColorType, fullChartParams))
389
+ .attr('font-size', fullChartParams.styles.textSize)
390
+ .attr('x', d => d.textX)
391
+ .attr('y', d => d.textY)
392
+ .each((d, i, n) => {
393
+ renderTspansOnAxis(d3.select(n[i]), {
394
+ textArr: datum.textArr,
395
+ textSizePx,
396
+ groupAxisPosition: i === 0
397
+ ? 'bottom' // x axis
398
+ : 'left', // y axis
399
+ isContainerRotated: false
400
+ })
401
+ })
402
+ })
403
+
404
+ return axisLabelSelection
405
+ }
406
+
407
+ function removeLabel (selection: d3.Selection<any, string, any, unknown>) {
408
+ const gUpdate = selection
409
+ .selectAll<SVGGElement, LabelDatum>(`g.${labelClassName}`)
410
+ .data([])
411
+
412
+ gUpdate.exit().remove()
413
+ }
414
+
415
+
416
+ export const XYAux = defineMultiValuePlugin(pluginConfig)(({ selection, rootSelection, name, subject, observer }) => {
417
+ const destroy$ = new Subject()
418
+
419
+ let isLabelMouseover: boolean = false
420
+
421
+ const rootRectSelection: d3.Selection<SVGRectElement, any, any, any> = rootSelection
422
+ .insert('rect', 'g')
423
+ .classed(getClassName(pluginName, 'rect'), true)
424
+ .attr('opacity', 0)
425
+
426
+ // const axisSelection: d3.Selection<SVGGElement, any, any, any> = selection
427
+ // .append('g')
428
+
429
+ const {
430
+ categorySelection$,
431
+ axesSelection$,
432
+ defsSelection$,
433
+ graphicGSelection$
434
+ } = multiValueSelectionsObservable({
435
+ selection,
436
+ pluginName,
437
+ clipPathID: 'test',
438
+ categoryLabels$: observer.isCategorySeprate$.pipe(
439
+ switchMap(isCategorySeprate => {
440
+ return iif(
441
+ () => isCategorySeprate,
442
+ observer.categoryLabels$,
443
+ // 如果沒分開的話只取一筆
444
+ observer.categoryLabels$.pipe(
445
+ map(d => [d[0]])
446
+ )
447
+ )
448
+ })
449
+ ),
450
+ containerPosition$: observer.containerPosition$,
451
+ graphicTransform$: observer.graphicTransform$
452
+ })
453
+
454
+ observer.layout$.pipe(
455
+ takeUntil(destroy$),
456
+ ).subscribe(d => {
457
+ rootRectSelection
458
+ .attr('width', d.rootWidth)
459
+ .attr('height', d.rootHeight)
460
+ })
461
+
462
+ // const highlightTarget$ = observer.fullChartParams$.pipe(
463
+ // takeUntil(destroy$),
464
+ // map(d => d.highlightTarget),
465
+ // distinctUntilChanged()
466
+ // )
467
+
468
+ // const rootMousemove$: Observable<any> = d3EventObservable(rootSelection, 'mousemove')
469
+ // .pipe(
470
+ // takeUntil(destroy$),
471
+ // debounceTime(10)
472
+ // )
473
+
474
+ // let r = 0
475
+ // rootMousemove$.subscribe(d => {
476
+ // r++
477
+ // console.log('r:', r)
478
+ // })
479
+
480
+ const columnAmount$ = observer.containerPosition$.pipe(
481
+ map(containerPosition => {
482
+ const maxColumnIndex = containerPosition.reduce((acc, current) => {
483
+ return current.columnIndex > acc ? current.columnIndex : acc
484
+ }, 0)
485
+ return maxColumnIndex + 1
486
+ }),
487
+ distinctUntilChanged()
488
+ )
489
+
490
+ const rowAmount$ = observer.containerPosition$.pipe(
491
+ map(containerPosition => {
492
+ const maxRowIndex = containerPosition.reduce((acc, current) => {
493
+ return current.rowIndex > acc ? current.rowIndex : acc
494
+ }, 0)
495
+ return maxRowIndex + 1
496
+ }),
497
+ distinctUntilChanged()
498
+ )
499
+
500
+ const textReverseTransform$ = observer.containerPosition$.pipe(
501
+ takeUntil(destroy$),
502
+ switchMap(async (d) => d),
503
+ map(containerPosition => {
504
+ // const axesRotateXYReverseValue = `rotateX(${data.gridAxesReverseTransform.rotateX}deg) rotateY(${data.gridAxesReverseTransform.rotateY}deg)`
505
+ // const axesRotateReverseValue = `rotate(${data.gridAxesReverseTransform.rotate}deg)`
506
+ const containerScaleReverseValue = `scale(${1 / containerPosition[0].scale[0]}, ${1 / containerPosition[0].scale[1]})`
507
+ // 抵消最外層scale
508
+ return `${containerScaleReverseValue}`
509
+ }),
510
+ distinctUntilChanged()
511
+ )
512
+
513
+ const xyPosition$ = multiValueXYPositionObservable({
514
+ rootSelection,
515
+ fullDataFormatter$: observer.fullDataFormatter$,
516
+ filteredXYMinMaxData$: observer.filteredXYMinMaxData$,
517
+ containerPosition$: observer.containerPosition$,
518
+ layout$: observer.layout$
519
+ }).pipe(
520
+ takeUntil(destroy$)
521
+ )
522
+
523
+ combineLatest({
524
+ axesSelection: axesSelection$,
525
+ // rootMousemove: rootMousemove$,
526
+ layout: observer.layout$,
527
+ xyPosition: xyPosition$,
528
+ // computedData: observer.computedData$,
529
+ fullParams: observer.fullParams$,
530
+ fullDataFormatter: observer.fullDataFormatter$,
531
+ fullChartParams: observer.fullChartParams$,
532
+ // highlightTarget: highlightTarget$,
533
+ textReverseTransform: textReverseTransform$,
534
+ // CategoryDataMap: observer.CategoryDataMap$,
535
+ textSizePx: observer.textSizePx$,
536
+ columnAmount: columnAmount$,
537
+ rowAmount: rowAmount$
538
+ }).pipe(
539
+ takeUntil(destroy$),
540
+ switchMap(async d => d),
541
+ ).subscribe(data => {
542
+ // 依event的座標取得group資料
543
+ const { x, y, xValue, yValue } = data.xyPosition
544
+
545
+ const lineData = createLineData({
546
+ axisX: x,
547
+ axisY: y,
548
+ layout: data.layout,
549
+ fullParams: data.fullParams,
550
+ })
551
+ renderLine({
552
+ selection: data.axesSelection,
553
+ pluginName: name,
554
+ lineData,
555
+ fullParams: data.fullParams,
556
+ fullChartParams: data.fullChartParams
557
+ })
558
+ const labelData = createLabelData({
559
+ axisX: x,
560
+ axisY: y,
561
+ xValue,
562
+ yValue,
563
+ fullParams: data.fullParams,
564
+ textSizePx: data.textSizePx,
565
+ layout: data.layout,
566
+ columnAmount: data.columnAmount,
567
+ rowAmount: data.rowAmount
568
+ })
569
+ const labelSelection = renderLabel({
570
+ selection: data.axesSelection,
571
+ labelData,
572
+ fullParams: data.fullParams,
573
+ fullDataFormatter: data.fullDataFormatter,
574
+ fullChartParams: data.fullChartParams,
575
+ textReverseTransform: data.textReverseTransform,
576
+ textSizePx: data.textSizePx
577
+ })
578
+
579
+ // label的事件
580
+ // labelSelection
581
+ // .on('mouseover', (event, datum) => {
582
+ // event.stopPropagation()
583
+ // // const { groupIndex, groupLabel } = data.xyPositionFn(event)
584
+
585
+ // isLabelMouseover = true
586
+
587
+ // subject.event$.next({
588
+ // type: 'multiValue',
589
+ // eventName: 'mouseover',
590
+ // pluginName,
591
+ // highlightTarget: data.highlightTarget,
592
+ // datum,
593
+ // category: [],
594
+ // categoryIndex: -1,
595
+ // categoryLabel: '',
596
+ // data: data.computedData,
597
+ // event,
598
+ // })
599
+ // })
600
+ // .on('mousemove', (event, datum) => {
601
+ // event.stopPropagation()
602
+ // // const { groupIndex, groupLabel } = data.xyPositionFn(event)
603
+
604
+ // subject.event$.next({
605
+ // type: 'multiValue',
606
+ // eventName: 'mousemove',
607
+ // pluginName,
608
+ // highlightTarget: data.highlightTarget,
609
+ // datum,
610
+ // category: [],
611
+ // categoryIndex: -1,
612
+ // categoryLabel: '',
613
+ // data: data.computedData,
614
+ // event,
615
+ // })
616
+ // })
617
+ // .on('mouseout', (event, datum) => {
618
+ // event.stopPropagation()
619
+ // // const { groupIndex, groupLabel } = data.xyPositionFn(event)
620
+
621
+ // isLabelMouseover = false
622
+
623
+ // subject.event$.next({
624
+ // type: 'multiValue',
625
+ // eventName: 'mouseout',
626
+ // pluginName,
627
+ // highlightTarget: data.highlightTarget,
628
+ // datum,
629
+ // category: [],
630
+ // categoryIndex: -1,
631
+ // categoryLabel: '',
632
+ // data: data.computedData,
633
+ // event,
634
+ // })
635
+ // })
636
+ // .on('click', (event, datum) => {
637
+ // event.stopPropagation()
638
+ // // const { groupIndex, groupLabel } = data.xyPositionFn(event)
639
+
640
+ // subject.event$.next({
641
+ // type: 'multiValue',
642
+ // eventName: 'click',
643
+ // pluginName,
644
+ // highlightTarget: data.highlightTarget,
645
+ // datum,
646
+ // category: [],
647
+ // categoryIndex: -1,
648
+ // categoryLabel: '',
649
+ // data: data.computedData,
650
+ // event,
651
+ // })
652
+ // })
653
+
654
+ })
655
+
656
+
657
+ const rootRectMouseout$ = d3EventObservable(rootRectSelection, 'mouseout').pipe(
658
+ takeUntil(destroy$),
659
+ )
660
+
661
+ combineLatest({
662
+ rootRectMouseout: rootRectMouseout$,
663
+ axesSelection: axesSelection$,
664
+ }).pipe(
665
+ takeUntil(destroy$),
666
+ switchMap(async d => d)
667
+ ).subscribe(data => {
668
+ setTimeout(() => {
669
+ // // @Q@ workaround - 不知為何和 label 會有衝突,當滑鼠移動到 label 上時,會觸發 mouseout 事件
670
+ // if (isLabelMouseover == true) {
671
+ // return
672
+ // }
673
+
674
+ removeLine(data.axesSelection)
675
+ removeLabel(data.axesSelection)
676
+ })
677
+ })
678
+
679
+ return () => {
680
+ destroy$.next(undefined)
681
+ rootRectSelection.remove()
682
+ }
683
+ })