Averm
Averm

Reputation: 11

Google chart : filter GeoChart

I'm trying to use a ControlWrapper to filter data to display on a GeoChart inside a dashboard. The problem is the following : I have to have three columns : 'Country', 'value' and 'quarter'. Now, I want to display information for one quarter only, which I can select thanks to a CategoryPicker. Then, using the dashboard features, I want to bind my picker with my GeoChart. However, I cannot display my GeoChart as it throw an error because I use three columns where I should only have two...

I know the trick is to use a DataView and hide the third column but then I cannot link my filter and my geochart.

Is there a workaround here? Part of my code can be found below. Btw, this works fine with other types of chart, but not with the GeoChart... why?

In advance, thanks a lot for your help !

var data = response.getDataTable();

        var dashboard = new google.visualization.Dashboard(document.getElementById('dashboard_div'));

        var quarterSelector = new google.visualization.ControlWrapper({
                'controlType': 'CategoryFilter',
                'containerId': 'filter_div',
                'options': {
                    'filterColumnLabel': 'Quarter',
                     ui: {
                        allowTyping: false,
                        allowMultiple: false,
                        allowNone: false
                    },
                    'chartView': {
                        'columns': [0,1]
                    },
                }
        });

        var mapChart = new google.visualization.ChartWrapper({
            'chartType': 'GeoChart',
            'containerId': 'regions_div',
            'options': {
                'displayMode': 'regions',
                'region': '150',
                'view': {'columns':[0,1]}
            }
        });         

    dashboard.bind(quarterSelector, mapChart);      
    dashboard.draw(data);

Upvotes: 1

Views: 895

Answers (1)

WhiteHat
WhiteHat

Reputation: 61230

first, chartView is part of ui on ChartRangeFilter -- not CategoryFilter

in addition, view: {'columns':[0,1]} should not be part of options
but on same level as options, chartType, and containerId

however, neither should be needed here...

google.charts.load('current', {
  packages: ['controls', 'geochart'],
  callback: drawChart
});

function drawChart() {
  var data = google.visualization.arrayToDataTable([
    ['Country', 'Popularity', 'Quarter'],
    ['Germany', 200, 1],
    ['United States', 300, 2],
    ['Brazil', 400, 3],
    ['Canada', 500, 4],
    ['France', 600, 3],
    ['RU', 700, 2]
  ]);

  var dashboard = new google.visualization.Dashboard(document.getElementById('dashboard_div'));

  var quarterSelector = new google.visualization.ControlWrapper({
    controlType: 'CategoryFilter',
    containerId: 'filter_div',
    options: {
      filterColumnLabel: 'Quarter',
      ui: {
        allowTyping: false,
        allowMultiple: false,
        allowNone: false
      }
    }
  });


  var mapChart = new google.visualization.ChartWrapper({
    chartType: 'GeoChart',
    containerId: 'regions_div',
    options: {
        displayMode: 'regions'
    }
  });

  dashboard.bind(quarterSelector, mapChart);
  dashboard.draw(data);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<script src="https://www.google.com/jsapi"></script>
<div id="dashboard_div">
  <div id="filter_div"></div>
  <div id="regions_div"></div>
</div>

Upvotes: 1

Related Questions