3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 public static function &barChart(&$params) {
67 if (empty($params['multiValues'])) {
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 = !empty($params['legend']) ?
$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 // set_labels function requires xValues array of string or x_axis_label
127 // so type casting array values to string values
128 array_walk($xValues, function(&$value, $index) {
129 $value = (string)$value;
131 $xLabels->set_labels($xValues);
133 // set angle for labels.
134 if ($xLabelAngle = CRM_Utils_Array
::value('xLabelAngle', $params)) {
135 $xLabels->rotate($xLabelAngle);
138 // create x axis obj.
139 $xAxis = new x_axis();
140 $xAxis->set_labels($xLabels);
142 //create y axis and set range.
143 $yAxis = new y_axis();
144 $yAxis->set_range($yMin, $yMax, $ySteps);
146 // create chart title obj.
147 $title = new title($chartTitle);
150 $chart = new open_flash_chart();
152 // add x axis w/ labels to chart.
153 $chart->set_x_axis($xAxis);
155 // add y axis values to chart.
156 $chart->add_y_axis($yAxis);
158 // set title to chart.
159 $chart->set_title($title);
161 // add bar element to chart.
162 foreach ($bars as $bar) {
163 $chart->add_element($bar);
166 // add x axis legend.
167 if ($xName = CRM_Utils_Array
::value('xname', $params)) {
168 $xLegend = new x_legend($xName);
169 $xLegend->set_style("{font-size: 13px; color:#000000; font-family: Verdana; text-align: center;}");
170 $chart->set_x_legend($xLegend);
173 // add y axis legend.
174 if ($yName = CRM_Utils_Array
::value('yname', $params)) {
175 $yLegend = new y_legend($yName);
176 $yLegend->set_style("{font-size: 13px; color:#000000; font-family: Verdana; text-align: center;}");
177 $chart->set_y_legend($yLegend);
184 * Build The Pie Gharph.
186 * @param array $params assoc array of name/value pairs
188 * @return object $chart object of open flash chart.
191 public static function &pieChart(&$params) {
193 if (empty($params)) {
196 $allValues = CRM_Utils_Array
::value('values', $params);
197 if (!is_array($allValues) ||
empty($allValues)) {
201 // get the required data.
203 foreach ($allValues as $label => $value) {
204 $values[] = new pie_value((double)$value, $label);
206 $graphTitle = !empty($params['legend']) ?
$params['legend'] : ts('Pie Chart');
209 $config = CRM_Core_Config
::singleton();
210 $symbol = $config->defaultCurrencySymbol
;
215 // call user define function to handle on click event.
216 if ($onClickFunName = CRM_Utils_Array
::value('on_click_fun_name', $params)) {
217 $pie->on_click($onClickFunName);
220 $pie->set_start_angle(35);
221 $pie->add_animation(new pie_fade());
222 $pie->add_animation(new pie_bounce(2));
225 $tooltip = CRM_Utils_Array
::value('tip', $params, "Amount is $symbol #val# of $symbol #total# <br>#percent#");
226 $pie->set_tooltip($tooltip);
229 $pie->set_colours(self
::$_colours);
231 $pie->set_values($values);
234 $chart = new open_flash_chart();
236 // create chart title obj.
237 $title = new title($graphTitle);
238 $chart->set_title($title);
240 $chart->add_element($pie);
241 $chart->x_axis
= NULL;
247 * Build The 3-D Bar Gharph.
249 * @param array $params assoc array of name/value pairs
251 * @return object $chart object of open flash chart.
254 public static function &bar_3dChart(&$params) {
256 if (empty($params)) {
260 // $params['values'] should contains the values for each
261 // criteria defined in $params['criteria']
262 $values = CRM_Utils_Array
::value('values', $params);
263 $criterias = CRM_Utils_Array
::value('criteria', $params);
264 if (!is_array($values) ||
empty($values) ||
!is_array($criterias) ||
empty($criterias)) {
268 // get the required data.
269 $xReferences = $xValueLabels = $xValues = $yValues = array();
271 foreach ($values as $xVal => $yVal) {
272 if (!is_array($yVal) ||
empty($yVal)) {
276 $xValueLabels[] = (string)$xVal;
277 foreach ($criterias as $criteria) {
278 $xReferences[$criteria][$xVal] = (double)CRM_Utils_Array
::value($criteria, $yVal, 0);
279 $yValues[] = (double)CRM_Utils_Array
::value($criteria, $yVal, 0);
283 if (empty($xReferences)) {
290 $config = CRM_Core_Config
::singleton();
291 $symbol = $config->defaultCurrencySymbol
;
294 $tooltip = CRM_Utils_Array
::value('tip', $params, "$symbol #val#");
297 foreach ($xReferences as $criteria => $values) {
298 $toolTipVal = $tooltip;
299 // for seperate tooltip for each criteria
300 if (is_array($tooltip)) {
301 $toolTipVal = CRM_Utils_Array
::value($criteria, $tooltip, "$symbol #val#");
304 // create bar_3d object
305 $xValues[$count] = new bar_3d();
307 $xValues[$count]->set_colour(self
::$_colours[$count]);
308 // define colur pattel with bar criterias
309 $xValues[$count]->key((string)$criteria, 12);
310 // define bar chart values
311 $xValues[$count]->set_values(array_values($values));
314 $xValues[$count]->set_tooltip($toolTipVal);
318 $chartTitle = !empty($params['legend']) ?
$params['legend'] : ts('Bar Chart');
320 //set y axis parameters.
323 // calculate max scale for graph.
324 $yMax = ceil(max($yValues));
325 if ($mod = $yMax %
(str_pad(5, strlen($yMax) - 1, 0))) {
326 $yMax +
= str_pad(5, strlen($yMax) - 1, 0) - $mod;
329 // if max value of y-axis <= 0, then set default values
338 // create x axis label obj.
339 $xLabels = new x_axis_labels();
340 $xLabels->set_labels($xValueLabels);
342 // set angle for labels.
343 if ($xLabelAngle = CRM_Utils_Array
::value('xLabelAngle', $params)) {
344 $xLabels->rotate($xLabelAngle);
347 // create x axis obj.
348 $xAxis = new x_axis();
349 $xAxis->set_labels($xLabels);
351 //create y axis and set range.
352 $yAxis = new y_axis();
353 $yAxis->set_range($yMin, $yMax, $ySteps);
355 // create chart title obj.
356 $title = new title($chartTitle);
359 $chart = new open_flash_chart();
361 // add x axis w/ labels to chart.
362 $chart->set_x_axis($xAxis);
364 // add y axis values to chart.
365 $chart->add_y_axis($yAxis);
367 // set title to chart.
368 $chart->set_title($title);
370 foreach ($xValues as $bar) {
371 // add bar element to chart.
372 $chart->add_element($bar);
375 // add x axis legend.
376 if ($xName = CRM_Utils_Array
::value('xname', $params)) {
377 $xLegend = new x_legend($xName);
378 $xLegend->set_style("{font-size: 13px; color:#000000; font-family: Verdana; text-align: center;}");
379 $chart->set_x_legend($xLegend);
382 // add y axis legend.
383 if ($yName = CRM_Utils_Array
::value('yname', $params)) {
384 $yLegend = new y_legend($yName);
385 $yLegend->set_style("{font-size: 13px; color:#000000; font-family: Verdana; text-align: center;}");
386 $chart->set_y_legend($yLegend);
399 public static function chart($rows, $chart, $interval) {
400 $chartData = $dateKeys = array();
404 foreach ($rows['receive_date'] as $key => $val) {
405 list($year, $month) = explode('-', $val);
406 $dateKeys[] = substr($rows['Month'][$key], 0, 3) . ' ' . $year;
408 $legend = ts('Monthly');
412 foreach ($rows['receive_date'] as $key => $val) {
413 list($year, $month) = explode('-', $val);
414 $dateKeys[] = 'Quarter ' . $rows['Quarter'][$key] . ' of ' . $year;
416 $legend = ts('Quarterly');
420 foreach ($rows['receive_date'] as $key => $val) {
421 list($year, $month) = explode('-', $val);
422 $dateKeys[] = 'Week ' . $rows['Week'][$key] . ' of ' . $year;
424 $legend = ts('Weekly');
428 foreach ($rows['receive_date'] as $key => $val) {
429 list($year, $month) = explode('-', $val);
432 $legend = ts('Yearly');
436 if (!empty($dateKeys)) {
438 if (!array_key_exists('multiValue', $rows)) {
439 $rows['multiValue'] = array($rows['value']);
441 foreach ($rows['multiValue'] as $key => $val) {
442 $graph[$key] = array_combine($dateKeys, $rows['multiValue'][$key]);
446 'legend' => "$legend " . CRM_Utils_Array
::value('legend', $rows, ts('Contribution')) . ' ' . ts('Summary'),
447 'values' => $graph[0],
448 'multiValues' => $graph,
449 'barKeys' => CRM_Utils_Array
::value('barKeys', $rows, array()),
453 // rotate the x labels.
454 $chartData['xLabelAngle'] = CRM_Utils_Array
::value('xLabelAngle', $rows, 0);
455 if (!empty($rows['tip'])) {
456 $chartData['tip'] = $rows['tip'];
460 $chartData['xname'] = CRM_Utils_Array
::value('xname', $rows);
461 $chartData['yname'] = CRM_Utils_Array
::value('yname', $rows);
463 // carry some chart params if pass.
465 'xSize', 'ySize', 'divName') as $f) {
466 if (!empty($rows[$f])) {
467 $chartData[$f] = $rows[$f];
471 return self
::buildChart($chartData, $chart);
482 public static function reportChart($rows, $chart, $interval, &$chartInfo) {
483 foreach ($interval as $key => $val) {
484 $graph[$val] = $rows['value'][$key];
489 'legend' => $chartInfo['legend'],
490 'xname' => $chartInfo['xname'],
491 'yname' => $chartInfo['yname'],
494 // rotate the x labels.
495 $chartData['xLabelAngle'] = CRM_Utils_Array
::value('xLabelAngle', $chartInfo, 20);
496 if (!empty($chartInfo['tip'])) {
497 $chartData['tip'] = $chartInfo['tip'];
500 // carry some chart params if pass.
502 'xSize', 'ySize', 'divName') as $f) {
503 if (!empty($rows[$f])) {
504 $chartData[$f] = $rows[$f];
508 return self
::buildChart($chartData, $chart);
512 * @param array $params
517 public static function buildChart(&$params, $chart) {
518 $openFlashChart = array();
519 if ($chart && is_array($params) && !empty($params)) {
520 // build the chart objects.
521 $chartObj = CRM_Utils_OpenFlashChart
::$chart($params);
523 $openFlashChart = array();
525 // calculate chart size.
526 $xSize = CRM_Utils_Array
::value('xSize', $params, 400);
527 $ySize = CRM_Utils_Array
::value('ySize', $params, 300);
528 if ($chart == 'barChart') {
529 $ySize = CRM_Utils_Array
::value('ySize', $params, 250);
530 $xSize = 60 * count($params['values']);
531 //hack to show tooltip.
533 $xSize = (count($params['values']) > 1) ?
100 * count($params['values']) : 170;
535 elseif ($xSize > 600 && count($params['values']) > 1) {
536 $xSize = (count($params['values']) +
400 / count($params['values'])) * count($params['values']);
540 // generate unique id for this chart instance
541 $uniqueId = md5(uniqid(rand(), TRUE));
543 $openFlashChart["chart_{$uniqueId}"]['size'] = array('xSize' => $xSize, 'ySize' => $ySize);
544 $openFlashChart["chart_{$uniqueId}"]['object'] = $chartObj;
546 // assign chart data to template
547 $template = CRM_Core_Smarty
::singleton();
548 $template->assign('uniqueId', $uniqueId);
549 $template->assign("openFlashChartData", json_encode($openFlashChart));
553 return $openFlashChart;