【问题标题】:Google Charts: Drawing Multiple Bar Charts with dual axis in IEGoogle Charts:在 IE 中绘制具有双轴的多个条形图
【发布时间】:2015-12-29 07:17:05
【问题描述】:

我正在同一页面上创建多个具有双轴的条形图。它在 chrome 中运行良好,但 在 IE 中无法运行。在 IE 中显示错误

“对象不支持‘包含’的属性或方法”

HTML & JavaScript 代码如下:

      

startChart();
      function startChart() {
       
          var data = new google.visualization.arrayToDataTable([
              ['Galaxy', 'Distance', 'Brightness'],
              ['Canis Major Dwarf', 8000, 23.3],
              ['Sagittarius Dwarf', 24000, 4.5],
              ['Ursa Major II Dwarf', 30000, 14.3],
              ['Lg. Magellanic Cloud', 50000, 0.9],
              ['Bootes I', 60000, 13.1]
          ]);

          var options = {
              width: 900,
              chart: {
                  title: 'Nearby galaxies',
                  subtitle: 'distance on the left, brightness on the right'
              },
              series: {
                  0: {
                      axis: 'distance'
                  }, // Bind series 0 to an axis named 'distance'.
                  1: {
                      axis: 'brightness'
                  } // Bind series 1 to an axis named 'brightness'.
              },
              axes: {
                  y: {
                      distance: {
                          label: 'parsecs'
                      }, // Left y-axis.
                      brightness: {
                          side: 'right',
                          label: 'apparent magnitude'
                      } // Right y-axis.
                  }
              }
          };

          var chart = new google.charts.Bar(document.getElementById('dual_y_div'));
          chart.draw(data, options);
  var chart1 = new google.charts.Bar(document.getElementById('dual_y_div1'));
          chart1.draw(data, options);

      };
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1.1','packages':['bar']}]}"></script>
<div id="dual_y_div" style="width: 900px; height: 500px;"></div>
<div id="dual_y_div1" style="width: 900px; height: 500px;"></div>

【问题讨论】:

    标签: jquery charts google-visualization bar-chart


    【解决方案1】:

    我猜这仍然是谷歌可视化 api 的错误。 (link)

    但是,我将条形图更改为柱形图并更改了轴的选项,以便它们与柱形图一起使用,并且成功了。

    但是这个https://jsfiddle.net/5b8au8t4/1/ 正在工作。

    startChart();
          function startChart() {
           
              var data = new google.visualization.arrayToDataTable([
                  ['Galaxy', 'Distance', 'Brightness'],
                  ['Canis Major Dwarf', 8000, 23.3],
                  ['Sagittarius Dwarf', 24000, 4.5],
                  ['Ursa Major II Dwarf', 30000, 14.3],
                  ['Lg. Magellanic Cloud', 50000, 0.9],
                  ['Bootes I', 60000, 13.1]
              ]);
    
              var options = {
                  width: 900,       
                  title: 'Nearby galaxies',
                  vAxes: {
    			0: {
    				title: 'parsecs',
    				
    				},
    			1: {
    				title: 'apparent magnitude',
    				
    			},
              },
                  series: {
    			0:{
    				targetAxisIndex:0,
    				
    				},
    			1:{
    				targetAxisIndex:1,
    				
    				},
                },
              };
              var chart = new google.visualization.ColumnChart(document.getElementById('dual_y_div'));
              chart.draw(data, options);
      var chart1 = new google.visualization.ColumnChart(document.getElementById('dual_y_div1'));
              chart1.draw(data, options);
    
          }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1.1','packages':['corechart']}]}"></script>
    <div id="dual_y_div" style="width: 900px; height: 500px;"></div>
    <div id="dual_y_div1" style="width: 900px; height: 500px;"></div>

    我希望这会对您有所帮助。

    【讨论】:

    • 非常感谢。正如您提到的,我将其更改为 ColumnChart 并且它正在工作。
    【解决方案2】:

    以下是一个示例,需要更改多个Material Charts

    google.charts.load('41', {packages: ['bar']});
    google.charts.setOnLoadCallback(startChart);
    
    function startChart() {
    
      var data = new google.visualization.arrayToDataTable([
        ['Galaxy', 'Distance', 'Brightness'],
        ['Canis Major Dwarf', 8000, 23.3],
        ['Sagittarius Dwarf', 24000, 4.5],
        ['Ursa Major II Dwarf', 30000, 14.3],
        ['Lg. Magellanic Cloud', 50000, 0.9],
        ['Bootes I', 60000, 13.1]
      ]);
    
      var options = {
        width: 900,
        chart: {
          title: 'Nearby galaxies',
          subtitle: 'distance on the left, brightness on the right'
        },
        series: {
          0: {
            axis: 'distance'
          }, // Bind series 0 to an axis named 'distance'.
          1: {
            axis: 'brightness'
          } // Bind series 1 to an axis named 'brightness'.
        },
        axes: {
          y: {
            distance: {
              label: 'parsecs'
            }, // Left y-axis.
            brightness: {
              side: 'right',
              label: 'apparent magnitude'
            } // Right y-axis.
          }
        }
      };
    
      var chart = new google.charts.Bar(document.getElementById('dual_y_div'));
      chart.draw(data, options);
      var chart1 = new google.charts.Bar(document.getElementById('dual_y_div1'));
      chart1.draw(data, options);
    };
    <head>
      <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
      <script src="https://www.gstatic.com/charts/loader.js"></script>
    </head>
    <body>
      <div id="dual_y_div" style="width: 900px; height: 500px;"></div>
      <div id="dual_y_div1" style="width: 900px; height: 500px;"></div>
    </body>

    【讨论】:

    • 很棒的发现@WhiteHat。现在它起作用了。但不鼓励将元标记用于生产用途,因为渲染页面内容可能会导致意外结果。谢谢。
    • 从答案中删除了 IE 的区别,报告 here 它也修复了其他浏览器...
    • 您介意记录一下实际的修复方法吗?也许以差异补丁的形式。这将帮助那些遇到同样问题并需要对其代码应用相同修复的人。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-15
    • 2022-01-09
    • 1970-01-01
    • 2019-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多