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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
|
# -*- coding: utf-8 -*-
import cairo
import math
class DynamicFlyer:
"""Producer of a nice graphic."""
def __init__(self, square):
"""Prepare the rendering of statistics."""
self._square = square
self._width = square * 2
self._height = square * 3 + 130
self._surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, self._width, self._height)
self._cr = cairo.Context(self._surface)
self._cr.select_font_face('Verdana', cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
self._backcolor = (1.0, 1.0, 1.0)
self._forecolor = (0.0, 0.0, 0.0)
self._cr.rectangle(0, 0, self._width, self._height)
self._cr.set_source_rgba(*self._backcolor, 1.0)
self._cr.fill()
self._caption_sep = 15
self._caption_width = 15
self._caption_space = 6
self._current_left = 0
self._current_top = 0
self._current_height = 0
self._next_top = 0
def _book_room(self, width, height):
"""Reserve space for drawings."""
if (self._current_left + width) > self._width:
self._current_left = 0
self._current_top = self._next_top
self._current_height = height
self._next_top = self._current_top + height
else:
if height > self._current_height:
self._next_top += (height - self._current_height)
self._current_height = height
self._cr.save()
self._cr.translate(self._current_left, self._current_top)
self._current_left += width
def print_header(self, text):
"""Display a main caption."""
self._cr.set_font_size(26)
fascent, fdescent, fheight, fxadvance, fyadvance = self._cr.font_extents()
x_off, y_off, tw, th = self._cr.text_extents(text)[:4]
self._book_room(self._width, fheight + 10)
nx = -tw / 2.0
ny = fheight
self._cr.save()
self._cr.translate(self._width / 2, 0)
self._cr.translate(nx, ny)
self._cr.move_to(0, 0)
self._cr.set_source_rgba(*self._forecolor, 1.0)
self._cr.show_text(text)
self._cr.restore()
def render_commit_timeline(self, start, end, data, created, color):
"""Render a chart for all commits."""
self._book_room(self._width, self._square)
hmargin = self._width * 0.1
vmargin = self._square * 0.1
width = self._width - 2 * hmargin
height = self._square - 2 * vmargin
self._cr.save()
self._cr.translate(hmargin, vmargin)
# Find the max and normalize
max_vol = max(data.values())
sx = width / (end - start)
sy = height / max_vol
self._cr.save()
self._cr.scale(sx, sy)
# Display volumes
first = min(data.keys())
if created:
self._cr.move_to(first, max_vol)
else:
self._cr.move_to(0, max_vol)
self._cr.line_to(0, max_vol - data[first])
for time in sorted(data.keys()):
vol = data[time]
self._cr.line_to(time, max_vol - vol)
last = max(data.keys())
self._cr.line_to(last, max_vol)
self._cr.close_path()
self._cr.set_source_rgba(*color, 0.5)
self._cr.fill_preserve()
self._cr.set_source_rgba(*color, 1.0)
self._cr.set_line_width(1 / sx)
self._cr.stroke()
# Display commits
for time, vol in data.items():
self._cr.rectangle(time - 2 / sx, max_vol - vol - 2 / sy, 4 / sx, 4 / sy)
self._cr.set_source_rgba(*color, 1.0)
self._cr.set_line_width(0.5)
self._cr.fill()
self._cr.restore()
# Display the borders
self._cr.move_to(0, 0)
self._cr.line_to(0, height)
self._cr.line_to(width, height)
self._cr.set_source_rgba(*self._forecolor, 0.7)
self._cr.stroke()
self._cr.move_to(0, 0)
self._cr.line_to(-4, 10)
self._cr.line_to(4, 10)
self._cr.set_source_rgba(*self._forecolor, 1.0)
self._cr.fill()
self._cr.move_to(width, height)
self._cr.line_to(width - 10, height - 4)
self._cr.line_to(width - 10, height + 4)
self._cr.set_source_rgba(*self._forecolor, 1.0)
self._cr.fill()
self._cr.restore()
# Print a caption as explaination
count = len(data.values())
min_vol = min(data.values())
unit = ''
if min_vol > 1000 or max_vol > 1000:
min_vol /= 1000
max_vol /= 1000
unit = 'k'
if min_vol > 1000 or max_vol > 1000:
min_vol /= 1000
max_vol /= 1000
unit = 'M'
caption = '%u commit%s - min: %u %sSLOC - max: %u %sSLOC' \
% (count, 's' if count > 1 else '', min_vol, unit, max_vol, unit)
self._cr.set_font_size(16)
fascent, fdescent, fheight, fxadvance, fyadvance = self._cr.font_extents()
x_off, y_off, tw, th = self._cr.text_extents(caption)[:4]
nx = -tw / 2.0
ny = fheight
self._cr.save()
self._cr.translate(self._width / 2, height + 2 * vmargin)
self._cr.translate(nx, ny)
self._cr.move_to(0, 0)
self._cr.set_source_rgba(*self._forecolor, 1.0)
self._cr.show_text(caption)
self._cr.restore()
def _render_pie(self, title, data, colors):
"""Render a pie chart."""
cx = self._square / 2
cy = (self._square / 2) * 0.8
radius = self._square / 4
# Processing data
stats = []
count = 0
total = 0
for k, v in data.items():
stats.append((v, k))
count += 1
total += v
stats = sorted(stats)
# Drawing results
done = 0
init_angle = - (stats[0][0] * math.pi) / (2 * total)
last_angle = init_angle
for stat in stats:
angle1 = last_angle
if (done + 1) == count:
angle2 = init_angle
else:
angle2 = angle1 + (stat[0] * math.pi) / total
self._cr.move_to(cx, cy)
self._cr.arc(cx, cy, radius, angle1, angle2)
self._cr.line_to(cx, cy)
self._cr.set_source_rgba(*colors[done], 0.5)
self._cr.fill_preserve()
self._cr.set_source_rgba(*self._backcolor, 1.0)
self._cr.set_line_width(2)
self._cr.stroke()
done += 1
last_angle = angle2
done = 0
last_angle = init_angle
for stat in stats:
angle1 = last_angle
if (done + 1) == count:
angle2 = init_angle
else:
angle2 = angle1 + (stat[0] * math.pi) / total
self._cr.arc(cx, cy, radius, angle1, angle2)
self._cr.set_source_rgba(*colors[done], 1.0)
self._cr.set_line_width(10)
self._cr.stroke()
done += 1
last_angle = angle2
# Printing the title
py = cy + radius * 1.4
self._cr.set_font_size(24)
fascent, fdescent, fheight, fxadvance, fyadvance = self._cr.font_extents()
x_off, y_off, tw, th = self._cr.text_extents(title)[:4]
nx = -tw / 2.0
ny = fheight / 2
self._cr.save()
self._cr.translate(cx, py)
self._cr.translate(nx, ny)
self._cr.move_to(0, 0)
self._cr.set_source_rgba(*self._forecolor, 1.0)
self._cr.show_text(title)
self._cr.restore()
py += fheight
# Printing the related captions
full_width = 0
self._cr.set_font_size(16)
for stat in stats:
x_off, y_off, tw, th = self._cr.text_extents(stat[1])[:4]
if full_width > 0:
full_width += self._caption_sep
full_width += self._caption_width + self._caption_space
full_width += tw
fascent, fdescent, fheight, fxadvance, fyadvance = self._cr.font_extents()
nx = -full_width / 2.0
ny = fheight / 2
done = 0
start = 0
for stat in stats:
x_off, y_off, tw, th = self._cr.text_extents(stat[1])[:4]
self._cr.save()
self._cr.translate(cx, py)
self._cr.translate(nx + start, ny)
# Color
top = -fheight + fdescent + (fheight - self._caption_width / 2) / 2
self._cr.rectangle(0, top, self._caption_width, self._caption_width / 2)
self._cr.set_source_rgba(*colors[done], 0.7)
self._cr.fill_preserve()
self._cr.set_source_rgba(*colors[done], 1.0)
self._cr.set_line_width(1)
self._cr.stroke()
# Text
self._cr.move_to(self._caption_width + self._caption_space, 0)
self._cr.set_source_rgba(*self._forecolor, 1.0)
self._cr.show_text(stat[1])
self._cr.restore()
start += self._caption_width + self._caption_space + tw + self._caption_sep
done += 1
def render_pie(self, title, data, colors):
"""Render a pie chart."""
self._book_room(self._square, self._square)
self._render_pie(title, data, colors)
self._cr.restore()
def print_footer(self, text):
"""Display an ending caption."""
self._cr.set_font_size(24)
fascent, fdescent, fheight, fxadvance, fyadvance = self._cr.font_extents()
x_off, y_off, tw, th = self._cr.text_extents(text)[:4]
nx = -tw / 2.0
ny = fheight
self._cr.save()
self._cr.translate(self._width / 2, self._height - 1.8 * fheight)
self._cr.translate(nx, 0)
self._cr.move_to(0, 0)
self._cr.set_source_rgba(*self._forecolor, 1.0)
self._cr.show_text(text)
self._cr.restore()
def save(self, filename):
"""Store all the renderings into a PNG file."""
self._surface.write_to_png(filename)
|