MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
mchart.class
Ir para a documentação deste ficheiro.
1<?php
2
26class MChart extends MDiv
27{
31 const TYPE_AREA = 'CategoryAxisRenderer';
32 const TYPE_BAR = 'BarRenderer';
33 const TYPE_DONUT = 'DonutRenderer';
34 const TYPE_LINE = 'Line';
35 const TYPE_PIE = 'PieRenderer';
36
37 const DEFAULT_STRING_FORMAT = '%.2f';
38
46 public $conf;
47
51 private $multipleData = array();
52
56 private $type;
57
61 private $replaces = array();
62
71 public function __construct($id, $data=array(), $title='', $type=NULL)
72 {
73 parent::__construct($id);
74
75 $this->page->addScriptForOlderIEs('jquery/excanvas.min.js');
76 $this->page->addScript('jquery/jquery.min.js');
77 $this->page->addScript('jquery/jquery.jqplot.min.js');
78 $this->page->addScript('jquery/plugins/jqplot.canvasTextRenderer.min.js');
79 $this->page->addScript('jquery/plugins/jqplot.enhancedLegendRenderer.min.js');
80
81 $this->page->addStyleURL('scripts/jquery/jquery.jqplot.min.css');
82 $this->page->addStyleURL('scripts/jquery/jquery.jqplot.miolo.css');
83
84
85 $this->conf = new stdClass();
86 $this->setMultipleData(array( $data ));
87
88 $this->conf->title = utf8_encode($title);
89 $this->conf->seriesDefaults->rendererOptions->showDataLabels = TRUE;
90 $this->conf->seriesDefaults->rendererOptions->sliceMargin = 5;
91 $this->conf->seriesDefaults->rendererOptions->shadowOffset = 0.5;
92 $this->conf->seriesDefaults->rendererOptions->shadowAlpha = 0.05;
93 $this->conf->seriesDefaults->rendererOptions->shadowDepth = 8;
94 $this->conf->seriesDefaults->rendererOptions->animation->show = true;
95 $this->conf->grid->drawBorder = FALSE;
96
97 $this->conf->seriesColors = array(
98 // Highcharts colors
99 '#4572A7', '#AA4643', '#89A54E', '#80699B', '#3D96AE', '#DB843D', '#92A8CD', '#A47D7C', '#B5CA92',
100 // Kuler colors
101 '#00A695', '#FF3300', '#FF6600', '#FFA600', '#FFD900',
102 );
103
104 $this->setType($type);
105
106 $this->setWidth('450px');
107 $this->setHeight('400px');
108 }
109
113 public function setWidth($width)
114 {
115 $this->addStyle('width', $width);
116 }
117
121 public function setHeight($height)
122 {
123 $this->addStyle('height', $height);
124 }
125
129 public function setBarWidth($width)
130 {
131 $this->conf->seriesDefaults->rendererOptions->barWidth = $width;
132 }
133
137 public function setType($type)
138 {
139 $this->type = $type;
140
141 switch ( $type )
142 {
143 case self::TYPE_AREA:
144 $this->setXAsCategory();
145 $this->conf->stackSeries = TRUE;
146 $this->conf->seriesDefaults->fill = TRUE;
147 break;
148
149 case self::TYPE_BAR:
150 $this->page->addScript('jquery/plugins/jqplot.barRenderer.min.js');
151 $this->replaces[] = $this->conf->seriesDefaults->renderer = 'jQuery.jqplot.BarRenderer';
152
153 $this->displayPointLabels();
154 break;
155
156 case self::TYPE_DONUT:
157 $this->replaces[] = $this->conf->seriesDefaults->renderer = "jQuery.jqplot.$type";
158
159 $this->page->addScript('jquery/plugins/jqplot.donutRenderer.min.js');
160 $this->conf->seriesDefaults->rendererOptions->dataLabelFormatString = self::DEFAULT_STRING_FORMAT . '%';
161
162 $this->displayLegend();
163 $this->displayHints();
164 break;
165
166 case self::TYPE_PIE:
167 $this->replaces[] = $this->conf->seriesDefaults->renderer = "jQuery.jqplot.$type";
168
169 $this->page->addScript('jquery/plugins/jqplot.pieRenderer.min.js');
170 $this->conf->seriesDefaults->rendererOptions->dataLabelFormatString = self::DEFAULT_STRING_FORMAT . '%';
171
172 $this->displayLegend();
173 $this->displayHints();
174 break;
175
176 default:
177 $this->page->addScript('jquery/plugins/jqplot.canvasAxisLabelRenderer.min.js');
178 $this->page->addScript('jquery/plugins/jqplot.highlighter.min.js');
179 $this->conf->highlighter->show = TRUE;
180 $this->conf->highlighter->sizeAdjust = 10;
181
182 $this->conf->axes->xaxis->tickOptions->formatString = self::DEFAULT_STRING_FORMAT;
183 $this->conf->axes->yaxis->tickOptions->formatString = '%.2f';
184 break;
185 }
186 }
187
191 public function setXAsCategory()
192 {
193 $this->page->addScript('jquery/plugins/jqplot.categoryAxisRenderer.min.js');
194 $this->replaces[] = $this->conf->axes->yaxis->renderer = 'jQuery.jqplot.CategoryAxisRenderer';
195 }
196
200 public function setYAsCategory()
201 {
202 $this->page->addScript('jquery/plugins/jqplot.categoryAxisRenderer.min.js');
203 $this->replaces[] = $this->conf->axes->yaxis->renderer = 'jQuery.jqplot.CategoryAxisRenderer';
204 }
205
209 public function setXTicks($ticks)
210 {
211 $this->setXAsCategory();
212 $this->conf->axes->xaxis->ticks = $ticks;
213 }
214
218 public function setYTicks($ticks)
219 {
220 $this->setYAsCategory();
221 $this->conf->axes->yaxis->ticks = $ticks;
222 }
223
227 public function displayPointLabels()
228 {
229 $this->page->addScript('jquery/plugins/jqplot.pointLabels.min.js');
230 $this->conf->seriesDefaults->pointLabels->show = TRUE;
231 }
232
236 public function displayLegend($display=TRUE)
237 {
238 $this->replaces[] = $this->conf->legend->renderer = 'jQuery.jqplot.EnhancedLegendRenderer';
239
240 $this->conf->legend->show = $display;
241 $this->conf->legend->location = 'e';
242 $this->conf->legend->rendererOptions->seriesToggle = 'normal';
243 $this->conf->legend->fontSize = '11px';
244 }
245
249 public function displayHints()
250 {
251 // Show hint with label
252 $jsCode = <<<JS
253\$("#$this->id").bind('jqplotDataHighlight', function(ev, seriesIndex, pointIndex, data) {
254 var \$this = \$(this);
255 \$this.attr('title', data[0]);
256});
257
258\$("#$this->id").bind('jqplotDataUnhighlight', function(ev, seriesIndex, pointIndex, data) {
259 var \$this = \$(this);
260 \$this.attr('title',"");
261});
262JS;
263 $this->page->onload($jsCode);
264 }
265
269 public function addData($data)
270 {
271 $this->multipleData[] = $data;
272 }
273
277 public function getMultipleData()
278 {
279 return $this->multipleData;
280 }
281
287 public function setMultipleData($multipleData)
288 {
289 // FIXME: jqPlot does not support ISO-8859-1 strings
290 foreach ( (array) $multipleData as $dataKey => $data )
291 {
292 foreach ( (array) $data as $lineKey => $line )
293 {
294 if ( is_array($line) )
295 {
296 foreach ( $line as $itemKey => $item )
297 {
298 if( is_numeric($item) )
299 {
300 $multipleData[$dataKey][$lineKey][$itemKey] = floatval($item);
301 }
302 elseif ( is_string($item) )
303 {
304 $multipleData[$dataKey][$lineKey][$itemKey] = utf8_encode($item);
305 }
306 }
307 }
308 elseif( is_numeric($line) )
309 {
310 $multipleData[$dataKey][$lineKey] = floatval($line);
311 }
312 elseif ( is_string($line) )
313 {
314 $multipleData[$dataKey][$lineKey] = utf8_encode($line);
315 }
316 }
317 }
318
319 $this->multipleData = $multipleData;
320 }
321
325 public function generate()
326 {
327 $jsonData = json_encode($this->multipleData);
328 $jsonConf = json_encode($this->conf);
329
330 // Replace some strings that must be JavaScript objects or functions
331 foreach ( $this->replaces as $replace )
332 {
333 $jsonConf = str_replace("\"$replace\"", $replace, $jsonConf);
334 }
335
336 $this->page->onload("$(document).ready(function () { $.jqplot.sprintf.decimalMark = ','; $.jqplot('$this->id', $jsonData, $jsonConf); });");
337
338 return parent::generate();
339 }
340}
341
342?>
setYAsCategory()
Definição mchart.class:200
const TYPE_LINE
Definição mchart.class:34
const TYPE_AREA
Definição mchart.class:31
setHeight($height)
Definição mchart.class:121
setXAsCategory()
Definição mchart.class:191
__construct($id, $data=array(), $title='', $type=NULL)
Definição mchart.class:71
const TYPE_DONUT
Definição mchart.class:33
setYTicks($ticks)
Definição mchart.class:218
setBarWidth($width)
Definição mchart.class:129
addData($data)
Definição mchart.class:269
generate()
Definição mchart.class:325
displayLegend($display=TRUE)
Definição mchart.class:236
setMultipleData($multipleData)
Definição mchart.class:287
getMultipleData()
Definição mchart.class:277
const TYPE_PIE
Definição mchart.class:35
displayPointLabels()
Definição mchart.class:227
const DEFAULT_STRING_FORMAT
Definição mchart.class:37
const TYPE_BAR
Definição mchart.class:32
setWidth($width)
Definição mchart.class:113
setType($type)
Definição mchart.class:137
setXTicks($ticks)
Definição mchart.class:209
displayHints()
Definição mchart.class:249
addStyle($name, $value)
Definição mcontrol.class:427
$title
Definição base.php:3