Ajout d'un attribut couleur pour chaque groupe d'article pour les graphiques
[bilio:bilio.git] / src / graph.rb
1 # coding: utf-8
2
3 class Graphique
4         
5         def initialize x, y, donnees, style=1, type=1, titre=nil, couleurs=nil
6         
7                 p = Rdata.new
8                 pal = []
9                 donnees.each do |d|
10                         p.add_point(d[:serie],d[:titre])
11                         pal << d[:color]
12                 end
13                 p.add_all_series
14                 p.remove_serie donnees[donnees.count-1][:titre]
15                 p.set_abscise_label_serie donnees[donnees.count-1][:titre]
16                 
17                 case type
18                         when 1 # Pie Graph
19                                 pal = (couleurs.nil? ? palette(donnees[0][:serie].count) : couleurs)
20                                 pie p, x, y, style, pal
21                         when 2 # Bar Graph
22                                 bar p, x, y, style, pal, titre
23                 end
24         
25         end
26         
27         def pie p, x, y, style, pal
28         
29                 ch = Rchart.new(x,y)
30                 
31                 ch.draw_filled_rounded_rectangle(7,7,x-7,y-7,5,240,240,240)
32                 ch.draw_rounded_rectangle(5,5,x-5,y-5,5,230,230,230)
33
34                 # Load palette from array [[r,g,b],[r1,g1,b1]]
35                 ch.load_color_palette pal
36                 
37                 rayon = y/2
38                 rayon *= 0.7 if style.eql?(1)
39                 rayon *= 0.9 if style.eql?(2)
40                 rayon *= 0.6 if style.eql?(3)
41                 ch.draw_filled_circle(((x/2)-80).ceil,(y/2).ceil+2,rayon.ceil,200,200,200) if style.eql?(1)
42
43                 ch.set_font_properties("tahoma.ttf",12)
44                 
45                 label = Rchart::PIE_PERCENTAGE_LABEL
46
47                 # Draw Basic Pie Graph
48                 ch.draw_basic_pie_graph(p.get_data,p.get_data_description,((x/2)-80).ceil,(y/2).ceil,rayon.ceil,label,255,255,218) if style.eql?(1)
49                 ch.draw_pie_graph(p.get_data,p.get_data_description,((x/2)-80).ceil,(y/2).ceil,rayon.ceil,label,false,50,20,5) if style.eql?(2)
50                 ch.draw_flat_pie_graph_with_shadow(p.get_data,p.get_data_description,((x/2)-80).ceil,(y/2).ceil,rayon.ceil,label,25) if style.eql?(3)
51                 ch.draw_pie_legend(x-170,15,p.get_data,p.get_data_description,250,250,250)
52
53                 @chemin = "../tmp/stat.png"
54                 ch.render_png(@chemin)
55         
56         end
57         
58         def bar p, x, y, style, pal, titre
59         
60                 p.set_x_axis_unit(titre);
61                 
62                 ch = Rchart.new(x, y)
63                 
64                 # Load palette from array [[r,g,b],[r1,g1,b1]]
65                 ch.load_color_palette pal
66
67     ch.set_font_properties("tahoma.ttf",8)
68     ch.set_graph_area(50,30,x-20,y-30)
69     ch.draw_filled_rounded_rectangle(7,7,x-7,y-7,5,240,240,240)
70     ch.draw_rounded_rectangle(5,5,x-5,y-5,5,230,230,230)
71     ch.draw_graph_area(255,255,255,true)
72     ch.draw_scale(p.get_data,p.get_data_description,Rchart::SCALE_NORMAL,150,150,150,true,0,0,true)
73     ch.draw_grid(4,true,230,230,230,50)
74     ch.set_font_properties("tahoma.ttf",6)
75     ch.draw_treshold(0,143,55,72,true,true)
76
77     #Bar Graph
78     ch.draw_bar_graph(p.get_data, p.get_data_description, true)
79
80     ch.set_font_properties("tahoma.ttf",12)
81     ch.draw_legend(x-110,10,p.get_data_description,255,255,255, rs = -1, gs = -1, bs = -1, rt = 0, gt = 0, bt = 0, border = false)
82     #ch.set_font_properties("tahoma.ttf",12)
83     #ch.draw_title(50,22,titre,50,50,50,585) unless titre.nil?
84    
85         @chemin = "../tmp/stat.png"
86                 ch.render_png(@chemin)
87         
88         end
89         
90         
91         def graph
92                 return @chemin
93         end
94         
95         def palette count
96                 
97                 p = []
98                 count.times do
99                         p << [rand(0..255), rand(0..255), rand(0..255)]
100                 end
101                 
102                 p
103                 
104         end
105
106 end