forked from jonkirkman/ringchart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ringchart.js
executable file
·191 lines (158 loc) · 6.2 KB
/
ringchart.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
window.ring_chart = function( container ) {
var rc = this;
// Returns a coordinate set relative to the axis of the arc
rc.pointOnArc = function( degrees, radius, center ) {
// position is calculated from sine/cosine of
// radius and center are used to offset the position
return {
x: center.x + (radius * Math.cos( degrees * (Math.PI / 180) )),
y: center.y + (radius * Math.sin( degrees * (Math.PI / 180) ))
};
};
// Tools for drawing an SVG Path
rc.draw = {
// SVG MoveTo: M x,y
moveTo: function( coord ) {
return 'M ' + coord.x + ',' + coord.y + ' ';
},
// SVG LineTo: L x,y
lineTo: function( coord ) {
return 'L ' + coord.x + ',' + coord.y + ' ';
},
// SVG ArcTo: A rx,ry xAxisRotate LargeArcFlag,SweepFlag x,y
arcTo: function( radius, coord, largeArc, clockwise ) {
var arc = 'A ';
arc += radius + ',' + radius + ' ';
arc += '0 ';
arc += largeArc + ',' + clockwise + ' ';
arc += coord.x + ',' + coord.y;
return arc;
},
// SVG close path: z
close: function() {
return 'z';
}
};
// Settings for our svg wrapper element
var svg = {
w: container.clientWidth,
h: container.clientHeight,
center: {
x: container.clientWidth / 2,
y: container.clientHeight / 2
}
};
svg.el = new LilSVG( 'svg', { width: svg.w, height: svg.h } );
// The colored ring
var ring = {
val: String( container.dataset.value ),
max: 100,
radius: Math.min( svg.w, svg.h ) * 0.5,
thickness: (Number( container.dataset.thickness ) || 20),
suffix: (container.dataset.suffix ? String( container.dataset.suffix ) : ''),
prefix: (container.dataset.prefix ? String( container.dataset.prefix ) : '')
};
// make sure the data is workable
if ( ring.val.indexOf('%') != -1 ) {
ring.val = ring.val.replace('%', '');
ring.suffix += '%';
}
else if ( ring.val.indexOf('/') != -1 ) {
var temp = ring.val.split('/');
ring.val = temp[0];
ring.max = temp[1];
}
if ( ring.suffix.indexOf('%') != -1) {
ring.max = 100;
}
ring.deg = Math.min( (ring.val / ring.max), 1 ) * 360;
ring.segments = [];
if (ring.deg > 0) {
var filled = { className: 'filled', points: [] };
filled.points.push( rc.pointOnArc( 0, ring.radius, svg.center ) );
// if (ring.deg == 360)
// filled.points.push( rc.pointOnArc( 179, ring.radius, svg.center ) );
filled.points.push( rc.pointOnArc( ring.deg, ring.radius, svg.center ) );
filled.points.push( rc.pointOnArc( ring.deg, ring.radius - ring.thickness, svg.center ) );
// if (ring.deg == 360)
// filled.points.push( rc.pointOnArc( 179, ring.radius - ring.thickness, svg.center ) );
filled.points.push( rc.pointOnArc( 0, ring.radius - ring.thickness, svg.center ) );
ring.segments.push( filled );
}
if (ring.deg < 360) {
var empty = { className: 'empty', points: [] };
empty.points.push( rc.pointOnArc( ring.deg, ring.radius, svg.center ) );
// if (ring.deg == 360)
// empty.points.push( rc.pointOnArc( 179, ring.radius, svg.center ) );
empty.points.push( rc.pointOnArc( 0, ring.radius, svg.center ) );
empty.points.push( rc.pointOnArc( 0, ring.radius - ring.thickness, svg.center ) );
// if (ring.deg == 360)
// empty.points.push( rc.pointOnArc( 179, ring.radius - ring.thickness, svg.center ) );
empty.points.push( rc.pointOnArc( ring.deg, ring.radius - ring.thickness, svg.center ) );
ring.segments.push( empty );
}
for (var i = 0; i < ring.segments.length; i++) {
ring.segments[i].d = rc.draw.moveTo();
ring.segments[i].d = rc.draw.arcTo();
}
/*
var p = ring.filled.points;
if (ring.deg > 0) {
ring.filled.d += 'M ' + p[0].x + ',' + p[0].y + ' ';
if (ring.deg == 360) {
p.push( rc.pointOnArc( 179, ring.radius, svg.center ) );
ring.filled.d += 'A ' + ring.radius + ',' + ring.radius + ' 0 0,1 ' + p[4].x + ',' + p[4].y + ' ';
}
ring.filled.d += 'A ' + ring.radius + ',' + ring.radius + ' 0 ' + (ring.deg < 180 ? 0 :1) + ',1 ' + p[1].x + ',' + p[1].y + ' ';
ring.filled.d += 'L ' + p[2].x + ',' + p[2].y + ' ';
if (ring.deg == 360) {
p.push( rc.pointOnArc( 179, ring.radius - ring.thickness, svg.center ) );
ring.filled.d += 'A ' + (ring.radius - ring.thickness) + ',' + (ring.radius - ring.thickness) + ' 0 1,0 ' + p[5].x + ',' + p[5].y + ' ';
}
ring.filled.d += 'A ' + (ring.radius - ring.thickness) + ',' + (ring.radius - ring.thickness) + ' 0 ' + (ring.deg < 180 ? 0 :1) + ',0 ' + p[3].x + ',' + p[3].y + ' ';
ring.filled.d += 'z';
ring.filled.el = new LilSVG( 'path', { 'class': 'filled', d: ring.filled.d, transform: 'rotate(-90 '+ svg.center.x +' ' + svg.center.y + ')' } );
svg.el.appendChild( ring.filled.el );
}
if (ring.deg < 360) {
ring.empty = { d: '' };
ring.empty.d += 'M ' + p[0].x + ',' + p[0].y + ' ';
if (ring.deg === 0) {
p.push( rc.pointOnArc( 179, ring.radius, svg.center ) );
ring.empty.d += 'A ' + ring.radius + ',' + ring.radius + ' 0 0,0 ' + p[4].x + ',' + p[4].y + ' ';
}
ring.empty.d += 'A ' + ring.radius + ',' + ring.radius + ' 0 ' + (ring.deg > 180 ? 0 :1) + ',0 ' + p[1].x + ',' + p[1].y + ' ';
ring.empty.d += 'L ' + p[2].x + ',' + p[2].y + ' ';
if (ring.deg === 0) {
p.push( rc.pointOnArc( 179, ring.radius - ring.thickness, svg.center ) );
ring.empty.d += 'A ' + (ring.radius - ring.thickness) + ',' + (ring.radius - ring.thickness) + ' 0 1,1 ' + p[5].x + ',' + p[5].y + ' ';
}
ring.empty.d += 'A ' + (ring.radius - ring.thickness) + ',' + (ring.radius - ring.thickness) + ' 0 ' + (ring.deg > 180 ? 0 :1) + ',1 ' + p[3].x + ',' + p[3].y + ' ';
ring.empty.d += 'z';
ring.empty.el = new LilSVG( 'path', { 'class': 'empty', d: ring.empty.d, transform: 'rotate(-90 '+ svg.center.x +' ' + svg.center.y + ')' } );
svg.el.appendChild( ring.empty.el );
}
*/
// The label in the middle
var text = new LilSVG( 'text', {
'class': 'label',
'text-anchor': 'middle',
'alignment-baseline': 'central',
x: svg.center.x,
y: svg.center.y
});
text.textContent = ring.val;
var prefix = new LilSVG( 'tspan', {
class: 'label-prefix'
});
prefix.textContent = ring.prefix;
text.insertBefore( prefix, text.firstChild );
var suffix = new LilSVG( 'tspan', {
class: 'label-suffix'
});
suffix.textContent = ring.suffix;
text.appendChild(suffix);
svg.el.appendChild( text );
container.appendChild( svg.el );
return rc;
};