WIP checkin
authorRich Lott / Artful Robot <forums@artfulrobot.uk>
Mon, 7 Oct 2019 20:25:00 +0000 (21:25 +0100)
committerRich Lott / Artful Robot <forums@artfulrobot.uk>
Tue, 8 Oct 2019 16:29:14 +0000 (18:29 +0200)
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)

19 files changed:
CRM/Contribute/Form/ContributionCharts.php
CRM/Report/Form/Contribute/Lybunt.php
CRM/Report/Form/Contribute/Summary.php
CRM/Report/Form/Contribute/Sybunt.php
CRM/Report/Form/Event/IncomeCountSummary.php
CRM/Report/Form/Event/Summary.php
CRM/Report/Form/Mailing/Bounce.php
CRM/Report/Form/Mailing/Clicks.php
CRM/Report/Form/Mailing/Opened.php
CRM/Report/Form/Mailing/Summary.php
CRM/Report/Form/Member/Summary.php
CRM/Report/Form/Membership/Summary.php
CRM/Utils/Chart.php [new file with mode: 0644]
CRM/Utils/OpenFlashChart.php [deleted file]
templates/CRM/Contact/Page/DashBoardDashlet.tpl
templates/CRM/Contribute/Form/ContributionCharts.tpl
templates/CRM/Report/Form/Layout/Graph.tpl
templates/CRM/common/chart.tpl [new file with mode: 0644]
templates/CRM/common/openFlashChart.tpl [deleted file]

index ac5866da5bd00bd10a6453274356642e48dfa157..8e4873c44e611d3f3adf4eceb8dafee0658b2275 100644 (file)
@@ -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));
   }
 
 }
index 9c6704058a645244a0ec98b060dc6b96ddabe1f2..15526bd03071c620cfaa5b7f0fcd958fbdf5fa01 100644 (file)
@@ -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']);
     }
   }
index bffb359186fc21a24a837b82c2afc00d7027d2ae..2239525f91290958e864a563e6055c50e98e2032 100644 (file)
@@ -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']);
       }
     }
index 56c3d45e31cb65f9bb6034151a12c6bee5f08eed..98ac7baefc27127dc917e69eef105b4f1c4ba3ff 100644 (file)
@@ -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']);
     }
   }
index 25c5c533b2bbfea7a83f5674fee6e4dc59a3c1e0..40373be36cc6329bde5f4492fbf56e61c9f86ef7 100644 (file)
@@ -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']);
         }
       }
     }
index bfbc8e5a9187fef7be3efd6db31c6e042c0b32f4..b046a7b779eb4daa34a96a9037f7d530da759545 100644 (file)
@@ -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']);
         }
       }
index 9cb491a484ae968b36bb2735c4af0ad3615ab586..9130fb95c746ab94388191171c4fa5ad43e525d4 100644 (file)
@@ -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']);
   }
 
index b2239e02b2e52c6743a8588a5f167b9b88c149a9..032e47d150637b331f60651dcc3fa3f02e2a7a93 100644 (file)
@@ -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']);
   }
 
index f040552ca47925724a0cde6558c01a390520b4bd..1c2893f2a881c3efcd8cfbb8a53fbfa05b985261 100644 (file)
@@ -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']);
   }
 
index 549eaf7cda531b6ee697b72ef3cce76c20005a45..a43467175c402c14fe1e6a4063c8f38bcbff041e 100644 (file)
@@ -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']);
   }
 
index 78084e4ab7163974f67af2b37fd51c7482bccda3..620189b553ea07aacd313b7107e72a30bbb1f852 100644 (file)
@@ -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']);
index 9ab478c0846ae746ae44c57f12c4aaaa89370705..de12e54c119f66c2cac64a923fdcddaab3e28957 100644 (file)
@@ -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 (file)
index 0000000..4bf9128
--- /dev/null
@@ -0,0 +1,331 @@
+<?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;
+  }
+
+}
diff --git a/CRM/Utils/OpenFlashChart.php b/CRM/Utils/OpenFlashChart.php
deleted file mode 100644 (file)
index 178f093..0000000
+++ /dev/null
@@ -1,567 +0,0 @@
-<?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;
-  }
-
-}
index 607abd348e9050d74e7c9a785a8d2cf700c68985..61d8adccf98ae0fb1facb18c05c79a8739cc537c 100644 (file)
@@ -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}
 <div class="crm-submit-buttons crm-dashboard-controls">
index 4db862fc7dd1c39e580d4da3089e4896b08cb743..3e6c01a0f98ac18d780ba938918a39cc9d0a87ba 100644 (file)
  </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;
   }
 
index b6c4bd37fe97576b66ca5ff2ab0a61028cfa91ad..01f829a25249878b9b25702d16b1761055cbdc6c 100644 (file)
  | 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}
diff --git a/templates/CRM/common/chart.tpl b/templates/CRM/common/chart.tpl
new file mode 100644 (file)
index 0000000..e389670
--- /dev/null
@@ -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        |
+ +--------------------------------------------------------------------+
+*}
+<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}
diff --git a/templates/CRM/common/openFlashChart.tpl b/templates/CRM/common/openFlashChart.tpl
deleted file mode 100644 (file)
index 0995cd1..0000000
+++ /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        |
- +--------------------------------------------------------------------+
-*}
-<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}