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

时间:2014-06-11 06:48:22

标签: 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>

1 个答案:

答案 0 :(得分:0)

我管理通过Google事件监听器捕获它们的错误

select
    t1.id,
    count(distinct(t1.volume_band)) as no_of_bands
from
(
    select
        id,
        case 
            when charged between 0 and 1000 then "A (0-1,000)" 
            when charged between 1000 and 3000 then "B (1,000 - 3,000)"
            when charged between 3000 and 5000 then "C (3,000  5,000)"
            when charged between 5000 and 7000 then "D (5,000 - 7,000)"
            when charged between 7000 and 9000 then "E (7,000 - 9,000)"
            when charged between 9000 and 11000 then "F (9000 - 11,000)"
            when charged between 11000 and 13000 then "G (11,000 - 13,000)"
            when charged between 13000 and 15000 then "H (13,000 - 15,000)"
            when charged between 15000 and 17000 then 'I (15,000 - 17,000)'
            when charged between 17000 and 20000 then 'J (17,000 - 20,000)'
            when charged between 20000 and 25000 then 'K (20,000 - 25,000)'
            when charged between 25000 and 30000 then 'L (25,000 - 30,000)'
            else "M (30,000+)"
        end as volume_band,
        count(payment_id)
    from
        table
    where
        DATE_FORMAT(payment_date,'%Y-%m') >= '2018-04'
    and user_type = 'a'
    group by 1,2) as t1
group by 1
order by 2 desc