首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >google.visualization.ChartWrapper组列视图

google.visualization.ChartWrapper组列视图
EN

Stack Overflow用户
提问于 2014-02-16 18:20:57
回答 1查看 4.5K关注 0票数 2

我是新使用谷歌可视化API加上我不是很熟悉的JavaScript。

我想要输出的是按第2栏中的标签分组。注意,纽约在图表上是重复的。我只想让图表按标签对第2列进行分组,并将第3列中的数值相加。有人知道怎么做吗?

代码语言:javascript
复制
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>
      Google Visualization API Sample
    </title>
    <script type="text/javascript" src="//www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load('visualization', '1.1', {packages: ['controls']});
    </script>
    <script type="text/javascript">
      function drawVisualization() {
        // Prepare the data
        var data = google.visualization.arrayToDataTable([
          ['Country', 'Region/State', 'City', 'Population'],
          ['USA', 'California', 'San Francisco', 776733],
          ['USA', 'California', 'Los Angeles', 3694820],
          ['USA', 'California', 'Mountain View', 70708],
          ['USA', 'New York', 'New York', 8175173],
          ['USA', 'New York', 'New York', 8175173],
          ['USA', 'New York', 'Albany', 857592],
          ['France', 'Ile-de-France', 'Paris', 2193031],
          ['France', 'Ile-de-France', 'Orly', 21372],
          ['France', 'Provence', 'Marseille', 852395],
          ['France', 'Provence', 'Nice', 348556]
        ]);

        // Define category pickers for 'Country', 'Region/State' and 'City'
        var countryPicker = new google.visualization.ControlWrapper({
          'controlType': 'CategoryFilter',
          'containerId': 'control1',
          'options': {
            'filterColumnLabel': 'Country',
            'ui': {
              'labelStacking': 'vertical',
              'allowTyping': false,
              'allowMultiple': false
            }
          }
        });

        var regionPicker = new google.visualization.ControlWrapper({
          'controlType': 'CategoryFilter',
          'containerId': 'control2',
          'options': {
            'filterColumnLabel': 'Region/State',
            'ui': {
              'labelStacking': 'vertical',
              'allowTyping': false,
              'allowMultiple': false
            }
          }
        });

        var cityPicker = new google.visualization.ControlWrapper({
          'controlType': 'CategoryFilter',
          'containerId': 'control3',
          'options': {
            'filterColumnLabel': 'City',
            'ui': {
              'labelStacking': 'vertical',
              'allowTyping': false,
              'allowMultiple': false
            }
          }
        });

        // Define a bar chart to show 'Population' data
        var barChart = new google.visualization.ChartWrapper({
          'chartType': 'BarChart',
          'containerId': 'chart1',
          'options': {
            'width': 400,
            'height': 300,
            'chartArea': {top: 0, right: 0, bottom: 0}
          },
          // Configure the barchart to use columns 2 (City) and 3 (Population)
          'view': {'columns': [2, 3]}
        });

        // Create the dashboard.
        new google.visualization.Dashboard(document.getElementById('dashboard')).
          // Configure the controls so that:
          // - the 'Country' selection drives the 'Region' one,
          // - the 'Region' selection drives the 'City' one,
          // - and finally the 'City' output drives the chart
          bind(countryPicker, regionPicker).
          bind(regionPicker, cityPicker).
          bind(cityPicker, barChart).
          // Draw the dashboard
          draw(data);
      }


      google.setOnLoadCallback(drawVisualization);
    </script>
  </head>
  <body style="font-family: Arial;border: 0 none;">
    <div id="dashboard">
      <table>
        <tr style='vertical-align: top'>
          <td style='width: 300px; font-size: 0.9em;'>
            <div id="control1"></div>
            <div id="control2"></div>
            <div id="control3"></div>
          </td>
          <td style='width: 600px'>
            <div style="float: left;" id="chart1"></div>
            <div style="float: left;" id="chart2"></div>
            <div style="float: left;" id="chart3"></div>
          </td>
        </tr>
      </table>
    </div>
  </body>
</html>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-19 23:42:35

要做到这一点,您需要一个中间可视化来充当仪表板中的代理(我通常使用一个表来实现这个功能)。仪表板将代理绑定到控件(而不是将图表绑定到控件)。在代理的“就绪”事件处理程序中,您需要聚合代理的数据并使用聚合数据绘制图表。下面是一个例子:

代码语言:javascript
复制
function drawVisualization() {
    // Prepare the data
    var data = google.visualization.arrayToDataTable([
        ['Country', 'Region/State', 'City', 'Population'],
        ['USA', 'California', 'San Francisco', 776733],
        ['USA', 'California', 'Los Angeles', 3694820],
        ['USA', 'California', 'Mountain View', 70708],
        ['USA', 'New York', 'New York', 8175173],
        ['USA', 'New York', 'New York', 8175173],
        ['USA', 'New York', 'Albany', 857592],
        ['France', 'Ile-de-France', 'Paris', 2193031],
        ['France', 'Ile-de-France', 'Orly', 21372],
        ['France', 'Provence', 'Marseille', 852395],
        ['France', 'Provence', 'Nice', 348556]
    ]);

    // Define category pickers for 'Country', 'Region/State' and 'City'
    var countryPicker = new google.visualization.ControlWrapper({
        'controlType': 'CategoryFilter',
        'containerId': 'control1',
        'options': {
            'filterColumnLabel': 'Country',
            'ui': {
                'labelStacking': 'vertical',
                'allowTyping': false,
                'allowMultiple': false
            }
        }
    });

    var regionPicker = new google.visualization.ControlWrapper({
        'controlType': 'CategoryFilter',
        'containerId': 'control2',
        'options': {
            'filterColumnLabel': 'Region/State',
            'ui': {
                'labelStacking': 'vertical',
                'allowTyping': false,
                'allowMultiple': false
            }
        }
    });

    var cityPicker = new google.visualization.ControlWrapper({
        'controlType': 'CategoryFilter',
        'containerId': 'control3',
        'options': {
            'filterColumnLabel': 'City',
            'ui': {
                'labelStacking': 'vertical',
                'allowTyping': false,
                'allowMultiple': false
            }
        }
    });

    // Define a bar chart to show 'Population' data
    var barChart = new google.visualization.ChartWrapper({
        'chartType': 'BarChart',
        'containerId': 'chart1',
        'options': {
            'width': 400,
            'height': 300,
            'chartArea': {top: 0, right: 0, bottom: 0}
        },
        // Configure the barchart to use columns 2 (City) and 3 (Population)
        'view': {'columns': [2, 3]}
    });

    var proxyTable = new google.visualization.ChartWrapper({
        chartType: 'Table',
        containerId: 'proxyTable',
        options: {
            // minimize the footprint of the table in HTML
            page: 'enable',
            pageSize: 1
        },
        view: {
            columns: [0]
        }
    });

    // create a "ready" event handler for proxyTable the handles data aggregation and drawing barChart
    google.visualization.events.addListener(proxyTable, 'ready', function () {
        var dt = proxyTable.getDataTable();
        var groupedData = google.visualization.data.group(dt, [0, 1, 2], [{
            column: 3,
            type: 'number',
            label: dt.getColumnLabel(3),
            aggregation: google.visualization.data.sum
        }]);
        // after grouping, the data will be sorted by column 0, then 1, then 2
        // if you want a different order, you have to re-sort
        barChart.setDataTable(groupedData);
        barChart.draw();
    });

    // Create the dashboard.
    new google.visualization.Dashboard(document.getElementById('dashboard')).
    // Configure the controls so that:
    // - the 'Country' selection drives the 'Region' one,
    // - the 'Region' selection drives the 'City' one,
    // - and finally the 'City' output drives proxyTable
    bind(countryPicker, regionPicker).
    bind(regionPicker, cityPicker).
    bind(cityPicker, proxyTable).
    // Draw the dashboard
    draw(data);
}
google.load('visualization', '1', {packages:['corechart', 'controls', 'table'], callback: drawVisualization});

参见这里的工作示例:http://jsfiddle.net/asgallant/MebSS/

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21815448

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档