1<?php 2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project 3// 4// All Rights Reserved. See copyright.txt for details and a complete list of authors. 5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details. 6// $Id$ 7 8require_once 'lib/graph-engine/core.php'; 9 10/** 11 * PieChartGraphic 12 * 13 * @uses Graphic 14 */ 15class PieChartGraphic extends Graphic 16{ 17 var $pie_data; 18 19 function __construct() 20 { 21 parent::__construct(); 22 $this->pie_data = []; 23 } 24 25 function getRequiredSeries() 26 { 27 return [ 28 'label' => false, 29 'value' => true, 30 'color' => false, 31 'style' => false 32 ]; 33 } 34 35 function _handleData($data) 36 { 37 $elements = count($data['value']); 38 39 if (! isset($data['color'])) { 40 $data['color'] = []; 41 for ($i = 0; $elements > $i; ++$i) { 42 $data['color'][] = $this->_getColor(); 43 } 44 } 45 46 if (! isset($data['style'])) { 47 for ($i = 0; $elements > $i; ++$i) { 48 $data['style'][] = 'FillStroke-' . $data['color'][$i]; 49 } 50 } 51 52 if (isset($data['label'])) { 53 foreach ($data['label'] as $key => $label) { 54 $this->addLegend($data['color'][$key], $label); 55 } 56 } 57 58 $total = array_sum($data['value']); 59 foreach ($data['value'] as $key => $value) { 60 if (is_numeric($value)) { 61 $this->pie_data[] = [$data['style'][$key], $value / $total * 360]; 62 } 63 } 64 65 return true; 66 } 67 68 function _drawContent(&$renderer) 69 { 70 $layout = $this->_layout(); 71 $centerX = $layout['pie-center-x']; 72 $centerY = $layout['pie-center-y']; 73 $radius = $layout['pie-radius']; 74 75 $base = 0; 76 77 foreach ($this->pie_data as $info) { 78 list($style, $degree) = $info; 79 $renderer->drawPie( 80 $centerX, 81 $centerY, 82 $radius, 83 $base, 84 $base + $degree, 85 $renderer->getStyle($style) 86 ); 87 88 $base += $degree; 89 } 90 } 91 92 function _drawLegendBox(&$renderer, $color) 93 { 94 $renderer->drawRectangle(0, 0, 1, 1, $renderer->getStyle("FillStroke-$color")); 95 } 96 97 function _default() 98 { 99 return array_merge( 100 parent::_default(), 101 [ 102 'pie-center-x' => 0.5, 103 'pie-center-y' => 0.5, 104 'pie-radius' => 0.4 105 ] 106 ); 107 } 108} 109