javascript - 删除谷歌图表错误消息并在数据库中没有数据时显示空图表

标签 javascript charts google-visualization

这是我的谷歌图表脚本。如果没有数据,我试图显示一个空图表,并隐藏消息“没有足够的列来绘制请求的图表”。你知道我的代码有什么问题吗?每当没有数据时,该错误消息仍然会出现。我引用过How to customize "not enough columns given to draw the requested chart" message?但它不适用于我的情况。谢谢。

<script type="text/javascript">
        // Load the Visualization API and the piechart package.
        google.load('visualization', '1.0', {'packages':['corechart']});

        // Set a callback to run when the Google Visualization API is loaded.
        google.setOnLoadCallback(drawChartCampaign);

        function errorHandler(errorMessage) {
            //curisosity, check out the error in the console
            console.log(errorMessage);

            //simply remove the error, the user never see it
            google.visualization.errors.removeError(errorMessage.id);
        }

        function drawChartCampaign() {
            var data = google.visualization.arrayToDataTable(<?= generateChartOverview($row['campaignId'], $start_date, $end_date, false, true, $ad); ?>);

            var options = {
                title: '',
                pointSize: 3,
                animation: { duration: 1000, easing: 'out' },
                hAxis: {title: 'Date', titleTextStyle: {color: '#999999'}<?php if($boolInterval){ echo " , showTextEvery : $showTextEvery"; } ?>},
                vAxis: {title: 'Clicks / Views', titleTextStyle: {color: '#999999'}, viewWindowMode:'explicit', viewWindow:{ min:0 }}  
            };

            var chart = new google.visualization.LineChart(document.getElementById('chart_div_adsdetails'));

            //attach the error handler here, before draw()
            google.visualization.events.addListener(chart, 'error', errorHandler);

            chart.draw(data, options);
        }
    </script>

最佳答案

我管理这个错误,用谷歌事件监听器捕获它们

var chart = new google.visualization.LineChart(document.querySelector('#chartContainer'));
google.visualization.events.addListener(chart, 'error', function (googleError) {
        google.visualization.errors.removeError(googleError.id);
        //here you can write your custom message or nothing if you want remove
        alert("error catch")
    });
  chart.draw(yourData, yourOptions);

关于javascript - 删除谷歌图表错误消息并在数据库中没有数据时显示空图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24156341/

相关文章:

javascript - IntelliJ IDEA 2017.1 设置中缺少 Javascript 库菜单

javascript - 在运行时将 Google Chart 容器更改为另一个容器

database-design - 高采样率数据的数据库设计,在多个缩放级别绘制图形

json - ASP.Net MVC - Google Charts 的 Json 数据

javascript - Google Apps 脚本 HTML 应用程序中的 Google 图表

javascript - Nuxtjs 和 Axios 上的计算属性?

javascript - JSONP 使用 - 范围或异步问题?使用 JQuery

javascript - 将图形 "google chart"转换为图像

javascript - React 属性传播符号也会传递给子元素吗?

javascript - 使用 HighCharts 格式化数据点和 Y 轴标签