1
1
<!--
2
- Implementation of a web component for displaying a pie chart based on data.
2
+ Implementation of a web component for displaying a pie chart
3
+ based on configuration options and data.
3
4
4
5
The component displays ...
5
6
6
7
<u-piechart></u-piechart>
7
8
8
9
## HTML Attributes
9
10
10
- * **value** -- The initial value attribute can be given by a html tag attribute
11
- * **nnn** -- The nnn attribute ...
12
-
13
- ## JavaScript accessible attributes
11
+ The style of the web component can be changed.
14
12
15
13
## JavaScript accessible methods
16
14
15
+ * setOptions(opts) -- Set chart options.
16
+ * draw(data) -- Draw the pie chart based on the data.
17
+
18
+ ## Options
19
+
20
+ The options are used to configure the chart and are passed using the setOptions function.
21
+
22
+ * **fontSize** -- The size of the values shown as text. Default is '3px'.
23
+ * **strokeWidth** -- The with of the stroke drawing a border around a segment. Default is 0.2.
24
+ * **showTitle** -- : false,
25
+ * **showValue** -- : false,
26
+ * ** showPercentage** -- : false,
27
+ * ** colors** -- : []
28
+
17
29
-->
18
30
19
31
<template>
@@ -28,8 +40,8 @@ The component displays ...
28
40
display: inline-block;
29
41
width: 300px;
30
42
aspect-ratio: 1;
31
- --fontSize : 3px;
32
- --strokeWidth : .2;
43
+ --fontSize: 3px;
44
+ --strokeWidth: .2;
33
45
}
34
46
35
47
svg {
42
54
}
43
55
44
56
.text {
57
+ fill: black;
45
58
stroke: 0;
46
59
font-size: var(--fontSize);
47
60
text-anchor: middle;
52
65
<script>
53
66
export default class UPieChart extends UComponent {
54
67
// constants
55
- RAD_OUT = 22;
68
+ # RAD_OUT = 22;
56
69
57
- DEFAULTOPTIONS = {
70
+ # DEFAULTOPTIONS = {
58
71
fontSize: '3px',
59
72
strokeWidth: 0.2,
60
73
showTitle: false,
@@ -63,18 +76,18 @@ export default class UPieChart extends UComponent {
63
76
colors: []
64
77
};
65
78
79
+ #options = this.#DEFAULTOPTIONS;
66
80
data = [];
67
- options = {};
68
81
69
82
/**
70
83
* Calculate a point on the circle, usable for svg paths
71
84
* @param {number} alpha degree of angle
72
85
* @param {number} r radius of circle
73
86
* @returns string with <x>,<y>.
74
87
*/
75
- _piePoint (alpha, r) {
88
+ #cPoint (alpha, r) {
76
89
return (String(Math.sin(alpha) * r) + ',' + String(-Math.cos(alpha) * r));
77
- } // _piePoint ()
90
+ } // #cPoint ()
78
91
79
92
80
93
/**
@@ -84,7 +97,7 @@ export default class UPieChart extends UComponent {
84
97
* @param {Object | undefined} attr attributes of the new element passed as Object
85
98
* @param {string | undefined} txt inner text content.
86
99
*/
87
- createSVGNode(parentNode, tagName, attr, txt) {
100
+ # createSVGNode(parentNode, tagName, attr, txt) {
88
101
var n = document.createElementNS("http://www.w3.org/2000/svg", tagName);
89
102
if (attr) {
90
103
Object.getOwnPropertyNames(attr).forEach(function(p) {
@@ -97,11 +110,11 @@ export default class UPieChart extends UComponent {
97
110
} // createSVGNode()
98
111
99
112
100
- _clearChildNodes (p) {
113
+ #clearChildNodes (p) {
101
114
Array.from(p.childNodes).forEach(function(c) {
102
115
c.remove();
103
116
});
104
- } // _clearChildNodes ()
117
+ } // #clearChildNodes ()
105
118
106
119
107
120
/**
@@ -115,21 +128,21 @@ export default class UPieChart extends UComponent {
115
128
_addSlice(start, size, color, value, title) {
116
129
var alpha = 2 * Math.PI * start;
117
130
var beta = 2 * Math.PI * (start + size);
118
- var opts = this.options;
131
+ var opts = this.# options;
119
132
120
133
// create pie slice path
121
134
let p =
122
- "M" + this._piePoint (alpha, this.RAD_OUT)
123
- + "A" + this.RAD_OUT + "," + this.RAD_OUT;
135
+ "M" + this.#cPoint (alpha, this.# RAD_OUT)
136
+ + "A" + this.# RAD_OUT + "," + this.# RAD_OUT;
124
137
125
138
if (size < 0.5) {
126
139
p += " 0 0 1 ";
127
140
} else {
128
141
p += " 0 1 1 ";
129
142
}
130
- p += this._piePoint (beta, this.RAD_OUT);
143
+ p += this.#cPoint (beta, this.# RAD_OUT);
131
144
p += "L0,0Z";
132
- var pNode = this.createSVGNode(this.shadowRoot.querySelector('#panel'), "path", {
145
+ var pNode = this.# createSVGNode(this.shadowRoot.querySelector('#panel'), "path", {
133
146
class: "segment",
134
147
style: "fill:" + color,
135
148
d: p
@@ -150,8 +163,8 @@ export default class UPieChart extends UComponent {
150
163
151
164
152
165
// create text element on top of pie slice
153
- var tPoint = this._piePoint ((alpha + beta) / 2, this.RAD_OUT * 0.7).split(',');
154
- this.createSVGNode(this.shadowRoot.querySelector('#values'), "text", {
166
+ var tPoint = this.#cPoint ((alpha + beta) / 2, this.# RAD_OUT * 0.7).split(',');
167
+ this.# createSVGNode(this.shadowRoot.querySelector('#values'), "text", {
155
168
class: "text",
156
169
style: "fill:" + ((lum > 127) ? "black" : "white"),
157
170
x: tPoint[0], y: tPoint[1]
@@ -160,35 +173,34 @@ export default class UPieChart extends UComponent {
160
173
} // _addSlice()
161
174
162
175
163
- init() {
164
- super.init();
165
- this.options = this.DEFAULTOPTIONS;
166
- }
176
+ // init() {
177
+ // super.init();
178
+ // }
167
179
168
180
169
181
/// Clear the pie chart.
170
182
/// Remove all visible elements.
171
183
clear() {
172
184
const dom = this.shadowRoot;
173
- this._clearChildNodes (dom.querySelector('#panel'));
174
- this._clearChildNodes (dom.querySelector('#values'));
185
+ this.#clearChildNodes (dom.querySelector('#panel'));
186
+ this.#clearChildNodes (dom.querySelector('#values'));
175
187
} // clear()
176
188
177
189
178
190
/// Set chart options.
179
191
/// @param opts: Options to control the look of the chart.
180
192
setOptions(opts) {
181
- this.options = Object.assign({}, this.DEFAULTOPTIONS , opts);
193
+ this.# options = Object.assign({}, this.#options , opts);
182
194
if (opts.colors) {
183
195
var cols = opts.colors;
184
196
if (typeof cols == "string")
185
- this.options.colors = cols.split(',');
197
+ this.# options.colors = cols.split(',');
186
198
else if (Array.isArray(cols))
187
- this.options.colors = [...cols];
199
+ this.# options.colors = [...cols];
188
200
}
189
201
// this.shadowRoot.style.setProperty('--fontSize', );
190
- this.style.setProperty('--fontSize', this.options.fontSize);
191
- this.style.setProperty('--strokeWidth', this.options.strokeWidth);
202
+ this.style.setProperty('--fontSize', this.# options.fontSize);
203
+ this.style.setProperty('--strokeWidth', this.# options.strokeWidth);
192
204
} // setOptions()
193
205
194
206
@@ -197,8 +209,8 @@ export default class UPieChart extends UComponent {
197
209
draw(data) {
198
210
this.clear();
199
211
200
- var cl = this.options.colors;
201
- var cll = this.options.colors.length
212
+ var cl = this.# options.colors;
213
+ var cll = this.# options.colors.length
202
214
203
215
if (data) {
204
216
// calculate sum of all parts:
0 commit comments