From dc61ee93168cacf5ae0025cd1dba1e2ad4dd1014 Mon Sep 17 00:00:00 2001 From: Rich Lott / Artful Robot Date: Mon, 7 Oct 2019 21:25:00 +0100 Subject: [PATCH] WIP checkin WIP: bar chart sort of working now Pie and Bar charts working, with download in SVG and PNG links oops remove accidental inclusion remove accidentally included crap remove 3d barchart as unnecessary (and unimplemented) --- CRM/Contribute/Form/ContributionCharts.php | 6 +- CRM/Report/Form/Contribute/Lybunt.php | 2 +- CRM/Report/Form/Contribute/Summary.php | 2 +- CRM/Report/Form/Contribute/Sybunt.php | 2 +- CRM/Report/Form/Event/IncomeCountSummary.php | 2 +- CRM/Report/Form/Event/Summary.php | 2 +- CRM/Report/Form/Mailing/Bounce.php | 2 +- CRM/Report/Form/Mailing/Clicks.php | 2 +- CRM/Report/Form/Mailing/Opened.php | 2 +- CRM/Report/Form/Mailing/Summary.php | 4 +- CRM/Report/Form/Member/Summary.php | 4 +- CRM/Report/Form/Membership/Summary.php | 2 +- CRM/Utils/Chart.php | 331 ++++++++++ CRM/Utils/OpenFlashChart.php | 567 ------------------ .../CRM/Contact/Page/DashBoardDashlet.tpl | 2 +- .../Contribute/Form/ContributionCharts.tpl | 20 +- templates/CRM/Report/Form/Layout/Graph.tpl | 76 +-- templates/CRM/common/chart.tpl | 167 ++++++ templates/CRM/common/openFlashChart.tpl | 64 -- 19 files changed, 556 insertions(+), 703 deletions(-) create mode 100644 CRM/Utils/Chart.php delete mode 100644 CRM/Utils/OpenFlashChart.php create mode 100644 templates/CRM/common/chart.tpl delete mode 100644 templates/CRM/common/openFlashChart.tpl diff --git a/CRM/Contribute/Form/ContributionCharts.php b/CRM/Contribute/Form/ContributionCharts.php index ac5866da5b..8e4873c44e 100644 --- a/CRM/Contribute/Form/ContributionCharts.php +++ b/CRM/Contribute/Form/ContributionCharts.php @@ -177,7 +177,7 @@ class CRM_Contribute_Form_ContributionCharts extends CRM_Core_Form { $funName = ($chartType == 'bvg') ? 'barChart' : 'pieChart'; // build the chart objects. - $values['object'] = CRM_Utils_OpenFlashChart::$funName($values); + $values['object'] = CRM_Utils_Chart::$funName($values); //build the urls. $urlCnt = 0; @@ -230,8 +230,8 @@ class CRM_Contribute_Form_ContributionCharts extends CRM_Core_Form { // finally assign this chart data to template. $this->assign('hasYearlyChart', $yearlyChart); $this->assign('hasByMonthChart', $monthlyChart); - $this->assign('hasOpenFlashChart', empty($chartData) ? FALSE : TRUE); - $this->assign('openFlashChartData', json_encode($chartData)); + $this->assign('hasChart', empty($chartData) ? FALSE : TRUE); + $this->assign('ChartData', json_encode($chartData)); } } diff --git a/CRM/Report/Form/Contribute/Lybunt.php b/CRM/Report/Form/Contribute/Lybunt.php index 9c6704058a..15526bd030 100644 --- a/CRM/Report/Form/Contribute/Lybunt.php +++ b/CRM/Report/Form/Contribute/Lybunt.php @@ -660,7 +660,7 @@ class CRM_Report_Form_Contribute_Lybunt extends CRM_Report_Form { ]; if ($this->_params['charts']) { // build chart. - CRM_Utils_OpenFlashChart::reportChart($graphRows, $this->_params['charts'], $interval, $chartInfo); + CRM_Utils_Chart::reportChart($graphRows, $this->_params['charts'], $interval, $chartInfo); $this->assign('chartType', $this->_params['charts']); } } diff --git a/CRM/Report/Form/Contribute/Summary.php b/CRM/Report/Form/Contribute/Summary.php index bffb359186..2239525f91 100644 --- a/CRM/Report/Form/Contribute/Summary.php +++ b/CRM/Report/Form/Contribute/Summary.php @@ -849,7 +849,7 @@ ROUND(AVG({$this->_aliases['civicrm_contribution_soft']}.amount), 2) as civicrm_ $config = CRM_Core_Config::Singleton(); $graphRows['xname'] = $this->_interval; $graphRows['yname'] = ts('Amount (%1)', array(1 => $config->defaultCurrency)); - CRM_Utils_OpenFlashChart::chart($graphRows, $this->_params['charts'], $this->_interval); + CRM_Utils_Chart::chart($graphRows, $this->_params['charts'], $this->_interval); $this->assign('chartType', $this->_params['charts']); } } diff --git a/CRM/Report/Form/Contribute/Sybunt.php b/CRM/Report/Form/Contribute/Sybunt.php index 56c3d45e31..98ac7baefc 100644 --- a/CRM/Report/Form/Contribute/Sybunt.php +++ b/CRM/Report/Form/Contribute/Sybunt.php @@ -544,7 +544,7 @@ class CRM_Report_Form_Contribute_Sybunt extends CRM_Report_Form { ]; if ($this->_params['charts']) { // build the chart. - CRM_Utils_OpenFlashChart::reportChart($graphRows, $this->_params['charts'], $interval, $chartInfo); + CRM_Utils_Chart::reportChart($graphRows, $this->_params['charts'], $interval, $chartInfo); $this->assign('chartType', $this->_params['charts']); } } diff --git a/CRM/Report/Form/Event/IncomeCountSummary.php b/CRM/Report/Form/Event/IncomeCountSummary.php index 25c5c533b2..40373be36c 100644 --- a/CRM/Report/Form/Event/IncomeCountSummary.php +++ b/CRM/Report/Form/Event/IncomeCountSummary.php @@ -399,7 +399,7 @@ class CRM_Report_Form_Event_IncomeCountSummary extends CRM_Report_Form { $chartInfo['xLabelAngle'] = 20; // build the chart. - CRM_Utils_OpenFlashChart::buildChart($chartInfo, $this->_params['charts']); + CRM_Utils_Chart::buildChart($chartInfo, $this->_params['charts']); } } } diff --git a/CRM/Report/Form/Event/Summary.php b/CRM/Report/Form/Event/Summary.php index bfbc8e5a91..b046a7b779 100644 --- a/CRM/Report/Form/Event/Summary.php +++ b/CRM/Report/Form/Event/Summary.php @@ -368,7 +368,7 @@ class CRM_Report_Form_Event_Summary extends CRM_Report_Form { $chartInfo['xLabelAngle'] = 20; // build the chart. - CRM_Utils_OpenFlashChart::buildChart($chartInfo, $this->_params['charts']); + CRM_Utils_Chart::buildChart($chartInfo, $this->_params['charts']); $this->assign('chartType', $this->_params['charts']); } } diff --git a/CRM/Report/Form/Mailing/Bounce.php b/CRM/Report/Form/Mailing/Bounce.php index 9cb491a484..9130fb95c7 100644 --- a/CRM/Report/Form/Mailing/Bounce.php +++ b/CRM/Report/Form/Mailing/Bounce.php @@ -429,7 +429,7 @@ class CRM_Report_Form_Mailing_Bounce extends CRM_Report_Form { } // build the chart. - CRM_Utils_OpenFlashChart::buildChart($chartInfo, $this->_params['charts']); + CRM_Utils_Chart::buildChart($chartInfo, $this->_params['charts']); $this->assign('chartType', $this->_params['charts']); } diff --git a/CRM/Report/Form/Mailing/Clicks.php b/CRM/Report/Form/Mailing/Clicks.php index b2239e02b2..032e47d150 100644 --- a/CRM/Report/Form/Mailing/Clicks.php +++ b/CRM/Report/Form/Mailing/Clicks.php @@ -349,7 +349,7 @@ class CRM_Report_Form_Mailing_Clicks extends CRM_Report_Form { } // build the chart. - CRM_Utils_OpenFlashChart::buildChart($chartInfo, $this->_params['charts']); + CRM_Utils_Chart::buildChart($chartInfo, $this->_params['charts']); $this->assign('chartType', $this->_params['charts']); } diff --git a/CRM/Report/Form/Mailing/Opened.php b/CRM/Report/Form/Mailing/Opened.php index f040552ca4..1c2893f2a8 100644 --- a/CRM/Report/Form/Mailing/Opened.php +++ b/CRM/Report/Form/Mailing/Opened.php @@ -340,7 +340,7 @@ class CRM_Report_Form_Mailing_Opened extends CRM_Report_Form { } // build the chart. - CRM_Utils_OpenFlashChart::buildChart($chartInfo, $this->_params['charts']); + CRM_Utils_Chart::buildChart($chartInfo, $this->_params['charts']); $this->assign('chartType', $this->_params['charts']); } diff --git a/CRM/Report/Form/Mailing/Summary.php b/CRM/Report/Form/Mailing/Summary.php index 549eaf7cda..a43467175c 100644 --- a/CRM/Report/Form/Mailing/Summary.php +++ b/CRM/Report/Form/Mailing/Summary.php @@ -42,7 +42,7 @@ class CRM_Report_Form_Mailing_Summary extends CRM_Report_Form { protected $_charts = [ '' => 'Tabular', - 'bar_3dChart' => 'Bar Chart', + 'barchart' => 'Bar Chart', ]; /** @@ -625,7 +625,7 @@ class CRM_Report_Form_Mailing_Summary extends CRM_Report_Form { $chartInfo['xSize'] = ((count($rows) * 125) + (count($rows) * count($criteria) * 40)); // build the chart. - CRM_Utils_OpenFlashChart::buildChart($chartInfo, $this->_params['charts']); + CRM_Utils_Chart::buildChart($chartInfo, $this->_params['charts']); $this->assign('chartType', $this->_params['charts']); } diff --git a/CRM/Report/Form/Member/Summary.php b/CRM/Report/Form/Member/Summary.php index 78084e4ab7..620189b553 100644 --- a/CRM/Report/Form/Member/Summary.php +++ b/CRM/Report/Form/Member/Summary.php @@ -524,10 +524,10 @@ GROUP BY {$this->_aliases['civicrm_contribution']}.currency 'xname' => ts('Member Since / Member Type'), 'yname' => ts('Fees'), ]; - CRM_Utils_OpenFlashChart::reportChart($graphRows, $this->_params['charts'], $interval, $chartInfo); + CRM_Utils_Chart::reportChart($graphRows, $this->_params['charts'], $interval, $chartInfo); } else { - CRM_Utils_OpenFlashChart::chart($graphRows, $this->_params['charts'], $this->_interval); + CRM_Utils_Chart::chart($graphRows, $this->_params['charts'], $this->_interval); } } $this->assign('chartType', $this->_params['charts']); diff --git a/CRM/Report/Form/Membership/Summary.php b/CRM/Report/Form/Membership/Summary.php index 9ab478c084..de12e54c11 100644 --- a/CRM/Report/Form/Membership/Summary.php +++ b/CRM/Report/Form/Membership/Summary.php @@ -342,7 +342,7 @@ LEFT JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} } // build chart. - CRM_Utils_OpenFlashChart::chart($graphRows, $this->_params['charts'], $this->_interval); + CRM_Utils_Chart::chart($graphRows, $this->_params['charts'], $this->_interval); } parent::endPostProcess(); } diff --git a/CRM/Utils/Chart.php b/CRM/Utils/Chart.php new file mode 100644 index 0000000000..4bf91281bf --- /dev/null +++ b/CRM/Utils/Chart.php @@ -0,0 +1,331 @@ + ts('Bar Chart')]; + + // ? Not sure what reports use this, but it's not implemented. + // 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#"); + + return $output; + } + + /** + * Build a pie chart. + * + * @param array $params + * Assoc array of name/value pairs. + * + * @return array + */ + public static function pieChart($params) { + $output = static::commonParamsManipulation($params); + if (empty($output)) { + return NULL; + } + $output['type'] = 'piechart'; + $output += ['title' => ts('Pie Chart')]; + + // ? Not sure what reports use this, but it's not implemented. + // 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#"); + + return $output; + } + + /** + * Common data manipulation for charts. + * + * @param array $params + * Assoc array of name/value pairs. + * + * @return array + */ + public static function commonParamsManipulation($params) { + if (empty($params)) { + return NULL; + } + $output = []; + if (empty($params['multiValues'])) { + $params['multiValues'] = [$params['values']]; + } + + $output['values'] = []; + foreach ($params['multiValues'] as $i => $dataSet) { + $output['values'][$i] = []; + foreach ($dataSet as $k => $v) { + $output['values'][$i][] = ['label' => $k, 'value' => (double) $v]; + } + } + if (!$output['values']) { + return NULL; + } + + // Ensure there's a legend (title) + if (!empty($params['legend'])) { + $output['title'] = $params['legend']; + } + + $output['symbol'] = CRM_Core_BAO_Country::defaultCurrencySymbol(); + + // ? Not sure what reports use this, but it's not implemented. + // 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#"); + + return $output; + } + + /** + * @param $rows + * @param $chart + * @param $interval + * + * @return array + */ + public static function chart($rows, $chart, $interval) { + $lcInterval = strtolower($interval); + $label = ucfirst($lcInterval); + $chartData = $dateKeys = []; + $intervalLabels = [ + 'year' => ts('Yearly'), + 'fiscalyear' => ts('Yearly (Fiscal)'), + 'month' => ts('Monthly'), + 'quarter' => ts('Quarterly'), + 'week' => ts('Weekly'), + 'yearweek' => ts('Weekly'), + ]; + + switch ($lcInterval) { + case 'month': + case 'quarter': + case 'week': + case 'yearweek': + foreach ($rows['receive_date'] as $key => $val) { + list($year, $month) = explode('-', $val); + $dateKeys[] = substr($rows[$interval][$key], 0, 3) . ' of ' . $year; + } + $legend = $intervalLabels[$lcInterval]; + break; + + default: + foreach ($rows['receive_date'] as $key => $val) { + list($year, $month) = explode('-', $val); + $dateKeys[] = $year; + } + $legend = ts("%1", [1 => $label]); + if (!empty($intervalLabels[$lcInterval])) { + $legend = $intervalLabels[$lcInterval]; + } + break; + } + + if (!empty($dateKeys)) { + $graph = []; + if (!array_key_exists('multiValue', $rows)) { + $rows['multiValue'] = [$rows['value']]; + } + foreach ($rows['multiValue'] as $key => $val) { + $graph[$key] = array_combine($dateKeys, $rows['multiValue'][$key]); + } + $chartData = [ + 'legend' => "$legend " . CRM_Utils_Array::value('legend', $rows, ts('Contribution')) . ' ' . ts('Summary'), + 'values' => $graph[0], + 'multiValues' => $graph, + 'barKeys' => CRM_Utils_Array::value('barKeys', $rows, []), + ]; + } + + // rotate the x labels. + $chartData['xLabelAngle'] = CRM_Utils_Array::value('xLabelAngle', $rows, 0); + if (!empty($rows['tip'])) { + $chartData['tip'] = $rows['tip']; + } + + // legend + $chartData['xname'] = CRM_Utils_Array::value('xname', $rows); + $chartData['yname'] = CRM_Utils_Array::value('yname', $rows); + + // carry some chart params if pass. + foreach ([ + 'xSize', + 'ySize', + 'divName', + ] as $f) { + if (!empty($rows[$f])) { + $chartData[$f] = $rows[$f]; + } + } + + return self::buildChart($chartData, $chart); + } + + /** + * @param $rows + * @param $chart + * @param $interval + * @param $chartInfo + * + * @return array + */ + public static function reportChart($rows, $chart, $interval, &$chartInfo) { + foreach ($interval as $key => $val) { + $graph[$val] = $rows['value'][$key]; + } + + $chartData = [ + 'values' => $graph, + 'legend' => $chartInfo['legend'], + 'xname' => $chartInfo['xname'], + 'yname' => $chartInfo['yname'], + ]; + + // rotate the x labels. + $chartData['xLabelAngle'] = CRM_Utils_Array::value('xLabelAngle', $chartInfo, 20); + if (!empty($chartInfo['tip'])) { + $chartData['tip'] = $chartInfo['tip']; + } + + // carry some chart params if pass. + foreach ([ + 'xSize', + 'ySize', + 'divName', + ] as $f) { + if (!empty($rows[$f])) { + $chartData[$f] = $rows[$f]; + } + } + + return self::buildChart($chartData, $chart); + } + + /** + * @param array $params + * @param $chart + * + * @return array + */ + public static function buildChart(&$params, $chart) { + $theChart = []; + if ($chart && is_array($params) && !empty($params)) { + // build the chart objects. + $chartObj = CRM_Utils_Chart::$chart($params); + + if ($chartObj) { + // calculate chart size. + $xSize = CRM_Utils_Array::value('xSize', $params, 400); + $ySize = CRM_Utils_Array::value('ySize', $params, 300); + if ($chart == 'barChart') { + $ySize = CRM_Utils_Array::value('ySize', $params, 250); + $xSize = 60 * count($params['values']); + // hack to show tooltip. + if ($xSize < 200) { + $xSize = (count($params['values']) > 1) ? 100 * count($params['values']) : 170; + } + elseif ($xSize > 600 && count($params['values']) > 1) { + $xSize = (count($params['values']) + 400 / count($params['values'])) * count($params['values']); + } + } + + // generate unique id for this chart instance + $uniqueId = md5(uniqid(rand(), TRUE)); + + $theChart["chart_{$uniqueId}"]['size'] = ['xSize' => $xSize, 'ySize' => $ySize]; + $theChart["chart_{$uniqueId}"]['object'] = $chartObj; + + // assign chart data to template + $template = CRM_Core_Smarty::singleton(); + $template->assign('uniqueId', $uniqueId); + $template->assign("chartData", json_encode($theChart)); + } + } + + return $theChart; + } + +} diff --git a/CRM/Utils/OpenFlashChart.php b/CRM/Utils/OpenFlashChart.php deleted file mode 100644 index 178f093bb0..0000000000 --- a/CRM/Utils/OpenFlashChart.php +++ /dev/null @@ -1,567 +0,0 @@ - $barVal) { - $bars[$barCount] = new bar_glass(); - - $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(); - // set_labels function requires xValues array of string or x_axis_label - // so type casting array values to string values - array_walk($xValues, function (&$value, $index) { - $value = (string) $value; - }); - $xLabels->set_labels($xValues); - - // set angle for labels. - if ($xLabelAngle = CRM_Utils_Array::value('xLabelAngle', $params)) { - $xLabels->rotate($xLabelAngle); - } - - // create x axis obj. - $xAxis = new x_axis(); - $xAxis->set_labels($xLabels); - - // create y axis and set range. - $yAxis = new y_axis(); - $yAxis->set_range($yMin, $yMax, $ySteps); - - // create chart title obj. - $title = new title($chartTitle); - - // create chart. - $chart = new open_flash_chart(); - - // add x axis w/ labels to chart. - $chart->set_x_axis($xAxis); - - // add y axis values to chart. - $chart->add_y_axis($yAxis); - - // set title to chart. - $chart->set_title($title); - - // add bar element to chart. - foreach ($bars as $bar) { - $chart->add_element($bar); - } - - // add x axis legend. - if ($xName = CRM_Utils_Array::value('xname', $params)) { - $xLegend = new x_legend($xName); - $xLegend->set_style("{font-size: 13px; color:#000000; font-family: Verdana; text-align: center;}"); - $chart->set_x_legend($xLegend); - } - - // add y axis legend. - if ($yName = CRM_Utils_Array::value('yname', $params)) { - $yLegend = new y_legend($yName); - $yLegend->set_style("{font-size: 13px; color:#000000; font-family: Verdana; text-align: center;}"); - $chart->set_y_legend($yLegend); - } - - return $chart; - } - - /** - * Build The Pie Gharph. - * - * @param array $params - * Assoc array of name/value pairs. - * - * @return object - * $chart object of open flash chart. - */ - public static function &pieChart(&$params) { - $chart = NULL; - if (empty($params)) { - return $chart; - } - $allValues = CRM_Utils_Array::value('values', $params); - if (!is_array($allValues) || empty($allValues)) { - return $chart; - } - - // get the required data. - $values = []; - foreach ($allValues as $label => $value) { - $values[] = new pie_value((double) $value, $label); - } - $graphTitle = !empty($params['legend']) ? $params['legend'] : ts('Pie Chart'); - - // get the currency. - $symbol = CRM_Core_BAO_Country::defaultCurrencySymbol(); - - $pie = new pie(); - $pie->radius(100); - - // call user define function to handle on click event. - if ($onClickFunName = CRM_Utils_Array::value('on_click_fun_name', $params)) { - $pie->on_click($onClickFunName); - } - - $pie->set_start_angle(35); - $pie->add_animation(new pie_fade()); - $pie->add_animation(new pie_bounce(2)); - - // set the tooltip. - $tooltip = CRM_Utils_Array::value('tip', $params, "Amount is $symbol #val# of $symbol #total#
#percent#"); - $pie->set_tooltip($tooltip); - - // set colours. - $pie->set_colours(self::$_colours); - - $pie->set_values($values); - - // create chart. - $chart = new open_flash_chart(); - - // create chart title obj. - $title = new title($graphTitle); - $chart->set_title($title); - - $chart->add_element($pie); - $chart->x_axis = NULL; - - return $chart; - } - - /** - * Build The 3-D Bar Gharph. - * - * @param array $params - * Assoc array of name/value pairs. - * - * @return object - * $chart object of open flash chart. - */ - public static function &bar_3dChart(&$params) { - $chart = NULL; - if (empty($params)) { - return $chart; - } - - // $params['values'] should contains the values for each - // criteria defined in $params['criteria'] - $values = CRM_Utils_Array::value('values', $params); - $criteria = CRM_Utils_Array::value('criteria', $params); - if (!is_array($values) || empty($values) || !is_array($criteria) || empty($criteria)) { - return $chart; - } - - // get the required data. - $xReferences = $xValueLabels = $xValues = $yValues = []; - - foreach ($values as $xVal => $yVal) { - if (!is_array($yVal) || empty($yVal)) { - continue; - } - - $xValueLabels[] = (string) $xVal; - foreach ($criteria as $criteria) { - $xReferences[$criteria][$xVal] = (double) CRM_Utils_Array::value($criteria, $yVal, 0); - $yValues[] = (double) CRM_Utils_Array::value($criteria, $yVal, 0); - } - } - - if (empty($xReferences)) { - - return $chart; - - } - - // get the currency. - $symbol = CRM_Core_BAO_Country::defaultCurrencySymbol(); - - // set the tooltip. - $tooltip = CRM_Utils_Array::value('tip', $params, "$symbol #val#"); - - $count = 0; - foreach ($xReferences as $criteria => $values) { - $toolTipVal = $tooltip; - // for separate tooltip for each criteria - if (is_array($tooltip)) { - $toolTipVal = CRM_Utils_Array::value($criteria, $tooltip, "$symbol #val#"); - } - - // create bar_3d object - $xValues[$count] = new bar_3d(); - // set colour pattel - $xValues[$count]->set_colour(self::$_colours[$count]); - // define colur pattel with bar criteria - $xValues[$count]->key((string) $criteria, 12); - // define bar chart values - $xValues[$count]->set_values(array_values($values)); - - // set tooltip - $xValues[$count]->set_tooltip($toolTipVal); - $count++; - } - - $chartTitle = !empty($params['legend']) ? $params['legend'] : ts('Bar Chart'); - - // set y axis parameters. - $yMin = 0; - - // calculate max scale for graph. - $yMax = ceil(max($yValues)); - if ($mod = $yMax % (str_pad(5, strlen($yMax) - 1, 0))) { - $yMax += str_pad(5, strlen($yMax) - 1, 0) - $mod; - } - - // if max value of y-axis <= 0, then set default values - if ($yMax <= 0) { - $ySteps = 1; - $yMax = 5; - } - else { - $ySteps = $yMax / 5; - } - - // create x axis label obj. - $xLabels = new x_axis_labels(); - $xLabels->set_labels($xValueLabels); - - // set angle for labels. - if ($xLabelAngle = CRM_Utils_Array::value('xLabelAngle', $params)) { - $xLabels->rotate($xLabelAngle); - } - - // create x axis obj. - $xAxis = new x_axis(); - $xAxis->set_labels($xLabels); - - // create y axis and set range. - $yAxis = new y_axis(); - $yAxis->set_range($yMin, $yMax, $ySteps); - - // create chart title obj. - $title = new title($chartTitle); - - // create chart. - $chart = new open_flash_chart(); - - // add x axis w/ labels to chart. - $chart->set_x_axis($xAxis); - - // add y axis values to chart. - $chart->add_y_axis($yAxis); - - // set title to chart. - $chart->set_title($title); - - foreach ($xValues as $bar) { - // add bar element to chart. - $chart->add_element($bar); - } - - // add x axis legend. - if ($xName = CRM_Utils_Array::value('xname', $params)) { - $xLegend = new x_legend($xName); - $xLegend->set_style("{font-size: 13px; color:#000000; font-family: Verdana; text-align: center;}"); - $chart->set_x_legend($xLegend); - } - - // add y axis legend. - if ($yName = CRM_Utils_Array::value('yname', $params)) { - $yLegend = new y_legend($yName); - $yLegend->set_style("{font-size: 13px; color:#000000; font-family: Verdana; text-align: center;}"); - $chart->set_y_legend($yLegend); - } - - return $chart; - } - - /** - * @param $rows - * @param $chart - * @param $interval - * - * @return array - */ - public static function chart($rows, $chart, $interval) { - $lcInterval = strtolower($interval); - $label = ucfirst($lcInterval); - $chartData = $dateKeys = []; - $intervalLabels = [ - 'year' => ts('Yearly'), - 'fiscalyear' => ts('Yearly (Fiscal)'), - 'month' => ts('Monthly'), - 'quarter' => ts('Quarterly'), - 'week' => ts('Weekly'), - 'yearweek' => ts('Weekly'), - ]; - - switch ($lcInterval) { - case 'month': - case 'quarter': - case 'week': - case 'yearweek': - foreach ($rows['receive_date'] as $key => $val) { - list($year, $month) = explode('-', $val); - $dateKeys[] = substr($rows[$interval][$key], 0, 3) . ' of ' . $year; - } - $legend = $intervalLabels[$lcInterval]; - break; - - default: - foreach ($rows['receive_date'] as $key => $val) { - list($year, $month) = explode('-', $val); - $dateKeys[] = $year; - } - $legend = ts("%1", [1 => $label]); - if (!empty($intervalLabels[$lcInterval])) { - $legend = $intervalLabels[$lcInterval]; - } - break; - } - - if (!empty($dateKeys)) { - $graph = []; - if (!array_key_exists('multiValue', $rows)) { - $rows['multiValue'] = [$rows['value']]; - } - foreach ($rows['multiValue'] as $key => $val) { - $graph[$key] = array_combine($dateKeys, $rows['multiValue'][$key]); - } - $chartData = [ - 'legend' => "$legend " . CRM_Utils_Array::value('legend', $rows, ts('Contribution')) . ' ' . ts('Summary'), - 'values' => $graph[0], - 'multiValues' => $graph, - 'barKeys' => CRM_Utils_Array::value('barKeys', $rows, []), - ]; - } - - // rotate the x labels. - $chartData['xLabelAngle'] = CRM_Utils_Array::value('xLabelAngle', $rows, 0); - if (!empty($rows['tip'])) { - $chartData['tip'] = $rows['tip']; - } - - // legend - $chartData['xname'] = CRM_Utils_Array::value('xname', $rows); - $chartData['yname'] = CRM_Utils_Array::value('yname', $rows); - - // carry some chart params if pass. - foreach ([ - 'xSize', - 'ySize', - 'divName', - ] as $f) { - if (!empty($rows[$f])) { - $chartData[$f] = $rows[$f]; - } - } - - return self::buildChart($chartData, $chart); - } - - /** - * @param $rows - * @param $chart - * @param $interval - * @param $chartInfo - * - * @return array - */ - public static function reportChart($rows, $chart, $interval, &$chartInfo) { - foreach ($interval as $key => $val) { - $graph[$val] = $rows['value'][$key]; - } - - $chartData = [ - 'values' => $graph, - 'legend' => $chartInfo['legend'], - 'xname' => $chartInfo['xname'], - 'yname' => $chartInfo['yname'], - ]; - - // rotate the x labels. - $chartData['xLabelAngle'] = CRM_Utils_Array::value('xLabelAngle', $chartInfo, 20); - if (!empty($chartInfo['tip'])) { - $chartData['tip'] = $chartInfo['tip']; - } - - // carry some chart params if pass. - foreach ([ - 'xSize', - 'ySize', - 'divName', - ] as $f) { - if (!empty($rows[$f])) { - $chartData[$f] = $rows[$f]; - } - } - - return self::buildChart($chartData, $chart); - } - - /** - * @param array $params - * @param $chart - * - * @return array - */ - public static function buildChart(&$params, $chart) { - $openFlashChart = []; - if ($chart && is_array($params) && !empty($params)) { - // build the chart objects. - $chartObj = CRM_Utils_OpenFlashChart::$chart($params); - - $openFlashChart = []; - if ($chartObj) { - // calculate chart size. - $xSize = CRM_Utils_Array::value('xSize', $params, 400); - $ySize = CRM_Utils_Array::value('ySize', $params, 300); - if ($chart == 'barChart') { - $ySize = CRM_Utils_Array::value('ySize', $params, 250); - $xSize = 60 * count($params['values']); - // hack to show tooltip. - if ($xSize < 200) { - $xSize = (count($params['values']) > 1) ? 100 * count($params['values']) : 170; - } - elseif ($xSize > 600 && count($params['values']) > 1) { - $xSize = (count($params['values']) + 400 / count($params['values'])) * count($params['values']); - } - } - - // generate unique id for this chart instance - $uniqueId = md5(uniqid(rand(), TRUE)); - - $openFlashChart["chart_{$uniqueId}"]['size'] = ['xSize' => $xSize, 'ySize' => $ySize]; - $openFlashChart["chart_{$uniqueId}"]['object'] = $chartObj; - - // assign chart data to template - $template = CRM_Core_Smarty::singleton(); - $template->assign('uniqueId', $uniqueId); - $template->assign("openFlashChartData", json_encode($openFlashChart)); - } - } - - return $openFlashChart; - } - -} diff --git a/templates/CRM/Contact/Page/DashBoardDashlet.tpl b/templates/CRM/Contact/Page/DashBoardDashlet.tpl index 607abd348e..61d8adccf9 100644 --- a/templates/CRM/Contact/Page/DashBoardDashlet.tpl +++ b/templates/CRM/Contact/Page/DashBoardDashlet.tpl @@ -24,7 +24,7 @@ +--------------------------------------------------------------------+ *} {include file="CRM/common/dashboard.tpl"} -{include file="CRM/common/openFlashChart.tpl"} +{include file="CRM/common/chart.tpl"} {* Alerts for critical configuration settings. *} {$communityMessages}
diff --git a/templates/CRM/Contribute/Form/ContributionCharts.tpl b/templates/CRM/Contribute/Form/ContributionCharts.tpl index 4db862fc7d..3e6c01a0f9 100644 --- a/templates/CRM/Contribute/Form/ContributionCharts.tpl +++ b/templates/CRM/Contribute/Form/ContributionCharts.tpl @@ -54,33 +54,31 @@
{/if} -{if $hasOpenFlashChart} -{include file="CRM/common/openFlashChart.tpl" contriChart=true} +{if $hasChart} +{include file="CRM/common/chart.tpl" contriChart=true} {literal} -{/literal} -{/if} + function buildChart( ) { + $.each( allData, function( chartID, chartValues ) { + var divName = {/literal}"chart_{$uniqueId}"{literal}; + createChart( chartID, divName, chartValues.size.xSize, chartValues.size.ySize, allData[chartID].object ); + }); + } + }); + + + {/literal} {/if} diff --git a/templates/CRM/common/chart.tpl b/templates/CRM/common/chart.tpl new file mode 100644 index 0000000000..e3896708df --- /dev/null +++ b/templates/CRM/common/chart.tpl @@ -0,0 +1,167 @@ +{* + +--------------------------------------------------------------------+ + | CiviCRM version 5 | + +--------------------------------------------------------------------+ + | Copyright CiviCRM LLC (c) 2004-2019 | + +--------------------------------------------------------------------+ + | This file is a part of CiviCRM. | + | | + | CiviCRM is free software; you can copy, modify, and distribute it | + | under the terms of the GNU Affero General Public License | + | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | + | | + | CiviCRM is distributed in the hope that it will be useful, but | + | WITHOUT ANY WARRANTY; without even the implied warranty of | + | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | + | See the GNU Affero General Public License for more details. | + | | + | You should have received a copy of the GNU Affero General Public | + | License and the CiviCRM Licensing Exception along | + | with this program; if not, contact CiviCRM LLC | + | at info[AT]civicrm[DOT]org. If you have questions about the | + | GNU Affero General Public License or the licensing of CiviCRM, | + | see the CiviCRM license FAQ at http://civicrm.org/licensing | + +--------------------------------------------------------------------+ +*} + + + + +{literal} + + +{/literal} diff --git a/templates/CRM/common/openFlashChart.tpl b/templates/CRM/common/openFlashChart.tpl deleted file mode 100644 index 0995cd1d59..0000000000 --- a/templates/CRM/common/openFlashChart.tpl +++ /dev/null @@ -1,64 +0,0 @@ -{* - +--------------------------------------------------------------------+ - | CiviCRM version 5 | - +--------------------------------------------------------------------+ - | Copyright CiviCRM LLC (c) 2004-2019 | - +--------------------------------------------------------------------+ - | This file is a part of CiviCRM. | - | | - | CiviCRM is free software; you can copy, modify, and distribute it | - | under the terms of the GNU Affero General Public License | - | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | - | | - | CiviCRM is distributed in the hope that it will be useful, but | - | WITHOUT ANY WARRANTY; without even the implied warranty of | - | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | - | See the GNU Affero General Public License for more details. | - | | - | You should have received a copy of the GNU Affero General Public | - | License and the CiviCRM Licensing Exception along | - | with this program; if not, contact CiviCRM LLC | - | at info[AT]civicrm[DOT]org. If you have questions about the | - | GNU Affero General Public License or the licensing of CiviCRM, | - | see the CiviCRM license FAQ at http://civicrm.org/licensing | - +--------------------------------------------------------------------+ -*} - - -{literal} - -{/literal} -- 2.25.1