CMS  Version 3.9
histogram.inc
Go to the documentation of this file.
1 <?php
9 {
10  var $type;
11  var $title;
12  var $values;
13  var $options = array();
14  var $onClick;
15  var $tooltips = array();
16 
18  {
19  $this->type = $type;
20  $this->title = $title;
21  $this->values = $values;
22  }
23 
24  function set($attr, $val)
25  {
26  $this->options[$attr] = $val;
27  return $this;
28  }
29 
30  function getOptions()
31  {
32  $options = array();
33  foreach ($this->options as $opt => $val)
34  {
35  if ($val === false) $val = "false";
36  if ($val === true) $val = "true";
37 
38  $options[] = "'$opt': $val";
39  }
40 
41  if ($this->onClick)
42  {
43  $options[] = "'onClick': {$this->onClick} ";
44  }
45 
46  if (count($this->tooltips))
47  {
48  $tooltips = "['".implode("', '", array_map(jsSafe, $this->tooltips))."']";
49  $options[] = "'toolTips': $tooltips";
50  }
51 
52  $options = "{".implode(", ", $options)."}";
53 
54  return $options;
55  }
56 
57  function toScript()
58  {
59  $v = array();
60 
61  foreach($this->values as $value)
62  {
63  if ($value === "" || !isset($value)) $value = "null";
64  $v[] = $value;
65  }
66 
67  $values = implode(", ", $v);
68  $options = $this->getOptions();
69 
70  $out = "new HistogramSeries('{$this->type}', '" . jsSafe($this->title) . "', [{$values}], $options)";
71  return $out;
72  }
73 
74  function showTooltips($tooltips = null)
75  {
76  if ($tooltips)
77  {
78  $this->tooltips = $tooltips;
79  }
80  else
81  {
82  $this->tooltips = $this->values;
83  }
84  }
85 }
86 
88 {
89  var $type;
90  var $title;
91  var $children = array();
92 
94  {
95  $this->type = $type;
96  $this->title = $title;
97  }
98 
99  function addSeries($series)
100  {
101  $this->children[] = $series;
102  }
103 
104  function toScript()
105  {
106  $options = $this->getOptions();
107  $out = "(new GroupedHistogramSeries('{$this->type}', '". jsSafe($this->title) ."', $options))";
108  foreach($this->children as $child)
109  {
110  $kid = $child->toScript();
111  $out .= ".addSeries({$kid})";
112  }
113 
114  return $out;
115  }
116 }
117 
119 {
120  var $type;
121  var $title;
122  var $children = array();
123 
125  {
126  $this->type = $type;
127  $this->title = $title;
128  }
129 
130  function addSeries($series)
131  {
132  $this->children[] = $series;
133  }
134 
135  function toScript()
136  {
137  $options = $this->getOptions();
138  $out = "(new StackedHistogramSeries('{$this->type}', '". jsSafe($this->title) ."', $options))";
139  foreach($this->children as $child)
140  {
141  $kid = $child->toScript();
142  $out .= ".addSeries({$kid})";
143  }
144 
145  return $out;
146  }
147 }
148 
149 
151 {
153  var $width;
154  var $height;
155  var $orientation = "vertical";
163  var $chartBackground = "#ddd";
164  var $palette;
165  var $labelSize = 12;
166  var $labelAngle = 0;
167  var $labelAnchor = "middle";
168  var $labelOffset = 20;
169  var $strokeWidth = 1;
170  var $animate = false;
171  var $shadow = true;
172  var $emboss = false;
173  var $legend = false;
174  var $legendX = 0;
175  var $legendY = 0;
178  var $xAxisTitle = "";
179  var $yAxisTitle = "";
180  var $series = array();
181  var $ticks = 10;
182  var $max = 0;
183  var $min = 0;
184  var $columnMargin = 0.2;
185  var $morphing = false;
186  var $startIndex = 0;
187  var $yAxisLabels = array();
188  var $title = "";
189  var $titleSize = 20;
190  var $enableDownload = true;
191  var $canvasBackground = "#FFFFFF";
192  var $units = "";
193  var $scaleToFit = false;
194  var $onDrawGrid = "null";
195  var $onDrawGridComplete = "null";
196  var $onDrawChart = "null";
197  var $onDrawChartComplete = "null";
198  var $onDrawLegend = "null";
199  var $onDrawLegendComplete = "null";
201  var $showGrid = true;
203 
204  var $caption = "";
205  var $captionX = 10;
206  var $captionY = 300;
207  var $captionWidth = 280;
208  var $captionAttributes = array();
209 
210  function __construct($id, $width = 300, $height = 300, $chartLeft = 25, $chartTop = 25, $chartWidth = 250, $chartHeight = 250, $palette = "standard")
211  {
212  $this->id = $id;
213  $this->setSize($width, $height);
214  $this->setContainerSize($width, $height);
215  $this->positionChart($chartLeft, $chartTop, $chartWidth, $chartHeight);
216 
217  $this->palette = $palette;
218  }
219 
220  function setSize($width, $height)
221  {
222  $this->width = $width;
223  $this->height = $height;
224  }
225 
226  function setContainerSize($width, $height = "0")
227  {
228  $this->containerWidth = $width;
229  $this->containerHeight = $height;
230  }
231 
232  function positionChart($left, $top, $width, $height)
233  {
234  $this->chartLeft = $left;
235  $this->chartTop = $top;
236  $this->chartWidth = $width;
237  $this->chartHeight = $height;
238  }
239 
240  function setLabels($labels)
241  {
242  $this->labels = $labels;
243  }
244 
245  function setLabelTooltips($labelTooltips)
246  {
247  $this->labelTooltips = $labelTooltips;
248  }
249 
250  function setLabelDecorations($labelDecorations)
251  {
252  $this->labelDecorations = $labelDecorations;
253  }
254 
255  function setLabelSize($size)
256  {
257  $this->labelSize = $size;
258  }
259 
260  function setYAxisLabels($labels)
261  {
262  $this->yAxisLabels = $labels;
263  }
264 
265  function setStrokeWidth($s)
266  {
267  $this->strokeWidth = $s;
268  }
269 
271  {
272  $this->palette = $palette;
273  }
274 
275  function showLegend($show, $x = 0, $y = 0)
276  {
277  $this->legend = $show;
278  $this->legendX = $x;
279  $this->legendY = $y;
280  }
281 
282  function addSeries($series)
283  {
284  $this->series[] = $series;
285  }
286 
287  function autoScale()
288  {
289  $max = 0;
290  $min = 0;
291 
292  foreach($this->series as $series)
293  {
294  foreach($series->values as $value)
295  {
296  if ($max === null || $value > $max) $max = $value;
297  if ($min === null || $value < $min) $min = $value;
298  }
299  }
300 
301  $logMax = intval(log10($max));
302  $logMin = intval(log10($min));
303  $log = max($logMax, $logMin);
304 
305  $denom = pow(10, $log);
306 
307  if ($max != 0)
308  {
309  $max = floor(($max / $denom) + 1) * $denom;
310  }
311 
312  if ($min != 0)
313  {
314  $min = floor(($min / $denom) - 1) * $denom;
315  }
316 
317  $this->max = $max;
318  $this->min = $min;
319  }
320 
321  function draw()
322  {
323  global $script;
324 
325  $labels = "'".implode("', '", $this->labels) . "'";
326  $labelTooltips = "'".implode("', '", $this->labelTooltips) . "'";
327 
328  if ($this->labelDecorations)
329  {
330  $decorations = array();
331  foreach($this->labelDecorations as $decoration)
332  {
333  $decorations[] = json_encode($decoration);
334  }
335 
336  $labelDecorations = implode(",", $decorations);
337  }
338 
339  //$labelTooltips = $labels;
340 
341  if (count($this->yAxisLabels))
342  {
343  $yaxis = "'".implode("', '", $this->yAxisLabels) . "'";
344  $yaxis = "\t{$this->id}.yAxisLabels = [ $yaxis ];";
345  }
346 
347  $animate = $this->animate ? "true" : "false";
348  $shadow = $this->shadow ? "true": "false";
349  $emboss = $this->emboss? "true" : "false";
350  $legend = $this->legend ? "true" : "false";
351  $enableDownload = $this->enableDownload ? "true" : "false";
352  $showGrid = $this->showGrid ? "true" : "false";
353 
354  $containerWidth = is_numeric($this->containerWidth) ? $this->containerWidth . "px" : $this->containerWidth;
355  $containerHeight = is_numeric($this->containerHeight) ? $this->containerHeight . "px" : $this->containerHeight;
356 
357  $data = "";
358 
359  foreach($this->series as $s)
360  {
361  $data .= "\t{$this->id}.addSeries(".$s->toScript().");\n";
362  }
363 
364  $jsClass = $this->morphing ? "MorphingHistogram" : "Histogram";
365 
366  if ($this->caption)
367  {
368  $caption = jsSafe($this->caption);
369  $captionAttrs = json_encode($this->captionAttributes);
370  $captionBlock = <<<ENDSCRIPT
371 ,
372  caption: '{$caption}',
373  captionX: {$this->captionX},
374  captionY: {$this->captionY},
375  captionWidth: {$this->captionWidth},
376  captionAttributes: {$captionAttrs}
377 ENDSCRIPT;
378  }
379 
380  $title = jsSafe($this->title);
381 
382  $script .= <<<ENDSCRIPT
383 <script type="text/javascript">
384 window.addEvent('domready', function ()
385 {
386  var {$this->id} = new $jsClass('{$this->id}',
387  {
388  width: {$this->width},
389  height: {$this->height},
390  orientation: '{$this->orientation}',
391  chartLeft: {$this->chartLeft},
392  chartTop: {$this->chartTop},
393  chartWidth: {$this->chartWidth},
394  chartHeight: {$this->chartHeight},
395  chartBackground: '{$this->chartBackground}',
396  palette: '{$this->palette}',
397  labelSize: {$this->labelSize},
398  labelAngle: {$this->labelAngle},
399  labelAnchor: '{$this->labelAnchor}',
400  labelOffset: {$this->labelOffset},
401  emptyValueLabel: '{$this->emptyValueLabel}',
402  strokeWidth: {$this->strokeWidth},
403  gridStrokeWidth: {$this->gridStrokeWidth},
404  showGrid: {$showGrid},
405  animate: {$animate},
406  shadow: {$shadow},
407  emboss: {$emboss},
408  legend: {$legend},
409  legendX: {$this->legendX},
410  legendY: {$this->legendY},
411  legendSwatchSize: {$this->legendSwatchSize},
412  legendLineHeight: {$this->legendLineHeight},
413  ticks: {$this->ticks},
414  max: {$this->max},
415  min: {$this->min},
416  startIndex: {$this->startIndex},
417  columnMargin: {$this->columnMargin},
418  title: '{$title}',
419  titleSize: {$this->titleSize},
420  enableDownload: {$enableDownload},
421  canvasBackground: '{$this->canvasBackground}',
422  units: '{$this->units}',
423  onDrawGrid: {$this->onDrawGrid},
424  onDrawGridComplete: {$this->onDrawGridComplete},
425  onDrawChart: {$this->onDrawChart},
426  onDrawChartComplete: {$this->onDrawChartComplete},
427  onDrawLegend: {$this->onDrawLegend},
428  onDrawLegendComplete: {$this->onDrawLegendComplete}{$captionBlock}
429  });
430 
431  {$this->id}.labels = [ {$labels} ];
432  {$this->id}.labelTooltips = [ {$labelTooltips} ];
433  {$this->id}.labelDecorations = [ {$labelDecorations} ];
434 
435 {$yaxis}
436 {$data}
437  {$this->id}.draw();
438 });
439 </script>
440 ENDSCRIPT;
441 
442  if ($this->scaleToFit)
443  {
444  $dimensions = "width: 100%; height: auto";
445  }
446  else
447  {
448  $dimensions = "width: {$containerWidth}; height: {$containerHeight};";
449  }
450 
451  $cssClass = ($this->cssClass) ? " class='{$this->cssClass}'" : "";
452 
453  echo "<div id='{$this->id}'{$cssClass} style='{$this->cssStyles}'></div>";
454  }
455 }?>
$left
Definition: help.inc:41
$out
Definition: page.inc:66
$helpTree width
Definition: tree.inc:45
$size
Definition: download.inc:47
$bookmark title
__construct($type, $title)
Definition: histogram.inc:93
$onDrawLegendComplete
Definition: histogram.inc:199
setSize($width, $height)
Definition: histogram.inc:220
setLabelDecorations($labelDecorations)
Definition: histogram.inc:250
addSeries($series)
Definition: histogram.inc:282
positionChart($left, $top, $width, $height)
Definition: histogram.inc:232
setLabels($labels)
Definition: histogram.inc:240
setYAxisLabels($labels)
Definition: histogram.inc:260
setLabelSize($size)
Definition: histogram.inc:255
showLegend($show, $x=0, $y=0)
Definition: histogram.inc:275
$onDrawChartComplete
Definition: histogram.inc:197
$captionAttributes
Definition: histogram.inc:208
$onDrawGridComplete
Definition: histogram.inc:195
__construct($id, $width=300, $height=300, $chartLeft=25, $chartTop=25, $chartWidth=250, $chartHeight=250, $palette="standard")
Definition: histogram.inc:210
setContainerSize($width, $height="0")
Definition: histogram.inc:226
setPalette($palette)
Definition: histogram.inc:270
setStrokeWidth($s)
Definition: histogram.inc:265
setLabelTooltips($labelTooltips)
Definition: histogram.inc:245
showTooltips($tooltips=null)
Definition: histogram.inc:74
__construct($type, $title, $values)
Definition: histogram.inc:17
__construct($type, $title)
Definition: histogram.inc:124
$desc height
Definition: event_edit.inc:64
$image caption
$dial scaleToFit
Definition: test_dial.inc:12
$dial shadow
Definition: test_dial.inc:11
$series
$chart ticks
$chart2 orientation
$chart max
$chart morphing
$pie emboss
Definition: test_pie.inc:10