Tidy up charts code, including avoiding js error on tabular data view
authorRich Lott / Artful Robot <forums@artfulrobot.uk>
Wed, 9 Oct 2019 08:31:36 +0000 (10:31 +0200)
committerRich Lott / Artful Robot <forums@artfulrobot.uk>
Wed, 9 Oct 2019 08:31:36 +0000 (10:31 +0200)
CRM/Contribute/Form/ContributionCharts.php
CRM/Utils/Chart.php
templates/CRM/Report/Form/Layout/Graph.tpl
templates/CRM/common/chart.tpl

index 8e4873c44e611d3f3adf4eceb8dafee0658b2275..70c58ce2cb8edda545fc3b821037c444294e68de 100644 (file)
@@ -231,7 +231,7 @@ class CRM_Contribute_Form_ContributionCharts extends CRM_Core_Form {
     $this->assign('hasYearlyChart', $yearlyChart);
     $this->assign('hasByMonthChart', $monthlyChart);
     $this->assign('hasChart', empty($chartData) ? FALSE : TRUE);
-    $this->assign('ChartData', json_encode($chartData));
+    $this->assign('chartData', json_encode($chartData ?? []));
   }
 
 }
index 4bf91281bff5ad339cbb4310dfcc66e47b278095..656277307b3718f71096e3eb420ecbc02de70713 100644 (file)
@@ -321,7 +321,7 @@ class CRM_Utils_Chart {
         // assign chart data to template
         $template = CRM_Core_Smarty::singleton();
         $template->assign('uniqueId', $uniqueId);
-        $template->assign("chartData", json_encode($theChart));
+        $template->assign("chartData", json_encode($theChart ?? []));
       }
     }
 
index 01f829a25249878b9b25702d16b1761055cbdc6c..f49ee1037580bcb1d23aaf2d4fe3608d8467556f 100644 (file)
   {if !$section}
     {include file="CRM/common/chart.tpl" divId="chart_$uniqueId"}
   {/if}
+  {if $chartData}
+    {literal}
+    <script type="text/javascript">
+       CRM.$(function($) {
+         // Build all charts.
+         var allData = {/literal}{$chartData}{literal};
 
-  {literal}
-  <script type="text/javascript">
-     CRM.$(function($) {
-      var allData = {/literal}{$chartData}{literal};
-       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');
-
-         //fetch object and 'POST' image
-         swfobject.getObjectById("chart_{/literal}{$uniqueId}{literal}").post_image(url, true, false);
-       });
-
-       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}
+         $("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');
+
+           //fetch object and 'POST' image
+           swfobject.getObjectById("chart_{/literal}{$uniqueId}{literal}").post_image(url, true, false);
+         });
+       });
+
+    </script>
+    {/literal}
+  {/if}
 {/if}
index e3896708dfd9a1ccda2f8f01b09c867587eaf535..5425871f121a27277d10234b16f332ea9930f1b9 100644 (file)
@@ -43,13 +43,9 @@ function createChart( chartID, divName, xSize, ySize, data ) {
     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));
-  }
+  // Figure out suitable size based on container size.
+  var w = Math.min(div.clientWidth - 32, 800);
+  var h = Math.min(400, parseInt(w / 2));
 
   var chartNode = document.createElement('div');
   var heading = document.createElement('h2');
@@ -61,6 +57,8 @@ function createChart( chartID, divName, xSize, ySize, data ) {
   div.style.marginRight = 'auto';
 
   var links = document.createElement('div');
+  links.style.textAlign = 'center';
+  links.style.marginBottom = '1rem';
   var linkSVG = document.createElement('a');
   linkSVG.href = '#';
   linkSVG.textContent = 'Download chart (SVG)';
@@ -116,11 +114,6 @@ function createChart( chartID, divName, xSize, ySize, data ) {
   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]);
@@ -161,7 +154,13 @@ function createChart( chartID, divName, xSize, ySize, data ) {
       .renderTitle(true);
   }
   // Delay rendering so that animation looks good.
-  window.setTimeout(() => dc.renderAll(), 1500);
+  window.setTimeout(() => {
+    div.appendChild(heading);
+    div.appendChild(chartNode);
+    div.appendChild(links);
+
+    dc.renderAll();
+  }, 1500);
 }
 </script>
 {/literal}