@orbcharts/plugins-basic 3.0.0-beta.4 → 3.0.0-beta.5

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 (192) hide show
  1. package/LICENSE +200 -200
  2. package/dist/{src → orbcharts-plugins-basic/src}/base/BaseBarStack.d.ts +2 -2
  3. package/dist/{src → orbcharts-plugins-basic/src}/base/BaseBars.d.ts +2 -2
  4. package/dist/{src → orbcharts-plugins-basic/src}/base/BaseBarsTriangle.d.ts +2 -2
  5. package/dist/{src → orbcharts-plugins-basic/src}/base/BaseDots.d.ts +2 -2
  6. package/dist/{src → orbcharts-plugins-basic/src}/base/BaseGroupAxis.d.ts +2 -2
  7. package/dist/{src → orbcharts-plugins-basic/src}/base/BaseLegend.d.ts +1 -1
  8. package/dist/{src → orbcharts-plugins-basic/src}/base/BaseLineAreas.d.ts +3 -3
  9. package/dist/{src → orbcharts-plugins-basic/src}/base/BaseLines.d.ts +3 -3
  10. package/dist/{src → orbcharts-plugins-basic/src}/base/BaseValueAxis.d.ts +3 -2
  11. package/dist/{src → orbcharts-plugins-basic/src}/grid/defaults.d.ts +1 -1
  12. package/dist/{src → orbcharts-plugins-basic/src}/grid/gridObservables.d.ts +5 -5
  13. package/dist/orbcharts-plugins-basic/src/index.d.ts +6 -0
  14. package/dist/orbcharts-plugins-basic/src/multiValue/defaults.d.ts +9 -0
  15. package/dist/orbcharts-plugins-basic/src/multiValue/index.d.ts +8 -0
  16. package/dist/orbcharts-plugins-basic/src/multiValue/multiValueObservables.d.ts +33 -0
  17. package/dist/orbcharts-plugins-basic/src/multiValue/plugins/MultiValueLegend.d.ts +1 -0
  18. package/dist/orbcharts-plugins-basic/src/multiValue/plugins/MultiValueTooltip.d.ts +1 -0
  19. package/dist/orbcharts-plugins-basic/src/multiValue/plugins/Scatter.d.ts +3 -0
  20. package/dist/orbcharts-plugins-basic/src/multiValue/plugins/ScatterBubbles.d.ts +3 -0
  21. package/dist/orbcharts-plugins-basic/src/multiValue/plugins/XYAux.d.ts +3 -0
  22. package/dist/orbcharts-plugins-basic/src/multiValue/plugins/XYAxes.d.ts +3 -0
  23. package/dist/orbcharts-plugins-basic/src/multiValue/plugins/XYZoom.d.ts +1 -0
  24. package/dist/{src → orbcharts-plugins-basic/src}/series/seriesObservables.d.ts +3 -3
  25. package/dist/{src → orbcharts-plugins-basic/src}/utils/observables.d.ts +1 -1
  26. package/dist/{src → orbcharts-plugins-basic/src}/utils/orbchartsUtils.d.ts +3 -3
  27. package/dist/orbcharts-plugins-basic.es.js +11809 -10185
  28. package/dist/orbcharts-plugins-basic.umd.js +66 -34
  29. package/dist/src/index.d.ts +1 -5
  30. package/lib/core-types.ts +7 -7
  31. package/lib/core.ts +6 -6
  32. package/lib/plugins-basic-types.ts +6 -6
  33. package/package.json +44 -44
  34. package/src/base/BaseBarStack.ts +780 -780
  35. package/src/base/BaseBars.ts +765 -765
  36. package/src/base/BaseBarsTriangle.ts +674 -674
  37. package/src/base/BaseDots.ts +464 -515
  38. package/src/base/BaseGroupAxis.ts +679 -679
  39. package/src/base/BaseLegend.ts +684 -680
  40. package/src/base/BaseLineAreas.ts +629 -629
  41. package/src/base/BaseLines.ts +706 -706
  42. package/src/base/BaseTooltip.ts +385 -385
  43. package/src/base/BaseValueAxis.ts +583 -580
  44. package/src/base/types.ts +2 -2
  45. package/src/const.ts +30 -30
  46. package/src/grid/defaults.ts +244 -244
  47. package/src/grid/gridObservables.ts +554 -545
  48. package/src/grid/index.ts +16 -16
  49. package/src/grid/plugins/BarStack.ts +64 -64
  50. package/src/grid/plugins/Bars.ts +69 -69
  51. package/src/grid/plugins/BarsPN.ts +66 -66
  52. package/src/grid/plugins/BarsTriangle.ts +73 -73
  53. package/src/grid/plugins/Dots.ts +68 -68
  54. package/src/grid/plugins/GridLegend.ts +107 -107
  55. package/src/grid/plugins/GridTooltip.ts +66 -66
  56. package/src/grid/plugins/GridZoom.ts +218 -218
  57. package/src/grid/plugins/GroupAux.ts +1103 -1098
  58. package/src/grid/plugins/GroupAxis.ts +97 -97
  59. package/src/grid/plugins/LineAreas.ts +65 -65
  60. package/src/grid/plugins/Lines.ts +59 -59
  61. package/src/grid/plugins/ValueAxis.ts +94 -93
  62. package/src/grid/plugins/ValueStackAxis.ts +96 -95
  63. package/src/index.ts +10 -9
  64. package/src/multiGrid/defaults.ts +224 -224
  65. package/src/multiGrid/index.ts +14 -14
  66. package/src/multiGrid/multiGridObservables.ts +49 -49
  67. package/src/multiGrid/plugins/MultiBarStack.ts +106 -106
  68. package/src/multiGrid/plugins/MultiBars.ts +108 -108
  69. package/src/multiGrid/plugins/MultiBarsTriangle.ts +114 -114
  70. package/src/multiGrid/plugins/MultiDots.ts +102 -102
  71. package/src/multiGrid/plugins/MultiGridLegend.ts +159 -159
  72. package/src/multiGrid/plugins/MultiGridTooltip.ts +66 -66
  73. package/src/multiGrid/plugins/MultiGroupAxis.ts +137 -137
  74. package/src/multiGrid/plugins/MultiLineAreas.ts +107 -107
  75. package/src/multiGrid/plugins/MultiLines.ts +101 -101
  76. package/src/multiGrid/plugins/MultiValueAxis.ts +134 -133
  77. package/src/multiGrid/plugins/MultiValueStackAxis.ts +134 -133
  78. package/src/multiGrid/plugins/OverlappingValueAxes.ts +300 -299
  79. package/src/multiGrid/plugins/OverlappingValueStackAxes.ts +299 -298
  80. package/src/multiValue/defaults.ts +163 -0
  81. package/src/multiValue/index.ts +9 -0
  82. package/src/multiValue/multiValueObservables.ts +258 -0
  83. package/src/multiValue/plugins/MultiValueLegend.ts +107 -0
  84. package/src/multiValue/plugins/MultiValueTooltip.ts +66 -0
  85. package/src/multiValue/plugins/Scatter.ts +426 -0
  86. package/src/multiValue/plugins/ScatterBubbles.ts +551 -0
  87. package/src/multiValue/plugins/XYAux.ts +682 -0
  88. package/src/multiValue/plugins/XYAxes.ts +677 -0
  89. package/src/multiValue/plugins/XYZoom.ts +300 -0
  90. package/src/noneData/defaults.ts +102 -102
  91. package/src/noneData/index.ts +3 -3
  92. package/src/noneData/plugins/Container.ts +27 -27
  93. package/src/noneData/plugins/Tooltip.ts +373 -373
  94. package/src/series/defaults.ts +206 -206
  95. package/src/series/index.ts +9 -9
  96. package/src/series/plugins/Bubbles.ts +603 -603
  97. package/src/series/plugins/Pie.ts +623 -623
  98. package/src/series/plugins/PieEventTexts.ts +283 -283
  99. package/src/series/plugins/PieLabels.ts +640 -640
  100. package/src/series/plugins/Rose.ts +516 -516
  101. package/src/series/plugins/RoseLabels.ts +600 -600
  102. package/src/series/plugins/SeriesLegend.ts +107 -107
  103. package/src/series/plugins/SeriesTooltip.ts +66 -66
  104. package/src/series/seriesObservables.ts +145 -145
  105. package/src/series/seriesUtils.ts +51 -51
  106. package/src/tree/defaults.ts +78 -78
  107. package/src/tree/index.ts +4 -4
  108. package/src/tree/plugins/TreeLegend.ts +100 -107
  109. package/src/tree/plugins/TreeMap.ts +333 -333
  110. package/src/tree/plugins/TreeTooltip.ts +66 -66
  111. package/src/utils/commonUtils.ts +21 -21
  112. package/src/utils/d3Graphics.ts +174 -174
  113. package/src/utils/d3Utils.ts +74 -73
  114. package/src/utils/observables.ts +14 -14
  115. package/src/utils/orbchartsUtils.ts +101 -100
  116. package/tsconfig.base.json +13 -13
  117. package/tsconfig.json +2 -2
  118. package/vite.config.js +22 -22
  119. package/dist/src/base/BaseGroupArea.d.ts +0 -0
  120. package/dist/src/multiValue/plugins/Scatter.d.ts +0 -0
  121. package/dist/src/multiValue/plugins/ScatterAxes.d.ts +0 -0
  122. package/dist/src/relationship/index.d.ts +0 -0
  123. package/src/base/BaseGroupArea.ts +0 -0
  124. package/src/multiValue/plugins/ScatterAxes.ts +0 -0
  125. /package/dist/{lib → orbcharts-plugins-basic/lib}/core-types.d.ts +0 -0
  126. /package/dist/{lib → orbcharts-plugins-basic/lib}/core.d.ts +0 -0
  127. /package/dist/{lib → orbcharts-plugins-basic/lib}/plugins-basic-types.d.ts +0 -0
  128. /package/dist/{src → orbcharts-plugins-basic/src}/base/BaseTooltip.d.ts +0 -0
  129. /package/dist/{src → orbcharts-plugins-basic/src}/base/types.d.ts +0 -0
  130. /package/dist/{src → orbcharts-plugins-basic/src}/const.d.ts +0 -0
  131. /package/dist/{src → orbcharts-plugins-basic/src}/grid/index.d.ts +0 -0
  132. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/BarStack.d.ts +0 -0
  133. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/Bars.d.ts +0 -0
  134. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/BarsPN.d.ts +0 -0
  135. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/BarsTriangle.d.ts +0 -0
  136. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/Dots.d.ts +0 -0
  137. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/GridLegend.d.ts +0 -0
  138. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/GridTooltip.d.ts +0 -0
  139. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/GridZoom.d.ts +0 -0
  140. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/GroupAux.d.ts +0 -0
  141. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/GroupAxis.d.ts +0 -0
  142. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/LineAreas.d.ts +0 -0
  143. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/Lines.d.ts +0 -0
  144. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/ValueAxis.d.ts +0 -0
  145. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/ValueStackAxis.d.ts +0 -0
  146. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/defaults.d.ts +0 -0
  147. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/index.d.ts +0 -0
  148. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/multiGridObservables.d.ts +0 -0
  149. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/plugins/MultiBarStack.d.ts +0 -0
  150. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/plugins/MultiBars.d.ts +0 -0
  151. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/plugins/MultiBarsTriangle.d.ts +0 -0
  152. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/plugins/MultiDots.d.ts +0 -0
  153. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/plugins/MultiGridLegend.d.ts +0 -0
  154. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/plugins/MultiGridTooltip.d.ts +0 -0
  155. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/plugins/MultiGroupAxis.d.ts +0 -0
  156. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/plugins/MultiLineAreas.d.ts +0 -0
  157. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/plugins/MultiLines.d.ts +0 -0
  158. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/plugins/MultiValueAxis.d.ts +0 -0
  159. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/plugins/MultiValueStackAxis.d.ts +0 -0
  160. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/plugins/OverlappingValueAxes.d.ts +0 -0
  161. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/plugins/OverlappingValueStackAxes.d.ts +0 -0
  162. /package/dist/{src/grid → orbcharts-plugins-basic/src/multiValue}/plugins/Ranking.d.ts +0 -0
  163. /package/dist/{src/grid → orbcharts-plugins-basic/src/multiValue}/plugins/RankingAxis.d.ts +0 -0
  164. /package/dist/{src → orbcharts-plugins-basic/src}/noneData/defaults.d.ts +0 -0
  165. /package/dist/{src → orbcharts-plugins-basic/src}/noneData/index.d.ts +0 -0
  166. /package/dist/{src → orbcharts-plugins-basic/src}/noneData/plugins/Container.d.ts +0 -0
  167. /package/dist/{src → orbcharts-plugins-basic/src}/noneData/plugins/Tooltip.d.ts +0 -0
  168. /package/dist/{src/multiValue → orbcharts-plugins-basic/src/relationship}/index.d.ts +0 -0
  169. /package/dist/{src → orbcharts-plugins-basic/src}/relationship/plugins/Relationship.d.ts +0 -0
  170. /package/dist/{src → orbcharts-plugins-basic/src}/series/defaults.d.ts +0 -0
  171. /package/dist/{src → orbcharts-plugins-basic/src}/series/index.d.ts +0 -0
  172. /package/dist/{src → orbcharts-plugins-basic/src}/series/plugins/Bubbles.d.ts +0 -0
  173. /package/dist/{src → orbcharts-plugins-basic/src}/series/plugins/Pie.d.ts +0 -0
  174. /package/dist/{src → orbcharts-plugins-basic/src}/series/plugins/PieEventTexts.d.ts +0 -0
  175. /package/dist/{src → orbcharts-plugins-basic/src}/series/plugins/PieLabels.d.ts +0 -0
  176. /package/dist/{src → orbcharts-plugins-basic/src}/series/plugins/Rose.d.ts +0 -0
  177. /package/dist/{src → orbcharts-plugins-basic/src}/series/plugins/RoseLabels.d.ts +0 -0
  178. /package/dist/{src → orbcharts-plugins-basic/src}/series/plugins/SeriesLegend.d.ts +0 -0
  179. /package/dist/{src → orbcharts-plugins-basic/src}/series/plugins/SeriesTooltip.d.ts +0 -0
  180. /package/dist/{src → orbcharts-plugins-basic/src}/series/plugins/Waffle.d.ts +0 -0
  181. /package/dist/{src → orbcharts-plugins-basic/src}/series/seriesUtils.d.ts +0 -0
  182. /package/dist/{src → orbcharts-plugins-basic/src}/tree/defaults.d.ts +0 -0
  183. /package/dist/{src → orbcharts-plugins-basic/src}/tree/index.d.ts +0 -0
  184. /package/dist/{src → orbcharts-plugins-basic/src}/tree/plugins/TreeLegend.d.ts +0 -0
  185. /package/dist/{src → orbcharts-plugins-basic/src}/tree/plugins/TreeMap.d.ts +0 -0
  186. /package/dist/{src → orbcharts-plugins-basic/src}/tree/plugins/TreeTooltip.d.ts +0 -0
  187. /package/dist/{src → orbcharts-plugins-basic/src}/utils/commonUtils.d.ts +0 -0
  188. /package/dist/{src → orbcharts-plugins-basic/src}/utils/d3Graphics.d.ts +0 -0
  189. /package/dist/{src → orbcharts-plugins-basic/src}/utils/d3Utils.d.ts +0 -0
  190. /package/dist/{vite.config.d.ts → orbcharts-plugins-basic/vite.config.d.ts} +0 -0
  191. /package/src/{grid → multiValue}/plugins/Ranking.ts +0 -0
  192. /package/src/{grid → multiValue}/plugins/RankingAxis.ts +0 -0
@@ -0,0 +1,682 @@
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
+ })
400
+ })
401
+ })
402
+
403
+ return axisLabelSelection
404
+ }
405
+
406
+ function removeLabel (selection: d3.Selection<any, string, any, unknown>) {
407
+ const gUpdate = selection
408
+ .selectAll<SVGGElement, LabelDatum>(`g.${labelClassName}`)
409
+ .data([])
410
+
411
+ gUpdate.exit().remove()
412
+ }
413
+
414
+
415
+ export const XYAux = defineMultiValuePlugin(pluginConfig)(({ selection, rootSelection, name, subject, observer }) => {
416
+ const destroy$ = new Subject()
417
+
418
+ let isLabelMouseover: boolean = false
419
+
420
+ const rootRectSelection: d3.Selection<SVGRectElement, any, any, any> = rootSelection
421
+ .insert('rect', 'g')
422
+ .classed(getClassName(pluginName, 'rect'), true)
423
+ .attr('opacity', 0)
424
+
425
+ // const axisSelection: d3.Selection<SVGGElement, any, any, any> = selection
426
+ // .append('g')
427
+
428
+ const {
429
+ categorySelection$,
430
+ axesSelection$,
431
+ defsSelection$,
432
+ graphicGSelection$
433
+ } = multiValueSelectionsObservable({
434
+ selection,
435
+ pluginName,
436
+ clipPathID: 'test',
437
+ categoryLabels$: observer.isCategorySeprate$.pipe(
438
+ switchMap(isCategorySeprate => {
439
+ return iif(
440
+ () => isCategorySeprate,
441
+ observer.categoryLabels$,
442
+ // 如果沒分開的話只取一筆
443
+ observer.categoryLabels$.pipe(
444
+ map(d => [d[0]])
445
+ )
446
+ )
447
+ })
448
+ ),
449
+ multiValueContainerPosition$: observer.multiValueContainerPosition$,
450
+ multiValueGraphicTransform$: observer.multiValueGraphicTransform$
451
+ })
452
+
453
+ observer.layout$.pipe(
454
+ takeUntil(destroy$),
455
+ ).subscribe(d => {
456
+ rootRectSelection
457
+ .attr('width', d.rootWidth)
458
+ .attr('height', d.rootHeight)
459
+ })
460
+
461
+ // const highlightTarget$ = observer.fullChartParams$.pipe(
462
+ // takeUntil(destroy$),
463
+ // map(d => d.highlightTarget),
464
+ // distinctUntilChanged()
465
+ // )
466
+
467
+ // const rootMousemove$: Observable<any> = d3EventObservable(rootSelection, 'mousemove')
468
+ // .pipe(
469
+ // takeUntil(destroy$),
470
+ // debounceTime(10)
471
+ // )
472
+
473
+ // let r = 0
474
+ // rootMousemove$.subscribe(d => {
475
+ // r++
476
+ // console.log('r:', r)
477
+ // })
478
+
479
+ const columnAmount$ = observer.multiValueContainerPosition$.pipe(
480
+ map(multiValueContainerPosition => {
481
+ const maxColumnIndex = multiValueContainerPosition.reduce((acc, current) => {
482
+ return current.columnIndex > acc ? current.columnIndex : acc
483
+ }, 0)
484
+ return maxColumnIndex + 1
485
+ }),
486
+ distinctUntilChanged()
487
+ )
488
+
489
+ const rowAmount$ = observer.multiValueContainerPosition$.pipe(
490
+ map(multiValueContainerPosition => {
491
+ const maxRowIndex = multiValueContainerPosition.reduce((acc, current) => {
492
+ return current.rowIndex > acc ? current.rowIndex : acc
493
+ }, 0)
494
+ return maxRowIndex + 1
495
+ }),
496
+ distinctUntilChanged()
497
+ )
498
+
499
+ const textReverseTransform$ = observer.multiValueContainerPosition$.pipe(
500
+ takeUntil(destroy$),
501
+ switchMap(async (d) => d),
502
+ map(multiValueContainerPosition => {
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 / multiValueContainerPosition[0].scale[0]}, ${1 / multiValueContainerPosition[0].scale[1]})`
506
+ // 抵消最外層scale
507
+ return `${containerScaleReverseValue}`
508
+ }),
509
+ distinctUntilChanged()
510
+ )
511
+
512
+ const xyPosition$ = multiValueXYPositionObservable({
513
+ rootSelection,
514
+ fullDataFormatter$: observer.fullDataFormatter$,
515
+ filteredMinMaxXYData$: observer.filteredMinMaxXYData$,
516
+ multiValueContainerPosition$: observer.multiValueContainerPosition$,
517
+ layout$: observer.layout$
518
+ }).pipe(
519
+ takeUntil(destroy$)
520
+ )
521
+
522
+ combineLatest({
523
+ axesSelection: axesSelection$,
524
+ // rootMousemove: rootMousemove$,
525
+ layout: observer.layout$,
526
+ xyPosition: xyPosition$,
527
+ // computedData: observer.computedData$,
528
+ fullParams: observer.fullParams$,
529
+ fullDataFormatter: observer.fullDataFormatter$,
530
+ fullChartParams: observer.fullChartParams$,
531
+ // highlightTarget: highlightTarget$,
532
+ textReverseTransform: textReverseTransform$,
533
+ // CategoryDataMap: observer.CategoryDataMap$,
534
+ textSizePx: observer.textSizePx$,
535
+ columnAmount: columnAmount$,
536
+ rowAmount: rowAmount$
537
+ }).pipe(
538
+ takeUntil(destroy$),
539
+ switchMap(async d => d),
540
+ ).subscribe(data => {
541
+ // 依event的座標取得group資料
542
+ const { x, y, xValue, yValue } = data.xyPosition
543
+
544
+ const lineData = createLineData({
545
+ axisX: x,
546
+ axisY: y,
547
+ layout: data.layout,
548
+ fullParams: data.fullParams,
549
+ })
550
+ renderLine({
551
+ selection: data.axesSelection,
552
+ pluginName: name,
553
+ lineData,
554
+ fullParams: data.fullParams,
555
+ fullChartParams: data.fullChartParams
556
+ })
557
+ const labelData = createLabelData({
558
+ axisX: x,
559
+ axisY: y,
560
+ xValue,
561
+ yValue,
562
+ fullParams: data.fullParams,
563
+ textSizePx: data.textSizePx,
564
+ layout: data.layout,
565
+ columnAmount: data.columnAmount,
566
+ rowAmount: data.rowAmount
567
+ })
568
+ const labelSelection = renderLabel({
569
+ selection: data.axesSelection,
570
+ labelData,
571
+ fullParams: data.fullParams,
572
+ fullDataFormatter: data.fullDataFormatter,
573
+ fullChartParams: data.fullChartParams,
574
+ textReverseTransform: data.textReverseTransform,
575
+ textSizePx: data.textSizePx
576
+ })
577
+
578
+ // label的事件
579
+ // labelSelection
580
+ // .on('mouseover', (event, datum) => {
581
+ // event.stopPropagation()
582
+ // // const { groupIndex, groupLabel } = data.xyPositionFn(event)
583
+
584
+ // isLabelMouseover = true
585
+
586
+ // subject.event$.next({
587
+ // type: 'multiValue',
588
+ // eventName: 'mouseover',
589
+ // pluginName,
590
+ // highlightTarget: data.highlightTarget,
591
+ // datum,
592
+ // category: [],
593
+ // categoryIndex: -1,
594
+ // categoryLabel: '',
595
+ // data: data.computedData,
596
+ // event,
597
+ // })
598
+ // })
599
+ // .on('mousemove', (event, datum) => {
600
+ // event.stopPropagation()
601
+ // // const { groupIndex, groupLabel } = data.xyPositionFn(event)
602
+
603
+ // subject.event$.next({
604
+ // type: 'multiValue',
605
+ // eventName: 'mousemove',
606
+ // pluginName,
607
+ // highlightTarget: data.highlightTarget,
608
+ // datum,
609
+ // category: [],
610
+ // categoryIndex: -1,
611
+ // categoryLabel: '',
612
+ // data: data.computedData,
613
+ // event,
614
+ // })
615
+ // })
616
+ // .on('mouseout', (event, datum) => {
617
+ // event.stopPropagation()
618
+ // // const { groupIndex, groupLabel } = data.xyPositionFn(event)
619
+
620
+ // isLabelMouseover = false
621
+
622
+ // subject.event$.next({
623
+ // type: 'multiValue',
624
+ // eventName: 'mouseout',
625
+ // pluginName,
626
+ // highlightTarget: data.highlightTarget,
627
+ // datum,
628
+ // category: [],
629
+ // categoryIndex: -1,
630
+ // categoryLabel: '',
631
+ // data: data.computedData,
632
+ // event,
633
+ // })
634
+ // })
635
+ // .on('click', (event, datum) => {
636
+ // event.stopPropagation()
637
+ // // const { groupIndex, groupLabel } = data.xyPositionFn(event)
638
+
639
+ // subject.event$.next({
640
+ // type: 'multiValue',
641
+ // eventName: 'click',
642
+ // pluginName,
643
+ // highlightTarget: data.highlightTarget,
644
+ // datum,
645
+ // category: [],
646
+ // categoryIndex: -1,
647
+ // categoryLabel: '',
648
+ // data: data.computedData,
649
+ // event,
650
+ // })
651
+ // })
652
+
653
+ })
654
+
655
+
656
+ const rootRectMouseout$ = d3EventObservable(rootRectSelection, 'mouseout').pipe(
657
+ takeUntil(destroy$),
658
+ )
659
+
660
+ combineLatest({
661
+ rootRectMouseout: rootRectMouseout$,
662
+ axesSelection: axesSelection$,
663
+ }).pipe(
664
+ takeUntil(destroy$),
665
+ switchMap(async d => d)
666
+ ).subscribe(data => {
667
+ setTimeout(() => {
668
+ // // @Q@ workaround - 不知為何和 label 會有衝突,當滑鼠移動到 label 上時,會觸發 mouseout 事件
669
+ // if (isLabelMouseover == true) {
670
+ // return
671
+ // }
672
+
673
+ removeLine(data.axesSelection)
674
+ removeLabel(data.axesSelection)
675
+ })
676
+ })
677
+
678
+ return () => {
679
+ destroy$.next(undefined)
680
+ rootRectSelection.remove()
681
+ }
682
+ })