CMS  Version 3.9
pie_chart.inc
Go to the documentation of this file.
1 <?php
7 /**************************************************************
8 
9  Copyright (c) 2010 Sonjara, Inc
10 
11  Permission is hereby granted, free of charge, to any person
12  obtaining a copy of this software and associated documentation
13  files (the "Software"), to deal in the Software without
14  restriction, including without limitation the rights to use,
15  copy, modify, merge, publish, distribute, sublicense, and/or sell
16  copies of the Software, and to permit persons to whom the
17  Software is furnished to do so, subject to the following
18  conditions:
19 
20  The above copyright notice and this permission notice shall be
21  included in all copies or substantial portions of the Software.
22 
23  Except as contained in this notice, the name(s) of the above
24  copyright holders shall not be used in advertising or otherwise
25  to promote the sale, use or other dealings in this Software
26  without prior written authorization.
27 
28  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
30  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
32  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
33  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
35  OTHER DEALINGS IN THE SOFTWARE.
36 
37 *****************************************************************/
38 
39 Fakoli::using("svg_charts");
40 
41 class PieChart
42 {
43  var $width;
44  var $height;
47  var $cx;
48  var $cy;
49  var $values;
50  var $labels;
51  var $palette;
52  var $labelSize = 12;
53  var $strokeWidth = 1;
54  var $animate = true;
55  var $shadow = true;
56  var $emboss = false;
57  var $legend = false;
58  var $legendX = 0;
59  var $legendY = 0;
62  var $percentages = true;
63  var $percentagesSize = 16;
65  var $onSectorOver = "null";
66  var $onSectorOut = "null";
67  var $onSectorClick = "null";
68  var $onLegendOver = "null";
69  var $onLegendOut = "null";
70  var $onLegendClick = "null";
71  var $onSelectionChanged = "null";
72  var $onDrawChart = "null";
73  var $onDrawChartComplete = "null";
74  var $onDrawLegend = "null";
75  var $onDrawLegendComplete = "null";
76  var $scaleToFit = false;
77  var $fixedSize = true;
78  var $fontFamily = 'Arial, Helvetica';
79  var $canvasBackground = "#FFFFFF";
80  var $selectable = false;
81  var $animateSelection = false;
82  var $preselected = null;
83  var $title = "";
84  var $titleX = 300;
85  var $titleY = 25;
86  var $titleSize = 14;
87 
88  function PieChart($id, $width = 300, $height = 300, $values = null, $labels = null, $palette = "standard")
89  {
90  $this->id = $id;
91  $this->setSize($width, $height);
92  $this->setContainerSize($width, $height);
93  $this->values = $values;
94  $this->labels = $labels;
95  $this->palette = $palette;
96  }
97 
98  function setSize($width, $height)
99  {
100  $this->width = $width;
101  $this->height = $height;
102  if (!$this->cx) $this->cx = $this->width / 2;
103  if (!$this->cy) $this->cy = $this->height / 2;
104  if (!$this->radius) $this->radius = min($width, $height) * .4;
105  }
106 
107  function setContainerSize($width, $height = "0")
108  {
109  $this->containerWidth = $width;
110  $this->containerHeight = $height;
111  }
112 
113  function setCenter($cx, $cy)
114  {
115  $this->cx = $cx;
116  $this->cy = $cy;
117  }
118 
119  function setRadius($r)
120  {
121  $this->radius = $r;
122  }
123 
124  function setLabelSize($size)
125  {
126  $this->labelSize = $size;
127  }
128 
129  function setStrokeWidth($s)
130  {
131  $this->strokeWidth = $s;
132  }
133 
134  function setFontFamily($font)
135  {
136  $this->fontFamily = $font;
137  }
138 
139  function showLegend($show, $x = 0, $y = 0)
140  {
141  $this->legend = $show;
142  $this->legendX = $x;
143  $this->legendY = $y;
144  }
145 
146  function showPercentages($show, $size = 16, $distance = 0.75)
147  {
148  $this->percentages = $show;
149  $this->percentagesSize = $size;
150  $this->percentagesDistance = $distance;
151  }
152 
153  function setTitle($title, $x = 300, $y = 25, $size = 14)
154  {
155  $this->title = $title;
156  $this->titleX = $x;
157  $this->titleY = $y;
158  $this->titleSize = $size;
159  }
160 
161  function draw()
162  {
163  global $script;
164  $values = implode(", ", $this->values);
165  $labels = "'".implode("', '", $this->labels) . "'";
166  $animate = $this->animate ? "true" : "false";
167  $shadow = $this->shadow ? "true": "false";
168  $emboss = $this->emboss ? "true" : "false";
169  $legend = $this->legend ? "true" : "false";
170  $percentages = $this->showPercentages ? "true" : "false";
171  $download = $this->enableDownload ? "true" : "false";
172  $selectable = $this->selectable ? "true": "false";
173  $animateSelection = $this->animateSelection ? "true" : "false";
174 
175  if (is_array($this->preselected))
176  {
177  $preselected = ",\n\t\tpreselected: [".implode(",", $this->preselected)."]";
178  }
179  else
180  {
181  $preselected = "";
182  }
183 
184  $containerWidth = is_numeric($this->containerWidth) ? $this->containerWidth . "px" : $this->containerWidth;
185  $containerHeight = is_numeric($this->containerHeight) ? $this->containerHeight . "px" : $this->containerHeight;
186 
187  if ($this->title)
188  {
189  $titleOptions = "\n\t\ttitle: '".jsSafe($this->title)."',\n\t\ttitlex: {$this->titleX},\n\t\ttitley: {$this->titleY},\n\t\ttitleSize: {$this->titleSize},\n";
190  }
191 
192  $script .= <<<ENDSCRIPT
193 <script type="text/javascript">
194 window.addEvent('domready', function ()
195 {
196  var {$this->id} = new PieChart('{$this->id}',
197  {{$titleOptions}
198  width: {$this->width},
199  height: {$this->height},
200  cx: {$this->cx},
201  cy: {$this->cy},
202  radius: {$this->radius},
203  palette: '{$this->palette}',
204  labelSize: {$this->labelSize},
205  fontFamily: '{$this->fontFamily}',
206  strokeWidth: {$this->strokeWidth},
207  animate: {$animate},
208  shadow: {$shadow},
209  emboss: {$emboss},
210  legend: {$legend},
211  legendX: {$this->legendX},
212  legendY: {$this->legendY},
213  legendSwatchSize: {$this->legendSwatchSize},
214  legendLineHeight: {$this->legendLineHeight},
215  percentages: {$percentages},
216  percentagesSize: {$this->percentagesSize},
217  onSectorOver: {$this->onSectorOver},
218  onSectorOut: {$this->onSectorOut},
219  onSectorClick: {$this->onSectorClick},
220  onLegendOver: {$this->onLegendOver},
221  onLegendOut: {$this->onLegendOut},
222  onLegendClick: {$this->onLegendClick},
223  onDrawChart: {$this->onDrawChart},
224  onDrawChartComplete: {$this->onDrawChartComplete},
225  onDrawLegend: {$this->onDrawLegend},
226  onDrawLegendComplete: {$this->onDrawLegendComplete},
227  onSelectionChanged: {$this->onSelectionChanged},
228  enableDownload: {$download},
229  canvasBackground: '{$this->canvasBackground}',
230  selectable: {$selectable}{$preselected},
231  animateSelection: {$animateSelection}
232  });
233  {$this->id}.values = [ $values ];
234  {$this->id}.labels = [ $labels ];
235 
236  {$this->id}.draw();
237 });
238 </script>
239 ENDSCRIPT;
240 
241  if ($this->scaleToFit)
242  {
243  $dimensions = "width: 100%; height: auto";
244  }
245  else if ($this->fixedSize)
246  {
247  $dimensions = "width: {$containerWidth}; height: {$containerHeight};";
248  }
249  else
250  {
251  $dimensions = "";
252  }
253 
254  echo "<div id='{$this->id}' style='$dimensions'></div>";
255  }
256 }?>
$helpTree width
Definition: tree.inc:45
$size
Definition: download.inc:47
$bookmark title
static using()
Import the datamodels, views and manifest for the specified component(s).
Definition: core.inc:116
$containerWidth
Definition: pie_chart.inc:45
setCenter($cx, $cy)
Definition: pie_chart.inc:113
$onSectorClick
Definition: pie_chart.inc:67
setFontFamily($font)
Definition: pie_chart.inc:134
$onSelectionChanged
Definition: pie_chart.inc:71
$legendLineHeight
Definition: pie_chart.inc:61
setLabelSize($size)
Definition: pie_chart.inc:124
setRadius($r)
Definition: pie_chart.inc:119
$containerHeight
Definition: pie_chart.inc:46
$animateSelection
Definition: pie_chart.inc:81
showPercentages($show, $size=16, $distance=0.75)
Definition: pie_chart.inc:146
$onDrawLegendComplete
Definition: pie_chart.inc:75
PieChart($id, $width=300, $height=300, $values=null, $labels=null, $palette="standard")
Definition: pie_chart.inc:88
$legendSwatchSize
Definition: pie_chart.inc:60
setTitle($title, $x=300, $y=25, $size=14)
Definition: pie_chart.inc:153
setContainerSize($width, $height="0")
Definition: pie_chart.inc:107
$percentagesSize
Definition: pie_chart.inc:63
$percentagesDistance
Definition: pie_chart.inc:64
$onLegendClick
Definition: pie_chart.inc:70
$onDrawChartComplete
Definition: pie_chart.inc:73
showLegend($show, $x=0, $y=0)
Definition: pie_chart.inc:139
$canvasBackground
Definition: pie_chart.inc:79
setStrokeWidth($s)
Definition: pie_chart.inc:129
setSize($width, $height)
Definition: pie_chart.inc:98
$desc height
Definition: event_edit.inc:64
$dial scaleToFit
Definition: test_dial.inc:12
$dial shadow
Definition: test_dial.inc:11
$pie emboss
Definition: test_pie.inc:10