Try to use PyCha chart library
[findit] / src / files / out_diabar.py
1 #!/usr/bin/env python
2 # -*-coding: utf-8 -*-
3 # vim: sw=4 ts=4 expandtab ai
4
5 from random import random
6 from math import pi, cos, sin, sqrt
7 import os.path
8
9 #==============================================================================
10
11 class Cli_Presentation(object):
12     pass
13
14 #==============================================================================
15
16 class Gtk_Presentation(object):
17     def __init__(self, filelist, maxdata=10):
18         import gtk
19         import cairo; global cairo
20         import pycha.bar; global pycha
21
22         drawing = gtk.DrawingArea()
23         drawing.connect('expose-event', self.expose_event)
24
25         # `maxdata` biggest files from list
26         filelist.sort(reverse=True)
27         filelist = filelist[:maxdata]
28
29         # only byte sizes
30         self.data = (
31             ('sizes',
32                 [(i, d[0]) for i, d in enumerate(filelist)]),
33         )
34
35         # labels for bar chart
36         ticks = [dict(v=i, label=os.path.basename(d[1])) for i, d in enumerate(filelist)]
37
38         self.options = {
39             'axis': {
40                 'x': {
41                     'ticks': ticks,
42                     'tickCount': 7,
43                     'rotate': 90,
44                 },
45                 'y': {
46                     'tickCount': 5,
47                     'tickPrecision' : 0,
48                 },
49             },
50             'background': {
51                 'chartColor': '#d8e7ec',
52                 'baseColor': '#efebe7',
53                 'lineColor': '#444444'
54 #                'hide': True,
55             },
56             'padding': {
57                 'left': 40,
58                 'right': 10,
59                 'top': 20,
60                 'bottom': 60,
61             },
62             'legend': {
63                 'hide': True,
64             },
65         }
66
67         self.toplevel = drawing
68
69     def expose_event(self, widget, event):
70         x, y, w, h, _ = widget.window.get_geometry()
71         surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h)
72
73         cr = widget.window.cairo_create()   # cairo context
74         cr.set_source_surface(surface, 0, 0)
75
76         chart = pycha.bar.VerticalBarChart(surface, self.options)
77         chart.addDataset(self.data)
78         chart.render()
79
80         cr.paint()