From 0c98098b64f06adad1d0dc9146bfbfb6a0cd6b2a Mon Sep 17 00:00:00 2001 From: Deepak Srivastava Date: Thu, 22 Aug 2013 18:38:38 +0530 Subject: [PATCH] CRM-13061, chart fixes --- CRM/Report/Form/Contribute/Summary.php | 37 ++++++-- CRM/Utils/OpenFlashChart.php | 115 ++++++++++++++----------- 2 files changed, 93 insertions(+), 59 deletions(-) diff --git a/CRM/Report/Form/Contribute/Summary.php b/CRM/Report/Form/Contribute/Summary.php index 7e20918d52..ab37082750 100644 --- a/CRM/Report/Form/Contribute/Summary.php +++ b/CRM/Report/Form/Contribute/Summary.php @@ -572,19 +572,38 @@ ROUND(AVG({$this->_aliases['civicrm_contribution_soft']}.amount), 2) as civicrm_ function buildChart(&$rows) { $graphRows = array(); - $count = 0; if (CRM_Utils_Array::value('charts', $this->_params)) { - foreach ($rows as $key => $row) { - if ($row['civicrm_contribution_receive_date_subtotal']) { - $graphRows['receive_date'][] = $row['civicrm_contribution_receive_date_start']; - $graphRows[$this->_interval][] = $row['civicrm_contribution_receive_date_interval']; - $graphRows['value'][] = $row['civicrm_contribution_total_amount_sum']; - $count++; + if (CRM_Utils_Array::value('receive_date', $this->_params['group_bys'])) { + + $contrib = CRM_Utils_Array::value('total_amount', $this->_params['fields']) ? TRUE : FALSE; + $softContrib = CRM_Utils_Array::value('soft_amount', $this->_params['fields']) ? TRUE : FALSE; + + foreach ($rows as $key => $row) { + if ($row['civicrm_contribution_receive_date_subtotal']) { + $graphRows['receive_date'][] = $row['civicrm_contribution_receive_date_start']; + $graphRows[$this->_interval][] = $row['civicrm_contribution_receive_date_interval']; + if ($softContrib && $contrib) { + // both contri & soft contri stats are present + $graphRows['multiValue'][0][] = $row['civicrm_contribution_total_amount_sum']; + $graphRows['multiValue'][1][] = $row['civicrm_contribution_soft_soft_amount_sum']; + } else if ($softContrib) { + // only soft contributions + $graphRows['multiValue'][0][] = $row['civicrm_contribution_soft_soft_amount_sum']; + } else { + // only contributions + $graphRows['multiValue'][0][] = $row['civicrm_contribution_total_amount_sum']; + } + } } - } - if (CRM_Utils_Array::value('receive_date', $this->_params['group_bys'])) { + if ($softContrib && $contrib) { + $graphRows['barKeys'][0] = ts('Contributions'); + $graphRows['barKeys'][1] = ts('Soft Credits'); + $graphRows['legend'] = ts('Contributions and Soft Credits'); + } else if ($softContrib) { + $graphRows['legend'] = ts('Soft Credits'); + } // build the chart. $config = CRM_Core_Config::Singleton(); diff --git a/CRM/Utils/OpenFlashChart.php b/CRM/Utils/OpenFlashChart.php index 1aa0b28504..2ad178f63a 100644 --- a/CRM/Utils/OpenFlashChart.php +++ b/CRM/Utils/OpenFlashChart.php @@ -64,22 +64,22 @@ class CRM_Utils_OpenFlashChart { if (empty($params)) { return $chart; } + if (!CRM_Utils_Array::value('multiValues', $params)) { + $params['multiValues'] = array($params['values']); + } - $values = CRM_Utils_Array::value('values', $params); + $values = CRM_Utils_Array::value('multiValues', $params); if (!is_array($values) || empty($values)) { return $chart; } // get the required data. - $xValues = $yValues = array(); - foreach ($values as $xVal => $yVal) { - $yValues[] = (double)$yVal; - - // we has to have x values as string. - $xValues[] = (string)$xVal; - } $chartTitle = CRM_Utils_Array::value('legend', $params) ? $params['legend'] : ts('Bar Chart'); + $xValues = $yValues = array(); + $xValues = array_keys($values[0]); + $yValues = array_values($values[0]); + //set y axis parameters. $yMin = 0; @@ -90,25 +90,36 @@ class CRM_Utils_OpenFlashChart { } $ySteps = $yMax / 5; - // $bar = new bar( ); - // glass seem to be more cool - $bar = new bar_glass(); - - //set values. - $bar->set_values($yValues); - - // call user define function to handle on click event. - if ($onClickFunName = CRM_Utils_Array::value('on_click_fun_name', $params)) { - $bar->set_on_click($onClickFunName); - } - - // get the currency. + $bars = array(); $config = CRM_Core_Config::singleton(); $symbol = $config->defaultCurrencySymbol; + foreach ($values as $barCount => $barVal) { + $bars[$barCount] = new bar_glass(); - // set the tooltip. - $tooltip = CRM_Utils_Array::value('tip', $params, "$symbol #val#"); - $bar->set_tooltip($tooltip); + $yValues = array_values($barVal); + foreach ($yValues as &$yVal) { + // type casting is required for chart to render values correctly + $yVal = (double) $yVal; + } + $bars[$barCount]->set_values($yValues); + if ($barCount > 0) { + // FIXME: for bars > 2, we'll need to come out with other colors + $bars[$barCount]->colour( '#BF3B69'); + } + + if ($barKey = CRM_Utils_Array::value($barCount, CRM_Utils_Array::value('barKeys', $params))) { + $bars[$barCount]->key($barKey,12); + } + + // call user define function to handle on click event. + if ($onClickFunName = CRM_Utils_Array::value('on_click_fun_name', $params)) { + $bars[$barCount]->set_on_click($onClickFunName); + } + + // get the currency to set in tooltip. + $tooltip = CRM_Utils_Array::value('tip', $params, "$symbol #val#"); + $bars[$barCount]->set_tooltip($tooltip); + } // create x axis label obj. $xLabels = new x_axis_labels(); @@ -143,7 +154,9 @@ class CRM_Utils_OpenFlashChart { $chart->set_title($title); // add bar element to chart. - $chart->add_element($bar); + foreach ($bars as $bar) { + $chart->add_element($bar); + } // add x axis legend. if ($xName = CRM_Utils_Array::value('xname', $params)) { @@ -372,59 +385,61 @@ class CRM_Utils_OpenFlashChart { } static function chart($rows, $chart, $interval) { - $chartData = array(); + $chartData = $dateKeys = array(); switch ($interval) { case 'Month': foreach ($rows['receive_date'] as $key => $val) { list($year, $month) = explode('-', $val); - $graph[substr($rows['Month'][$key], 0, 3) . ' ' . $year] = $rows['value'][$key]; + $dateKeys[] = substr($rows['Month'][$key], 0, 3) . ' ' . $year; } - - $chartData = array( - 'values' => $graph, - 'legend' => ts('Monthly Contribution Summary'), - ); + $legend = ts('Monthly'); break; case 'Quarter': foreach ($rows['receive_date'] as $key => $val) { list($year, $month) = explode('-', $val); - $graph['Quarter ' . $rows['Quarter'][$key] . ' of ' . $year] = $rows['value'][$key]; + $dateKeys[] = 'Quarter ' . $rows['Quarter'][$key] . ' of ' . $year; } - - $chartData = array( - 'values' => $graph, - 'legend' => ts('Quarterly Contribution Summary'), - ); + $legend = ts('Quarterly'); break; case 'Week': foreach ($rows['receive_date'] as $key => $val) { list($year, $month) = explode('-', $val); - $graph['Week ' . $rows['Week'][$key] . ' of ' . $year] = $rows['value'][$key]; + $dateKeys[] = 'Week ' . $rows['Week'][$key] . ' of ' . $year; } - - $chartData = array( - 'values' => $graph, - 'legend' => ts('Weekly Contribution Summary'), - ); + $legend = ts('Weekly'); break; case 'Year': foreach ($rows['receive_date'] as $key => $val) { list($year, $month) = explode('-', $val); - $graph[$year] = $rows['value'][$key]; + $dateKeys[] = $year; } - $chartData = array( - 'values' => $graph, - 'legend' => ts('Yearly Contribution Summary'), - ); + $legend = ts('Yearly'); break; } + if (!empty($dateKeys)) { + $graph = array(); + if (!array_key_exists('multiValue', $rows)) { + $rows['multiValue'] = array($rows['value']); + } + foreach ($rows['multiValue'] as $key => $val) { + $graph[$key] = array_combine($dateKeys, $rows['multiValue'][$key]); + } + $chartData = + array( + 'legend' => "$legend " . CRM_Utils_Array::value('legend', $rows, ts('Contribution')) . ' ' . ts('Summary'), + 'values' => $graph[0], + 'multiValues' => $graph, + 'barKeys' => CRM_Utils_Array::value('barKeys', $rows, array()), + ); + } + // rotate the x labels. - $chartData['xLabelAngle'] = CRM_Utils_Array::value('xLabelAngle', $rows, 20); + $chartData['xLabelAngle'] = CRM_Utils_Array::value('xLabelAngle', $rows, 0); if (CRM_Utils_Array::value('tip', $rows)) { $chartData['tip'] = $rows['tip']; } -- 2.25.1