Project

General

Profile

Revision 9791e4f3

Added by Marc Nicolas almost 9 years ago

Mise en place de la nouvelle API Pygal avec le nouveau bar_chart

View differences:

webstatDev/idoc_Maison/webstat/libChart4StatSitools2.py
19 19
except BaseException:
20 20
    sys.exit("Import failed in module libStatSitools :\n\tdatetime module is required")
21 21
try:
22
    from datetime import date
23
except BaseException:
24
    sys.exit("Import failed in module libStatSitools :\n\tdate module is required")
25
try:
22 26
    import pygal
23 27
except BaseException:
24 28
    sys.exit("Import failed in module libStatSitools :\n\tpygal module is required")
......
28 32
    sys.exit("Import failed in module libStatSitools :\n\tdateutil.relativedelta module is required")
29 33

  
30 34
#PATH FOR DEV
31
home_base = "/home/marc/MyDev/sitools-idoc/webstatDev/idoc_Maison"
35
home_base = "/home/marc/Projet/idoc_Maison"
32 36
#PATH FOR PROD
33 37
#home_base = "/var/www/django/mon_projet/"
34 38

  
......
116 120
    for dd in sorted(dates_tmp_not_sorted, key=lambda d: map(int, d.split('/'))):
117 121
        dates_tmp_str_sorted.append(dd.split('/')[1]+"/"+dd.split('/')[0][2:])
118 122

  
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 123
    for i in range(0, int(diff_month(start_date, end_date)+1)):
123 124
        t = datetime.combine(start_date, datetime.min.time())+relativedelta(months=i)
124 125
        dates_sorted.append(t)
......
131 132
    list_tmp2 = []
132 133
    list_tmp.append("")
133 134

  
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
135
    #bar_chart = pygal.DateY(x_label_rotation=90, human_readable=True, y_scale=1073741824)
136
    #bar_chart.title = title_chart
137
    #bar_chart.x_label_format = "%m/%Y"
138
    #bar_chart.x_labels = dates_sorted
139
    #print "************* dates_sorted : ", dates_sorted
139 140
    try:
140 141
        for data in dates_sorted:
141 142
            if data not in dico_data2.keys():
......
143 144
            list_tmp2.append((data, dico_data2[data]))
144 145
        list_tmp.append(list_tmp2)
145 146

  
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
147
        #bar_chart.add(list_tmp[0],  (list_tmp[1][i] for i in range(0, len(list_tmp[1]))))
148
        #bar_chart.show_legend = False
149
        date_svg = add_date()
150
        url_output_bar_chart_svg = url_png+"Bar_by_Month_"+date_svg+".svg"
151
        #bar_chart.render_to_file(home_base+url_output_bar_chart_svg)
152
        #url_output_bar_chart_png = url_bar_chart_png(list_tmp, dates_sorted, title_chart)
153

  
154
        datetimeline = pygal.DateTimeLine(x_label_rotation=35, truncate_label=-1,human_readable=True,x_value_formatter=lambda dt: dt.strftime('%m/%Y'))
155
        datetimeline.title = title_chart
156
        datetimeline.x_labels = dico_data2.keys()
157
        datetimeline.add("",list_tmp2)
158

  
159
        datetimeline.show_legend = False
148 160
        date_svg = add_date()
149 161
        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)
162
        datetimeline.render_to_file(home_base+url_output_bar_chart_svg)
163
        url_output_bar_chart_png = url_bar_chart_png(list_tmp2, dico_data2.keys(), title_chart)
152 164
    except BaseException, e:
153 165
        print str(e)
154 166
    return [url_output_bar_chart_svg, url_output_bar_chart_png]
155 167

  
156 168

  
157 169
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]))))
170
    datetimeline = pygal.DateTimeLine(x_label_rotation=35, truncate_label=-1,human_readable=True,x_value_formatter=lambda dt: dt.strftime('%m/%Y'))
171
    datetimeline.x_labels = dates
172
    datetimeline.title = title
173
    datetimeline.add("",list_data)
163 174
    # A METTRE POUR CREER LE PNG !
164
    bar_chart.print_values = False
165
    bar_chart.show_legend = False
175
    datetimeline.print_values = False
176
    datetimeline.show_legend = False
166 177
    url_output_png = url_png+"Bar_by_Month_"+add_date()+".png"
167
    bar_chart.render_to_png(home_base+url_output_png)
178

  
179
    datetimeline.render_to_png(home_base+url_output_png)
180

  
168 181
    return url_output_png
169 182

  
170 183

  
171 184
def diff_month(d2, d1):
172 185
    return (d1.year - d2.year)*12 + d1.month - d2.month
186

  
187
def format_download_vol(vol_down):
188
    vol_data = ""
189
    try:
190
        if 1000 <= vol_down < 1000000:
191
            vol_data = str(round(vol_down/1000, 2))+" Ko"
192
        elif 1000000 <= vol_down < 1000000000:
193
            vol_data = str(round(vol_down/1000000, 2))+" Mo"
194
        elif 1000000000 <= vol_down < (1000000000*1000):
195
            vol_data = str(round(vol_down/1000000000, 2))+" Go"
196
    except BaseException, e:
197
        print str(e)
198
    return vol_data

Also available in: Unified diff