2

我正在使用 Google 图表,并且可以使用选择菜单查看图表:

<select id="form_frame1" name="frame1" onchange="getChart(this);">
   <option value="area_chart_google" >Area Chart</option>
   <option value="area_chart_2" selected="selected">Stacked Chart</option>
</select>

我的 getChart 函数是:

function getChart(selection) {
 if (selection.value == "area_chart_2") {
    document.getElementById('area_chart_2').style.display = 'block';
    document.getElementById('area_chart_google').style.display = 'none';
}

 else {   
    document.getElementById('area_chart_2').style.display = 'none';
    document.getElementById('area_chart_google').style.display = 'block';
   }
}

使用 Display: 'block' by default (area_chart_google) 时工作​​正常。问题是一旦我隐藏(无)图表(area_chart_google)然后我显示它(块)我得到这个错误:

In Firebug: google-visualization-errors-all-1 
In Chrome: Cannot read property 'length' of null
In Safari: 'null' is not an object

这是我的 HTML(脚本在正文中,我在标题上使用 JSAPI):

<div id="area_chart_google" class="area-chart"></div>
<script type="text/javascript">
  google.load("visualization", "1", {packages:["corechart"]});
  google.setOnLoadCallback(drawChart);
  function drawChart() {
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Header');
    data.addColumn('number', '');
    data.addColumn('number', '');
    data.addRows([
      ['Monday',300,43],
      ['Tuesday',250,545],
      ['Wednesday',122,78],
      ['Thursday',348,92],
      ['Friday',23,61],
      ['Saturday',39,93]
    ]);
    var options = {
      title: '',
      hAxis: {title: '',  titleTextStyle: {color: '#efed22'}},
      backgroundColor: '#efede9',
      legend: 'none'
    };
    var chart = new google.visualization.AreaChart(document.getElementById('area_chart_google'));
    chart.draw(data, options);
  }
</script>
4

3 回答 3

1

这对我有用:

function getChart(selection) {
  if (selection.value == "area_chart_2") {
  document.getElementById('area_chart_2').style.display = 'block';
  document.getElementById('area_chart_google').style.display = 'none';
}

else {   
  document.getElementById('area_chart_2').style.display = 'none';
  document.getElementById('area_chart_google').style.display = 'block';
  drawChart(); //call this function when using this Google Chart
 }
}
于 2013-03-08T19:33:46.373 回答
0

在这里猜测,但它可能会发生,因为某些脚本动态尝试读取隐藏 div 的长度属性。

如this answer中所述, display none 页面中没有空间。您可以尝试一些选项:

  1. Use visibility: hidden
  2. 尝试隐藏area_chart_2and 的包装area_chart_google元素
于 2013-03-08T17:13:16.777 回答
0

您的问题被标记为 jQuery,所以这是一个 jQuery 解决方案:

function getChart(selection) {

   var $area_chart_2 = $("#area_chart_2"),
       $area_chart_google = $("#area_chart_google");

   if (selection.value === "area_chart_2") {
      $area_chart_2.show(); //You could also use $area_chart_2.css("display", "block");
      $area_chart_google.hide(); //Or use $area_chart_google.css("display", "none");
   } else {   
      $area_chart_2.hide();
      $area_chart_google.show();
   }
}
于 2013-03-08T17:15:41.857 回答