3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
34 require_once 'packages/OpenFlashChart/php-ofc-library/open-flash-chart.php';
37 * Build various graphs using Open Flash Chart library.
39 class CRM_Utils_OpenFlashChart
{
45 private static $_colours = [
61 * Build The Bar Gharph.
63 * @param array $params
64 * Assoc array of name/value pairs.
67 * $chart object of open flash chart.
69 public static function &barChart(&$params) {
74 if (empty($params['multiValues'])) {
75 $params['multiValues'] = [$params['values']];
78 $values = CRM_Utils_Array
::value('multiValues', $params);
79 if (!is_array($values) ||
empty($values)) {
83 // get the required data.
84 $chartTitle = !empty($params['legend']) ?
$params['legend'] : ts('Bar Chart');
86 $xValues = $yValues = [];
87 $xValues = array_keys($values[0]);
88 $yValues = array_values($values[0]);
90 // set y axis parameters.
93 // calculate max scale for graph.
94 $yMax = ceil(max($yValues));
95 if ($mod = $yMax %
(str_pad(5, strlen($yMax) - 1, 0))) {
96 $yMax +
= str_pad(5, strlen($yMax) - 1, 0) - $mod;
101 $symbol = CRM_Core_BAO_Country
::defaultCurrencySymbol();
102 foreach ($values as $barCount => $barVal) {
103 $bars[$barCount] = new bar_glass();
105 $yValues = array_values($barVal);
106 foreach ($yValues as &$yVal) {
107 // type casting is required for chart to render values correctly
108 $yVal = (double) $yVal;
110 $bars[$barCount]->set_values($yValues);
112 // FIXME: for bars > 2, we'll need to come out with other colors
113 $bars[$barCount]->colour('#BF3B69');
116 if ($barKey = CRM_Utils_Array
::value($barCount, CRM_Utils_Array
::value('barKeys', $params))) {
117 $bars[$barCount]->key($barKey, 12);
120 // call user define function to handle on click event.
121 if ($onClickFunName = CRM_Utils_Array
::value('on_click_fun_name', $params)) {
122 $bars[$barCount]->set_on_click($onClickFunName);
125 // get the currency to set in tooltip.
126 $tooltip = CRM_Utils_Array
::value('tip', $params, "$symbol #val#");
127 $bars[$barCount]->set_tooltip($tooltip);
130 // create x axis label obj.
131 $xLabels = new x_axis_labels();
132 // set_labels function requires xValues array of string or x_axis_label
133 // so type casting array values to string values
134 array_walk($xValues, function (&$value, $index) {
135 $value = (string) $value;
137 $xLabels->set_labels($xValues);
139 // set angle for labels.
140 if ($xLabelAngle = CRM_Utils_Array
::value('xLabelAngle', $params)) {
141 $xLabels->rotate($xLabelAngle);
144 // create x axis obj.
145 $xAxis = new x_axis();
146 $xAxis->set_labels($xLabels);
148 // create y axis and set range.
149 $yAxis = new y_axis();
150 $yAxis->set_range($yMin, $yMax, $ySteps);
152 // create chart title obj.
153 $title = new title($chartTitle);
156 $chart = new open_flash_chart();
158 // add x axis w/ labels to chart.
159 $chart->set_x_axis($xAxis);
161 // add y axis values to chart.
162 $chart->add_y_axis($yAxis);
164 // set title to chart.
165 $chart->set_title($title);
167 // add bar element to chart.
168 foreach ($bars as $bar) {
169 $chart->add_element($bar);
172 // add x axis legend.
173 if ($xName = CRM_Utils_Array
::value('xname', $params)) {
174 $xLegend = new x_legend($xName);
175 $xLegend->set_style("{font-size: 13px; color:#000000; font-family: Verdana; text-align: center;}");
176 $chart->set_x_legend($xLegend);
179 // add y axis legend.
180 if ($yName = CRM_Utils_Array
::value('yname', $params)) {
181 $yLegend = new y_legend($yName);
182 $yLegend->set_style("{font-size: 13px; color:#000000; font-family: Verdana; text-align: center;}");
183 $chart->set_y_legend($yLegend);
190 * Build The Pie Gharph.
192 * @param array $params
193 * Assoc array of name/value pairs.
196 * $chart object of open flash chart.
198 public static function &pieChart(&$params) {
200 if (empty($params)) {
203 $allValues = CRM_Utils_Array
::value('values', $params);
204 if (!is_array($allValues) ||
empty($allValues)) {
208 // get the required data.
210 foreach ($allValues as $label => $value) {
211 $values[] = new pie_value((double) $value, $label);
213 $graphTitle = !empty($params['legend']) ?
$params['legend'] : ts('Pie Chart');
216 $symbol = CRM_Core_BAO_Country
::defaultCurrencySymbol();
221 // call user define function to handle on click event.
222 if ($onClickFunName = CRM_Utils_Array
::value('on_click_fun_name', $params)) {
223 $pie->on_click($onClickFunName);
226 $pie->set_start_angle(35);
227 $pie->add_animation(new pie_fade());
228 $pie->add_animation(new pie_bounce(2));
231 $tooltip = CRM_Utils_Array
::value('tip', $params, "Amount is $symbol #val# of $symbol #total# <br>#percent#");
232 $pie->set_tooltip($tooltip);
235 $pie->set_colours(self
::$_colours);
237 $pie->set_values($values);
240 $chart = new open_flash_chart();
242 // create chart title obj.
243 $title = new title($graphTitle);
244 $chart->set_title($title);
246 $chart->add_element($pie);
247 $chart->x_axis
= NULL;
253 * Build The 3-D Bar Gharph.
255 * @param array $params
256 * Assoc array of name/value pairs.
259 * $chart object of open flash chart.
261 public static function &bar_3dChart(&$params) {
263 if (empty($params)) {
267 // $params['values'] should contains the values for each
268 // criteria defined in $params['criteria']
269 $values = CRM_Utils_Array
::value('values', $params);
270 $criteria = CRM_Utils_Array
::value('criteria', $params);
271 if (!is_array($values) ||
empty($values) ||
!is_array($criteria) ||
empty($criteria)) {
275 // get the required data.
276 $xReferences = $xValueLabels = $xValues = $yValues = [];
278 foreach ($values as $xVal => $yVal) {
279 if (!is_array($yVal) ||
empty($yVal)) {
283 $xValueLabels[] = (string) $xVal;
284 foreach ($criteria as $criteria) {
285 $xReferences[$criteria][$xVal] = (double) CRM_Utils_Array
::value($criteria, $yVal, 0);
286 $yValues[] = (double) CRM_Utils_Array
::value($criteria, $yVal, 0);
290 if (empty($xReferences)) {
297 $symbol = CRM_Core_BAO_Country
::defaultCurrencySymbol();
300 $tooltip = CRM_Utils_Array
::value('tip', $params, "$symbol #val#");
303 foreach ($xReferences as $criteria => $values) {
304 $toolTipVal = $tooltip;
305 // for separate tooltip for each criteria
306 if (is_array($tooltip)) {
307 $toolTipVal = CRM_Utils_Array
::value($criteria, $tooltip, "$symbol #val#");
310 // create bar_3d object
311 $xValues[$count] = new bar_3d();
313 $xValues[$count]->set_colour(self
::$_colours[$count]);
314 // define colur pattel with bar criteria
315 $xValues[$count]->key((string) $criteria, 12);
316 // define bar chart values
317 $xValues[$count]->set_values(array_values($values));
320 $xValues[$count]->set_tooltip($toolTipVal);
324 $chartTitle = !empty($params['legend']) ?
$params['legend'] : ts('Bar Chart');
326 // set y axis parameters.
329 // calculate max scale for graph.
330 $yMax = ceil(max($yValues));
331 if ($mod = $yMax %
(str_pad(5, strlen($yMax) - 1, 0))) {
332 $yMax +
= str_pad(5, strlen($yMax) - 1, 0) - $mod;
335 // if max value of y-axis <= 0, then set default values
344 // create x axis label obj.
345 $xLabels = new x_axis_labels();
346 $xLabels->set_labels($xValueLabels);
348 // set angle for labels.
349 if ($xLabelAngle = CRM_Utils_Array
::value('xLabelAngle', $params)) {
350 $xLabels->rotate($xLabelAngle);
353 // create x axis obj.
354 $xAxis = new x_axis();
355 $xAxis->set_labels($xLabels);
357 // create y axis and set range.
358 $yAxis = new y_axis();
359 $yAxis->set_range($yMin, $yMax, $ySteps);
361 // create chart title obj.
362 $title = new title($chartTitle);
365 $chart = new open_flash_chart();
367 // add x axis w/ labels to chart.
368 $chart->set_x_axis($xAxis);
370 // add y axis values to chart.
371 $chart->add_y_axis($yAxis);
373 // set title to chart.
374 $chart->set_title($title);
376 foreach ($xValues as $bar) {
377 // add bar element to chart.
378 $chart->add_element($bar);
381 // add x axis legend.
382 if ($xName = CRM_Utils_Array
::value('xname', $params)) {
383 $xLegend = new x_legend($xName);
384 $xLegend->set_style("{font-size: 13px; color:#000000; font-family: Verdana; text-align: center;}");
385 $chart->set_x_legend($xLegend);
388 // add y axis legend.
389 if ($yName = CRM_Utils_Array
::value('yname', $params)) {
390 $yLegend = new y_legend($yName);
391 $yLegend->set_style("{font-size: 13px; color:#000000; font-family: Verdana; text-align: center;}");
392 $chart->set_y_legend($yLegend);
405 public static function chart($rows, $chart, $interval) {
406 $lcInterval = strtolower($interval);
407 $label = ucfirst($lcInterval);
408 $chartData = $dateKeys = [];
410 'year' => ts('Yearly'),
411 'fiscalyear' => ts('Yearly (Fiscal)'),
412 'month' => ts('Monthly'),
413 'quarter' => ts('Quarterly'),
414 'week' => ts('Weekly'),
415 'yearweek' => ts('Weekly'),
418 switch ($lcInterval) {
423 foreach ($rows['receive_date'] as $key => $val) {
424 list($year, $month) = explode('-', $val);
425 $dateKeys[] = substr($rows[$interval][$key], 0, 3) . ' of ' . $year;
427 $legend = $intervalLabels[$lcInterval];
431 foreach ($rows['receive_date'] as $key => $val) {
432 list($year, $month) = explode('-', $val);
435 $legend = ts("%1", [1 => $label]);
436 if (!empty($intervalLabels[$lcInterval])) {
437 $legend = $intervalLabels[$lcInterval];
442 if (!empty($dateKeys)) {
444 if (!array_key_exists('multiValue', $rows)) {
445 $rows['multiValue'] = [$rows['value']];
447 foreach ($rows['multiValue'] as $key => $val) {
448 $graph[$key] = array_combine($dateKeys, $rows['multiValue'][$key]);
451 'legend' => "$legend " . CRM_Utils_Array
::value('legend', $rows, ts('Contribution')) . ' ' . ts('Summary'),
452 'values' => $graph[0],
453 'multiValues' => $graph,
454 'barKeys' => CRM_Utils_Array
::value('barKeys', $rows, []),
458 // rotate the x labels.
459 $chartData['xLabelAngle'] = CRM_Utils_Array
::value('xLabelAngle', $rows, 0);
460 if (!empty($rows['tip'])) {
461 $chartData['tip'] = $rows['tip'];
465 $chartData['xname'] = CRM_Utils_Array
::value('xname', $rows);
466 $chartData['yname'] = CRM_Utils_Array
::value('yname', $rows);
468 // carry some chart params if pass.
474 if (!empty($rows[$f])) {
475 $chartData[$f] = $rows[$f];
479 return self
::buildChart($chartData, $chart);
490 public static function reportChart($rows, $chart, $interval, &$chartInfo) {
491 foreach ($interval as $key => $val) {
492 $graph[$val] = $rows['value'][$key];
497 'legend' => $chartInfo['legend'],
498 'xname' => $chartInfo['xname'],
499 'yname' => $chartInfo['yname'],
502 // rotate the x labels.
503 $chartData['xLabelAngle'] = CRM_Utils_Array
::value('xLabelAngle', $chartInfo, 20);
504 if (!empty($chartInfo['tip'])) {
505 $chartData['tip'] = $chartInfo['tip'];
508 // carry some chart params if pass.
514 if (!empty($rows[$f])) {
515 $chartData[$f] = $rows[$f];
519 return self
::buildChart($chartData, $chart);
523 * @param array $params
528 public static function buildChart(&$params, $chart) {
529 $openFlashChart = [];
530 if ($chart && is_array($params) && !empty($params)) {
531 // build the chart objects.
532 $chartObj = CRM_Utils_OpenFlashChart
::$chart($params);
534 $openFlashChart = [];
536 // calculate chart size.
537 $xSize = CRM_Utils_Array
::value('xSize', $params, 400);
538 $ySize = CRM_Utils_Array
::value('ySize', $params, 300);
539 if ($chart == 'barChart') {
540 $ySize = CRM_Utils_Array
::value('ySize', $params, 250);
541 $xSize = 60 * count($params['values']);
542 // hack to show tooltip.
544 $xSize = (count($params['values']) > 1) ?
100 * count($params['values']) : 170;
546 elseif ($xSize > 600 && count($params['values']) > 1) {
547 $xSize = (count($params['values']) +
400 / count($params['values'])) * count($params['values']);
551 // generate unique id for this chart instance
552 $uniqueId = md5(uniqid(rand(), TRUE));
554 $openFlashChart["chart_{$uniqueId}"]['size'] = ['xSize' => $xSize, 'ySize' => $ySize];
555 $openFlashChart["chart_{$uniqueId}"]['object'] = $chartObj;
557 // assign chart data to template
558 $template = CRM_Core_Smarty
::singleton();
559 $template->assign('uniqueId', $uniqueId);
560 $template->assign("openFlashChartData", json_encode($openFlashChart));
564 return $openFlashChart;