3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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
{
47 private static $_colours = array(
63 * Build The Bar Gharph.
65 * @param array $params
66 * Assoc array of name/value pairs.
69 * $chart object of open flash chart.
71 public static function &barChart(&$params) {
76 if (empty($params['multiValues'])) {
77 $params['multiValues'] = array($params['values']);
80 $values = CRM_Utils_Array
::value('multiValues', $params);
81 if (!is_array($values) ||
empty($values)) {
85 // get the required data.
86 $chartTitle = !empty($params['legend']) ?
$params['legend'] : ts('Bar Chart');
88 $xValues = $yValues = array();
89 $xValues = array_keys($values[0]);
90 $yValues = array_values($values[0]);
92 //set y axis parameters.
95 // calculate max scale for graph.
96 $yMax = ceil(max($yValues));
97 if ($mod = $yMax %
(str_pad(5, strlen($yMax) - 1, 0))) {
98 $yMax +
= str_pad(5, strlen($yMax) - 1, 0) - $mod;
103 $config = CRM_Core_Config
::singleton();
104 $symbol = $config->defaultCurrencySymbol
;
105 foreach ($values as $barCount => $barVal) {
106 $bars[$barCount] = new bar_glass();
108 $yValues = array_values($barVal);
109 foreach ($yValues as &$yVal) {
110 // type casting is required for chart to render values correctly
111 $yVal = (double) $yVal;
113 $bars[$barCount]->set_values($yValues);
115 // FIXME: for bars > 2, we'll need to come out with other colors
116 $bars[$barCount]->colour('#BF3B69');
119 if ($barKey = CRM_Utils_Array
::value($barCount, CRM_Utils_Array
::value('barKeys', $params))) {
120 $bars[$barCount]->key($barKey, 12);
123 // call user define function to handle on click event.
124 if ($onClickFunName = CRM_Utils_Array
::value('on_click_fun_name', $params)) {
125 $bars[$barCount]->set_on_click($onClickFunName);
128 // get the currency to set in tooltip.
129 $tooltip = CRM_Utils_Array
::value('tip', $params, "$symbol #val#");
130 $bars[$barCount]->set_tooltip($tooltip);
133 // create x axis label obj.
134 $xLabels = new x_axis_labels();
135 // set_labels function requires xValues array of string or x_axis_label
136 // so type casting array values to string values
137 array_walk($xValues, function (&$value, $index) {
138 $value = (string) $value;
140 $xLabels->set_labels($xValues);
142 // set angle for labels.
143 if ($xLabelAngle = CRM_Utils_Array
::value('xLabelAngle', $params)) {
144 $xLabels->rotate($xLabelAngle);
147 // create x axis obj.
148 $xAxis = new x_axis();
149 $xAxis->set_labels($xLabels);
151 //create y axis and set range.
152 $yAxis = new y_axis();
153 $yAxis->set_range($yMin, $yMax, $ySteps);
155 // create chart title obj.
156 $title = new title($chartTitle);
159 $chart = new open_flash_chart();
161 // add x axis w/ labels to chart.
162 $chart->set_x_axis($xAxis);
164 // add y axis values to chart.
165 $chart->add_y_axis($yAxis);
167 // set title to chart.
168 $chart->set_title($title);
170 // add bar element to chart.
171 foreach ($bars as $bar) {
172 $chart->add_element($bar);
175 // add x axis legend.
176 if ($xName = CRM_Utils_Array
::value('xname', $params)) {
177 $xLegend = new x_legend($xName);
178 $xLegend->set_style("{font-size: 13px; color:#000000; font-family: Verdana; text-align: center;}");
179 $chart->set_x_legend($xLegend);
182 // add y axis legend.
183 if ($yName = CRM_Utils_Array
::value('yname', $params)) {
184 $yLegend = new y_legend($yName);
185 $yLegend->set_style("{font-size: 13px; color:#000000; font-family: Verdana; text-align: center;}");
186 $chart->set_y_legend($yLegend);
193 * Build The Pie Gharph.
195 * @param array $params
196 * Assoc array of name/value pairs.
199 * $chart object of open flash chart.
201 public static function &pieChart(&$params) {
203 if (empty($params)) {
206 $allValues = CRM_Utils_Array
::value('values', $params);
207 if (!is_array($allValues) ||
empty($allValues)) {
211 // get the required data.
213 foreach ($allValues as $label => $value) {
214 $values[] = new pie_value((double) $value, $label);
216 $graphTitle = !empty($params['legend']) ?
$params['legend'] : ts('Pie Chart');
219 $config = CRM_Core_Config
::singleton();
220 $symbol = $config->defaultCurrencySymbol
;
225 // call user define function to handle on click event.
226 if ($onClickFunName = CRM_Utils_Array
::value('on_click_fun_name', $params)) {
227 $pie->on_click($onClickFunName);
230 $pie->set_start_angle(35);
231 $pie->add_animation(new pie_fade());
232 $pie->add_animation(new pie_bounce(2));
235 $tooltip = CRM_Utils_Array
::value('tip', $params, "Amount is $symbol #val# of $symbol #total# <br>#percent#");
236 $pie->set_tooltip($tooltip);
239 $pie->set_colours(self
::$_colours);
241 $pie->set_values($values);
244 $chart = new open_flash_chart();
246 // create chart title obj.
247 $title = new title($graphTitle);
248 $chart->set_title($title);
250 $chart->add_element($pie);
251 $chart->x_axis
= NULL;
257 * Build The 3-D Bar Gharph.
259 * @param array $params
260 * Assoc array of name/value pairs.
263 * $chart object of open flash chart.
265 public static function &bar_3dChart(&$params) {
267 if (empty($params)) {
271 // $params['values'] should contains the values for each
272 // criteria defined in $params['criteria']
273 $values = CRM_Utils_Array
::value('values', $params);
274 $criteria = CRM_Utils_Array
::value('criteria', $params);
275 if (!is_array($values) ||
empty($values) ||
!is_array($criteria) ||
empty($criteria)) {
279 // get the required data.
280 $xReferences = $xValueLabels = $xValues = $yValues = array();
282 foreach ($values as $xVal => $yVal) {
283 if (!is_array($yVal) ||
empty($yVal)) {
287 $xValueLabels[] = (string) $xVal;
288 foreach ($criteria as $criteria) {
289 $xReferences[$criteria][$xVal] = (double) CRM_Utils_Array
::value($criteria, $yVal, 0);
290 $yValues[] = (double) CRM_Utils_Array
::value($criteria, $yVal, 0);
294 if (empty($xReferences)) {
301 $config = CRM_Core_Config
::singleton();
302 $symbol = $config->defaultCurrencySymbol
;
305 $tooltip = CRM_Utils_Array
::value('tip', $params, "$symbol #val#");
308 foreach ($xReferences as $criteria => $values) {
309 $toolTipVal = $tooltip;
310 // for separate tooltip for each criteria
311 if (is_array($tooltip)) {
312 $toolTipVal = CRM_Utils_Array
::value($criteria, $tooltip, "$symbol #val#");
315 // create bar_3d object
316 $xValues[$count] = new bar_3d();
318 $xValues[$count]->set_colour(self
::$_colours[$count]);
319 // define colur pattel with bar criteria
320 $xValues[$count]->key((string) $criteria, 12);
321 // define bar chart values
322 $xValues[$count]->set_values(array_values($values));
325 $xValues[$count]->set_tooltip($toolTipVal);
329 $chartTitle = !empty($params['legend']) ?
$params['legend'] : ts('Bar Chart');
331 //set y axis parameters.
334 // calculate max scale for graph.
335 $yMax = ceil(max($yValues));
336 if ($mod = $yMax %
(str_pad(5, strlen($yMax) - 1, 0))) {
337 $yMax +
= str_pad(5, strlen($yMax) - 1, 0) - $mod;
340 // if max value of y-axis <= 0, then set default values
349 // create x axis label obj.
350 $xLabels = new x_axis_labels();
351 $xLabels->set_labels($xValueLabels);
353 // set angle for labels.
354 if ($xLabelAngle = CRM_Utils_Array
::value('xLabelAngle', $params)) {
355 $xLabels->rotate($xLabelAngle);
358 // create x axis obj.
359 $xAxis = new x_axis();
360 $xAxis->set_labels($xLabels);
362 //create y axis and set range.
363 $yAxis = new y_axis();
364 $yAxis->set_range($yMin, $yMax, $ySteps);
366 // create chart title obj.
367 $title = new title($chartTitle);
370 $chart = new open_flash_chart();
372 // add x axis w/ labels to chart.
373 $chart->set_x_axis($xAxis);
375 // add y axis values to chart.
376 $chart->add_y_axis($yAxis);
378 // set title to chart.
379 $chart->set_title($title);
381 foreach ($xValues as $bar) {
382 // add bar element to chart.
383 $chart->add_element($bar);
386 // add x axis legend.
387 if ($xName = CRM_Utils_Array
::value('xname', $params)) {
388 $xLegend = new x_legend($xName);
389 $xLegend->set_style("{font-size: 13px; color:#000000; font-family: Verdana; text-align: center;}");
390 $chart->set_x_legend($xLegend);
393 // add y axis legend.
394 if ($yName = CRM_Utils_Array
::value('yname', $params)) {
395 $yLegend = new y_legend($yName);
396 $yLegend->set_style("{font-size: 13px; color:#000000; font-family: Verdana; text-align: center;}");
397 $chart->set_y_legend($yLegend);
410 public static function chart($rows, $chart, $interval) {
411 $chartData = $dateKeys = array();
415 foreach ($rows['receive_date'] as $key => $val) {
416 list($year, $month) = explode('-', $val);
417 $dateKeys[] = substr($rows['Month'][$key], 0, 3) . ' ' . $year;
419 $legend = ts('Monthly');
423 foreach ($rows['receive_date'] as $key => $val) {
424 list($year, $month) = explode('-', $val);
425 $dateKeys[] = 'Quarter ' . $rows['Quarter'][$key] . ' of ' . $year;
427 $legend = ts('Quarterly');
431 foreach ($rows['receive_date'] as $key => $val) {
432 list($year, $month) = explode('-', $val);
433 $dateKeys[] = 'Week ' . $rows['Week'][$key] . ' of ' . $year;
435 $legend = ts('Weekly');
439 foreach ($rows['receive_date'] as $key => $val) {
440 list($year, $month) = explode('-', $val);
443 $legend = ts('Yearly');
447 if (!empty($dateKeys)) {
449 if (!array_key_exists('multiValue', $rows)) {
450 $rows['multiValue'] = array($rows['value']);
452 foreach ($rows['multiValue'] as $key => $val) {
453 $graph[$key] = array_combine($dateKeys, $rows['multiValue'][$key]);
456 'legend' => "$legend " . CRM_Utils_Array
::value('legend', $rows, ts('Contribution')) . ' ' . ts('Summary'),
457 'values' => $graph[0],
458 'multiValues' => $graph,
459 'barKeys' => CRM_Utils_Array
::value('barKeys', $rows, array()),
463 // rotate the x labels.
464 $chartData['xLabelAngle'] = CRM_Utils_Array
::value('xLabelAngle', $rows, 0);
465 if (!empty($rows['tip'])) {
466 $chartData['tip'] = $rows['tip'];
470 $chartData['xname'] = CRM_Utils_Array
::value('xname', $rows);
471 $chartData['yname'] = CRM_Utils_Array
::value('yname', $rows);
473 // carry some chart params if pass.
479 if (!empty($rows[$f])) {
480 $chartData[$f] = $rows[$f];
484 return self
::buildChart($chartData, $chart);
495 public static function reportChart($rows, $chart, $interval, &$chartInfo) {
496 foreach ($interval as $key => $val) {
497 $graph[$val] = $rows['value'][$key];
502 'legend' => $chartInfo['legend'],
503 'xname' => $chartInfo['xname'],
504 'yname' => $chartInfo['yname'],
507 // rotate the x labels.
508 $chartData['xLabelAngle'] = CRM_Utils_Array
::value('xLabelAngle', $chartInfo, 20);
509 if (!empty($chartInfo['tip'])) {
510 $chartData['tip'] = $chartInfo['tip'];
513 // carry some chart params if pass.
519 if (!empty($rows[$f])) {
520 $chartData[$f] = $rows[$f];
524 return self
::buildChart($chartData, $chart);
528 * @param array $params
533 public static function buildChart(&$params, $chart) {
534 $openFlashChart = array();
535 if ($chart && is_array($params) && !empty($params)) {
536 // build the chart objects.
537 $chartObj = CRM_Utils_OpenFlashChart
::$chart($params);
539 $openFlashChart = array();
541 // calculate chart size.
542 $xSize = CRM_Utils_Array
::value('xSize', $params, 400);
543 $ySize = CRM_Utils_Array
::value('ySize', $params, 300);
544 if ($chart == 'barChart') {
545 $ySize = CRM_Utils_Array
::value('ySize', $params, 250);
546 $xSize = 60 * count($params['values']);
547 //hack to show tooltip.
549 $xSize = (count($params['values']) > 1) ?
100 * count($params['values']) : 170;
551 elseif ($xSize > 600 && count($params['values']) > 1) {
552 $xSize = (count($params['values']) +
400 / count($params['values'])) * count($params['values']);
556 // generate unique id for this chart instance
557 $uniqueId = md5(uniqid(rand(), TRUE));
559 $openFlashChart["chart_{$uniqueId}"]['size'] = array('xSize' => $xSize, 'ySize' => $ySize);
560 $openFlashChart["chart_{$uniqueId}"]['object'] = $chartObj;
562 // assign chart data to template
563 $template = CRM_Core_Smarty
::singleton();
564 $template->assign('uniqueId', $uniqueId);
565 $template->assign("openFlashChartData", json_encode($openFlashChart));
569 return $openFlashChart;