-
Notifications
You must be signed in to change notification settings - Fork 25
/
graph.html.haml
106 lines (94 loc) · 2.98 KB
/
graph.html.haml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
!!! 5
- width, height = 1440, 800
- x_type = data[:types][0].to_json
%html
%head
%script(type="text/javascript" src="https://www.google.com/jsapi")
:css
html, p, div {
margin: 0;
padding: 0;
}
body {
padding: 5px;
}
#chart_div {
width: #{width}px;
height: #{height}px;
clear: both;
}
.subtext {
font-size: 12px;
color: #ccc;
float: left;
}
.right {
float: right;
}
:javascript
// Load the Visualization API and the piechart package.
google.load('visualization', '1.0', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
var queryData = #{data.to_json};
var data = queryData.data;
var typeConvert = function (x) { return "" + x; };
if (queryData.date) {
typeConvert = function (x) { return new Date(x); };
}
if (queryData.number) {
typeConvert = function (x) { return parseFloat(x); }
}
for (var i = 0, length = data.length; i < length; ++i) {
var row = data[i];
row[0] = typeConvert(row[0]);
}
var hAxis = { };
if (queryData.number) {
hAxis.baseline = 0;
}
var vAxes = [];
for (var i = 1, length = queryData.fields.length; i < length; ++i) {
vAxes.push({
baseline: 0,
format: queryData.number_format
});
}
// Callback that creates and populates a data table,
// instantiates the pie chart, passes in the data and
// draws it.
function drawChart() {
// Create the data table.
var data = new google.visualization.DataTable();
data.addColumn(#{x_type}, Date.parse(queryData.fields[0]));
for (var i = 1, length = queryData.fields.length; i < length; ++i) {
data.addColumn('number', queryData.fields[i]);
}
data.addRows(queryData.data);
var formatter = new google.visualization.NumberFormat({
pattern: queryData.number_format
});
for (var i = 1, length = queryData.fields.length; i < length; ++i) {
formatter.format(data, i);
}
// Set chart options
var options = {
title: queryData.title,
width: #{width},
height: #{height},
hAxis: hAxis,
animation: { duration: 150 },
colors: ['#1B91E0', '#B80D49', '#15B80D', '#999600',
'#00B39E', '#0056B3', '#8600B3', '#B36E00']
};
if (queryData.chart_type !== 'Pie') {
options.vAxes = vAxes;
}
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.#{data[:chart_type]}Chart(document.getElementById('chart_div'));
chart.draw(data, options);
}
%body
%p.subtext.right
= Time.now.getutc.strftime('%Y-%m-%d %H:%M:%S')
#chart_div