Exemplo n.º 1
0
 function drawSunburstLevel($image, $datas, $params = array())
 {
     global $LANG;
     $width = $params['width'] - 70;
     $height = $params['height'] - 120;
     $gsum = PluginMreportingMisc::getArraySum($datas);
     $index = 0;
     $x = $width / 2;
     $y = $height / 2 + 60;
     $params['depth'] = isset($params['depth']) ? $params['depth'] : PluginMreportingMisc::getArrayDepth($datas);
     $params['start_angle'] = isset($params['start_angle']) ? $params['start_angle'] : 0;
     $params['max_angle'] = isset($params['max_angle']) ? $params['max_angle'] : 360;
     $params['level'] = isset($params['level']) ? $params['level'] : 0;
     $params['current_index'] = isset($params['current_index']) ? $params['current_index'] : false;
     $step = $height / $params['depth'];
     $radius = $step * ($params['level'] + 1);
     $darkerpalette = self::getDarkerPalette();
     foreach ($datas as $key => $data) {
         if (is_array($data)) {
             arsort($data);
             $params2 = array();
             $params2 = $params;
             $sum = PluginMreportingMisc::getArraySum($data);
             $angle = $params['max_angle'] * $sum / $gsum;
             $params2['max_angle'] = $angle;
             $params2['start_angle'] = $params['start_angle'];
             $params2['level'] = $params['level'] + 1;
             $params2['current_index'] = $params['current_index'] === false ? $index : $params['current_index'];
             $this->drawSunburstLevel($image, $data, $params2);
         } else {
             $angle = $params['max_angle'] * $data / $gsum;
         }
         //get colors
         $palette = $this->getPalette(5);
         if ($params['current_index'] === false) {
             $color = $palette[$index];
         } else {
             $color = $palette[$params['current_index']];
             //get lighter color
             $color = "0x00" . substr(self::lighter($color, 15 * $params['level'] * $index), 0, 6);
         }
         $darkercolor = "0x00" . substr(self::darker($color), 0, 6);
         $color_rbg = self::colorHexToRGB($color);
         $darkercolor_rbg = self::colorHexToRGB($darkercolor);
         //show data arc (tow arcs : 1st border color, 2nd content color)
         //(Never use deg2rad() in loops, use $rad = ($deg * M_PI / 180) instead which is faster!)
         imageSmoothArc($image, $x, $y, $radius + 1, $radius + 1, $darkercolor_rbg, $params['start_angle'] * M_PI / 180, ($params['start_angle'] + $angle) * M_PI / 180);
         imageSmoothArc($image, $x, $y, $radius - 1, $radius - 1, $color_rbg, ($params['start_angle'] + 0.8 / ($params['level'] + 1)) * M_PI / 180, ($params['start_angle'] + $angle - 0.8 / ($params['level'] + 1)) * M_PI / 180);
         //text associated with pie arc (only for angle > 2°)
         $am = $params['start_angle'] + $angle / 2;
         //mediant angle
         $amr = $am * M_PI / 180;
         //mediant angle in radiant
         //adjust label position (in fonction of angle position)
         $dx = $dy = 0;
         if ($amr >= 7 * M_PI / 4 || $amr <= M_PI / 4) {
             $dx = 0;
         }
         if ($amr >= M_PI / 4 && $amr <= 3 * M_PI / 4) {
             $dx = ($amr - M_PI / 4) * 2 / M_PI;
         }
         if ($amr >= 3 * M_PI / 4 && $amr <= 5 * M_PI / 4) {
             $dx = 1;
         }
         if ($amr >= 5 * M_PI / 4 && $amr <= 7 * M_PI / 4) {
             $dx = 1 - ($amr - M_PI * 5 / 4) * 2 / M_PI;
         }
         if ($amr >= 7 * M_PI / 4) {
             $dy = ($amr - M_PI - 3 * M_PI / 4) * 2 / M_PI;
         }
         if ($amr <= M_PI / 4) {
             $dy = 1 - $amr * 2 / M_PI;
         }
         if ($amr >= M_PI / 4 && $amr <= 3 * M_PI / 4) {
             $dy = 1;
         }
         if ($amr >= 3 * M_PI / 4 && $amr <= 5 * M_PI / 4) {
             $dy = 1 - ($amr - 3 * M_PI / 4) * 2 / M_PI;
         }
         if ($amr >= 5 * M_PI / 4 && $amr <= 7 * M_PI / 4) {
             $dy = 0;
         }
         //get label size
         $box = @imageTTFBbox($this->fontsize, $this->fontangle, $this->font, $key);
         $tw = abs($box[4] - $box[0]);
         $th = abs($box[5] - $box[1]);
         //define label position
         if (is_array($data)) {
             //show label inside its arc
             $xtext = $x - $dx * $tw + cos($amr) * (0.5 * $radius - $step / 3);
             $ytext = $y + $dy * $th - sin($amr) * (0.5 * $radius - $step / 4);
         } else {
             //show label outside of its arc
             $xtext = $x + 3 - $dx * $tw + cos($amr) * (0.5 * $radius + $step / 16);
             $ytext = $y + $dy * $th - sin($amr) * (0.5 * $radius + $step / 8);
         }
         //draw label
         imagettftext($image, $this->fontsize, $this->fontangle, $xtext, $ytext, $darkercolor, $this->font, $key);
         //values labels
         if ($angle > 5) {
             //mediant start angle in radiant (adjusted for left align label to its arc)
             $samr = ($params['start_angle'] + 10 / ($params['level'] + 1)) * M_PI / 180;
             //get label size
             $box = @imageTTFBbox($this->fontsize, $this->fontangle, $this->font, is_array($data) ? $gsum : $data);
             $tw = abs($box[4] - $box[0]);
             $th = abs($box[5] - $box[1]);
             //define label position
             $xtext = $x - $dx * $tw + cos($samr) * (0.5 * $radius - $step / 8);
             $ytext = $y + $dy * $th - sin($samr) * (0.5 * $radius - $step / 16);
             //draw label
             imagettftext($image, $this->fontsize, $this->fontangle, $xtext, $ytext, $this->black, $this->font, is_array($data) ? $sum : $data);
         }
         $params['start_angle'] += $angle;
         $index++;
     }
     return $image;
 }
Exemplo n.º 2
0
 /**
  * show Graph datas
  *
  * @param $datas, ex : array( 'test1' => 15, 'test2' => 25)
  * @param $unit, ex : '%', 'Kg' (optionnal)
  * @param $labels2, ex : dates
  * @param $flip_data, flip array if necessary
  */
 static function showGraphDatas($datas = array(), $unit = '', $labels2 = array(), $flip_data = false, $show_graph = false)
 {
     global $LANG, $CFG_GLPI;
     $simpledatas = false;
     $treedatas = false;
     //simple and tree array
     $depth = PluginMreportingMisc::getArrayDepth($datas);
     if (!$labels2 && $depth < 2) {
         $simpledatas = true;
     }
     if ($_REQUEST['gtype'] == "sunburst") {
         $treedatas = true;
     }
     if ($flip_data == true) {
         $labels2 = array_flip($labels2);
     }
     $types = array();
     foreach ($datas as $k => $v) {
         if (is_array($v)) {
             foreach ($v as $key => $val) {
                 if (isset($labels2[$key])) {
                     $types[$key][$k] = $val;
                 }
             }
         }
     }
     if ($flip_data != true) {
         $tmp = $datas;
         $datas = $types;
         $types = $tmp;
     }
     //simple array
     if ($simpledatas) {
         $datas = array($LANG['plugin_mreporting']["export"][1] => 0);
     }
     $rand = mt_rand();
     echo "<br><table class='tab_cadre' width='90%'>";
     echo "<tr class='tab_bg_1'><th>";
     echo "<a href=\"javascript:showHideDiv('view_datas{$rand}','viewimg','" . $CFG_GLPI["root_doc"] . "/pics/deplier_down.png','" . $CFG_GLPI["root_doc"] . "/pics/deplier_up.png');\">";
     if ($show_graph) {
         $img = "deplier_down.png";
     } else {
         $img = "deplier_up.png";
     }
     echo "<img alt='' name='viewimg' src=\"" . $CFG_GLPI["root_doc"] . "/pics/{$img}\">&nbsp;";
     echo $LANG['plugin_mreporting']["export"][2];
     echo "</a>";
     echo "</th>";
     echo "</tr>";
     echo "</table>";
     if ($show_graph) {
         $visibility = "display:none;";
     } else {
         $visibility = "display:inline;";
     }
     echo "<div align='center' style='" . $visibility . "' id='view_datas{$rand}'>";
     echo "<table class='tab_cadre' width='90%'>";
     echo "<tr class='tab_bg_1'>";
     if (!$treedatas) {
         echo "<th></th>";
     }
     foreach ($datas as $label => $cols) {
         if (!empty($labels2)) {
             echo "<th>" . $labels2[$label] . "</th>";
         } else {
             echo "<th>" . $label . "</th>";
         }
     }
     echo "</tr>";
     if ($treedatas) {
         echo "<tr class='tab_bg_1'>";
         self::showGraphTreeDatas($types, $flip_data);
         echo "</tr>";
     } else {
         foreach ($types as $label2 => $cols) {
             echo "<tr class='tab_bg_1'>";
             echo "<td>" . $label2 . "</td>";
             if ($simpledatas) {
                 //simple array
                 echo "<td class='center'>" . $cols . " " . $unit . "</td>";
             } else {
                 if ($treedatas) {
                     //multiple array
                     self::showGraphTreeDatas($cols, $flip_data);
                 } else {
                     //multiple array
                     foreach ($cols as $date => $nb) {
                         if (!is_array($nb)) {
                             echo "<td class='center'>" . $nb . " " . $unit . "</td>";
                         }
                     }
                 }
             }
             echo "</tr>";
         }
     }
     echo "</table>";
     echo "</div><br>";
 }