| 1 | import cairo |
|---|
| 2 | from itertools import izip |
|---|
| 3 | from StringIO import StringIO |
|---|
| 4 | |
|---|
| 5 | from pycha.chart import Option |
|---|
| 6 | from pycha.pie import PieChart |
|---|
| 7 | from pycha.color import DEFAULT_COLOR |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | def pieChart(fd, width, height, title, data, labels, bgColor=None, labelColor='#000000', colorScheme=DEFAULT_COLOR): |
|---|
| 11 | dataSet = [(name, [[0, value]]) for name, value in izip(labels, data)] |
|---|
| 12 | axisLabels = [dict(v=i, label=label) for i, label in enumerate(labels)] |
|---|
| 13 | |
|---|
| 14 | options = Option( |
|---|
| 15 | title=title, |
|---|
| 16 | titleFont='Times', |
|---|
| 17 | titleFontSize=24, |
|---|
| 18 | pieRadius=0.35, |
|---|
| 19 | |
|---|
| 20 | legend=Option(hide=True), |
|---|
| 21 | colorScheme=colorScheme, |
|---|
| 22 | background=Option(baseColor=bgColor), |
|---|
| 23 | axis=Option(labelColor=labelColor, |
|---|
| 24 | x=Option(ticks=axisLabels))) |
|---|
| 25 | |
|---|
| 26 | surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height) |
|---|
| 27 | chart = PieChart(surface, options) |
|---|
| 28 | chart.addDataset(dataSet) |
|---|
| 29 | chart.render() |
|---|
| 30 | |
|---|
| 31 | surface.write_to_png(fd) |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | def contributors(manager, limit=10, **kw): |
|---|
| 35 | labels, data = zip(*sorted(manager.topContributors(limit=limit), key=lambda x: x[1])) |
|---|
| 36 | fd = StringIO() |
|---|
| 37 | |
|---|
| 38 | title = u'Top %d %s contributors' % (len(data), manager.channel) |
|---|
| 39 | pieChart(fd=fd, |
|---|
| 40 | width=700, |
|---|
| 41 | height=700, |
|---|
| 42 | title=title, |
|---|
| 43 | data=data, |
|---|
| 44 | labels=labels, |
|---|
| 45 | **kw) |
|---|
| 46 | |
|---|
| 47 | fd.seek(0) |
|---|
| 48 | return fd |
|---|