-SECOND PART-
//this function below is for creating some column/bar chart
function DiagramBar($legend_x='', $legend_y='', $dtarray=0, $data_title='')
{
global $css_drawn;
if(!is_array($legend_x)) $legend_x = $this->legendx;
if(!is_array($legend_y)) $legend_y = $this->legendy;
if(count($this->ShowPercents)>0)
$this->showdigits |= 1;
if(empty($css_drawn))
{ //<2>
$css_drawn = 1;
?>
<?
} //<2> - draw css block for drawing bars
$data = (is_array($dtarray))? $dtarray : $this->data;
// if data array not passed, we use prepared array filled by GatherData()
$bar[] = 'bar-v01.png';
$bar[] = 'bar-v02.png';
$bar[] = 'bar-v03.png';
$bar[] = 'bar-v04.png';
$bar[] = 'bar-v05.png';
$bar[] = 'bar-v06.png';
$bar[] = 'bar-v07.png';
$bar[] = 'bar-v08.png';
$bar[] = 'bar-v09.png';
$bar[] = 'bar-v10.png';
$bar[] = 'bar-v11.png';
$bar[] = 'bar-v12.png';
if(!empty($this->btilemode)) { // tiled picture arrenged with css
echo "<style TYPE='text/css'>\n";
for($ii=0; $ii<count($bar); $ii++) {
$img = $this->imgpath.$bar[$ii]; //shows the bar(s) based on array above ($bar[] = 'bar-v12.png';, etc)
echo " td.tbar{$ii} { background-image: url($img); background-repeat: repeat; }\n";
}
echo "</style>\n";
}
if(empty($this->graf_height)) $this->graf_height = 240; // workarea height
if(empty($this->bwidth)) $this->bwidth = 0; // one bar width, px, 0=auto
if(!empty($data_title))
echo "<h3 align=center>$data_title</h3>\n<p>";
$bgpic = $this->imgpath.'bar-bg'.$this->graf_height. '.png'; // background picture under bars
$maxval = 0;
$minval = 0;
for($i=0; $i < count($legend_x); $i++) //loop as much as date existed ($legend_x)
{
for($j=0; $j < count($legend_y); $j++) //loop as much as maximum value ($legend_y)
{
$vl = empty($data[$i][$j]) ? 0 : floatval($data[$i][$j]); //if (date & value doesn't exist) -> $vl = 0, else -> get float value from $vl
$maxval = max($maxval, $vl);
$minval = min($minval, $vl);
}
}
// $maxval,$minval - compute values nearest to max, base-multiplied numbers
$pos_part = ($maxval > 0); // true - positive area exist
$neg_part = ($minval < 0); // true - negative area exist
if($pos_part)
{ //<1-1>
$maxfound = false;
$decbase = array(0.01, 0.016, 0.02, 0.024, 0.032, 0.04, 0.05,0.06,0.08);
for($tt=0; ($tt < 15) && (!$maxfound); $tt++)
{ //<2>
for($ii=0; $ii < count($decbase); $ii++) { //<3>
if ($maxval < $decbase[$ii]) {
$maxval = $decbase[$ii];
$maxfound = true;
break;
}
$decbase[$ii] = $decbase[$ii]*10; // next loop - next scale check
} //<3>
} //<2>
} //<1-1>
$Ystep = floor($maxval/4); // one measure along Y-axis weight
if($neg_part)
{ //<1-1>
$decbase = array(0.01, 0.016, 0.02, 0.024, 0.032, 0.04, 0.05,0.06,0.08);
$maxfound = false;
for($tt=0; ($tt < 15) && (!$maxfound); $tt++)
{ //<2>
for($ii=0; $ii < count($decbase); $ii++) { //<3>
if ( $minval >(-1)*$decbase[$ii]) {
$minval = (-1)*$decbase[$ii];
$maxfound = true;
break;
}
$decbase[$ii] *= 10; // next loop - next scale check
} //<3>
} //<2>
$Ystep = abs($minval)/4;
} //<1-1>
// evaluate positive and negative parts scales
// So if max=200 and min=-40 we make Y-axis "+200...0...-50"
// $steps_pos|neg - number of "steps" on Y axis (pos. and neg.)
$steps_pos = ($pos_part)? 4 : 0;
$steps_neg = ($neg_part)? 4 : 0;
if($pos_part && $neg_part) { //<2>
if($maxval>=abs($minval)) { // <3> (+)area greater, cut down negative part
$Ystep = $maxval/4;
if (abs($minval) <= $maxval/4) $steps_neg = 1; // 1/4
elseif (abs($minval) <= $maxval/2) $steps_neg = 2; // 2/4
elseif (abs($minval) <= $maxval*3/4) $steps_neg = 3; // 2/4
else $steps_neg = 4; // 4/4
$minval = $maxval*$steps_neg/(-4);
} //<3>
else { // <3> (-)area greater, cut down positive part
$Ystep = abs($minval)/4;
if ($maxval <= abs($minval)/4) $steps_pos = 1; // 1/4
elseif ($maxval <= abs($minval)/2) $steps_pos = 2; // 2/4
elseif ($maxval <= abs($minval)*3/4) $steps_pos = 3; // 3/4
else $steps_pos = 4;
$maxval = $minval*$steps_pos/(-4);
} //<3>
} //<2>
// so, we have $pos_part(true), $neg_part(true), $Ystep, $steps_pos, $steps_neg
// I need to know what columns/rows must be skipped because of empty...
$draw_cx = count($legend_x)? array_fill(0,count($legend_x),1) : array('');
$draw_cy = count($legend_y)? array_fill(0,count($legend_y),1) : array('');
$cnt_x = count($legend_x);
$cnt_y = count($legend_y);
if(empty($this->drawempty_x)) { // will draw only non-zero X-columns
$draw_cx = array_fill(0,count($legend_x),0);
$cnt_x = 0;
for($kx=0;$kx<count($legend_x); $kx++) {
for($ky=0;$ky<count($legend_y);$ky++) {
if(floatval($data[$kx][$ky])) { $draw_cx[$kx]=1; $cnt_x++; break;}
}
}
}
$cspan_x =$cnt_x+2; // for colspan in "header cells"
if(empty($this->drawempty_y)) { // will draw only non-zero X-columns
$cnt_y = 0;
$draw_cy = array_fill(0,count($legend_y),0);
for($ky=0;$ky<count($legend_y); $ky++) {
for($kx=0;$kx<count($legend_x);$kx++) {
if(isset($data[$kx][$ky]) && floatval($data[$kx][$ky])!=0) { $draw_cy[$ky]=1; $cnt_y++; break;}
}
}
}
if( empty($this->bwidth))
{ // compute optimal width for one bar
$this->bwidth = 740/($cspan_x*max($cnt_y,1));
$this->bwidth = floor($this->bwidth);
}
else
if(($this->autoshrink>0) && ($cspan_x*$cnt_y)*$this->bwidth > $this->autoshrink)
{ // ïðèíóäèòåëüíî óìåíüøàþ øèðèíó ñòîëáèêà, åñëè èõ ñëèøêîì ìíîãî
$this->bwidth = floor($this->autoshrink/($cspan_x*max($cnt_y,11)));
}
$this->bwidth = max($this->bwidth,4); // width is at least 4px
$ewidth = $this->bwidth+2; // empty bars width, plus border 2px
$height_q = floor($this->graf_height/4); //set the bar height
$bars_width = 240 + max($cnt_x,1)*max($cnt_y,1)*$this->bwidth; // minimal pixel width needed for diagram
if($bars_width>900)
{
$gr_width = '100%';
$tdwidth = floor(100/max($cnt_x,1)).'%'; // one chart "block" width
}
else
{
$gr_width = $bars_width;
$tdwidth = max($cnt_y,1)*($this->bwidth+2);
}