如何删除谷歌图表中的默认错误消息

时间:2016-01-20 11:34:55

标签: charts google-visualization

如何删除google图表中显示的默认错误,如下所示:

enter image description here

1 个答案:

答案 0 :(得分:9)

要删除Google错误,请在图表或其他对象上收听'error'事件 触发事件时,请使用google.visualization.errors.removeError

在此,我故意导致错误,将其从Google chart中移除,并将其显示在我自己的div



google.load('visualization', '1', {packages:['corechart'], callback: drawChart});

function drawChart() {
  var data = google.visualization.arrayToDataTable([
    ["Element", "Density", { role: "style" } ]
  ]);

  var view = new google.visualization.DataView(data);
  view.setColumns([0, 1,
                   { calc: "stringify",
                     sourceColumn: 1,
                     type: "string",
                     role: "annotation" },
                   2]);

  var chart = new google.visualization.ColumnChart(document.getElementById("chart_div"));

  google.visualization.events.addListener(chart, 'error', function (googleError) {
      google.visualization.errors.removeError(googleError.id);
      document.getElementById("error_msg").innerHTML = "Message removed = '" + googleError.message + "'";
  });

  chart.draw(view, {height: 20});
}

<script src="https://www.google.com/jsapi"></script>
<div id="chart_div"></div>
<div id="error_msg"></div>
&#13;
&#13;
&#13;