[REF] final cleanup - call bulkCreate from migrate_utils
[civicrm-core.git] / CRM / Utils / OpenFlashChart.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33
34 require_once 'packages/OpenFlashChart/php-ofc-library/open-flash-chart.php';
35
36 /**
37 * Build various graphs using Open Flash Chart library.
38 */
39 class CRM_Utils_OpenFlashChart {
40
41 /**
42 * Colours.
43 * @var array
44 */
45 private static $_colours = [
46 "#C3CC38",
47 "#C8B935",
48 "#CEA632",
49 "#D3932F",
50 "#D9802C",
51 "#FA6900",
52 "#DC9B57",
53 "#F78F01",
54 "#5AB56E",
55 "#6F8069",
56 "#C92200",
57 "#EB6C5C",
58 ];
59
60 /**
61 * Build The Bar Gharph.
62 *
63 * @param array $params
64 * Assoc array of name/value pairs.
65 *
66 * @return object
67 * $chart object of open flash chart.
68 */
69 public static function &barChart(&$params) {
70 $chart = NULL;
71 if (empty($params)) {
72 return $chart;
73 }
74 if (empty($params['multiValues'])) {
75 $params['multiValues'] = [$params['values']];
76 }
77
78 $values = CRM_Utils_Array::value('multiValues', $params);
79 if (!is_array($values) || empty($values)) {
80 return $chart;
81 }
82
83 // get the required data.
84 $chartTitle = !empty($params['legend']) ? $params['legend'] : ts('Bar Chart');
85
86 $xValues = $yValues = [];
87 $xValues = array_keys($values[0]);
88 $yValues = array_values($values[0]);
89
90 // set y axis parameters.
91 $yMin = 0;
92
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;
97 }
98 $ySteps = $yMax / 5;
99
100 $bars = [];
101 $symbol = CRM_Core_BAO_Country::defaultCurrencySymbol();
102 foreach ($values as $barCount => $barVal) {
103 $bars[$barCount] = new bar_glass();
104
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;
109 }
110 $bars[$barCount]->set_values($yValues);
111 if ($barCount > 0) {
112 // FIXME: for bars > 2, we'll need to come out with other colors
113 $bars[$barCount]->colour('#BF3B69');
114 }
115
116 if ($barKey = CRM_Utils_Array::value($barCount, CRM_Utils_Array::value('barKeys', $params))) {
117 $bars[$barCount]->key($barKey, 12);
118 }
119
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);
123 }
124
125 // get the currency to set in tooltip.
126 $tooltip = CRM_Utils_Array::value('tip', $params, "$symbol #val#");
127 $bars[$barCount]->set_tooltip($tooltip);
128 }
129
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;
136 });
137 $xLabels->set_labels($xValues);
138
139 // set angle for labels.
140 if ($xLabelAngle = CRM_Utils_Array::value('xLabelAngle', $params)) {
141 $xLabels->rotate($xLabelAngle);
142 }
143
144 // create x axis obj.
145 $xAxis = new x_axis();
146 $xAxis->set_labels($xLabels);
147
148 // create y axis and set range.
149 $yAxis = new y_axis();
150 $yAxis->set_range($yMin, $yMax, $ySteps);
151
152 // create chart title obj.
153 $title = new title($chartTitle);
154
155 // create chart.
156 $chart = new open_flash_chart();
157
158 // add x axis w/ labels to chart.
159 $chart->set_x_axis($xAxis);
160
161 // add y axis values to chart.
162 $chart->add_y_axis($yAxis);
163
164 // set title to chart.
165 $chart->set_title($title);
166
167 // add bar element to chart.
168 foreach ($bars as $bar) {
169 $chart->add_element($bar);
170 }
171
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);
177 }
178
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);
184 }
185
186 return $chart;
187 }
188
189 /**
190 * Build The Pie Gharph.
191 *
192 * @param array $params
193 * Assoc array of name/value pairs.
194 *
195 * @return object
196 * $chart object of open flash chart.
197 */
198 public static function &pieChart(&$params) {
199 $chart = NULL;
200 if (empty($params)) {
201 return $chart;
202 }
203 $allValues = CRM_Utils_Array::value('values', $params);
204 if (!is_array($allValues) || empty($allValues)) {
205 return $chart;
206 }
207
208 // get the required data.
209 $values = [];
210 foreach ($allValues as $label => $value) {
211 $values[] = new pie_value((double) $value, $label);
212 }
213 $graphTitle = !empty($params['legend']) ? $params['legend'] : ts('Pie Chart');
214
215 // get the currency.
216 $symbol = CRM_Core_BAO_Country::defaultCurrencySymbol();
217
218 $pie = new pie();
219 $pie->radius(100);
220
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);
224 }
225
226 $pie->set_start_angle(35);
227 $pie->add_animation(new pie_fade());
228 $pie->add_animation(new pie_bounce(2));
229
230 // set the tooltip.
231 $tooltip = CRM_Utils_Array::value('tip', $params, "Amount is $symbol #val# of $symbol #total# <br>#percent#");
232 $pie->set_tooltip($tooltip);
233
234 // set colours.
235 $pie->set_colours(self::$_colours);
236
237 $pie->set_values($values);
238
239 // create chart.
240 $chart = new open_flash_chart();
241
242 // create chart title obj.
243 $title = new title($graphTitle);
244 $chart->set_title($title);
245
246 $chart->add_element($pie);
247 $chart->x_axis = NULL;
248
249 return $chart;
250 }
251
252 /**
253 * Build The 3-D Bar Gharph.
254 *
255 * @param array $params
256 * Assoc array of name/value pairs.
257 *
258 * @return object
259 * $chart object of open flash chart.
260 */
261 public static function &bar_3dChart(&$params) {
262 $chart = NULL;
263 if (empty($params)) {
264 return $chart;
265 }
266
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)) {
272 return $chart;
273 }
274
275 // get the required data.
276 $xReferences = $xValueLabels = $xValues = $yValues = [];
277
278 foreach ($values as $xVal => $yVal) {
279 if (!is_array($yVal) || empty($yVal)) {
280 continue;
281 }
282
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);
287 }
288 }
289
290 if (empty($xReferences)) {
291
292 return $chart;
293
294 }
295
296 // get the currency.
297 $symbol = CRM_Core_BAO_Country::defaultCurrencySymbol();
298
299 // set the tooltip.
300 $tooltip = CRM_Utils_Array::value('tip', $params, "$symbol #val#");
301
302 $count = 0;
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#");
308 }
309
310 // create bar_3d object
311 $xValues[$count] = new bar_3d();
312 // set colour pattel
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));
318
319 // set tooltip
320 $xValues[$count]->set_tooltip($toolTipVal);
321 $count++;
322 }
323
324 $chartTitle = !empty($params['legend']) ? $params['legend'] : ts('Bar Chart');
325
326 // set y axis parameters.
327 $yMin = 0;
328
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;
333 }
334
335 // if max value of y-axis <= 0, then set default values
336 if ($yMax <= 0) {
337 $ySteps = 1;
338 $yMax = 5;
339 }
340 else {
341 $ySteps = $yMax / 5;
342 }
343
344 // create x axis label obj.
345 $xLabels = new x_axis_labels();
346 $xLabels->set_labels($xValueLabels);
347
348 // set angle for labels.
349 if ($xLabelAngle = CRM_Utils_Array::value('xLabelAngle', $params)) {
350 $xLabels->rotate($xLabelAngle);
351 }
352
353 // create x axis obj.
354 $xAxis = new x_axis();
355 $xAxis->set_labels($xLabels);
356
357 // create y axis and set range.
358 $yAxis = new y_axis();
359 $yAxis->set_range($yMin, $yMax, $ySteps);
360
361 // create chart title obj.
362 $title = new title($chartTitle);
363
364 // create chart.
365 $chart = new open_flash_chart();
366
367 // add x axis w/ labels to chart.
368 $chart->set_x_axis($xAxis);
369
370 // add y axis values to chart.
371 $chart->add_y_axis($yAxis);
372
373 // set title to chart.
374 $chart->set_title($title);
375
376 foreach ($xValues as $bar) {
377 // add bar element to chart.
378 $chart->add_element($bar);
379 }
380
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);
386 }
387
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);
393 }
394
395 return $chart;
396 }
397
398 /**
399 * @param $rows
400 * @param $chart
401 * @param $interval
402 *
403 * @return array
404 */
405 public static function chart($rows, $chart, $interval) {
406 $lcInterval = strtolower($interval);
407 $label = ucfirst($lcInterval);
408 $chartData = $dateKeys = [];
409 $intervalLabels = [
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'),
416 ];
417
418 switch ($lcInterval) {
419 case 'month':
420 case 'quarter':
421 case 'week':
422 case 'yearweek':
423 foreach ($rows['receive_date'] as $key => $val) {
424 list($year, $month) = explode('-', $val);
425 $dateKeys[] = substr($rows[$interval][$key], 0, 3) . ' of ' . $year;
426 }
427 $legend = $intervalLabels[$lcInterval];
428 break;
429
430 default:
431 foreach ($rows['receive_date'] as $key => $val) {
432 list($year, $month) = explode('-', $val);
433 $dateKeys[] = $year;
434 }
435 $legend = ts("%1", [1 => $label]);
436 if (!empty($intervalLabels[$lcInterval])) {
437 $legend = $intervalLabels[$lcInterval];
438 }
439 break;
440 }
441
442 if (!empty($dateKeys)) {
443 $graph = [];
444 if (!array_key_exists('multiValue', $rows)) {
445 $rows['multiValue'] = [$rows['value']];
446 }
447 foreach ($rows['multiValue'] as $key => $val) {
448 $graph[$key] = array_combine($dateKeys, $rows['multiValue'][$key]);
449 }
450 $chartData = [
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, []),
455 ];
456 }
457
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'];
462 }
463
464 // legend
465 $chartData['xname'] = CRM_Utils_Array::value('xname', $rows);
466 $chartData['yname'] = CRM_Utils_Array::value('yname', $rows);
467
468 // carry some chart params if pass.
469 foreach ([
470 'xSize',
471 'ySize',
472 'divName',
473 ] as $f) {
474 if (!empty($rows[$f])) {
475 $chartData[$f] = $rows[$f];
476 }
477 }
478
479 return self::buildChart($chartData, $chart);
480 }
481
482 /**
483 * @param $rows
484 * @param $chart
485 * @param $interval
486 * @param $chartInfo
487 *
488 * @return array
489 */
490 public static function reportChart($rows, $chart, $interval, &$chartInfo) {
491 foreach ($interval as $key => $val) {
492 $graph[$val] = $rows['value'][$key];
493 }
494
495 $chartData = [
496 'values' => $graph,
497 'legend' => $chartInfo['legend'],
498 'xname' => $chartInfo['xname'],
499 'yname' => $chartInfo['yname'],
500 ];
501
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'];
506 }
507
508 // carry some chart params if pass.
509 foreach ([
510 'xSize',
511 'ySize',
512 'divName',
513 ] as $f) {
514 if (!empty($rows[$f])) {
515 $chartData[$f] = $rows[$f];
516 }
517 }
518
519 return self::buildChart($chartData, $chart);
520 }
521
522 /**
523 * @param array $params
524 * @param $chart
525 *
526 * @return array
527 */
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);
533
534 $openFlashChart = [];
535 if ($chartObj) {
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.
543 if ($xSize < 200) {
544 $xSize = (count($params['values']) > 1) ? 100 * count($params['values']) : 170;
545 }
546 elseif ($xSize > 600 && count($params['values']) > 1) {
547 $xSize = (count($params['values']) + 400 / count($params['values'])) * count($params['values']);
548 }
549 }
550
551 // generate unique id for this chart instance
552 $uniqueId = md5(uniqid(rand(), TRUE));
553
554 $openFlashChart["chart_{$uniqueId}"]['size'] = ['xSize' => $xSize, 'ySize' => $ySize];
555 $openFlashChart["chart_{$uniqueId}"]['object'] = $chartObj;
556
557 // assign chart data to template
558 $template = CRM_Core_Smarty::singleton();
559 $template->assign('uniqueId', $uniqueId);
560 $template->assign("openFlashChartData", json_encode($openFlashChart));
561 }
562 }
563
564 return $openFlashChart;
565 }
566
567 }