-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpiechart.js
142 lines (126 loc) · 4.48 KB
/
piechart.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
#!/usr/bin/env gjs
imports.gi.versions.GObject = "3.0";
imports.gi.versions.Clutter = "1.0";
imports.gi.versions.St = "1.0";
const {GObject, Clutter, St, Pango, PangoCairo} = imports.gi;
const Cairo = imports.cairo;
var PieChart = GObject.registerClass(
class PieChart extends Clutter.Actor{
_init(width, height, percentage, warning=30, danger=10,
normalColor='#00FF00', warningColor='#FFFF00',
dangerColor='#FF0000'){
super._init();
this._width = width;
this._height = height;
this._percentage = percentage;
this._warning = warning;
this._danger = danger;
this.setNormalColor(normalColor);
this.setWarningColor(warningColor)
this.setDangerColor(dangerColor)
this._canvas = new Clutter.Canvas();
this._canvas.set_size(width, height);
this._canvas.connect('draw', (canvas, cr, width, height)=>{
this._draw(canvas, cr, width, height);
});
this.redraw();
this.set_content(this._canvas);
this.set_size(width, height);
}
setWidth(width){
this._width = width;
}
setHeight(height){
this._height = height;
}
setPercentage(percentage){
this._percentage = percentage;
}
getPercentage(){
return this._percentage;
}
setWarning(warning){
this._warning = warning;
}
setDanger(danger){
this._danger = danger;
}
setNormalColor(normalColor){
this._normalColor = Clutter.Color.from_string(normalColor)[1];
}
setWarningColor(warningColor){
this._warningColor = Clutter.Color.from_string(warningColor)[1];
}
setDangerColor(dangerColor){
this._dangerColor = Clutter.Color.from_string(dangerColor)[1];
}
redraw(){
this._canvas.invalidate();
}
_draw(canvas, cr, width, height){
// Clear the canvas
cr.save();
cr.setOperator(Cairo.Operator.CLEAR);
cr.paint();
cr.restore();
cr.setOperator(Cairo.Operator.OVER);
let linew = width * 0.15;
// Begin to paint
cr.save();
cr.setLineWidth(linew);
Clutter.cairo_set_source_color(cr, new Clutter.Color({
red: 60,
blue: 60,
green: 60,
alpha: 255
}));
cr.arc((width) / 2,
(height) / 2,
parseInt((width - linew) / 2 * 0.8),
0, 2 * Math.PI)
cr.stroke();
cr.restore();
cr.save();
cr.setLineWidth(linew);
if(this._percentage < this._danger){
Clutter.cairo_set_source_color(cr, this._dangerColor);
}else if(this._percentage < this._warning){
Clutter.cairo_set_source_color(cr, this._warningColor);
}else{
Clutter.cairo_set_source_color(cr, this._normalColor);
}
cr.arc((width) / 2,
(height) / 2,
parseInt((width - linew) / 2 * 0.8),
Math.PI * 2* (1 - this._percentage / 100), 0);
cr.stroke();
cr.restore();
cr.save();
Clutter.cairo_set_source_color(
cr,
new Clutter.Color({
red: 255,
blue: 255,
green: 255,
alpha: 255
}));
let texttoshow = this._percentage.toString() + "%";
this._write_centered_text(cr, width/2, height/2, texttoshow,
'Ubuntu', '10');
cr.restore();
cr.$dispose();
}
_write_centered_text(cr, x, y, text, font, size){
let pg_layout = PangoCairo.create_layout(cr);
let pg_context = pg_layout.get_context();
pg_layout.set_font_description(
Pango.FontDescription.from_string('%s %s'.format(font, size)));
pg_layout.set_text(text, -1);
PangoCairo.update_layout(cr, pg_layout);
let text_size = pg_layout.get_pixel_size();
cr.moveTo(x - text_size[0]/2, y + text_size[1]/4);
cr.setFontSize(size);
cr.showText(text);
}
}
);