How to remove default error message in google char

2019-04-22 22:09发布

问题:

How to remove default error shown in google chart, which looks like so:

回答1:

To remove google errors, listen for the 'error' event on the chart or other object.
When the event is fired, use google.visualization.errors.removeError

Here, I intentionally cause an error, remove it from the google chart, and display it in my own 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>