@orbcharts/plugins-basic 3.0.0-alpha.75 → 3.0.0-alpha.77

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 (143) hide show
  1. package/LICENSE +200 -200
  2. package/dist/lib/plugins-basic-types.d.ts +1 -0
  3. package/dist/orbcharts-plugins-basic.es.js +3461 -3468
  4. package/dist/orbcharts-plugins-basic.umd.js +17 -17
  5. package/dist/src/base/BaseBarStack.d.ts +1 -5
  6. package/dist/src/base/BaseBars.d.ts +1 -6
  7. package/dist/src/base/BaseBarsTriangle.d.ts +1 -6
  8. package/dist/src/base/BaseDots.d.ts +2 -8
  9. package/dist/src/base/BaseGroupAxis.d.ts +2 -16
  10. package/dist/src/base/BaseLegend.d.ts +2 -15
  11. package/dist/src/base/BaseLineAreas.d.ts +1 -4
  12. package/dist/src/base/BaseLines.d.ts +1 -4
  13. package/dist/src/base/BaseValueAxis.d.ts +2 -16
  14. package/dist/src/grid/defaults.d.ts +1 -1
  15. package/dist/src/grid/index.d.ts +0 -1
  16. package/dist/src/grid/plugins/BarStack.d.ts +1 -1
  17. package/dist/src/grid/plugins/Bars.d.ts +1 -1
  18. package/dist/src/grid/plugins/BarsPN.d.ts +1 -1
  19. package/dist/src/grid/plugins/BarsTriangle.d.ts +1 -1
  20. package/dist/src/grid/plugins/Dots.d.ts +1 -1
  21. package/dist/src/grid/plugins/GridLegend.d.ts +1 -1
  22. package/dist/src/grid/plugins/GroupAux.d.ts +1 -1
  23. package/dist/src/grid/plugins/GroupAxis.d.ts +1 -1
  24. package/dist/src/grid/plugins/LineAreas.d.ts +1 -1
  25. package/dist/src/grid/plugins/Lines.d.ts +1 -1
  26. package/dist/src/grid/plugins/ScalingArea.d.ts +1 -1
  27. package/dist/src/grid/plugins/ValueAxis.d.ts +1 -1
  28. package/dist/src/grid/plugins/ValueStackAxis.d.ts +1 -1
  29. package/dist/src/multiGrid/defaults.d.ts +1 -1
  30. package/dist/src/multiGrid/index.d.ts +0 -1
  31. package/dist/src/multiGrid/plugins/MultiBarStack.d.ts +1 -1
  32. package/dist/src/multiGrid/plugins/MultiBars.d.ts +1 -1
  33. package/dist/src/multiGrid/plugins/MultiBarsTriangle.d.ts +1 -1
  34. package/dist/src/multiGrid/plugins/MultiDots.d.ts +1 -1
  35. package/dist/src/multiGrid/plugins/MultiGridLegend.d.ts +1 -1
  36. package/dist/src/multiGrid/plugins/MultiGroupAxis.d.ts +1 -1
  37. package/dist/src/multiGrid/plugins/MultiLineAreas.d.ts +1 -1
  38. package/dist/src/multiGrid/plugins/MultiLines.d.ts +1 -1
  39. package/dist/src/multiGrid/plugins/MultiValueAxis.d.ts +1 -1
  40. package/dist/src/multiGrid/plugins/MultiValueStackAxis.d.ts +1 -1
  41. package/dist/src/multiGrid/plugins/OverlappingValueAxes.d.ts +1 -1
  42. package/dist/src/multiGrid/plugins/OverlappingValueStackAxes.d.ts +1 -1
  43. package/dist/src/noneData/defaults.d.ts +1 -1
  44. package/dist/src/noneData/index.d.ts +0 -1
  45. package/dist/src/noneData/plugins/Container.d.ts +1 -1
  46. package/dist/src/noneData/plugins/Tooltip.d.ts +1 -1
  47. package/dist/src/series/defaults.d.ts +1 -1
  48. package/dist/src/series/index.d.ts +0 -1
  49. package/dist/src/series/plugins/Bubbles.d.ts +1 -1
  50. package/dist/src/series/plugins/Pie.d.ts +1 -1
  51. package/dist/src/series/plugins/PieEventTexts.d.ts +1 -1
  52. package/dist/src/series/plugins/PieLabels.d.ts +1 -1
  53. package/dist/src/series/plugins/Rose.d.ts +1 -1
  54. package/dist/src/series/plugins/RoseLabels.d.ts +1 -1
  55. package/dist/src/series/plugins/SeriesLegend.d.ts +1 -1
  56. package/dist/src/tree/defaults.d.ts +1 -1
  57. package/dist/src/tree/index.d.ts +0 -1
  58. package/dist/src/tree/plugins/TreeLegend.d.ts +1 -1
  59. package/dist/src/tree/plugins/TreeMap.d.ts +1 -1
  60. package/lib/core-types.ts +7 -7
  61. package/lib/core.ts +6 -6
  62. package/lib/plugins-basic-types.ts +6 -0
  63. package/package.json +44 -43
  64. package/src/base/BaseBarStack.ts +780 -779
  65. package/src/base/BaseBars.ts +765 -764
  66. package/src/base/BaseBarsTriangle.ts +674 -672
  67. package/src/base/BaseDots.ts +515 -513
  68. package/src/base/BaseGroupAxis.ts +679 -678
  69. package/src/base/BaseLegend.ts +644 -642
  70. package/src/base/BaseLineAreas.ts +629 -627
  71. package/src/base/BaseLines.ts +706 -704
  72. package/src/base/BaseValueAxis.ts +580 -578
  73. package/src/base/types.ts +2 -2
  74. package/src/const.ts +30 -30
  75. package/src/grid/defaults.ts +128 -128
  76. package/src/grid/gridObservables.ts +545 -545
  77. package/src/grid/index.ts +15 -15
  78. package/src/grid/plugins/BarStack.ts +64 -64
  79. package/src/grid/plugins/Bars.ts +69 -69
  80. package/src/grid/plugins/BarsPN.ts +66 -66
  81. package/src/grid/plugins/BarsTriangle.ts +73 -73
  82. package/src/grid/plugins/Dots.ts +68 -68
  83. package/src/grid/plugins/GridLegend.ts +96 -96
  84. package/src/grid/plugins/GroupAux.ts +1098 -1098
  85. package/src/grid/plugins/GroupAxis.ts +97 -97
  86. package/src/grid/plugins/LineAreas.ts +65 -65
  87. package/src/grid/plugins/Lines.ts +59 -59
  88. package/src/grid/plugins/ScalingArea.ts +218 -218
  89. package/src/grid/plugins/ValueAxis.ts +93 -93
  90. package/src/grid/plugins/ValueStackAxis.ts +95 -95
  91. package/src/index.ts +9 -9
  92. package/src/multiGrid/defaults.ts +158 -158
  93. package/src/multiGrid/index.ts +13 -13
  94. package/src/multiGrid/multiGridObservables.ts +49 -49
  95. package/src/multiGrid/plugins/MultiBarStack.ts +106 -106
  96. package/src/multiGrid/plugins/MultiBars.ts +108 -108
  97. package/src/multiGrid/plugins/MultiBarsTriangle.ts +114 -114
  98. package/src/multiGrid/plugins/MultiDots.ts +102 -102
  99. package/src/multiGrid/plugins/MultiGridLegend.ts +148 -148
  100. package/src/multiGrid/plugins/MultiGroupAxis.ts +137 -137
  101. package/src/multiGrid/plugins/MultiLineAreas.ts +107 -107
  102. package/src/multiGrid/plugins/MultiLines.ts +101 -101
  103. package/src/multiGrid/plugins/MultiValueAxis.ts +133 -133
  104. package/src/multiGrid/plugins/MultiValueStackAxis.ts +133 -133
  105. package/src/multiGrid/plugins/OverlappingValueAxes.ts +299 -299
  106. package/src/multiGrid/plugins/OverlappingValueStackAxes.ts +298 -298
  107. package/src/noneData/defaults.ts +102 -102
  108. package/src/noneData/index.ts +3 -3
  109. package/src/noneData/plugins/Container.ts +27 -27
  110. package/src/noneData/plugins/Tooltip.ts +373 -373
  111. package/src/series/defaults.ts +149 -149
  112. package/src/series/index.ts +9 -9
  113. package/src/series/plugins/Bubbles.ts +603 -603
  114. package/src/series/plugins/Pie.ts +623 -623
  115. package/src/series/plugins/PieEventTexts.ts +283 -283
  116. package/src/series/plugins/PieLabels.ts +640 -640
  117. package/src/series/plugins/Rose.ts +516 -516
  118. package/src/series/plugins/RoseLabels.ts +600 -600
  119. package/src/series/plugins/SeriesLegend.ts +96 -96
  120. package/src/series/seriesObservables.ts +145 -145
  121. package/src/series/seriesUtils.ts +51 -51
  122. package/src/tree/defaults.ts +23 -23
  123. package/src/tree/index.ts +3 -3
  124. package/src/tree/plugins/TreeLegend.ts +96 -96
  125. package/src/tree/plugins/TreeMap.ts +333 -333
  126. package/src/utils/commonUtils.ts +21 -21
  127. package/src/utils/d3Graphics.ts +174 -174
  128. package/src/utils/d3Utils.ts +73 -73
  129. package/src/utils/observables.ts +14 -14
  130. package/src/utils/orbchartsUtils.ts +100 -100
  131. package/tsconfig.base.json +13 -13
  132. package/tsconfig.json +2 -2
  133. package/vite.config.js +22 -22
  134. package/dist/src/grid/types.d.ts +0 -93
  135. package/dist/src/multiGrid/types.d.ts +0 -61
  136. package/dist/src/noneData/types.d.ts +0 -24
  137. package/dist/src/series/types.d.ts +0 -73
  138. package/dist/src/tree/types.d.ts +0 -21
  139. package/src/grid/types.ts +0 -124
  140. package/src/multiGrid/types.ts +0 -73
  141. package/src/noneData/types.ts +0 -26
  142. package/src/series/types.ts +0 -87
  143. package/src/tree/types.ts +0 -23
@@ -1,22 +1,22 @@
1
- // 取得文字寬度
2
- export function measureTextWidth (text: string, size: number = 10) {
3
- const context = document.createElement("canvas").getContext("2d")
4
- let width = context?.measureText(text)?.width ?? 0
5
- return width * size / 10 // 以10為基準
6
- }
7
-
8
- // 取得最小及最大值 - 數字陣列
9
- export function getMinAndMax (data: number[]): [number, number] {
10
- const defaultMinAndMax: [number, number] = [0, 0] // default
11
- if (!data.length) {
12
- return defaultMinAndMax
13
- }
14
- const minAndMax: [number, number] = data.reduce((prev, current) => {
15
- // [min, max]
16
- return [
17
- current < prev[0] ? current : prev[0],
18
- current > prev[1] ? current : prev[1]
19
- ]
20
- }, [data[0], data[0]])
21
- return minAndMax
1
+ // 取得文字寬度
2
+ export function measureTextWidth (text: string, size: number = 10) {
3
+ const context = document.createElement("canvas").getContext("2d")
4
+ let width = context?.measureText(text)?.width ?? 0
5
+ return width * size / 10 // 以10為基準
6
+ }
7
+
8
+ // 取得最小及最大值 - 數字陣列
9
+ export function getMinAndMax (data: number[]): [number, number] {
10
+ const defaultMinAndMax: [number, number] = [0, 0] // default
11
+ if (!data.length) {
12
+ return defaultMinAndMax
13
+ }
14
+ const minAndMax: [number, number] = data.reduce((prev, current) => {
15
+ // [min, max]
16
+ return [
17
+ current < prev[0] ? current : prev[0],
18
+ current > prev[1] ? current : prev[1]
19
+ ]
20
+ }, [data[0], data[0]])
21
+ return minAndMax
22
22
  }
@@ -1,175 +1,175 @@
1
-
2
- type RenderCircleTextParams = {
3
- text: string,
4
- radius: number,
5
- lineHeight: number,
6
- isBreakAll: boolean,
7
- limit?:number
8
- }
9
-
10
- type Line = { width: number; text: string }
11
-
12
- export function renderCircleText (selection: d3.Selection<any, any, any, any>, {
13
- text,
14
- radius,
15
- lineHeight,
16
- isBreakAll = false,
17
- limit = 0
18
- }: RenderCircleTextParams): d3.Selection<SVGTSpanElement, Line, SVGTextElement, any> | undefined {
19
- if (selection == null || text == null) {
20
- console.error("selection or text is not defined")
21
- return
22
- }
23
- if (radius == null) {
24
- const getBox = selection.node().getBBox()
25
- radius = getBox.width / 2
26
- }
27
-
28
- function getWords (text: string) {
29
- let words
30
- if (isBreakAll) {
31
- words = text.split('')
32
- } else {
33
- words = text.split(/\s+/g) // To hyphenate: /\s+|(?<=-)/
34
- }
35
- if (!words[words.length - 1]) words.pop()
36
- if (!words[0]) words.shift()
37
- return words
38
- }
39
-
40
- // 省略文章字數
41
- function ellipisText (text:string, limit:number) {
42
- if (text && limit) {
43
- if (text.length > limit) {
44
- text = text.substring(0, limit) + "..."; // 超過字數以"..."取代
45
- }
46
- }
47
- return text;
48
- }
49
-
50
- function measureWidth (text: string) {
51
- const context = document.createElement("canvas").getContext("2d")
52
- // return text => context.measureText(text).width
53
- return context?.measureText(text)?.width ?? 0
54
- }
55
-
56
- function getTargetWidth (text: string) {
57
- const m = measureWidth(text.trim())
58
- const result = Math.sqrt(m * lineHeight)
59
- return result
60
- // return(
61
- // Math.sqrt(measureWidth(text.trim()) * lineHeight)
62
- // )
63
- }
64
-
65
- function getLines (words: string[], targetWidth: number) {
66
- let line: Line = { width: 0, text: '' }
67
- let lineWidth0 = Infinity
68
- const lines: Array<Line> = []
69
- let space = " "
70
- if (isBreakAll) {
71
- space = ""
72
- }
73
- for (let i = 0, n = words.length; i < n; ++i) {
74
- const lineText1 = (line.text ? line.text + space : '') + words[i]
75
- const lineWidth1 = measureWidth(lineText1)
76
- if ((lineWidth0 + lineWidth1) / 2 < targetWidth) {
77
- line.width = lineWidth0 = lineWidth1
78
- line.text = lineText1
79
- } else {
80
- lineWidth0 = measureWidth(words[i])
81
- line = {width: lineWidth0, text: words[i]}
82
- lines.push(line)
83
- }
84
- }
85
- return lines
86
- }
87
-
88
- function getTextRadius (lines: Array<Line>) {
89
- let radius = 0
90
- for (let i = 0, n = lines.length; i < n; ++i) {
91
- const dy: number = (Math.abs(i - n / 2 + 0.5) + 0.5) * lineHeight
92
- const dx: number = lines[i].width / 2
93
- radius = Math.max(radius, Math.sqrt(dx ** 2 + dy ** 2))
94
- }
95
- return radius
96
- }
97
-
98
- function draw (selection: d3.Selection<any, any, any, any>, text: string) {
99
- if(limit > 0) text = ellipisText(text,limit)
100
- const words = getWords(text)
101
- const targetWidth = getTargetWidth(text)
102
- const lines = getLines(words, targetWidth)
103
- const textRadius = getTextRadius(lines)
104
-
105
- let t = selection.select<SVGTextElement>("text")
106
- if (!t.size()) {
107
- t = selection.append("text")
108
- }
109
- t.attr("transform", `translate(${0},${0}) scale(${radius / textRadius})`)
110
- const tspanUpdate = t.selectAll<SVGTSpanElement, Line>("tspan")
111
- .data(lines)
112
- const tspanEnter = tspanUpdate.enter()
113
- .append<SVGTSpanElement>("tspan")
114
- .attr("x", 0)
115
- .merge(tspanUpdate as d3.Selection<SVGTSpanElement, Line, SVGTextElement, undefined>)
116
- .attr("y", (d: Line, i: number) => (i - lines.length / 2 + 0.8) * lineHeight)
117
- .text((d: Line) => d.text)
118
- tspanUpdate.exit().remove()
119
-
120
- // return selection.node()
121
- return tspanUpdate.merge(tspanEnter)
122
- }
123
-
124
- return draw(selection, text)
125
- }
126
-
127
- // 圖軸上的多行tspan
128
- export function renderTspansOnAxis (textSelection: d3.Selection<d3.BaseType, unknown, null, undefined>, {
129
- textArr,
130
- textSizePx,
131
- groupAxisPosition
132
- }: {
133
- textArr: string[]
134
- textSizePx: number
135
- groupAxisPosition: 'top' | 'right' | 'bottom' | 'left'
136
- }) {
137
- // -- 將原本單行文字改為多行文字 --
138
- textSelection.text(null) // 先清空原本的 text
139
-
140
- const textX = Number(textSelection.attr('x'))
141
- let textY = Number(textSelection.attr('y'))
142
- if (groupAxisPosition === 'top') {
143
- // 當文字在上方時,要往上偏移第一行的高度
144
- textY -= (textArr.length - 1) * textSizePx
145
- }
146
-
147
- textSelection
148
- .selectAll('tspan')
149
- .data(textArr)
150
- .join('tspan')
151
- .attr('x', textX)
152
- .attr('y', (_d, _i) => textY + _i * textSizePx)
153
- .text(d => d)
154
- }
155
-
156
- // 四象限上的多行tspan
157
- export function renderTspansOnQuadrant (textSelection: d3.Selection<d3.BaseType, unknown, null, undefined>, {
158
- textArr,
159
- textSizePx,
160
- quadrant
161
- }: {
162
- textArr: string[]
163
- textSizePx: number
164
- quadrant: number
165
- }) {
166
- textSelection
167
- .selectAll('tspan')
168
- .data(textArr)
169
- .join('tspan')
170
- .attr('x', 0)
171
- .attr('y', (_d, _i) => quadrant == 1 || quadrant == 2
172
- ? - (textArr.length - 1 - _i) * textSizePx
173
- : _i * textSizePx)
174
- .text(d => d)
1
+
2
+ type RenderCircleTextParams = {
3
+ text: string,
4
+ radius: number,
5
+ lineHeight: number,
6
+ isBreakAll: boolean,
7
+ limit?:number
8
+ }
9
+
10
+ type Line = { width: number; text: string }
11
+
12
+ export function renderCircleText (selection: d3.Selection<any, any, any, any>, {
13
+ text,
14
+ radius,
15
+ lineHeight,
16
+ isBreakAll = false,
17
+ limit = 0
18
+ }: RenderCircleTextParams): d3.Selection<SVGTSpanElement, Line, SVGTextElement, any> | undefined {
19
+ if (selection == null || text == null) {
20
+ console.error("selection or text is not defined")
21
+ return
22
+ }
23
+ if (radius == null) {
24
+ const getBox = selection.node().getBBox()
25
+ radius = getBox.width / 2
26
+ }
27
+
28
+ function getWords (text: string) {
29
+ let words
30
+ if (isBreakAll) {
31
+ words = text.split('')
32
+ } else {
33
+ words = text.split(/\s+/g) // To hyphenate: /\s+|(?<=-)/
34
+ }
35
+ if (!words[words.length - 1]) words.pop()
36
+ if (!words[0]) words.shift()
37
+ return words
38
+ }
39
+
40
+ // 省略文章字數
41
+ function ellipisText (text:string, limit:number) {
42
+ if (text && limit) {
43
+ if (text.length > limit) {
44
+ text = text.substring(0, limit) + "..."; // 超過字數以"..."取代
45
+ }
46
+ }
47
+ return text;
48
+ }
49
+
50
+ function measureWidth (text: string) {
51
+ const context = document.createElement("canvas").getContext("2d")
52
+ // return text => context.measureText(text).width
53
+ return context?.measureText(text)?.width ?? 0
54
+ }
55
+
56
+ function getTargetWidth (text: string) {
57
+ const m = measureWidth(text.trim())
58
+ const result = Math.sqrt(m * lineHeight)
59
+ return result
60
+ // return(
61
+ // Math.sqrt(measureWidth(text.trim()) * lineHeight)
62
+ // )
63
+ }
64
+
65
+ function getLines (words: string[], targetWidth: number) {
66
+ let line: Line = { width: 0, text: '' }
67
+ let lineWidth0 = Infinity
68
+ const lines: Array<Line> = []
69
+ let space = " "
70
+ if (isBreakAll) {
71
+ space = ""
72
+ }
73
+ for (let i = 0, n = words.length; i < n; ++i) {
74
+ const lineText1 = (line.text ? line.text + space : '') + words[i]
75
+ const lineWidth1 = measureWidth(lineText1)
76
+ if ((lineWidth0 + lineWidth1) / 2 < targetWidth) {
77
+ line.width = lineWidth0 = lineWidth1
78
+ line.text = lineText1
79
+ } else {
80
+ lineWidth0 = measureWidth(words[i])
81
+ line = {width: lineWidth0, text: words[i]}
82
+ lines.push(line)
83
+ }
84
+ }
85
+ return lines
86
+ }
87
+
88
+ function getTextRadius (lines: Array<Line>) {
89
+ let radius = 0
90
+ for (let i = 0, n = lines.length; i < n; ++i) {
91
+ const dy: number = (Math.abs(i - n / 2 + 0.5) + 0.5) * lineHeight
92
+ const dx: number = lines[i].width / 2
93
+ radius = Math.max(radius, Math.sqrt(dx ** 2 + dy ** 2))
94
+ }
95
+ return radius
96
+ }
97
+
98
+ function draw (selection: d3.Selection<any, any, any, any>, text: string) {
99
+ if(limit > 0) text = ellipisText(text,limit)
100
+ const words = getWords(text)
101
+ const targetWidth = getTargetWidth(text)
102
+ const lines = getLines(words, targetWidth)
103
+ const textRadius = getTextRadius(lines)
104
+
105
+ let t = selection.select<SVGTextElement>("text")
106
+ if (!t.size()) {
107
+ t = selection.append("text")
108
+ }
109
+ t.attr("transform", `translate(${0},${0}) scale(${radius / textRadius})`)
110
+ const tspanUpdate = t.selectAll<SVGTSpanElement, Line>("tspan")
111
+ .data(lines)
112
+ const tspanEnter = tspanUpdate.enter()
113
+ .append<SVGTSpanElement>("tspan")
114
+ .attr("x", 0)
115
+ .merge(tspanUpdate as d3.Selection<SVGTSpanElement, Line, SVGTextElement, undefined>)
116
+ .attr("y", (d: Line, i: number) => (i - lines.length / 2 + 0.8) * lineHeight)
117
+ .text((d: Line) => d.text)
118
+ tspanUpdate.exit().remove()
119
+
120
+ // return selection.node()
121
+ return tspanUpdate.merge(tspanEnter)
122
+ }
123
+
124
+ return draw(selection, text)
125
+ }
126
+
127
+ // 圖軸上的多行tspan
128
+ export function renderTspansOnAxis (textSelection: d3.Selection<d3.BaseType, unknown, null, undefined>, {
129
+ textArr,
130
+ textSizePx,
131
+ groupAxisPosition
132
+ }: {
133
+ textArr: string[]
134
+ textSizePx: number
135
+ groupAxisPosition: 'top' | 'right' | 'bottom' | 'left'
136
+ }) {
137
+ // -- 將原本單行文字改為多行文字 --
138
+ textSelection.text(null) // 先清空原本的 text
139
+
140
+ const textX = Number(textSelection.attr('x'))
141
+ let textY = Number(textSelection.attr('y'))
142
+ if (groupAxisPosition === 'top') {
143
+ // 當文字在上方時,要往上偏移第一行的高度
144
+ textY -= (textArr.length - 1) * textSizePx
145
+ }
146
+
147
+ textSelection
148
+ .selectAll('tspan')
149
+ .data(textArr)
150
+ .join('tspan')
151
+ .attr('x', textX)
152
+ .attr('y', (_d, _i) => textY + _i * textSizePx)
153
+ .text(d => d)
154
+ }
155
+
156
+ // 四象限上的多行tspan
157
+ export function renderTspansOnQuadrant (textSelection: d3.Selection<d3.BaseType, unknown, null, undefined>, {
158
+ textArr,
159
+ textSizePx,
160
+ quadrant
161
+ }: {
162
+ textArr: string[]
163
+ textSizePx: number
164
+ quadrant: number
165
+ }) {
166
+ textSelection
167
+ .selectAll('tspan')
168
+ .data(textArr)
169
+ .join('tspan')
170
+ .attr('x', 0)
171
+ .attr('y', (_d, _i) => quadrant == 1 || quadrant == 2
172
+ ? - (textArr.length - 1 - _i) * textSizePx
173
+ : _i * textSizePx)
174
+ .text(d => d)
175
175
  }
@@ -1,73 +1,73 @@
1
- import * as d3 from 'd3'
2
-
3
- export function getSvgGElementSize (selection: d3.Selection<SVGGElement, any, any, any>): DOMRect {
4
- try {
5
- return selection.node()!.getBBox()
6
- } catch (e: any) {
7
- throw new Error(e)
8
- }
9
- }
10
-
11
- // 使用字串加入svg
12
- export function appendSvg (selection: d3.Selection<any, any, any, any>, svgString: string): void {
13
- function parseSvg (svgString: string) {
14
- const div = document.createElementNS('http://www.w3.org/1999/xhtml', 'div');
15
- div.innerHTML= '<svg xmlns="http://www.w3.org/2000/svg">'+ svgString +'</svg>';
16
- const frag = document.createDocumentFragment()
17
- while (frag && div?.firstChild?.firstChild)
18
- frag.appendChild(div.firstChild.firstChild);
19
- return frag;
20
- }
21
- // 刪除現有子節點
22
- const node = selection.node()
23
- while(node.hasChildNodes())
24
- {
25
- node.removeChild(node.firstChild);
26
- }
27
- // 加入dom
28
- selection.node().appendChild(parseSvg(svgString))
29
- }
30
-
31
- export function getD3TransitionEase (easeName: string) {
32
- if (easeName.substring(0, 4) !== 'ease') {
33
- return d3.easeCubic
34
- }
35
- return (d3 as any)[easeName] ?? d3.easeCubic
36
- }
37
-
38
- export function makeD3Arc ({ axisWidth, innerRadius, outerRadius, padAngle, cornerRadius }: {
39
- axisWidth: number
40
- innerRadius: number
41
- outerRadius: number
42
- padAngle: number
43
- cornerRadius: number
44
- }): d3.Arc<any, d3.DefaultArcObject> {
45
- const arcScale = d3.scaleLinear()
46
- .domain([0, 1])
47
- .range([0, axisWidth / 2])
48
-
49
- const _outerRadius = arcScale(outerRadius)!
50
-
51
- return d3.arc()
52
- .innerRadius(arcScale(innerRadius)!)
53
- .outerRadius(_outerRadius)
54
- .padAngle(padAngle)
55
- .padRadius(_outerRadius)
56
- .cornerRadius(cornerRadius)
57
- }
58
-
59
- export const parseTickFormatValue = (value: any, tickFormat: string | ((text: d3.NumberValue) => string)) => {
60
- if (tickFormat! instanceof Function == true) {
61
- return (tickFormat as ((text: d3.NumberValue) => string))(value)
62
- }
63
- return d3.format(tickFormat as string)!(value)
64
- }
65
-
66
- export const parseDateTickFormatValue = (value: any, tickFormat: string | ((text: d3.NumberValue) => string)) => {
67
- if (tickFormat! instanceof Function == true) {
68
- return (tickFormat as ((text: d3.NumberValue) => string))(value)
69
- }
70
- return d3.timeFormat(tickFormat as string)!(value)
71
- }
72
-
73
-
1
+ import * as d3 from 'd3'
2
+
3
+ export function getSvgGElementSize (selection: d3.Selection<SVGGElement, any, any, any>): DOMRect {
4
+ try {
5
+ return selection.node()!.getBBox()
6
+ } catch (e: any) {
7
+ throw new Error(e)
8
+ }
9
+ }
10
+
11
+ // 使用字串加入svg
12
+ export function appendSvg (selection: d3.Selection<any, any, any, any>, svgString: string): void {
13
+ function parseSvg (svgString: string) {
14
+ const div = document.createElementNS('http://www.w3.org/1999/xhtml', 'div');
15
+ div.innerHTML= '<svg xmlns="http://www.w3.org/2000/svg">'+ svgString +'</svg>';
16
+ const frag = document.createDocumentFragment()
17
+ while (frag && div?.firstChild?.firstChild)
18
+ frag.appendChild(div.firstChild.firstChild);
19
+ return frag;
20
+ }
21
+ // 刪除現有子節點
22
+ const node = selection.node()
23
+ while(node.hasChildNodes())
24
+ {
25
+ node.removeChild(node.firstChild);
26
+ }
27
+ // 加入dom
28
+ selection.node().appendChild(parseSvg(svgString))
29
+ }
30
+
31
+ export function getD3TransitionEase (easeName: string) {
32
+ if (easeName.substring(0, 4) !== 'ease') {
33
+ return d3.easeCubic
34
+ }
35
+ return (d3 as any)[easeName] ?? d3.easeCubic
36
+ }
37
+
38
+ export function makeD3Arc ({ axisWidth, innerRadius, outerRadius, padAngle, cornerRadius }: {
39
+ axisWidth: number
40
+ innerRadius: number
41
+ outerRadius: number
42
+ padAngle: number
43
+ cornerRadius: number
44
+ }): d3.Arc<any, d3.DefaultArcObject> {
45
+ const arcScale = d3.scaleLinear()
46
+ .domain([0, 1])
47
+ .range([0, axisWidth / 2])
48
+
49
+ const _outerRadius = arcScale(outerRadius)!
50
+
51
+ return d3.arc()
52
+ .innerRadius(arcScale(innerRadius)!)
53
+ .outerRadius(_outerRadius)
54
+ .padAngle(padAngle)
55
+ .padRadius(_outerRadius)
56
+ .cornerRadius(cornerRadius)
57
+ }
58
+
59
+ export const parseTickFormatValue = (value: any, tickFormat: string | ((text: d3.NumberValue) => string)) => {
60
+ if (tickFormat! instanceof Function == true) {
61
+ return (tickFormat as ((text: d3.NumberValue) => string))(value)
62
+ }
63
+ return d3.format(tickFormat as string)!(value)
64
+ }
65
+
66
+ export const parseDateTickFormatValue = (value: any, tickFormat: string | ((text: d3.NumberValue) => string)) => {
67
+ if (tickFormat! instanceof Function == true) {
68
+ return (tickFormat as ((text: d3.NumberValue) => string))(value)
69
+ }
70
+ return d3.timeFormat(tickFormat as string)!(value)
71
+ }
72
+
73
+
@@ -1,14 +1,14 @@
1
- import * as d3 from 'd3'
2
- import { Observable, merge, distinctUntilChanged, fromEvent } from 'rxjs'
3
-
4
- export function d3EventObservable(selection: d3.Selection<any, any, any, any>, event: any) {
5
- // Start with an observable that will never emit
6
- let obs = new Observable(() => {});
7
- selection.each(function () {
8
- // Create observables from each of the elements
9
- const events = fromEvent(this as any, event);
10
- // Merge the observables into one
11
- obs = merge(obs, events);
12
- });
13
- return obs;
14
- }
1
+ import * as d3 from 'd3'
2
+ import { Observable, merge, distinctUntilChanged, fromEvent } from 'rxjs'
3
+
4
+ export function d3EventObservable(selection: d3.Selection<any, any, any, any>, event: any) {
5
+ // Start with an observable that will never emit
6
+ let obs = new Observable(() => {});
7
+ selection.each(function () {
8
+ // Create observables from each of the elements
9
+ const events = fromEvent(this as any, event);
10
+ // Merge the observables into one
11
+ obs = merge(obs, events);
12
+ });
13
+ return obs;
14
+ }