Google Charts API - Dashboard: use calculated columns in a table

2k Views Asked by At

I have a simple display panel shows Google and I want to add a calculated column in my DataView using SetColumn as shown in this code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<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="http://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([
      ['Name', 'Gender', 'Age', 'Donuts eaten'],
      ['Michael' , 'Male', 12, 5],
      ['Elisa', 'Female', 20, 7],
      ['Robert', 'Male', 7, 3],
      ['John', 'Male', 54, 2],
      ['Jessica', 'Female', 22, 6],
      ['Aaron', 'Male', 3, 1],
      ['Margareth', 'Female', 42, 8],
      ['Miranda', 'Female', 33, 6]
    ]);
    var view = new google.visualization.DataView(data);
    view.setColumns([0,1,2,3,1,{
                       calc: mas,
                       type: 'number',
                       label: 'x10',
    }])
      function mas(view2,row) {
          var a = view2.getValue(row,2)*10;
          return a;
      }
    // Define a slider control for the Age column.
    var slider = new google.visualization.ControlWrapper({
      'controlType': 'NumberRangeFilter',
      'containerId': 'control1',
      'options': {
        'filterColumnLabel': 'Age',
      'ui': {'labelStacking': 'vertical'}
      }
    });

    // Define a category picker control for the Gender column
    var categoryPicker = new google.visualization.ControlWrapper({
      'controlType': 'CategoryFilter',
      'containerId': 'control2',
      'options': {
        'filterColumnLabel': 'Gender',
        'ui': {
        'labelStacking': 'vertical',
          'allowTyping': false,
          'allowMultiple': false
        }
      }
    });

    // Define a Pie chart
    var pie = new google.visualization.ChartWrapper({
      'chartType': 'PieChart',
      'containerId': 'chart1',
      'options': {
        'width': 300,
        'height': 300,
        'legend': 'none',
        'title': 'Donuts eaten per person',
        'chartArea': {'left': 15, 'top': 15, 'right': 0, 'bottom': 0},
        'pieSliceText': 'label'
      },
      // Instruct the piechart to use colums 0 (Name) and 3 (Donuts Eaten)
      // from the 'data' DataTable.
      'view': {'columns': [0, 3]}
    });

    // Define a table
    var table = new google.visualization.ChartWrapper({
      'chartType': 'Table',
      'containerId': 'chart2',
      'options': {
        'width': '300px'
      }
    });

    // 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([slider, categoryPicker], [pie, table]).
        // Draw the entire dashboard.
        draw(view);
  }


  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>

this code is based on another's in "Google Code Playground" and can be tested on the spot. I am stucked with a problem, when I put setColumn(0,1,2,3,1, "the Calculated column") to allow the table show me these columns, the calculated column is not shown in the table, neither can be used in the PieChart to declare which columns are relevant for the chart. How I can show a calculated column and use it in a dashboard?

Thanks in advance!

0

There are 0 best solutions below