Project

General

Profile

Download (5.67 KB) Statistics
| Branch: | Revision:

git_sitools_idoc / webstatDev / idoc_Maison.OLD / webstat / libChart4StatSitools2.py @ 9791e4f3

1 9791e4f3 Marc Nicolas
#!/usr/bin/env python
2
# -*-coding:utf-8-*-
3
4
import sys
5
try:
6
    import random
7
except BaseException:
8
    sys.exit("Import failed in module libStatSitools :\n\trandom module is required")
9
try:
10
    import cairo
11
except BaseException:
12
    sys.exit("Import failed in module libStatSitools :\n\tcairo module is required")
13
try:
14
    import pycha.pie as pie
15
except BaseException:
16
    sys.exit("Import failed in module libStatSitools :\n\tpycha module is required")
17
try:
18
    from datetime import datetime
19
except BaseException:
20
    sys.exit("Import failed in module libStatSitools :\n\tdatetime module is required")
21
try:
22
    import pygal
23
except BaseException:
24
    sys.exit("Import failed in module libStatSitools :\n\tpygal module is required")
25
try:
26
    from dateutil.relativedelta import relativedelta
27
except:
28
    sys.exit("Import failed in module libStatSitools :\n\tdateutil.relativedelta module is required")
29
30
#PATH FOR DEV
31
home_base = "/home/marc/MyDev/sitools-idoc/webstatDev/idoc_Maison"
32
#PATH FOR PROD
33
#home_base = "/var/www/django/mon_projet/"
34
35
url_png = "/static/chart/"
36
37
time_format_for_by_month = "%Y %m"
38
39
def create_dataset_from_data(dico_data):
40
    graph_dataset = [(data, [[0, dico_data[data]]]) for data in dico_data]
41
    return graph_dataset
42
43
44
def add_date():
45
    now = datetime.now()
46
    tmp = str(now).split(" ")
47
    date_to_add = tmp[0].split("-")[0]+tmp[0].split("-")[1]+tmp[0].split("-")[2]+tmp[1].split(".")[0].split(":")[0]+tmp[1].\
48
        split(".")[0].split(":")[1]+tmp[1].split(".")[0].split(":")[2]+tmp[1].split(".")[1]
49
    return date_to_add
50
51
52
def svg_chart(dico_data, title_chart):
53
54
    pie_chart_pygal = pygal.Pie()
55
    pie_chart_pygal.title = title_chart
56
    date = add_date()
57
58
    url_output_svg = url_png+title_chart+date+".svg"
59
    print "**********************************************"
60
    print "*************  Nb entrée pour", title_chart, " : ", len(dico_data)
61
    print "**********************************************"
62
    dico_data_tmp = dico_data.copy()
63
    if len(dico_data) > 24:
64
        dico_data2 = {}
65
        i = 0
66
        for key in dico_data_tmp:
67
            if i > 24:
68
                dico_data2[key] = dico_data_tmp[key]
69
                del dico_data[key]
70
            i += 1
71
72
73
    for data in dico_data:
74
            pie_chart_pygal.add(data, dico_data[data])
75
76
    if len(dico_data_tmp) > 24:
77
        for datas in dico_data2:
78
            pie_chart_pygal.add(datas, dico_data2[datas], secondary=True)
79
80
    output_svg = home_base+url_output_svg
81
    try:
82
        pie_chart_pygal.render_to_file(output_svg)
83
    except BaseException, e:
84
        print 'In svg Exception : ', str(e)
85
86
    url_chart_png = png_chart(dico_data, title_chart)
87
    return [url_output_svg, url_chart_png]
88
89
90
def png_chart(dico_data, title_chart):
91
92
    pie_chart_pygal_png = pygal.Pie()
93
    pie_chart_pygal_png.title = title_chart
94
    
95
    date = add_date()
96
    url_output_png = url_png+title_chart+date+".png"
97
    for data in dico_data:
98
        pie_chart_pygal_png.add(data, dico_data[data])
99
    output_png = home_base+url_output_png
100
    try:
101
        pie_chart_pygal_png.render_to_png(output_png)
102
    except BaseException, e:
103
        print 'In PNG Exception : ', str(e)
104
    return url_output_png
105
106
107
def svg_bar_chart(dico_data, start_date, end_date, title_chart):
108
109
    dates_tmp_not_sorted = []
110
    dates_tmp_str_sorted = []
111
    dates_sorted = []
112
    dico_data2 = {}
113
    for date in dico_data.keys():
114
        dates_tmp_not_sorted.append(str(date[1])+"/"+str(date[0]))
115
116
    for dd in sorted(dates_tmp_not_sorted, key=lambda d: map(int, d.split('/'))):
117
        dates_tmp_str_sorted.append(dd.split('/')[1]+"/"+dd.split('/')[0][2:])
118
119
#    date_min = datetime.strptime(dates_tmp_str_sorted[0], "%m/%y")
120
#    date_max = datetime.strptime(dates_tmp_str_sorted[len(dates_tmp_str_sorted)-1], "%m/%y")
121
122
    for i in range(0, int(diff_month(start_date, end_date)+1)):
123
        t = datetime.combine(start_date, datetime.min.time())+relativedelta(months=i)
124
        dates_sorted.append(t)
125
126
    for x in dates_tmp_str_sorted:
127
        date_test = datetime.strptime(str(x), "%m/%y")
128
        dico_data2[date_test] = dico_data[(date_test.month, date_test.year)]
129
130
    list_tmp = []
131
    list_tmp2 = []
132
    list_tmp.append("")
133
134
    bar_chart = pygal.DateY(x_label_rotation=90, human_readable=True, y_scale=1073741824)
135
    bar_chart.title = title_chart
136
    bar_chart.x_label_format = "%m/%Y"
137
    bar_chart.x_labels = dates_sorted
138
    print "************* dates_sorted : ", dates_sorted
139
    try:
140
        for data in dates_sorted:
141
            if data not in dico_data2.keys():
142
                dico_data2[data] = 0
143
            list_tmp2.append((data, dico_data2[data]))
144
        list_tmp.append(list_tmp2)
145
146
        bar_chart.add(list_tmp[0],  (list_tmp[1][i] for i in range(0, len(list_tmp[1]))))
147
        bar_chart.show_legend = False
148
        date_svg = add_date()
149
        url_output_bar_chart_svg = url_png+"Bar_by_Month_"+date_svg+".svg"
150
        bar_chart.render_to_file(home_base+url_output_bar_chart_svg)
151
        url_output_bar_chart_png = url_bar_chart_png(list_tmp, dates_sorted, title_chart)
152
    except BaseException, e:
153
        print str(e)
154
    return [url_output_bar_chart_svg, url_output_bar_chart_png]
155
156
157
def url_bar_chart_png(list_data, dates, title):
158
    bar_chart = pygal.DateY(x_label_rotation=90)
159
    bar_chart.title = title
160
    bar_chart.x_label_format = "%m/%Y"
161
    bar_chart.x_labels = dates
162
    bar_chart.add(list_data[0],  (list_data[1][i] for i in range(0, len(list_data[1]))))
163
    # A METTRE POUR CREER LE PNG !
164
    bar_chart.print_values = False
165
    bar_chart.show_legend = False
166
    url_output_png = url_png+"Bar_by_Month_"+add_date()+".png"
167
    bar_chart.render_to_png(home_base+url_output_png)
168
    return url_output_png
169
170
171
def diff_month(d2, d1):
172
    return (d1.year - d2.year)*12 + d1.month - d2.month