|
1 #!/usr/bin/python |
|
2 # |
|
3 # Copyright (C) 2008 Google Inc. |
|
4 # |
|
5 # Licensed under the Apache License, Version 2.0 (the "License"); |
|
6 # you may not use this file except in compliance with the License. |
|
7 # You may obtain a copy of the License at |
|
8 # |
|
9 # http://www.apache.org/licenses/LICENSE-2.0 |
|
10 # |
|
11 # Unless required by applicable law or agreed to in writing, software |
|
12 # distributed under the License is distributed on an "AS IS" BASIS, |
|
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 # See the License for the specific language governing permissions and |
|
15 # limitations under the License. |
|
16 |
|
17 """Example of static use of Google Visualization Python API.""" |
|
18 |
|
19 __author__ = "Misha Seltzer" |
|
20 |
|
21 import gviz_api |
|
22 |
|
23 page_template = """ |
|
24 <html> |
|
25 <head> |
|
26 <title>Static example</title> |
|
27 <script src="http://www.google.com/jsapi" type="text/javascript"></script> |
|
28 <script> |
|
29 google.load("visualization", "1", {packages:["table"]}); |
|
30 |
|
31 google.setOnLoadCallback(drawTable); |
|
32 function drawTable() { |
|
33 %(jscode)s |
|
34 var jscode_table = new google.visualization.Table(document.getElementById('table_div_jscode')); |
|
35 jscode_table.draw(jscode_data, {showRowNumber: true}); |
|
36 |
|
37 var json_table = new google.visualization.Table(document.getElementById('table_div_json')); |
|
38 var json_data = new google.visualization.DataTable(%(json)s, 0.5); |
|
39 json_table.draw(json_data, {showRowNumber: true}); |
|
40 } |
|
41 </script> |
|
42 </head> |
|
43 <body> |
|
44 <H1>Table created using ToJSCode</H1> |
|
45 <div id="table_div_jscode"></div> |
|
46 <H1>Table created using ToJSon</H1> |
|
47 <div id="table_div_json"></div> |
|
48 </body> |
|
49 </html> |
|
50 """ |
|
51 |
|
52 |
|
53 def main(): |
|
54 # Creating the data |
|
55 description = {"name": ("string", "Name"), |
|
56 "salary": ("number", "Salary"), |
|
57 "full_time": ("boolean", "Full Time Employee")} |
|
58 data = [{"name": "Mike", "salary": (10000, "$10,000"), "full_time": True}, |
|
59 {"name": "Jim", "salary": (800, "$800"), "full_time": False}, |
|
60 {"name": "Alice", "salary": (12500, "$12,500"), "full_time": True}, |
|
61 {"name": "Bob", "salary": (7000, "$7,000"), "full_time": True}] |
|
62 |
|
63 # Loading it into gviz_api.DataTable |
|
64 data_table = gviz_api.DataTable(description) |
|
65 data_table.LoadData(data) |
|
66 |
|
67 # Creating a JavaScript code string |
|
68 jscode = data_table.ToJSCode("jscode_data", |
|
69 columns_order=("name", "salary", "full_time"), |
|
70 order_by="salary") |
|
71 # Creating a JSon string |
|
72 json = data_table.ToJSon(columns_order=("name", "salary", "full_time"), |
|
73 order_by="salary") |
|
74 |
|
75 # Putting the JS code and JSon string into the template |
|
76 print page_template % vars() |
|
77 |
|
78 |
|
79 if __name__ == "__main__": |
|
80 main() |