javascript - 谷歌可视化错误

标签 javascript jquery html css google-visualization

我有 google vis 数据表和仪表板(控件过滤器、字符串控件、数据表、图表)。所有元素都是绑定(bind)的。

所以我从数据库中获取 JSON 并发送到 gViz api 以绘制表格、图表和控件。 当我使用 contols 过滤一些数据时,如果该字符串没有数据,那么我会从谷歌可视化中得到这样的错误:

One or more participants failed to draw()×

Table has no rows.×

和类似的消息。所以在我的应用程序中,这太丑陋了,所以有什么办法可以不显示它吗? 所以如果没有数据不显示错误...

我尝试使用 CSS 选项:

#google-visualization-errors-all-3 {
   display:none;
}

但这对我来说不是很好的解决方案。

有没有其他方法,也许在仪表板的 google visualusation api 中?

更新:

var slider;
    var ajdi = '';
      function drawVisualization() {
        
       var cssClassNames = {
        'headerRow': 'zaglavlje',
        'tableRow': 'red',
        'oddTableRow': 'red1',
        'selectedTableRow': 'orange-background large-font',
        'hoverTableRow': 'prekoreda',
        'headerCell': 'gold-border',
        'tableCell': 'cell',
        'rowNumberCell': 'underline-blue-font'
    };

        
       var json = $.ajax({
                    url: 'getzadaci.php', // make this url point to the data file
                    dataType: 'json',
                    async: false
                }).responseText;
                
                // Create our data table out of JSON data loaded from server.
        var data = new google.visualization.DataTable(json);
        //dodajemo kolonu sa kontrolama
        
        
        
    
        
      
        // Define a category picker control for the Gender column
        var categoryPicker = new google.visualization.ControlWrapper({
          'controlType': 'CategoryFilter',
          'containerId': 'control1',
          'options': {
            'filterColumnLabel': 'Status',
            'ui': {
            'labelStacking': 'vertical',
              'allowTyping': false,
              'allowMultiple': true,
                          'caption': 'Status'

            }
          }
        });
        
         var categoryPicker1 = new google.visualization.ControlWrapper({
         'controlType': 'CategoryFilter',
          'containerId': 'control2',
          'options': {
            'filterColumnIndex': 8,
            'ui': {
            'labelStacking': 'horizontal',
              'allowTyping': false,
              'allowMultiple': true,
              'caption': 'Parcela'
            }
          }
        });
         var categoryPicker2 = new google.visualization.ControlWrapper({
         'controlType': 'CategoryFilter',
          'containerId': 'control4',
          'options': {
            'filterColumnIndex': 2,
            'ui': {
            'labelStacking': 'horizontal',
              'allowTyping': false,
              'allowMultiple': true,
              'caption': 'Vrsta zadatka'
            }
          }
        });
      
        
        var stringFilter1 = new google.visualization.ControlWrapper({
          'controlType': 'StringFilter',
          'containerId': 'control3',
          'options': {
                      'matchType': 'any',
          'filterColumnIndex': 1,
          'ui': {'labelStacking': 'vertical'}
          }
        });
        
        var slider = new google.visualization.ControlWrapper({
          'controlType': 'DateRangeFilter',
          'containerId': 'control5',
          'options': {
            'filterColumnLabel': 'Pocetak',
         'ui': {'labelStacking': 'vertical'}
          }
        });
      
        // Define a Pie chart
       
  
      
        // Define a table
        var table = new google.visualization.ChartWrapper({
          'chartType': 'Table',
          'containerId': 'chart2',
          'cssClassNames': 'cssClassNames',
          'view': { 'columns': [1,2,12,5,6,8,11] },
          'options': {
        cssClassNames: cssClassNames,
        allowHtml: true
    }
        });
        
       var timeline = new google.visualization.ChartWrapper({
    chartType: 'Timeline',
    containerId: 'chart5',
    options: {
    height: '350',
    timeline: { colorByRowLabel: true, 
                backgroundColor: '#ffd' },
    //timeline.barLabelStyle: {color: '#000', fontName: 'Arial', fontSize: '13px'},
    //backgroundColor: '#fff',
    colors: ['#55c2a2', '#89d168', '#d3eb87','#8ec63e', '#FFF0BA','#FF542E', '#CFD6DE', '#ADC1D6', '#7297BA']
    //timeline: { rowLabelStyle: {fontName: 'Helvetica', fontSize: 24, color: '#603913' },
               // barLabelStyle: { fontName: 'Garamond', fontSize: 14 } }
    },
    view: {
        // as an example, use columns "Naziv", "Vrsta", "Pocetak", and "Zavrsetak" for the timeline
        columns: [8, 2, 5, 6]
    },
    
});

           var formatter_short = new google.visualization.DateFormat({formatType: 'short'});
           formatter_short.format(data, 5);
           formatter_short.format(data, 6);

        
        new google.visualization.events.addListener(table, 'ready', function () {
        google.visualization.events.addListener(table.getChart(), 'select', function () {
            var selection = table.getChart().getSelection();
            // iterate over all selected rows
            for (var i = 0; i < selection.length; i++) {
              //$("#edit").removeClass("edit btn btn-success")
              //$('#edit').addClass('edit btn btn-success');
              ajdi = table.getDataTable().getValue(selection[i].row,0);
              $("#vrednostid").val(table.getDataTable().getValue(selection[i].row,0));
              $("#naziv1").val(table.getDataTable().getValue(selection[i].row,1));
              $("#vrsta_rada1").val(table.getDataTable().getValue(selection[i].row,2));
              $("#status1").val(table.getDataTable().getValue(selection[i].row,3));
              $("#opis1").val(table.getDataTable().getValue(selection[i].row,4));
              $("#usluzno1").val(table.getDataTable().getValue(selection[i].row,9));

              var p = new Date(table.getDataTable().getValue(selection[i].row,5));
              $("#dp31").datepicker("setDate", p);
              
              var z = new Date(table.getDataTable().getValue(selection[i].row,6));
              $("#dp41").datepicker("setDate", z);
              
              //$("#parcele1").val(table.getDataTable().getValue(selection[i].row,8));
              //$("#parcele1").select2("val", ["3","19"]);
              var id = table.getDataTable().getValue(selection[i].row,10);
              var naziv = table.getDataTable().getValue(selection[i].row,8);
              

var idArr = (id.lastIndexOf(',') == (id.length - 1) ? id.substr(0, id.length - 1) : id).split(', ');
var nazivArr = (naziv.lastIndexOf(',') == (naziv.length - 1) ? naziv.substr(0, naziv.length - 1) : naziv).split(', ');
var objHold = [];
for(var j=0,length=idArr.length;j<length;j++)
{
    if(idArr[j] && nazivArr[j]) // make sure both arrays have a value
    objHold.push({id: idArr[j], naziv: nazivArr[j]});
}
$("#parcele1").select2("data", objHold);

            }
        });
    });
        
        

      
        // Create a dashboard
        new google.visualization.Dashboard(document.getElementById('dashboard')).
            // Establish bindings, declaring the both the slider and the category
            // picker will drive both charts.
    bind([categoryPicker, categoryPicker1, categoryPicker2, slider, stringFilter1], [table, timeline]).
            // Draw the entire dashboard.
            draw(data, {'allowHtml':true, 'cssClassNames': 'cssClassNames'});      }
      //table.draw(data, {'allowHtml':true, 'cssClassNames': cssClassNames});      }

function dashboardReady() {
        // The dashboard is ready to accept interaction. Configure the buttons to
        // programmatically affect the dashboard when clicked.
      
        // Change the slider selected range when clicked.
        document.getElementById('rangeButton').onclick = function() {
          slider.setState({'lowValue': 2, 'highValue': 5});
          slider.draw();
        };
      
        // Change the pie chart rendering options when clicked.
        document.getElementById('optionsButton').onclick = function() {
          piechart.setOption('is3D', true);
          piechart.draw();
        };
      }
      


      google.setOnLoadCallback(drawVisualization);// JavaScript Document
      
     

这是我的代码,所以重要的部分是 .ajac 函数,所以我如何在这里集成 if row>0 来显示图表???

最佳答案

我在使用 Google Charts API 时遇到了同样的问题。为了解决这个问题,我只需要检查通过过滤器的行数。如果它 >= 1,则绘制图表。否则做任何你需要做的事;不画图表,显示错误等

关于javascript - 谷歌可视化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24461464/

相关文章:

javascript - 如何模拟用户使用 javascript (JQuery) 按下 Ctrl "plus"加号或减号?

php - jquery 插件创建带有背景图像的方 block

html - CSS:即使鼠标抬起后悬停也不会退出

html - css:如何使元素不重叠+自动将它们放在相关位置

javascript - 用另一个输入字段值更新隐藏的输入字段值?

javascript - Angular 的 $rootScope.$digest

javascript - "callback is not a function"Node.js

php - 添加 php 代码时未包含 jQuery 源文件

html - 如何在悬停状态下设置静态文本颜色?

javascript - jquery 递增索引输入数组