https://google-developers.appspot.com/chart/interactive/docs/quick_start
We can input the data directly from a contingency table created in R:
> rs<-read.table('regSuperCensusMod.csv',header=T,sep=',')
> rsi<-rs[rs$IndOrg=="IND",]
> ct<-table(rsi[,c('Candidate','RegularSuper')])
> ct
RegularSuper
Candidate Regular Super
barack obama 28864 41
mitt romney 10470 159
newt gingrich 1266 18
1 <html>
2 <head>
3 <script type="text/javascript" src="https://www.google.com/jsapi"></script>
4 <script type="text/javascript">
5 google.load("visualization", "1", {packages:["corechart"]});
6 google.setOnLoadCallback(drawChart);
7 function drawChart() {
8 var data = new google.visualization.DataTable();
9 data.addColumn('string', 'candidate');
10 data.addColumn('number', 'Regular');
11 data.addColumn('number', 'Super');
12 data.addRows([
13 ["barack obama",28864,41],
14 ["mitt romney", 10470, 159],
15 ["newt gingrich", 1266, 18]
16 ]);
17 var options = {
18 title: 'Regular vs Super PAC # contributions',
19 vAxis: {title: 'Candidate', titleTextStyle: {color: 'blue'}},
20 hAxis: {title: '# Contributions', titleTextStyle: {color: 'blue'}}
21 };
22 var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
23 chart.draw(data, options);
24 }
25 </script>
26 </head>
27 <body>
28 <div id="chart_div" style="width: 900px; height: 500px;"></div>
29 </body>
30 </html>
Here is the relevant excerpt from code that reads in data files:
1 description = [("candidate","string"),("Regular","number"),("Super","number")]
2 data=[]
3 for line in open('/home/qtw/public_html/data/regsuper.csv'):
4 bits=line.split(',')
5 #skip the header file since that is still hard-coded
6 if bits[0]=='candidate': continue
7 data.append((bits[0],int(bits[1]),int(bits[2])))
8 #create a DataTable object
9 data_table = gviz_api.DataTable(description)
10 data_table.LoadData(data)
First let's take some starter code from the documentation:
1 function drawRegionsMap() {
2 var data = google.visualization.arrayToDataTable([
3 ['Country', 'Popularity'],
4 ['Germany', 200],
5 ['United States', 300],
6 ['Brazil', 400],
7 ['Canada', 500],
8 ['France', 600],
9 ['RU', 700]
10 ]);
11
12 var options = {};
13
14 var chart = new google.visualization.GeoChart(document.getElementById('chart_div'));
15 chart.draw(data, options);
16 };
We can provide fake data to the map to test that we set the parameters correctly:
1 function drawRegionsMap() {
2 var data = google.visualization.arrayToDataTable([
3 ['State', 'Diversity Index'],
4 ['US-AL', 0.48],
5 ['US-AR', 0.42],
6 ['US-AZ', 0.596],
7 ]);
8 var options = {'region':'US','resolution':'provinces'};
9 var chart = new google.visualization.GeoChart(document.getElementById('chart_div'));
10 chart.draw(data, options);
11 };
Code excerpt from Python CGI script to create schema and data:
1 #first create the schema, explaining the columns and their types
2 description = [("State","string"),("Diversity Index","number")]
3 #now make the data, as a list of tuples
4 data=[]
5
6 for line in open('/home/qtw/public_html/data/statedemo.csv'):
7 bits=line.split(',')
8 #skip the header file since that is still hard-coded
9 if bits[0]=='State': continue
10 data.append(("US-"+bits[0],int(100*float(bits[3].strip()))))
11
12 #create a DataTable object
13 data_table = gviz_api.DataTable(description)
14 data_table.LoadData(data)
This code:
1 var chart = new google.visualization.IntensityMap(
2 document.getElementById('chart_div'));
3 chart.draw(data, {'region':'usa'});
Can also be written like this:
1 var chart = new google.visualization.ChartWrapper({
2 chartType: 'IntensityMap',
3 dataTable: data,
4 options: {'region':'usa'},
5 containerId:'chart_div'
6 });
7 chart.draw();
Here is some example code using a DataView:
1 var data = new google.visualization.DataTable(%(json_str)s);
2 var dview = new google.visualization.DataView(data);
3 ...
4 chart.draw(dview, {});
Here is an example from the Google documentation:
1 // This call will group the table by column 0 values.
2 // It will also show column 3, which will be a sum of
3 // values in that column for that row group.
4 var result = google.visualization.data.group(
5 dt,
6 [0],
7 [{'column': 3, 'aggregation': google.visualization.data.sum, 'type': 'number'}]
8 );
9
10 *Input table*
11 1 'john' 'doe' 10
12 1 'jane' 'doe' 100
13 3 'jill' 'jones' 50
14 3 'jack' 'jones' 75
15 5 'al' 'weisenheimer' 500
16
17 *Output table*
18 1 110
19 3 125
20 5 500
Table of Contents | t |
---|---|
Exposé | ESC |
Full screen slides | e |
Presenter View | p |
Source Files | s |
Slide Numbers | n |
Toggle screen blanking | b |
Show/hide slide context | c |
Notes | 2 |
Help | h |