3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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) {
68 $values = CRM_Utils_Array
::value('values', $params);
69 if (!is_array($values) ||
empty($values)) {
73 // get the required data.
74 $xValues = $yValues = array();
75 foreach ($values as $xVal => $yVal) {
76 $yValues[] = (double)$yVal;
78 // we has to have x values as string.
79 $xValues[] = (string)$xVal;
81 $chartTitle = CRM_Utils_Array
::value('legend', $params) ?
$params['legend'] : ts('Bar Chart');
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 // glass seem to be more cool
95 $bar = new bar_glass();
98 $bar->set_values($yValues);
100 // call user define function to handle on click event.
101 if ($onClickFunName = CRM_Utils_Array
::value('on_click_fun_name', $params)) {
102 $bar->set_on_click($onClickFunName);
106 $config = CRM_Core_Config
::singleton();
107 $symbol = $config->defaultCurrencySymbol
;
110 $tooltip = CRM_Utils_Array
::value('tip', $params, "$symbol #val#");
111 $bar->set_tooltip($tooltip);
113 // create x axis label obj.
114 $xLabels = new x_axis_labels();
115 $xLabels->set_labels($xValues);
117 // set angle for labels.
118 if ($xLabelAngle = CRM_Utils_Array
::value('xLabelAngle', $params)) {
119 $xLabels->rotate($xLabelAngle);
122 // create x axis obj.
123 $xAxis = new x_axis();
124 $xAxis->set_labels($xLabels);
126 //create y axis and set range.
127 $yAxis = new y_axis();
128 $yAxis->set_range($yMin, $yMax, $ySteps);
130 // create chart title obj.
131 $title = new title($chartTitle);
134 $chart = new open_flash_chart();
136 // add x axis w/ labels to chart.
137 $chart->set_x_axis($xAxis);
139 // add y axis values to chart.
140 $chart->add_y_axis($yAxis);
142 // set title to chart.
143 $chart->set_title($title);
145 // add bar element to chart.
146 $chart->add_element($bar);
148 // add x axis legend.
149 if ($xName = CRM_Utils_Array
::value('xname', $params)) {
150 $xLegend = new x_legend($xName);
151 $xLegend->set_style("{font-size: 13px; color:#000000; font-family: Verdana; text-align: center;}");
152 $chart->set_x_legend($xLegend);
155 // add y axis legend.
156 if ($yName = CRM_Utils_Array
::value('yname', $params)) {
157 $yLegend = new y_legend($yName);
158 $yLegend->set_style("{font-size: 13px; color:#000000; font-family: Verdana; text-align: center;}");
159 $chart->set_y_legend($yLegend);
166 * Build The Pie Gharph.
168 * @param array $params assoc array of name/value pairs
170 * @return object $chart object of open flash chart.
173 static function &pieChart(&$params) {
175 if (empty($params)) {
178 $allValues = CRM_Utils_Array
::value('values', $params);
179 if (!is_array($allValues) ||
empty($allValues)) {
183 // get the required data.
185 foreach ($allValues as $label => $value) {
186 $values[] = new pie_value((double)$value, $label);
188 $graphTitle = CRM_Utils_Array
::value('legend', $params) ?
$params['legend'] : ts('Pie Chart');
191 $config = CRM_Core_Config
::singleton();
192 $symbol = $config->defaultCurrencySymbol
;
197 // call user define function to handle on click event.
198 if ($onClickFunName = CRM_Utils_Array
::value('on_click_fun_name', $params)) {
199 $pie->on_click($onClickFunName);
202 $pie->set_start_angle(35);
203 $pie->add_animation(new pie_fade());
204 $pie->add_animation(new pie_bounce(2));
207 $tooltip = CRM_Utils_Array
::value('tip', $params, "Amount is $symbol #val# of $symbol #total# <br>#percent#");
208 $pie->set_tooltip($tooltip);
211 $pie->set_colours(self
::$_colours);
213 $pie->set_values($values);
216 $chart = new open_flash_chart();
218 // create chart title obj.
219 $title = new title($graphTitle);
220 $chart->set_title($title);
222 $chart->add_element($pie);
223 $chart->x_axis
= NULL;
229 * Build The 3-D Bar Gharph.
231 * @param array $params assoc array of name/value pairs
233 * @return object $chart object of open flash chart.
236 static function &bar_3dChart(&$params) {
238 if (empty($params)) {
242 // $params['values'] should contains the values for each
243 // criteria defined in $params['criteria']
244 $values = CRM_Utils_Array
::value('values', $params);
245 $criterias = CRM_Utils_Array
::value('criteria', $params);
246 if (!is_array($values) ||
empty($values) ||
!is_array($criterias) ||
empty($criterias)) {
250 // get the required data.
251 $xReferences = $xValueLabels = $xValues = $yValues = array();
253 foreach ($values as $xVal => $yVal) {
254 if (!is_array($yVal) ||
empty($yVal)) {
258 $xValueLabels[] = (string)$xVal;
259 foreach ($criterias as $criteria) {
260 $xReferences[$criteria][$xVal] = (double)CRM_Utils_Array
::value($criteria, $yVal, 0);
261 $yValues[] = (double)CRM_Utils_Array
::value($criteria, $yVal, 0);
265 if (empty($xReferences)) {
272 $config = CRM_Core_Config
::singleton();
273 $symbol = $config->defaultCurrencySymbol
;
276 $tooltip = CRM_Utils_Array
::value('tip', $params, "$symbol #val#");
279 foreach ($xReferences as $criteria => $values) {
280 $toolTipVal = $tooltip;
281 // for seperate tooltip for each criteria
282 if (is_array($tooltip)) {
283 $toolTipVal = CRM_Utils_Array
::value($criteria, $tooltip, "$symbol #val#");
286 // create bar_3d object
287 $xValues[$count] = new bar_3d();
289 $xValues[$count]->set_colour(self
::$_colours[$count]);
290 // define colur pattel with bar criterias
291 $xValues[$count]->key((string)$criteria, 12);
292 // define bar chart values
293 $xValues[$count]->set_values(array_values($values));
296 $xValues[$count]->set_tooltip($toolTipVal);
300 $chartTitle = CRM_Utils_Array
::value('legend', $params) ?
$params['legend'] : ts('Bar Chart');
302 //set y axis parameters.
305 // calculate max scale for graph.
306 $yMax = ceil(max($yValues));
307 if ($mod = $yMax %
(str_pad(5, strlen($yMax) - 1, 0))) {
308 $yMax +
= str_pad(5, strlen($yMax) - 1, 0) - $mod;
311 // if max value of y-axis <= 0, then set default values
320 // create x axis label obj.
321 $xLabels = new x_axis_labels();
322 $xLabels->set_labels($xValueLabels);
324 // set angle for labels.
325 if ($xLabelAngle = CRM_Utils_Array
::value('xLabelAngle', $params)) {
326 $xLabels->rotate($xLabelAngle);
329 // create x axis obj.
330 $xAxis = new x_axis();
331 $xAxis->set_labels($xLabels);
333 //create y axis and set range.
334 $yAxis = new y_axis();
335 $yAxis->set_range($yMin, $yMax, $ySteps);
337 // create chart title obj.
338 $title = new title($chartTitle);
341 $chart = new open_flash_chart();
343 // add x axis w/ labels to chart.
344 $chart->set_x_axis($xAxis);
346 // add y axis values to chart.
347 $chart->add_y_axis($yAxis);
349 // set title to chart.
350 $chart->set_title($title);
352 foreach ($xValues as $bar) {
353 // add bar element to chart.
354 $chart->add_element($bar);
357 // add x axis legend.
358 if ($xName = CRM_Utils_Array
::value('xname', $params)) {
359 $xLegend = new x_legend($xName);
360 $xLegend->set_style("{font-size: 13px; color:#000000; font-family: Verdana; text-align: center;}");
361 $chart->set_x_legend($xLegend);
364 // add y axis legend.
365 if ($yName = CRM_Utils_Array
::value('yname', $params)) {
366 $yLegend = new y_legend($yName);
367 $yLegend->set_style("{font-size: 13px; color:#000000; font-family: Verdana; text-align: center;}");
368 $chart->set_y_legend($yLegend);
374 static function chart($rows, $chart, $interval) {
375 $chartData = array();
379 foreach ($rows['receive_date'] as $key => $val) {
380 list($year, $month) = explode('-', $val);
381 $graph[substr($rows['Month'][$key], 0, 3) . ' ' . $year] = $rows['value'][$key];
386 'legend' => ts('Monthly Contribution Summary'),
391 foreach ($rows['receive_date'] as $key => $val) {
392 list($year, $month) = explode('-', $val);
393 $graph['Quarter ' . $rows['Quarter'][$key] . ' of ' . $year] = $rows['value'][$key];
398 'legend' => ts('Quarterly Contribution Summary'),
403 foreach ($rows['receive_date'] as $key => $val) {
404 list($year, $month) = explode('-', $val);
405 $graph['Week ' . $rows['Week'][$key] . ' of ' . $year] = $rows['value'][$key];
410 'legend' => ts('Weekly Contribution Summary'),
415 foreach ($rows['receive_date'] as $key => $val) {
416 list($year, $month) = explode('-', $val);
417 $graph[$year] = $rows['value'][$key];
421 'legend' => ts('Yearly Contribution Summary'),
426 // rotate the x labels.
427 $chartData['xLabelAngle'] = CRM_Utils_Array
::value('xLabelAngle', $rows, 20);
428 if (CRM_Utils_Array
::value('tip', $rows)) {
429 $chartData['tip'] = $rows['tip'];
433 $chartData['xname'] = CRM_Utils_Array
::value('xname', $rows);
434 $chartData['yname'] = CRM_Utils_Array
::value('yname', $rows);
436 // carry some chart params if pass.
438 'xSize', 'ySize', 'divName') as $f) {
439 if (CRM_Utils_Array
::value($f, $rows)) {
440 $chartData[$f] = $rows[$f];
444 return self
::buildChart($chartData, $chart);
447 static function reportChart($rows, $chart, $interval, &$chartInfo) {
448 foreach ($interval as $key => $val) {
449 $graph[$val] = $rows['value'][$key];
454 'legend' => $chartInfo['legend'],
455 'xname' => $chartInfo['xname'],
456 'yname' => $chartInfo['yname'],
459 // rotate the x labels.
460 $chartData['xLabelAngle'] = CRM_Utils_Array
::value('xLabelAngle', $chartInfo, 20);
461 if (CRM_Utils_Array
::value('tip', $chartInfo)) {
462 $chartData['tip'] = $chartInfo['tip'];
465 // carry some chart params if pass.
467 'xSize', 'ySize', 'divName') as $f) {
468 if (CRM_Utils_Array
::value($f, $rows)) {
469 $chartData[$f] = $rows[$f];
473 return self
::buildChart($chartData, $chart);
476 static function buildChart(&$params, $chart) {
477 $openFlashChart = array();
478 if ($chart && is_array($params) && !empty($params)) {
479 // build the chart objects.
480 $chartObj = CRM_Utils_OpenFlashChart
::$chart($params);
482 $openFlashChart = array();
484 // calculate chart size.
485 $xSize = CRM_Utils_Array
::value('xSize', $params, 400);
486 $ySize = CRM_Utils_Array
::value('ySize', $params, 300);
487 if ($chart == 'barChart') {
488 $ySize = CRM_Utils_Array
::value('ySize', $params, 250);
489 $xSize = 60 * count($params['values']);
490 //hack to show tooltip.
492 $xSize = (count($params['values']) > 1) ?
100 * count($params['values']) : 170;
494 elseif ($xSize > 600 && count($params['values']) > 1) {
495 $xSize = (count($params['values']) +
400 / count($params['values'])) * count($params['values']);
499 // generate unique id for this chart instance
500 $uniqueId = md5(uniqid(rand(), TRUE));
502 $openFlashChart["chart_{$uniqueId}"]['size'] = array('xSize' => $xSize, 'ySize' => $ySize);
503 $openFlashChart["chart_{$uniqueId}"]['object'] = $chartObj;
505 // assign chart data to template
506 $template = CRM_Core_Smarty
::singleton();
507 $template->assign('uniqueId', $uniqueId);
508 $template->assign("openFlashChartData", json_encode($openFlashChart));
512 return $openFlashChart;