$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;
// 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));
}
}
];
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']);
}
}
$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']);
}
}
];
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']);
}
}
$chartInfo['xLabelAngle'] = 20;
// build the chart.
- CRM_Utils_OpenFlashChart::buildChart($chartInfo, $this->_params['charts']);
+ CRM_Utils_Chart::buildChart($chartInfo, $this->_params['charts']);
}
}
}
$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']);
}
}
}
// 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']);
}
}
// 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']);
}
}
// 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']);
}
protected $_charts = [
'' => 'Tabular',
- 'bar_3dChart' => 'Bar Chart',
+ 'barchart' => 'Bar Chart',
];
/**
$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']);
}
'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']);
}
// build chart.
- CRM_Utils_OpenFlashChart::chart($graphRows, $this->_params['charts'], $this->_interval);
+ CRM_Utils_Chart::chart($graphRows, $this->_params['charts'], $this->_interval);
}
parent::endPostProcess();
}
--- /dev/null
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | 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 |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ *
+ * @package CRM
+ * @copyright CiviCRM LLC (c) 2004-2019
+ */
+
+/**
+ * Build various graphs using the dc chart library.
+ */
+class CRM_Utils_Chart {
+
+ /**
+ * Colours.
+ * @var array
+ */
+ private static $_colours = [
+ "#C3CC38",
+ "#C8B935",
+ "#CEA632",
+ "#D3932F",
+ "#D9802C",
+ "#FA6900",
+ "#DC9B57",
+ "#F78F01",
+ "#5AB56E",
+ "#6F8069",
+ "#C92200",
+ "#EB6C5C",
+ ];
+
+ /**
+ * Build The Bar Gharph.
+ *
+ * @param array $params
+ * Assoc array of name/value pairs.
+ *
+ * @return object
+ * $chart object of open flash chart.
+ */
+ public static function barChart($params) {
+ $output = static::commonParamsManipulation($params);
+ if (empty($output)) {
+ return NULL;
+ }
+ $output['type'] = 'barchart';
+ // Default title
+ $output += ['title' => 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;
+ }
+
+}
+++ /dev/null
-<?php
-/*
- +--------------------------------------------------------------------+
- | 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 |
- +--------------------------------------------------------------------+
- */
-
-/**
- *
- * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2019
- */
-
-require_once 'packages/OpenFlashChart/php-ofc-library/open-flash-chart.php';
-
-/**
- * Build various graphs using Open Flash Chart library.
- */
-class CRM_Utils_OpenFlashChart {
-
- /**
- * Colours.
- * @var array
- */
- private static $_colours = [
- "#C3CC38",
- "#C8B935",
- "#CEA632",
- "#D3932F",
- "#D9802C",
- "#FA6900",
- "#DC9B57",
- "#F78F01",
- "#5AB56E",
- "#6F8069",
- "#C92200",
- "#EB6C5C",
- ];
-
- /**
- * Build The Bar Gharph.
- *
- * @param array $params
- * Assoc array of name/value pairs.
- *
- * @return object
- * $chart object of open flash chart.
- */
- public static function &barChart(&$params) {
- $chart = NULL;
- if (empty($params)) {
- return $chart;
- }
- if (empty($params['multiValues'])) {
- $params['multiValues'] = [$params['values']];
- }
-
- $values = CRM_Utils_Array::value('multiValues', $params);
- if (!is_array($values) || empty($values)) {
- return $chart;
- }
-
- // get the required data.
- $chartTitle = !empty($params['legend']) ? $params['legend'] : ts('Bar Chart');
-
- $xValues = $yValues = [];
- $xValues = array_keys($values[0]);
- $yValues = array_values($values[0]);
-
- // 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;
- }
- $ySteps = $yMax / 5;
-
- $bars = [];
- $symbol = CRM_Core_BAO_Country::defaultCurrencySymbol();
- foreach ($values as $barCount => $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# <br>#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;
- }
-
-}
+--------------------------------------------------------------------+
*}
{include file="CRM/common/dashboard.tpl"}
-{include file="CRM/common/openFlashChart.tpl"}
+{include file="CRM/common/chart.tpl"}
{* Alerts for critical configuration settings. *}
{$communityMessages}
<div class="crm-submit-buttons crm-dashboard-controls">
</div>
{/if}
-{if $hasOpenFlashChart}
-{include file="CRM/common/openFlashChart.tpl" contriChart=true}
+{if $hasChart}
+{include file="CRM/common/chart.tpl" contriChart=true}
{literal}
<script type="text/javascript">
+ var allData = {/literal}{$chartData}{literal};
+
CRM.$(function($) {
- var chartData = {/literal}{$openFlashChartData}{literal};
- $.each(chartData, function(chartID, chartValues) {
- createSWFObject(chartID, chartValues.divName, chartValues.size.xSize, chartValues.size.ySize, 'loadData');
+ $.each(allData, function(chartID, chartValues) {
+ createChart(chartID, chartValues.divName, chartValues.size.xSize, chartValues.size.ySize, 'loadData');
});
});
function loadData( chartID ) {
- var allData = {/literal}{$openFlashChartData}{literal};
- return JSON.stringify(allData[chartID].object);
+ return allData[chartID].object;
}
function byMonthOnClick( barIndex ) {
- var allData = {/literal}{$openFlashChartData}{literal};
- var url = eval( "allData.by_month.on_click_urls.url_" + barIndex );
+ var url = allData.by_month.on_click_urls['url_' + barIndex];
if ( url ) window.location.href = url;
}
function byYearOnClick( barIndex ) {
- var allData = {/literal}{$openFlashChartData}{literal};
- var url = eval( "allData.by_year.on_click_urls.url_" + barIndex );
+ var url = allData.by_year.on_click_urls['url_' + barIndex];
if ( url ) window.location.href = url;
}
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*}
-{assign var=uploadURL value=$config->imageUploadURL|replace:'/persist/contribute/':'/persist/'|cat:'openFlashChart/'}
+{assign var=uploadURL value=$config->imageUploadURL|replace:'/persist/contribute/':'/persist/'}
{* Display weekly,Quarterly,monthly and yearly contributions using pChart (Bar and Pie) *}
{if $chartEnabled and $chartSupported}
-<div class='crm-flashchart'>
-<table class="chart">
- <tr>
- <td>
- {if $outputMode eq 'print' OR $outputMode eq 'pdf'}
- <img src="{$uploadURL|cat:$chartId}.png" />
- {else}
- <div id="open_flash_chart_{$uniqueId}"></div>
- {/if}
- </td>
- </tr>
-</table>
-</div>
-
-{if !$printOnly} {* NO print section starts *}
-{if !$section}
- {include file="CRM/common/openFlashChart.tpl" divId="open_flash_chart_$uniqueId"}
+ <div class='crm-chart'>
+ {if $outputMode eq 'print' OR $outputMode eq 'pdf'}
+ <img src="{$uploadURL|cat:$chartId}.png" />
+ {else}
+ <div id="chart_{$uniqueId}"></div>
+ {/if}
+ </div>
{/if}
-{literal}
-<script type="text/javascript">
- CRM.$(function($) {
- buildChart( );
-
- $("input[id$='submit_print'],input[id$='submit_pdf']").bind('click', function(e){
- // image creator php file path and append image name
- var url = CRM.url('civicrm/report/chart', 'name=' + '{/literal}{$chartId}{literal}' + '.png');
+{if !$printOnly} {* NO print section starts *}
+ {if !$section}
+ {include file="CRM/common/chart.tpl" divId="chart_$uniqueId"}
+ {/if}
- //fetch object and 'POST' image
- swfobject.getObjectById("open_flash_chart_{/literal}{$uniqueId}{literal}").post_image(url, true, false);
- });
+ {literal}
+ <script type="text/javascript">
+ CRM.$(function($) {
+ var allData = {/literal}{$chartData}{literal};
+ buildChart( );
- function buildChart( ) {
- var chartData = {/literal}{$openFlashChartData}{literal};
- $.each( chartData, function( chartID, chartValues ) {
- var divName = {/literal}"open_flash_chart_{$uniqueId}"{literal};
- var loadDataFunction = {/literal}"loadData{$uniqueId}"{literal};
+ $("input[id$='submit_print'],input[id$='submit_pdf']").bind('click', function(e){
+ // image creator php file path and append image name
+ var url = CRM.url('civicrm/report/chart', 'name=' + '{/literal}{$chartId}{literal}' + '.png');
- createSWFObject( chartID, divName, chartValues.size.xSize, chartValues.size.ySize, loadDataFunction );
+ //fetch object and 'POST' image
+ swfobject.getObjectById("chart_{/literal}{$uniqueId}{literal}").post_image(url, true, false);
});
- }
- });
- function loadData{/literal}{$uniqueId}{literal}( chartID ) {
- var allData = {/literal}{$openFlashChartData}{literal};
- return JSON.stringify(allData[chartID].object);
- }
-</script>
-{/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 );
+ });
+ }
+ });
+
+ </script>
+ {/literal}
{/if}
--- /dev/null
+{*
+ +--------------------------------------------------------------------+
+ | 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 |
+ +--------------------------------------------------------------------+
+*}
+<script src="{$config->resourceBase}/bower_components/d3/d3.min.js"></script>
+<script src="{$config->resourceBase}/bower_components/crossfilter2/crossfilter.min.js"></script>
+<script src="{$config->resourceBase}/bower_components/dc-2.1.x/dc.min.js"></script>
+<style src="{$config->resourceBase}/bower_components/dc-2.1.x/dc.min.css"></style>
+{literal}
+<style>
+ .dc-chart path.domain {
+ fill: none;
+ stroke: black;
+ }
+</style>
+<script type="text/javascript">
+function createChart( chartID, divName, xSize, ySize, data ) {
+
+ var div = document.getElementById(divName);
+ if (!div) {
+ console.log("no element found for chart id ", divName);
+ return;
+ }
+
+ // Figure out width.
+ var w=800;
+ var h=400;
+ if (div) {
+ w = Math.min(div.clientWidth - 32, 800);
+ h = Math.min(400, parseInt(w / 2));
+ }
+
+ var chartNode = document.createElement('div');
+ var heading = document.createElement('h2');
+ heading.textContent = data.title;
+ heading.style.marginBottom = '1rem';
+ heading.style.textAlign = 'center';
+ div.style.width = w + 'px';
+ div.style.marginLeft = 'auto';
+ div.style.marginRight = 'auto';
+
+ var links = document.createElement('div');
+ var linkSVG = document.createElement('a');
+ linkSVG.href = '#';
+ linkSVG.textContent = 'Download chart (SVG)';
+ linkSVG.addEventListener('click', e => {
+ e.preventDefault();
+ e.stopPropagation();
+ // Create an image.
+ var svg = div.querySelector('svg');
+ var xml = new XMLSerializer().serializeToString(svg);
+ var image64 = 'data:image/svg+xml;base64,' + btoa(xml);
+
+ downloadImageUrl('image/svg+xml', image64, data.title.replace(/[^a-zA-Z0-9-]+/g, '') + '.svg');
+ });
+ function downloadImageUrl(mime, url, filename) {
+ var downloadLink = document.createElement('a');
+ downloadLink.download = filename;
+ downloadLink.href = url;
+ downloadLink.downloadurl = [mime, downloadLink.download, url].join(':');
+ document.body.append(downloadLink);
+ downloadLink.click();
+ document.body.removeChild(downloadLink);
+ }
+ var linkPNG = document.createElement('a');
+ linkPNG.href = '#';
+ linkPNG.textContent = 'Download chart (PNG)';
+ linkPNG.addEventListener('click', e => {
+ e.preventDefault();
+ e.stopPropagation();
+ // Create an image.
+
+ var canvas = document.createElement('canvas');
+ canvas.width = w;
+ canvas.height = h;
+ div.appendChild(canvas);
+
+ var svg = div.querySelector('svg');
+ var xml = new XMLSerializer().serializeToString(svg);
+ var svg64 = btoa(xml);
+ var b64Start = 'data:image/svg+xml;base64,';
+ var image64 = b64Start + svg64;
+
+ var img = document.createElement('img');
+ img.onload = function() {
+ canvas.getContext('2d').drawImage(img, 0, 0);
+ // canvas.style.display = 'block';
+ var imgURL = canvas.toDataURL('image/png');
+ downloadImageUrl('image/png', imgURL, data.title.replace(/[^a-zA-Z0-9-]+/g, '') + '.png');
+ div.removeChild(canvas);
+ };
+ img.src = image64;
+ });
+
+ links.appendChild(linkSVG);
+ links.appendChild(document.createTextNode(' | '));
+ links.appendChild(linkPNG);
+ //dlLink.dataset.downloadurl = [MIME_TYPE, dlLink.download, dlLink.href].join(':');
+
+ div.appendChild(heading);
+ div.appendChild(chartNode);
+ div.appendChild(links);
+
+ var crossfilterData, ndx, dataDimension, dataGroup, chart;
+ ndx = crossfilter(data.values[0]);
+ dataDimension = ndx.dimension(d => d.label);
+ dataGroup = dataDimension.group().reduceSum(d => d.value);
+ var ordinals = data.values[0].map(d => d.label);
+
+ if (data.type === 'barchart') {
+ chart = dc.barChart(chartNode)
+ .width(w)
+ .height(h)
+ .dimension(dataDimension)
+ .group(dataGroup)
+ .gap(4) // px
+ .x(d3.scale.ordinal(ordinals).domain(ordinals))
+ .xUnits(dc.units.ordinal)
+ .margins({top: 10, right: 30, bottom: 30, left: 90})
+ .elasticY(true)
+ .renderLabel(false)
+ .renderHorizontalGridLines(true)
+ .title(item=> item.key + ': ' + item.value)
+ //.turnOnControls(true)
+ .renderTitle(true);
+ }
+ else if (data.type === 'piechart') {
+ chart = dc.pieChart(chartNode)
+ .width(w)
+ .height(h)
+ .radius(parseInt(h / 2) - 5) // define pie radius
+ .innerRadius(parseInt(h / 4) - 5) // optional
+ .externalRadiusPadding(5)
+ .legend(dc.legend().legendText(d => d.name).y(5))
+ .dimension(dataDimension)
+ .group(dataGroup)
+ .renderLabel(false)
+ .title(item=> item.key + ': ' + item.value)
+ .turnOnControls(true)
+ .renderTitle(true);
+ }
+ // Delay rendering so that animation looks good.
+ window.setTimeout(() => dc.renderAll(), 1500);
+}
+</script>
+{/literal}
+++ /dev/null
-{*
- +--------------------------------------------------------------------+
- | 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 |
- +--------------------------------------------------------------------+
-*}
-<script type="text/javascript" src="{$config->resourceBase}packages/OpenFlashChart/js/json/openflashchart.packed.js"></script>
-<script type="text/javascript" src="{$config->resourceBase}packages/OpenFlashChart/js/swfobject.js"></script>
-{literal}
-<script type="text/javascript">
- function createSWFObject( chartID, divName, xSize, ySize, loadDataFunction ) {
- var flashFilePath = {/literal}"{$config->resourceBase}packages/OpenFlashChart/open-flash-chart.swf"{literal};
-
- //create object.
- swfobject.embedSWF( flashFilePath, divName,
- xSize, ySize, "9.0.0",
- "expressInstall.swf",
- {"get-data":loadDataFunction, "id":chartID},
- null,
- {"wmode": 'transparent'}
- );
- }
- OFC = {};
- OFC.jquery = {
- name: "jQuery",
- image: function(src) { return "<img src='data:image/png;base64," + $('#'+src)[0].get_img_binary() + "' />"},
- popup: function(src) {
- var img_win = window.open('', 'Save Chart as Image');
- // HTML, HEAD, and BODY tags in JS literals obfuscated to avoid being parsed as DOM elements.
- var html = 'html', head = 'head', body = 'body';
- img_win.document.write('<' + html + '><' + head + '><title>Save Chart as Image<\/title><\/' + head + '><' + body + '>' + OFC.jquery.image(src) + ' <\/' + body + '><\/' + html + '>');
- img_win.document.close();
- }
- }
-
-function save_image( divName ) {
- var divId = {/literal}"{$contriChart}"{literal} ? 'open_flash_chart_'+divName : {/literal}"{$divId}"{literal};
- if( !divId ) {
- divId = 'open_flash_'+divName;
- }
- OFC.jquery.popup( divId );
-}
-
-</script>
-{/literal}