case 'MONTH':
        $xrange = WEBPAGE::$dbh->getAll(sprintf('select 1000*YEAR(c.date)+MONTH(c.date) as date from tblCalendar as c where c.date >= "%s" and c.date <= "%s" group by date', $_GET['dateFrom'], $_GET['dateTo']));
        $mrow = WEBPAGE::$dbh->getAll(sprintf('select 1000*YEAR(cp.date)+MONTH(cp.date) as date, sum(cp.clients)/30 as clients, sum(cp.female)/30 as female, sum(cp.male)/30 as male, sum(cp.client_i)/30 as client_i, sum(cp.client_b)/30 as client_b from tblClientPortfolio as cp where 1000*YEAR(cp.date)+MONTH(cp.date) >= "%s" and 1000*YEAR(cp.date)+MONTH(cp.date) <= "%s" and %s = %s group by date', $xrange[0]['date'], $xrange[count($xrange) - 1]['date'], $_GET['mode'], $_GET[$_GET['mode']]));
        $xmin = sprintf('%s/%s', $xrange[0]['date'] - 1000 * floor($xrange[0]['date'] / 1000), floor($xrange[0]['date'] / 1000));
        $xmax = sprintf('%s/%s', $xrange[count($xrange) - 1]['date'] - 1000 * floor($xrange[count($xrange) - 1]['date'] / 1000), floor($xrange[count($xrange) - 1]['date'] / 1000));
        break;
    case 'YEAR':
        $xrange = WEBPAGE::$dbh->getAll(sprintf('select YEAR(c.date) as date from tblCalendar as c where c.date >= "%s" and c.date <= "%s" group by date', $_GET['dateFrom'], $_GET['dateTo']));
        $mrow = WEBPAGE::$dbh->getAll(sprintf('select YEAR(cp.date) as date, sum(cp.clients)/365 as clients, sum(cp.female)/365 as female, sum(cp.male)/365 as male, sum(cp.client_i)/365 as client_i, sum(cp.client_b)/365 as client_b from tblClientPortfolio as cp where YEAR(cp.date) >= "%s" and YEAR(cp.date) <= "%s" and %s = %s group by date', $xrange[0]['date'], $xrange[count($xrange) - 1]['date'], $_GET['mode'], $_GET[$_GET['mode']]));
        $xmin = $xrange[0]['date'];
        $xmax = $xrange[count($xrange) - 1]['date'];
        break;
}
if (!count($mrow) || count($xrange) < 2) {
    $graph = new Graph('', $_LABELS[$_GET['cycle']], $_LABELS['RP.GRP.Performance.clientPortfolio'], array(), array());
    Graph::printEmpty($graph);
    exit;
}
// initialize data array()
$data[$_LABELS['total']] = array();
foreach ($xrange as $i => $j) {
    $data[$_LABELS['total']][$j['date']] = 0;
    $data[$_LABELS['tblClients.gender.F']][$j['date']] = 0;
    $data[$_LABELS['tblClients.gender.M']][$j['date']] = 0;
    $data[$_LABELS['tblLoanTypes.borrower_type.B']][$j['date']] = 0;
    $data[$_LABELS['tblLoanTypes.borrower_type.I']][$j['date']] = 0;
}
// populate data array() with actual values from sql
foreach ($mrow as $key => $val) {
    $data[$_LABELS['total']][$val['date']] = $val['clients'];
    $data[$_LABELS['tblClients.gender.M']][$val['date']] = $val['male'];
Example #2
0
 function printBends($graph)
 {
     if (!is_array($graph->series)) {
         Graph::printEmpty($graph);
     }
     $image = Graph::create_canvas($graph);
     $series_names = array_keys($graph->series);
     $series_num = count($series_names);
     $y_values = array();
     for ($j = 0; $j < $series_num; $j++) {
         $x_series[$j] = array_keys($graph->series[$series_names[$j]]);
         $num = count($x_series[$j]);
         for ($i = 0; $i < $num; $i++) {
             $y_series[$j][$i] = $graph->series[$series_names[$j]][$x_series[$j][$i]];
         }
         $y_values = array_merge($y_values, $y_series[$j]);
     }
     // string x-series values?
     $x_values = array();
     for ($j = 0; $j < $series_num; $j++) {
         $num = count($x_series[$j]);
         for ($i = 0; $i < $num; $i++) {
             $x_series[$j][$i] = floatval($x_series[$j][$i]);
         }
         $x_values = array_merge($x_values, $x_series[$j]);
     }
     //calculation of active window bounderies
     sort($x_values);
     $x_values = array_values(array_unique($x_values));
     sort($y_values);
     $y_values = array_values(array_unique($y_values));
     $y_num = count($y_values);
     $x_num = count($x_values);
     $V_STEP = $y_values[$y_num - 1] / 10;
     $H_STEP = round(GRAPH_WIDTH / ($x_num - 1), 0);
     $H_WIDTH = ($x_num - 1) * $H_STEP;
     $image = Graph::create_grid($image, $H_WIDTH, $H_STEP, $V_STEP, $graph->xname, $graph->yname, $graph->options);
     $OFFSET = $H_WIDTH * $x_values[0] / ($x_values[$x_num - 1] - $x_values[0]);
     $color_legend = imagecolorallocate($image, $graph->text_color[r], $graph->text_color[g], $graph->text_color[b]);
     // prints adjustment points and legend...
     for ($j = 0; $j < $series_num; $j++) {
         $r = hexdec(substr($graph->plot_colors[$j], 1, 2));
         $g = hexdec(substr($graph->plot_colors[$j], 3, 2));
         $b = hexdec(substr($graph->plot_colors[$j], 5, 2));
         $color[$j] = imagecolorallocate($image, $r, $g, $b);
         imagefilledrectangle($image, LEGEND_MARGIN_LEFT - 20, GRAPH_MARGIN_TOP + $j * (GRAPH_HEIGHT / 12), LEGEND_MARGIN_LEFT - 15, GRAPH_MARGIN_TOP + $j * (GRAPH_HEIGHT / 12) + 4, $color[$j]);
         imagestring($image, 2, LEGEND_MARGIN_LEFT, GRAPH_MARGIN_TOP + ($j * (GRAPH_HEIGHT / 12) - 5), $series_names[$j], $color_legend);
         $num = count($x_series[$j]);
         for ($i = 0; $i < $num; $i++) {
             $x1 = MARGIN_LEFT - $OFFSET + $H_WIDTH * $x_series[$j][$i] / ($x_values[$x_num - 1] - $x_values[0]);
             $y1 = PLOT_BASE - GRAPH_HEIGHT * ($y_series[$j][$i] / $y_values[$y_num - 1]);
             imagefilledarc($image, $x1, $y1, 5, 5, 0, 360, $color[$j], 4);
         }
     }
     //prints X-Labels
     $num = count($x_values);
     $x2 = -$H_STEP;
     for ($i = 0; $i < $num; $i++) {
         $x1 = MARGIN_LEFT - $OFFSET + $H_WIDTH * $x_values[$i] / ($x_values[$x_num - 1] - $x_values[0]);
         if ($x1 - $x2 >= $H_STEP) {
             imagestring($image, 2, $x1, XLABEL_MARGIN_TOP, $x_values[$i], $color_legend);
         } else {
             $x1 = $x2;
         }
         $x2 = $x1;
     }
     for ($i = 0; $i < $series_num; $i++) {
         $function_string = Graph::createLagrange($x_series[$i], $y_series[$i]);
         $parsed_function = Graph::parse_function($function_string);
         //plot the function
         $n = count($x_series[$i]) - 1;
         $old_x = $x_series[$i][0];
         $old_y = $y_series[$i][0];
         $PLOT_H_STEP = 0.005 * ($x_series[$i][$n] - $x_series[$i][0]);
         for ($x = $x_series[$i][0]; $x < $x_series[$i][$n]; $x += $PLOT_H_STEP) {
             $y = "\$y = " . $parsed_function . ";";
             eval($y);
             if ($x < $old_x) {
                 $old_x = $x_series[$i][0];
                 $old_y = $y_series[$i][0];
             }
             $x_norm = MARGIN_LEFT - $OFFSET + $H_WIDTH * $x / ($x_values[$x_num - 1] - $x_values[0]);
             $y_norm = max(PLOT_BASE - GRAPH_HEIGHT * ($y / $y_values[$y_num - 1]), PLOT_BASE - GRAPH_HEIGHT);
             $y_norm = min($y_norm, PLOT_BASE);
             //only plot from the second time on
             if ($old_x != $x_series[$i][0]) {
                 imageline($image, $old_x_norm, $old_y_norm, $x_norm, $y_norm, $color[$i]);
             }
             $old_x = $x;
             $old_x_norm = $x_norm;
             $old_y = $y;
             $old_y_norm = $y_norm;
         }
     }
     //set content type
     header("Content-type: image/jpeg");
     //send image
     Imagejpeg($image, '', 100);
     ImageDestroy($image);
 }