3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
36 require_once 'packages/OpenFlashChart/php-ofc-library/open-flash-chart.php';
39 * Build various graphs using Open Flash Chart library.
41 class CRM_Utils_OpenFlashChart
{
48 private static $_colours = array(
49 "#C3CC38", "#C8B935", "#CEA632", "#D3932F",
50 "#D9802C", "#FA6900", "#DC9B57", "#F78F01",
51 "#5AB56E", "#6F8069", "#C92200", "#EB6C5C",
55 * Build The Bar Gharph.
57 * @param array $params assoc array of name/value pairs
59 * @return object $chart object of open flash chart.
62 static function &barChart(&$params) {
67 if (!CRM_Utils_Array
::value('multiValues', $params)) {
68 $params['multiValues'] = array($params['values']);
71 $values = CRM_Utils_Array
::value('multiValues', $params);
72 if (!is_array($values) ||
empty($values)) {
76 // get the required data.
77 $chartTitle = CRM_Utils_Array
::value('legend', $params) ?
$params['legend'] : ts('Bar Chart');
79 $xValues = $yValues = array();
80 $xValues = array_keys($values[0]);
81 $yValues = array_values($values[0]);
83 //set y axis parameters.
86 // calculate max scale for graph.
87 $yMax = ceil(max($yValues));
88 if ($mod = $yMax %
(str_pad(5, strlen($yMax) - 1, 0))) {
89 $yMax +
= str_pad(5, strlen($yMax) - 1, 0) - $mod;
94 $config = CRM_Core_Config
::singleton();
95 $symbol = $config->defaultCurrencySymbol
;
96 foreach ($values as $barCount => $barVal) {
97 $bars[$barCount] = new bar_glass();
99 $yValues = array_values($barVal);
100 foreach ($yValues as &$yVal) {
101 // type casting is required for chart to render values correctly
102 $yVal = (double) $yVal;
104 $bars[$barCount]->set_values($yValues);
106 // FIXME: for bars > 2, we'll need to come out with other colors
107 $bars[$barCount]->colour( '#BF3B69');
110 if ($barKey = CRM_Utils_Array
::value($barCount, CRM_Utils_Array
::value('barKeys', $params))) {
111 $bars[$barCount]->key($barKey,12);
114 // call user define function to handle on click event.
115 if ($onClickFunName = CRM_Utils_Array
::value('on_click_fun_name', $params)) {
116 $bars[$barCount]->set_on_click($onClickFunName);
119 // get the currency to set in tooltip.
120 $tooltip = CRM_Utils_Array
::value('tip', $params, "$symbol #val#");
121 $bars[$barCount]->set_tooltip($tooltip);
124 // create x axis label obj.
125 $xLabels = new x_axis_labels();
126 $xLabels->set_labels($xValues);
128 // set angle for labels.
129 if ($xLabelAngle = CRM_Utils_Array
::value('xLabelAngle', $params)) {
130 $xLabels->rotate($xLabelAngle);
133 // create x axis obj.
134 $xAxis = new x_axis();
135 $xAxis->set_labels($xLabels);
137 //create y axis and set range.
138 $yAxis = new y_axis();
139 $yAxis->set_range($yMin, $yMax, $ySteps);
141 // create chart title obj.
142 $title = new title($chartTitle);
145 $chart = new open_flash_chart();
147 // add x axis w/ labels to chart.
148 $chart->set_x_axis($xAxis);
150 // add y axis values to chart.
151 $chart->add_y_axis($yAxis);
153 // set title to chart.
154 $chart->set_title($title);
156 // add bar element to chart.
157 foreach ($bars as $bar) {
158 $chart->add_element($bar);
161 // add x axis legend.
162 if ($xName = CRM_Utils_Array
::value('xname', $params)) {
163 $xLegend = new x_legend($xName);
164 $xLegend->set_style("{font-size: 13px; color:#000000; font-family: Verdana; text-align: center;}");
165 $chart->set_x_legend($xLegend);
168 // add y axis legend.
169 if ($yName = CRM_Utils_Array
::value('yname', $params)) {
170 $yLegend = new y_legend($yName);
171 $yLegend->set_style("{font-size: 13px; color:#000000; font-family: Verdana; text-align: center;}");
172 $chart->set_y_legend($yLegend);
179 * Build The Pie Gharph.
181 * @param array $params assoc array of name/value pairs
183 * @return object $chart object of open flash chart.
186 static function &pieChart(&$params) {
188 if (empty($params)) {
191 $allValues = CRM_Utils_Array
::value('values', $params);
192 if (!is_array($allValues) ||
empty($allValues)) {
196 // get the required data.
198 foreach ($allValues as $label => $value) {
199 $values[] = new pie_value((double)$value, $label);
201 $graphTitle = CRM_Utils_Array
::value('legend', $params) ?
$params['legend'] : ts('Pie Chart');
204 $config = CRM_Core_Config
::singleton();
205 $symbol = $config->defaultCurrencySymbol
;
210 // call user define function to handle on click event.
211 if ($onClickFunName = CRM_Utils_Array
::value('on_click_fun_name', $params)) {
212 $pie->on_click($onClickFunName);
215 $pie->set_start_angle(35);
216 $pie->add_animation(new pie_fade());
217 $pie->add_animation(new pie_bounce(2));
220 $tooltip = CRM_Utils_Array
::value('tip', $params, "Amount is $symbol #val# of $symbol #total# <br>#percent#");
221 $pie->set_tooltip($tooltip);
224 $pie->set_colours(self
::$_colours);
226 $pie->set_values($values);
229 $chart = new open_flash_chart();
231 // create chart title obj.
232 $title = new title($graphTitle);
233 $chart->set_title($title);
235 $chart->add_element($pie);
236 $chart->x_axis
= NULL;
242 * Build The 3-D Bar Gharph.
244 * @param array $params assoc array of name/value pairs
246 * @return object $chart object of open flash chart.
249 static function &bar_3dChart(&$params) {
251 if (empty($params)) {
255 // $params['values'] should contains the values for each
256 // criteria defined in $params['criteria']
257 $values = CRM_Utils_Array
::value('values', $params);
258 $criterias = CRM_Utils_Array
::value('criteria', $params);
259 if (!is_array($values) ||
empty($values) ||
!is_array($criterias) ||
empty($criterias)) {
263 // get the required data.
264 $xReferences = $xValueLabels = $xValues = $yValues = array();
266 foreach ($values as $xVal => $yVal) {
267 if (!is_array($yVal) ||
empty($yVal)) {
271 $xValueLabels[] = (string)$xVal;
272 foreach ($criterias as $criteria) {
273 $xReferences[$criteria][$xVal] = (double)CRM_Utils_Array
::value($criteria, $yVal, 0);
274 $yValues[] = (double)CRM_Utils_Array
::value($criteria, $yVal, 0);
278 if (empty($xReferences)) {
285 $config = CRM_Core_Config
::singleton();
286 $symbol = $config->defaultCurrencySymbol
;
289 $tooltip = CRM_Utils_Array
::value('tip', $params, "$symbol #val#");
292 foreach ($xReferences as $criteria => $values) {
293 $toolTipVal = $tooltip;
294 // for seperate tooltip for each criteria
295 if (is_array($tooltip)) {
296 $toolTipVal = CRM_Utils_Array
::value($criteria, $tooltip, "$symbol #val#");
299 // create bar_3d object
300 $xValues[$count] = new bar_3d();
302 $xValues[$count]->set_colour(self
::$_colours[$count]);
303 // define colur pattel with bar criterias
304 $xValues[$count]->key((string)$criteria, 12);
305 // define bar chart values
306 $xValues[$count]->set_values(array_values($values));
309 $xValues[$count]->set_tooltip($toolTipVal);
313 $chartTitle = CRM_Utils_Array
::value('legend', $params) ?
$params['legend'] : ts('Bar Chart');
315 //set y axis parameters.
318 // calculate max scale for graph.
319 $yMax = ceil(max($yValues));
320 if ($mod = $yMax %
(str_pad(5, strlen($yMax) - 1, 0))) {
321 $yMax +
= str_pad(5, strlen($yMax) - 1, 0) - $mod;
324 // if max value of y-axis <= 0, then set default values
333 // create x axis label obj.
334 $xLabels = new x_axis_labels();
335 $xLabels->set_labels($xValueLabels);
337 // set angle for labels.
338 if ($xLabelAngle = CRM_Utils_Array
::value('xLabelAngle', $params)) {
339 $xLabels->rotate($xLabelAngle);
342 // create x axis obj.
343 $xAxis = new x_axis();
344 $xAxis->set_labels($xLabels);
346 //create y axis and set range.
347 $yAxis = new y_axis();
348 $yAxis->set_range($yMin, $yMax, $ySteps);
350 // create chart title obj.
351 $title = new title($chartTitle);
354 $chart = new open_flash_chart();
356 // add x axis w/ labels to chart.
357 $chart->set_x_axis($xAxis);
359 // add y axis values to chart.
360 $chart->add_y_axis($yAxis);
362 // set title to chart.
363 $chart->set_title($title);
365 foreach ($xValues as $bar) {
366 // add bar element to chart.
367 $chart->add_element($bar);
370 // add x axis legend.
371 if ($xName = CRM_Utils_Array
::value('xname', $params)) {
372 $xLegend = new x_legend($xName);
373 $xLegend->set_style("{font-size: 13px; color:#000000; font-family: Verdana; text-align: center;}");
374 $chart->set_x_legend($xLegend);
377 // add y axis legend.
378 if ($yName = CRM_Utils_Array
::value('yname', $params)) {
379 $yLegend = new y_legend($yName);
380 $yLegend->set_style("{font-size: 13px; color:#000000; font-family: Verdana; text-align: center;}");
381 $chart->set_y_legend($yLegend);
387 static function chart($rows, $chart, $interval) {
388 $chartData = $dateKeys = array();
392 foreach ($rows['receive_date'] as $key => $val) {
393 list($year, $month) = explode('-', $val);
394 $dateKeys[] = substr($rows['Month'][$key], 0, 3) . ' ' . $year;
396 $legend = ts('Monthly');
400 foreach ($rows['receive_date'] as $key => $val) {
401 list($year, $month) = explode('-', $val);
402 $dateKeys[] = 'Quarter ' . $rows['Quarter'][$key] . ' of ' . $year;
404 $legend = ts('Quarterly');
408 foreach ($rows['receive_date'] as $key => $val) {
409 list($year, $month) = explode('-', $val);
410 $dateKeys[] = 'Week ' . $rows['Week'][$key] . ' of ' . $year;
412 $legend = ts('Weekly');
416 foreach ($rows['receive_date'] as $key => $val) {
417 list($year, $month) = explode('-', $val);
420 $legend = ts('Yearly');
424 if (!empty($dateKeys)) {
426 if (!array_key_exists('multiValue', $rows)) {
427 $rows['multiValue'] = array($rows['value']);
429 foreach ($rows['multiValue'] as $key => $val) {
430 $graph[$key] = array_combine($dateKeys, $rows['multiValue'][$key]);
434 'legend' => "$legend " . CRM_Utils_Array
::value('legend', $rows, ts('Contribution')) . ' ' . ts('Summary'),
435 'values' => $graph[0],
436 'multiValues' => $graph,
437 'barKeys' => CRM_Utils_Array
::value('barKeys', $rows, array()),
441 // rotate the x labels.
442 $chartData['xLabelAngle'] = CRM_Utils_Array
::value('xLabelAngle', $rows, 0);
443 if (CRM_Utils_Array
::value('tip', $rows)) {
444 $chartData['tip'] = $rows['tip'];
448 $chartData['xname'] = CRM_Utils_Array
::value('xname', $rows);
449 $chartData['yname'] = CRM_Utils_Array
::value('yname', $rows);
451 // carry some chart params if pass.
453 'xSize', 'ySize', 'divName') as $f) {
454 if (CRM_Utils_Array
::value($f, $rows)) {
455 $chartData[$f] = $rows[$f];
459 return self
::buildChart($chartData, $chart);
462 static function reportChart($rows, $chart, $interval, &$chartInfo) {
463 foreach ($interval as $key => $val) {
464 $graph[$val] = $rows['value'][$key];
469 'legend' => $chartInfo['legend'],
470 'xname' => $chartInfo['xname'],
471 'yname' => $chartInfo['yname'],
474 // rotate the x labels.
475 $chartData['xLabelAngle'] = CRM_Utils_Array
::value('xLabelAngle', $chartInfo, 20);
476 if (CRM_Utils_Array
::value('tip', $chartInfo)) {
477 $chartData['tip'] = $chartInfo['tip'];
480 // carry some chart params if pass.
482 'xSize', 'ySize', 'divName') as $f) {
483 if (CRM_Utils_Array
::value($f, $rows)) {
484 $chartData[$f] = $rows[$f];
488 return self
::buildChart($chartData, $chart);
491 static function buildChart(&$params, $chart) {
492 $openFlashChart = array();
493 if ($chart && is_array($params) && !empty($params)) {
494 // build the chart objects.
495 $chartObj = CRM_Utils_OpenFlashChart
::$chart($params);
497 $openFlashChart = array();
499 // calculate chart size.
500 $xSize = CRM_Utils_Array
::value('xSize', $params, 400);
501 $ySize = CRM_Utils_Array
::value('ySize', $params, 300);
502 if ($chart == 'barChart') {
503 $ySize = CRM_Utils_Array
::value('ySize', $params, 250);
504 $xSize = 60 * count($params['values']);
505 //hack to show tooltip.
507 $xSize = (count($params['values']) > 1) ?
100 * count($params['values']) : 170;
509 elseif ($xSize > 600 && count($params['values']) > 1) {
510 $xSize = (count($params['values']) +
400 / count($params['values'])) * count($params['values']);
514 // generate unique id for this chart instance
515 $uniqueId = md5(uniqid(rand(), TRUE));
517 $openFlashChart["chart_{$uniqueId}"]['size'] = array('xSize' => $xSize, 'ySize' => $ySize);
518 $openFlashChart["chart_{$uniqueId}"]['object'] = $chartObj;
520 // assign chart data to template
521 $template = CRM_Core_Smarty
::singleton();
522 $template->assign('uniqueId', $uniqueId);
523 $template->assign("openFlashChartData", json_encode($openFlashChart));
527 return $openFlashChart;