【问题标题】:Remove google chart error message and display an empty chart when there's no data from the database当数据库中没有数据时,删除谷歌图表错误消息并显示一个空图表
【发布时间】:2014-06-11 06:48:22
【问题描述】:

这是我的谷歌图表脚本。如果没有数据,我会尝试显示一个空图表并隐藏消息“没有足够的列来绘制请求的图表”。你知道我的代码有什么问题吗?只要没有数据,该错误消息仍然会出现。我参考了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>

【问题讨论】:

  • 你知道在那种情况下你从&lt;?= generateChartOverview($row['campaignId'], $start_date, $end_date, false, true, $ad); ?&gt;得到什么吗?
  • 是的,如果有数据,它会显示图表并且工作正常。但是当还没有数据时,图表会显示该错误消息。
  • 我明白了,但是&lt;?= generateChartOverview($row['campaignId'], $start_date, $end_date, false, true, $ad); ?&gt; 的结果是什么?一个空字符串,一个空数组,一个空数组的数组还是一些垃圾? arrayToDataTable() 的参数是什么?

标签: javascript charts google-visualization


【解决方案1】:

我用谷歌事件监听器来管理这个错误

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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-29
    • 1970-01-01
    • 1970-01-01
    • 2017-02-23
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多