Example #1
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);
 }