Skip to content

Commit 65e98e8

Browse files
committed
sfc component cleanup
1 parent f25cab7 commit 65e98e8

File tree

5 files changed

+51
-197
lines changed

5 files changed

+51
-197
lines changed

chartgauge.js

-123
This file was deleted.

chartgauge.svg

-39
This file was deleted.

microsvg.js

+3
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,6 @@ function _setOptions(options) {
6666
Object.assign(document.api.options, options);
6767
} // _setOptions
6868

69+
70+
71+

packdist.js

+1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ const assets = [
135135
{ m: 'js', src: 'sfc/loader.js' },
136136
{ m: 'm', src: 'sfc/u-colorpick.vue' },
137137
{ m: 'm', src: 'sfc/u-piechart.vue' },
138+
{ m: 'm', src: 'sfc/u-gaugechart.vue' },
138139

139140
{ m: 'xml', src: 'chartline.svg' },
140141
{ m: 'js', src: 'chartline.js' },

sfc/u-piechart.vue

+47-35
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
<!--
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.
34

45
The component displays ...
56

67
<u-piechart></u-piechart>
78

89
## HTML Attributes
910

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.
1412

1513
## JavaScript accessible methods
1614

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+
1729
-->
1830

1931
<template>
@@ -28,8 +40,8 @@ The component displays ...
2840
display: inline-block;
2941
width: 300px;
3042
aspect-ratio: 1;
31-
--fontSize : 3px;
32-
--strokeWidth : .2;
43+
--fontSize: 3px;
44+
--strokeWidth: .2;
3345
}
3446

3547
svg {
@@ -42,6 +54,7 @@ svg {
4254
}
4355

4456
.text {
57+
fill: black;
4558
stroke: 0;
4659
font-size: var(--fontSize);
4760
text-anchor: middle;
@@ -52,9 +65,9 @@ svg {
5265
<script>
5366
export default class UPieChart extends UComponent {
5467
// constants
55-
RAD_OUT = 22;
68+
#RAD_OUT = 22;
5669

57-
DEFAULTOPTIONS = {
70+
#DEFAULTOPTIONS = {
5871
fontSize: '3px',
5972
strokeWidth: 0.2,
6073
showTitle: false,
@@ -63,18 +76,18 @@ export default class UPieChart extends UComponent {
6376
colors: []
6477
};
6578

79+
#options = this.#DEFAULTOPTIONS;
6680
data = [];
67-
options = {};
6881

6982
/**
7083
* Calculate a point on the circle, usable for svg paths
7184
* @param {number} alpha degree of angle
7285
* @param {number} r radius of circle
7386
* @returns string with <x>,<y>.
7487
*/
75-
_piePoint(alpha, r) {
88+
#cPoint(alpha, r) {
7689
return (String(Math.sin(alpha) * r) + ',' + String(-Math.cos(alpha) * r));
77-
} // _piePoint()
90+
} // #cPoint()
7891

7992

8093
/**
@@ -84,7 +97,7 @@ export default class UPieChart extends UComponent {
8497
* @param {Object | undefined} attr attributes of the new element passed as Object
8598
* @param {string | undefined} txt inner text content.
8699
*/
87-
createSVGNode(parentNode, tagName, attr, txt) {
100+
#createSVGNode(parentNode, tagName, attr, txt) {
88101
var n = document.createElementNS("http://www.w3.org/2000/svg", tagName);
89102
if (attr) {
90103
Object.getOwnPropertyNames(attr).forEach(function(p) {
@@ -97,11 +110,11 @@ export default class UPieChart extends UComponent {
97110
} // createSVGNode()
98111

99112

100-
_clearChildNodes(p) {
113+
#clearChildNodes(p) {
101114
Array.from(p.childNodes).forEach(function(c) {
102115
c.remove();
103116
});
104-
} // _clearChildNodes()
117+
} // #clearChildNodes()
105118

106119

107120
/**
@@ -115,21 +128,21 @@ export default class UPieChart extends UComponent {
115128
_addSlice(start, size, color, value, title) {
116129
var alpha = 2 * Math.PI * start;
117130
var beta = 2 * Math.PI * (start + size);
118-
var opts = this.options;
131+
var opts = this.#options;
119132

120133
// create pie slice path
121134
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;
124137

125138
if (size < 0.5) {
126139
p += " 0 0 1 ";
127140
} else {
128141
p += " 0 1 1 ";
129142
}
130-
p += this._piePoint(beta, this.RAD_OUT);
143+
p += this.#cPoint(beta, this.#RAD_OUT);
131144
p += "L0,0Z";
132-
var pNode = this.createSVGNode(this.shadowRoot.querySelector('#panel'), "path", {
145+
var pNode = this.#createSVGNode(this.shadowRoot.querySelector('#panel'), "path", {
133146
class: "segment",
134147
style: "fill:" + color,
135148
d: p
@@ -150,8 +163,8 @@ export default class UPieChart extends UComponent {
150163

151164

152165
// 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", {
155168
class: "text",
156169
style: "fill:" + ((lum > 127) ? "black" : "white"),
157170
x: tPoint[0], y: tPoint[1]
@@ -160,35 +173,34 @@ export default class UPieChart extends UComponent {
160173
} // _addSlice()
161174

162175

163-
init() {
164-
super.init();
165-
this.options = this.DEFAULTOPTIONS;
166-
}
176+
// init() {
177+
// super.init();
178+
// }
167179

168180

169181
/// Clear the pie chart.
170182
/// Remove all visible elements.
171183
clear() {
172184
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'));
175187
} // clear()
176188

177189

178190
/// Set chart options.
179191
/// @param opts: Options to control the look of the chart.
180192
setOptions(opts) {
181-
this.options = Object.assign({}, this.DEFAULTOPTIONS, opts);
193+
this.#options = Object.assign({}, this.#options, opts);
182194
if (opts.colors) {
183195
var cols = opts.colors;
184196
if (typeof cols == "string")
185-
this.options.colors = cols.split(',');
197+
this.#options.colors = cols.split(',');
186198
else if (Array.isArray(cols))
187-
this.options.colors = [...cols];
199+
this.#options.colors = [...cols];
188200
}
189201
// 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);
192204
} // setOptions()
193205

194206

@@ -197,8 +209,8 @@ export default class UPieChart extends UComponent {
197209
draw(data) {
198210
this.clear();
199211

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
202214

203215
if (data) {
204216
// calculate sum of all parts:

0 commit comments

Comments
 (0)