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
35 * Build various graphs using the dc chart library.
37 class CRM_Utils_Chart
{
43 private static $_colours = [
59 * Build The Bar Gharph.
61 * @param array $params
62 * Assoc array of name/value pairs.
65 * $chart object of open flash chart.
67 public static function barChart($params) {
68 $output = static::commonParamsManipulation($params);
72 $output['type'] = 'barchart';
74 $output +
= ['title' => ts('Bar Chart')];
76 // ? Not sure what reports use this, but it's not implemented.
77 // call user define function to handle on click event.
78 // if ($onClickFunName = CRM_Utils_Array::value('on_click_fun_name', $params)) {
79 // $bars[$barCount]->set_on_click($onClickFunName);
82 //// get the currency to set in tooltip.
83 //$tooltip = CRM_Utils_Array::value('tip', $params, "$symbol #val#");
91 * @param array $params
92 * Assoc array of name/value pairs.
96 public static function pieChart($params) {
97 $output = static::commonParamsManipulation($params);
101 $output['type'] = 'piechart';
102 $output +
= ['title' => ts('Pie Chart')];
104 // ? Not sure what reports use this, but it's not implemented.
105 // call user define function to handle on click event.
106 // if ($onClickFunName = CRM_Utils_Array::value('on_click_fun_name', $params)) {
107 // $bars[$barCount]->set_on_click($onClickFunName);
110 //// get the currency to set in tooltip.
111 //$tooltip = CRM_Utils_Array::value('tip', $params, "$symbol #val#");
117 * Common data manipulation for charts.
119 * @param array $params
120 * Assoc array of name/value pairs.
124 public static function commonParamsManipulation($params) {
125 if (empty($params)) {
129 if (empty($params['multiValues'])) {
130 $params['multiValues'] = [$params['values']];
133 $output['values'] = [];
134 foreach ($params['multiValues'] as $i => $dataSet) {
135 $output['values'][$i] = [];
136 foreach ($dataSet as $k => $v) {
137 $output['values'][$i][] = ['label' => $k, 'value' => (double) $v];
140 if (!$output['values']) {
144 // Ensure there's a legend (title)
145 if (!empty($params['legend'])) {
146 $output['title'] = $params['legend'];
149 $output['symbol'] = CRM_Core_BAO_Country
::defaultCurrencySymbol();
151 // ? Not sure what reports use this, but it's not implemented.
152 // call user define function to handle on click event.
153 // if ($onClickFunName = CRM_Utils_Array::value('on_click_fun_name', $params)) {
154 // $bars[$barCount]->set_on_click($onClickFunName);
157 //// get the currency to set in tooltip.
158 //$tooltip = CRM_Utils_Array::value('tip', $params, "$symbol #val#");
170 public static function chart($rows, $chart, $interval) {
171 $lcInterval = strtolower($interval);
172 $label = ucfirst($lcInterval);
173 $chartData = $dateKeys = [];
175 'year' => ts('Yearly'),
176 'fiscalyear' => ts('Yearly (Fiscal)'),
177 'month' => ts('Monthly'),
178 'quarter' => ts('Quarterly'),
179 'week' => ts('Weekly'),
180 'yearweek' => ts('Weekly'),
183 switch ($lcInterval) {
188 foreach ($rows['receive_date'] as $key => $val) {
189 list($year, $month) = explode('-', $val);
190 $dateKeys[] = substr($rows[$interval][$key], 0, 3) . ' of ' . $year;
192 $legend = $intervalLabels[$lcInterval];
196 foreach ($rows['receive_date'] as $key => $val) {
197 list($year, $month) = explode('-', $val);
200 $legend = ts("%1", [1 => $label]);
201 if (!empty($intervalLabels[$lcInterval])) {
202 $legend = $intervalLabels[$lcInterval];
207 if (!empty($dateKeys)) {
209 if (!array_key_exists('multiValue', $rows)) {
210 $rows['multiValue'] = [$rows['value']];
212 foreach ($rows['multiValue'] as $key => $val) {
213 $graph[$key] = array_combine($dateKeys, $rows['multiValue'][$key]);
216 'legend' => "$legend " . CRM_Utils_Array
::value('legend', $rows, ts('Contribution')) . ' ' . ts('Summary'),
217 'values' => $graph[0],
218 'multiValues' => $graph,
219 'barKeys' => CRM_Utils_Array
::value('barKeys', $rows, []),
223 // rotate the x labels.
224 $chartData['xLabelAngle'] = CRM_Utils_Array
::value('xLabelAngle', $rows, 0);
225 if (!empty($rows['tip'])) {
226 $chartData['tip'] = $rows['tip'];
230 $chartData['xname'] = CRM_Utils_Array
::value('xname', $rows);
231 $chartData['yname'] = CRM_Utils_Array
::value('yname', $rows);
233 // carry some chart params if pass.
239 if (!empty($rows[$f])) {
240 $chartData[$f] = $rows[$f];
244 return self
::buildChart($chartData, $chart);
255 public static function reportChart($rows, $chart, $interval, &$chartInfo) {
256 foreach ($interval as $key => $val) {
257 $graph[$val] = $rows['value'][$key];
262 'legend' => $chartInfo['legend'],
263 'xname' => $chartInfo['xname'],
264 'yname' => $chartInfo['yname'],
267 // rotate the x labels.
268 $chartData['xLabelAngle'] = CRM_Utils_Array
::value('xLabelAngle', $chartInfo, 20);
269 if (!empty($chartInfo['tip'])) {
270 $chartData['tip'] = $chartInfo['tip'];
273 // carry some chart params if pass.
279 if (!empty($rows[$f])) {
280 $chartData[$f] = $rows[$f];
284 return self
::buildChart($chartData, $chart);
288 * @param array $params
293 public static function buildChart(&$params, $chart) {
295 if ($chart && is_array($params) && !empty($params)) {
296 // build the chart objects.
297 $chartObj = CRM_Utils_Chart
::$chart($params);
300 // calculate chart size.
301 $xSize = CRM_Utils_Array
::value('xSize', $params, 400);
302 $ySize = CRM_Utils_Array
::value('ySize', $params, 300);
303 if ($chart == 'barChart') {
304 $ySize = CRM_Utils_Array
::value('ySize', $params, 250);
305 $xSize = 60 * count($params['values']);
306 // hack to show tooltip.
308 $xSize = (count($params['values']) > 1) ?
100 * count($params['values']) : 170;
310 elseif ($xSize > 600 && count($params['values']) > 1) {
311 $xSize = (count($params['values']) +
400 / count($params['values'])) * count($params['values']);
315 // generate unique id for this chart instance
316 $uniqueId = md5(uniqid(rand(), TRUE));
318 $theChart["chart_{$uniqueId}"]['size'] = ['xSize' => $xSize, 'ySize' => $ySize];
319 $theChart["chart_{$uniqueId}"]['object'] = $chartObj;
321 // assign chart data to template
322 $template = CRM_Core_Smarty
::singleton();
323 $template->assign('uniqueId', $uniqueId);
324 $template->assign("chartData", json_encode($theChart ??
[]));