def _vertical_bars(self, title, x_axis, y_axis): try: x_labels = map(lambda dataset: dataset[x_axis], self._value_list.values()[0]) g = bar.VerticalBar(x_labels) g.stack = 'side' g.scale_integers = True g.width, g.height = 640, 480 g.graph_title = title g.show_graph_title = True for legend, dataset_seq in self._value_list.iteritems(): y_values = map(lambda ntuple: float(ntuple[y_axis]), dataset_seq) g.add_data({'data': y_values, 'title': legend}) return g.burn() except NameError, e: ## Failed to import svg.charts library, unable to create a ## chart-diagram. Instead return hand-made SVG with error message. return '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="640" viewBox="0 0 640 480" height="480"><text x="10" y="20" font-size="18" style="fill:red;"><tspan x="10" y="45">Warning: failed to generate SVG chart diagram.</tspan><tspan x="10" y="85">Missing python library svg.charts, perform:</tspan> <tspan x="10" y="125">sudo pip install svg.charts</tspan> </text> </svg>'
def vertical_top(cls): g = bar.VerticalBar(cls.fields, dict(stack='top')) assert g.stack == 'top' g.scale_integers = True g.width, g.height = 640, 480 g.graph_title = 'Question 7' g.show_graph_title = True g.add_data({'data': [-2, 3, 1, 3, 1], 'title': 'Female'}) g.add_data({'data': [0, 2, 1, 5, 4], 'title': 'Male'}) return g
def vertical_large(cls): g = bar.VerticalBar(cls.fields) options = dict( scale_integers=True, stack='side', width=640, height=480, graph_title='Question 8', show_graph_title=True, no_css=False, ) g.__dict__.update(options) g.add_data(dict(data=[2, 22, 98, 143, 82], title='intermediate')) g.add_data(dict(data=[2, 26, 106, 193, 105], title='old')) return g
nb = { 'total': int(i['NbTotal']), 'retards': int(i['NbRetards']), 'ok': int(i['NbTotal'])-int(i['NbRetards']) } # Attention, csv ne renvoie que des str, d'où la fonction int() # on ajoute à la fin de la liste le pourcentage de retard pour ce trajet retards.append(nb['retards']/nb['total']*100) # idem pour le pourcentage de trains à l'heure ok.append(nb['ok']/nb['total']*100) # g => objet de type VerticalBar avec les champs définis plus haut g = bar.VerticalBar(fields) # options g.stack = 'side' # les jeux de données seront affichés côte à # côte. Mettre 'top' pour un empilement g.show_graph_title = True # On affiche le titre (il est défini en dessous) g.graph_title = "Pourcentage de trains à l'heure/en retard en Avril 2012 (source SNCF)" g.show_data_values = False # On n'affiche pas de valeur numériques g.width, g.height = 1200,700 # On définit hauteur et largeur g.rotate_x_labels = True # Les noms de champ (en abscisse) seront # tournés de 90° (plus lisible) g.scale_integers = True # Les repères en fond seront à des nombres entiers g.font_size = 10 # taille de police (peu important) # Ajout du premier jeu de données : trains à l'heure