Bonjour,

je cherchais un moyen de créer facilement des histogrammes sans avoir recours à des bibliothèques additionnelles complexes à installer ou utiliser (je suis une bille).

J'ai donc fait ce script qui fonctionne (à condition de disposer d'images 'blue.gif', 'gray.gif', 'green.gif', etc... de 20px x 100px) et je voudrais savoir s'il y a des inconvénients selon vous à procéder ainsi (j'ai vérifier l'utilisation de ressources et ça n'a pas l'air problématique) :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
 
<?
###########GRAPH MAKER
	# Dataset arrays to be called $datasetn, n being from 0 to ...
	# Dataset arrays must have the same kind of index (ie 1,2,3,4,... or jan, feb, mar,...)
	# Longest dataset must start with first data (ie jan) and finish with last (ie dec)
	# other datasets can start AND end with any value between longest dataset ends, ends included
 
	$barcol=array('blue','grey','green','olive','purple','maroon','silver');
 
	$dslabel0="My company";
 
		$dataset0=array(	'jan'	=>	3120,
							'feb'	=>	3280,
							'mar'	=>	3540,
							'apr'	=>	3765,
							'may'	=>	3954,
							'jun'	=>	4210,
							'jul'	=>	4382,
							'aug'	=>	4572,
							'sep'	=>	4821,
							'oct'	=>	5031,
							'nov'	=>	5291,
							'dec'	=>	5340);
 
		$dslabel1="Country max";
 
		$dataset1=array(	'jan'	=>	2780,
							'feb'	=>	3124,
							'mar'	=>	3241,
							'apr'	=>	3790,
							'may'	=>	3857,
							'jun'	=>	4320,
							'jul'	=>	4450,
							'aug'	=>	4560,
							'sep'	=>	4810,
							'oct'	=>	4982,
							'nov'	=>	5074,
							'dec'	=>	5232);
 
		$dslabel2="Country average";
 
		$dataset2=array(	'mar'	=>	3501,
							'apr'	=>	3754,
							'may'	=>	3953,
							'jun'	=>	3953,
							'jul'	=>	4267,
							'aug'	=>	4621,
							'sep'	=>	5481);
 
	#Stows dataset rray names and min/max values in an array $data_ends
	$dataset_nb=0;
 
	while(isset(${'dataset'.$dataset_nb}))
	{
 
		#Finds which dataset has the greatest number of values and min and max values
		$nb_values=count(${'dataset'.$dataset_nb});
 
		if((!isset($max_nb_val)) || (count(${'dataset'.$dataset_nb})>$max_nb_val))
		{
			$max_nb_val=count(${'dataset'.$dataset_nb}); 		#Longest dataset number of values
			$long_dataset='dataset'.$dataset_nb;				#Longest dataset name
		}
		if((!isset($min_data)) || (min(${'dataset'.$dataset_nb})<$min_data))		#Any dataset min value
		{
			$min_data=min(${'dataset'.$dataset_nb});
			$min_dataset='dataset'.$dataset_nb;
		}
		if((!isset($max_data)) ||  (max(${'dataset'.$dataset_nb})>$max_data))		#Any dataset max value
		{
			$max_data=max(${'dataset'.$dataset_nb});
			$max_dataset='dataset'.$dataset_nb;
		}
		if(!isset(${'dslabel'.$dataset_nb})){${'dslabel'.$dataset_nb}="";}
 
		$next_dataset=$dataset_nb+1;
		++$dataset_nb;
		if(!isset(${'dataset'.$next_dataset})){break;}
	}
 
	$dataset_no=$dataset_nb-1;
 
	if((!isset($yunit)) || (empty($yunit)) || ($yunit=="")){$yunit="";} 										#X axis unit (ie 'USD')
	if((!isset($graph_width)) || (empty($graph_width)) || ($graph_width==0)){$graph_width=500;} 				#Total graph width in px
	if((!isset($graph_height)) || (empty($graph_height)) || ($graph_height==0)){$graph_height=200;}				#Total graph height in px
	if((!isset($bar_width_ratio)) || (empty($bar_width_ratio)) || ($bar_width_ratio==0)){$bar_width_ratio=0.6;}	#padding
	$step_width=round(($graph_width)/count($dataset0)); 														#width of a row in px
	$bar_width=round((($graph_width*$bar_width_ratio)/count($dataset0))/$dataset_nb); 							#width of a bar in px
	if((!isset($y_step_nb)) || (empty($y_step_nb)) || ($y_step_nb==0)){$y_step_nb=10;}							#Nb of step Y axis
	$graph_line_height=round($graph_height/($y_step_nb));
 
	# Finds the gap between max and min value
	$y_total_gap=$max_data-$min_data;											#ie. 2220
 
	# Finds the interval bewteen points
	$y_step_val_abs=round($y_total_gap/$y_step_nb);								#ie. 222
 
	# Finds the *10 value of the gap between points
	$y_step_val_len=strlen(round($y_step_val_abs))-1;							#ie. 2
 
	#Finds the Y step to use 													#ie. 300
	$y_step_val_use=ceil($y_step_val_abs/(pow(10,$y_step_val_len)))*(pow(10,$y_step_val_len));
 
	#Finds the Y total gap 
	$y_total_gap_use=$y_step_val_use*$y_step_nb;
 
	$y_btm_value=floor($min_data/$y_step_val_use)*$y_step_val_use;
	$y_top_value=$y_btm_value+($y_step_val_use*$y_step_nb);
 
	#echo "<div class='z'>".$y_step_val_use."</div>";
 
	$curb="";
	$x_axis="";
 
	$i=1;
	foreach(${$long_dataset} AS $key => $value)
	{
		$barset="";
		$datalist="";
		$d=0;
		while($d<$dataset_nb)
		{
			$img_color=$barcol[$d];
			if(!isset(${'dataline'.$d})){${'dataline'.$d}="<TD align='left' colspan='2' bgcolor='".$img_color."' style='font-size:7pt;color:white; border: 1px solid ".$img_color.";'><NOBR>".${'dslabel'.$d}."<NOBR></TD><TD bgcolor='".$img_color."'></TD>";}
 
			if(array_key_exists($key, ${'dataset'.$d}))
			{
				#Stow the img height in px OR % (image height is 100)
				$img_size=round(((${'dataset'.$d}[$key]-$y_btm_value)/$y_total_gap_use)*$graph_height);
 
				$barset.="<img src='./images/graph/".$img_color.".gif' height='".$img_size."' width='".$bar_width."' align='bottom' title='".${'dataset'.$d}[$key]."'>";
				${'dataline'.$d}.="<TD align='center' style='font-size:7pt;color:".$img_color."; border-top: 1px solid #DCDCDC; border-right: 1px solid #DCDCDC;'>".${'dataset'.$d}[$key]."</TD>";
			}
			else
			{
				${'dataline'.$d}.="<TD align='center' style='font-size:7pt;color:".$img_color.";border-top: 1px solid #DCDCDC; border-right: 1px solid #DCDCDC;'></TD>";
			}
			${'label'.$i}=$key; #Stow label in a variable label1='jan'
			++$d;
		}
		$rowspan=$y_step_nb+1;
 
		$curb.="<TD rowspan='".$rowspan."' width='".$step_width."px' align='left' style='border-right: 1px solid #DCDCDC';>".$barset."</TD>";
		$x_axis.="<TD align='center'>".${'label'.$i}."</TD>";
		++$i;
	}
 
	$graph_content="<TR valign='bottom' height='".$graph_line_height."px'><TD rowspan='".$rowspan."' width='30px' align='left' valign='middle' style='font-size:7pt;font-weight:bold;'>".$yunit."</TD><TD align='center' width='30px' style='font-size:8pt;'>".$y_top_value."</TD><TD style='border-bottom: solid blue 1px;border-right: solid blue 1px;'></TD>".$curb."</TR>";
 
	$graph_y="";
 
	$y_value=$y_top_value-$y_step_val_use;
 
	$j=0;
	while($j<$y_step_nb)
	{
		$graph_y.="<TR valign='bottom' height='".$graph_line_height."px'><TD align='center' width='35px' style='font-size:8pt;'>".$y_value."</TD><TD style='border-bottom: solid blue 1px;border-right: solid blue 1px;'></TD></TR>";
		$y_value=$y_value-$y_step_val_use;
		++$j;
	}
 
	$colspan=count($dataset0)+3;
 
	#####DEBUG
	// $graph="<br><br><TABLE cellpadding='1px' cellspacing='0px' border='0px' bordercolor='#F5F5F5'  align='center' width='".$graph_width."'><TR valign='middle' align='middle' style='font-size:8pt;color:blue;' >";
	// 
	// $graph.="<TD width='10%'>Series</TD><TD width='10%'>Longest</TD><TD width='10%'>Nb values</TD><TD width='10%'>Min value</TD><TD width='10%'>Max value</TD><TD width='10%'>Y div.</TD><TD width='10%'>Range</TD><TD width='10%'>Used range</TD><TD width='10%'>Div. value</TD><TD width='10%'>Used div. value</TD></TR>";
	// 	
	// $graph.="<TR valign='middle' align='middle' style='font-size:8pt;color:blue;' ><TD width='10%'>".$dataset_nb."</TD><TD width='10%'>".$long_dataset."</TD><TD width='10%'>".$max_nb_val."</TD><TD width='10%'>".$min_data."</TD><TD width='10%'>".$max_data."</TD><TD width='10%'>".$y_step_nb."</TD><TD width='10%'>".$y_total_gap."</TD><TD width='10%'>".$y_total_gap_use."</TD><TD width='10%'>".$y_step_val_abs."</TD><TD width='10%'>".$y_step_val_use."</TD>";
	// 
	// $graph.="</TR></TABLE>";
	####END DEBUG
 
	$graph="<br><br><TABLE cellpadding='1px' cellspacing='0px' border='0px' bordercolor='#F5F5F5'  align='center' width='".$graph_width."'><TR valign='bottom' bgcolor='gray' >".$graph_content.$graph_y."</TR><TR height='1px'><TD colspan='3'></TD><TD COLSPAN='".count($dataset0)."' style='border-top:1px solid blue;' ></TD></TR><TR valign='top' height='".$graph_line_height."px' style='font-size:8pt;'><TD></TD><TD></TD><TD></TD>".$x_axis."</TR>";
 
	$datatable="";
	$d=0;
	while($d<$dataset_nb)
	{
		$datatable.="<TR valign='middle'>".${'dataline'.$d}."</TR>";
		++$d;
		if(!isset(${'dataset'.$d})){break;}
	}
	$datatable.="<TD COLSPAN='".$colspan."' style='border-top:1px solid blue;' ></TD></TR></TABLE>";
 
	$graph.=$datatable;
 
echo $graph;
 
?>
Merci pour vos commentaires..

Px