Google charts aren't working in IE11, (but works in chrome) how to fix it?

狂风中的少年 提交于 2020-01-15 09:19:29

问题


I have two pages, one index.html page that shows all charts and one page that shows a chart and table. Surprisingly, all charts from index.html can be seen in IE11 but when I open a page with a chart and table it won't.

What should I change in the code, thus the IE could recognize charts?

Here is the code I was using provided by google and made some adjustments.

<html>
  <head>

    <link href="stylesheet.css" rel="stylesheet" type="text/css">

    <script type="text/javascript" src="loader.js"></script>

    <script type="text/javascript">

     google.charts.load("current", {packages:["corechart"]});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
  var data = google.visualization.arrayToDataTable([
    ['Metric', 'Score'],
    ['Uniqueness', 45.3],
    ['', 54.7]
  ]);

  var view = new google.visualization.DataView(data);
  view.setColumns([0, 1]);

  var options = {
    legend: 'none',
    title: 'Unique Identifiability',
    pieHole: 0.6,
    colors: ['#ff0000', '#808080']

  };

  var chart = new google.visualization.PieChart(document.getElementById('uniqueness'));



  google.visualization.events.addListener(chart, 'select', function () {
    var selection = chart.getSelection(); 



  });



  chart.draw(view, options);
}

        google.charts.load('current', {'packages':['table']});
        google.charts.setOnLoadCallback(drawTable);

        function drawTable() {
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Business Rule',);
        data.addColumn('string', '%',);
        data.addColumn('string', 'Total OK Values',);
        data.addColumn('string', 'Total Values',);

        data.addRows([
          ['Birth date must be filled', '58%','75365','129940'],
          ['Country must be filled', '98%','294028','300029'],
        ]);

        var table = new google.visualization.Table(document.getElementById('table_div'));

        table.draw(data, {showRowNumber: false, width: '100%', height: '100%'});
      }




    </script>

  </head>
  <body>



    <table class="columns">
      <tr>

        <td>
        <div><a href="index.html" class="next">BACK BUTTON</a></div>
        <div id="uniqueness" style="width: 1200px; height: 700px;"></div>
         <div id="table_div"></div>
        </td>

      </tr>


    </table>

  </body>
</html>

回答1:


IE doesn't like the extra commas in the addColumn statements...

data.addColumn('string', 'Business Rule',);
data.addColumn('string', '%',);
data.addColumn('string', 'Total OK Values',);
data.addColumn('string', 'Total Values',);

not needed, remove them...

data.addColumn('string', 'Business Rule');
data.addColumn('string', '%');
data.addColumn('string', 'Total OK Values');
data.addColumn('string', 'Total Values');

also, google charts only needs to be loaded once per page,
not once per chart.

you can load multiple packages at the same time.

google.charts.load('current', {packages:['corechart', 'table']});

see following working snippet...

google.charts.load('current', {packages:['corechart', 'table']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
  var data = google.visualization.arrayToDataTable([
    ['Metric', 'Score'],
    ['Uniqueness', 45.3],
    ['', 54.7]
  ]);

  var view = new google.visualization.DataView(data);
  view.setColumns([0, 1]);

  var options = {
    legend: 'none',
    title: 'Unique Identifiability',
    pieHole: 0.6,
    colors: ['#ff0000', '#808080']

  };

  var chart = new google.visualization.PieChart(document.getElementById('uniqueness'));
  google.visualization.events.addListener(chart, 'select', function () {
    var selection = chart.getSelection();
  });
  chart.draw(view, options);


  var data = new google.visualization.DataTable();
  data.addColumn('string', 'Business Rule');
  data.addColumn('string', '%');
  data.addColumn('string', 'Total OK Values');
  data.addColumn('string', 'Total Values');

  data.addRows([
    ['Birth date must be filled', '58%','75365','129940'],
    ['Country must be filled', '98%','294028','300029'],
  ]);

  var table = new google.visualization.Table(document.getElementById('table_div'));

  table.draw(data, {showRowNumber: false, width: '100%', height: '100%'});
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="uniqueness" style="width: 1200px; height: 700px;"></div>
<div id="table_div"></div>


来源:https://stackoverflow.com/questions/53519811/google-charts-arent-working-in-ie11-but-works-in-chrome-how-to-fix-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!