Class: SimpleChartkick
Overview
SimpleOutput
Copyright 2014 Austen Higgins-Cassidy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Instance Method Summary
collapse
-
#areachart(data) ⇒ Object
-
#barchart(data) ⇒ Object
-
#chart_div(data, type = "LineChart", name = nil) ⇒ Object
-
#check_title(name, options) ⇒ Object
-
#columnchart(data) ⇒ Object
-
#div(content, name = nil) ⇒ Object
-
#getMultiSeriesHashes ⇒ Object
-
#initialize(filename = "results.html", title = "Html Results Page", javascript_path = "../include") ⇒ SimpleChartkick
constructor
A new instance of SimpleChartkick.
-
#linechart(data) ⇒ Object
-
#new_data_callback(name) ⇒ Object
-
#options_callback(options) ⇒ Object
-
#p(content, name = nil) ⇒ Object
-
#piechart(data) ⇒ Object
-
#save ⇒ Object
-
#set_x_callback(data, name, options) ⇒ Object
-
#write_html(content, index) ⇒ Object
Rendering Functions functions.
#advance_series, #annotate, #appendHash, #appendPoints, #appendXY, #append_callback, #append_series_name, #getDataAsPoints, #getDataAsXY, #getSeriesHashes, #newData, #new_data_check, #setHash, #setOptions, #setPoints, #setXData, #setXY, #setYData, #set_y_callback, #translate_name
Constructor Details
#initialize(filename = "results.html", title = "Html Results Page", javascript_path = "../include") ⇒ SimpleChartkick
Returns a new instance of SimpleChartkick.
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/simplechartkick.rb', line 23
def initialize(filename="results.html", title="Html Results Page", javascript_path="../include")
super()
@filename = filename
@metadata = {}
@series_next = 0
chartkick_path = javascript_path + ((javascript_path[-1] == "/") ? "chartkick.js" : "/chartkick.js");
@html = "<html>\n<title>\n#{title}\n</title>\n<script src='http://www.google.com/jsapi'></script>\n
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js'></script>
<script src='#{chartkick_path}'></script>\n<body>\n"
end
|
Instance Method Details
#areachart(data) ⇒ Object
132
133
134
|
# File 'lib/simplechartkick.rb', line 132
def areachart(data)
self.chart_div("AreaChart", data)
end
|
#barchart(data) ⇒ Object
128
129
130
|
# File 'lib/simplechartkick.rb', line 128
def barchart(data)
self.chart_div("BarChart",data)
end
|
#chart_div(data, type = "LineChart", name = nil) ⇒ Object
102
103
104
105
106
107
108
109
110
|
# File 'lib/simplechartkick.rb', line 102
def chart_div(data, type="LineChart", name=nil)
@chart_id = @chart_id + 1
if name != nil
self.div("<h1>#{name}</h1>")
end
self.write_html("<div id='chart-#{@chart_id}' style='height:450px;'></div>\n", @chart_id);
@js_block += "new Chartkick.#{type}('chart-#{@chart_id}'," + data.to_json + ", {'format':'string'});\n"
end
|
#check_title(name, options) ⇒ Object
35
36
37
38
39
40
41
42
|
# File 'lib/simplechartkick.rb', line 35
def check_title(name, options)
if options.has_key?('series')
@metadata[name]['series_titles'] << options['series']
else
@metadata[name]['series_titles'] << "set-#{@series_next}"
@series_next += 1
end
end
|
#columnchart(data) ⇒ Object
124
125
126
|
# File 'lib/simplechartkick.rb', line 124
def columnchart(data)
self.chart_div("ColumnChart",data)
end
|
#div(content, name = nil) ⇒ Object
84
85
86
87
88
89
90
91
|
# File 'lib/simplechartkick.rb', line 84
def div(content, name = nil)
if name == nil
index = @chart_id
else
index = @chart_names[name]
end
self.write_html("<div>#{content}</div>\n", index)
end
|
#getMultiSeriesHashes ⇒ Object
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
# File 'lib/simplechartkick.rb', line 136
def getMultiSeriesHashes
data_hash = {}
@x.each_pair do |(key, x_series)|
data_hash[key] = []
y_series = @y[key]
x_series.each_with_index do |x_data, index|
y_data = y_series[index]
series_key = @series_names[key][index]
data_hash[key] << {'name' => series_key}
data_hash[key].last['data'] = {}
x_data.each_with_index do |x_point, index|
y_point = y_data[index]
data_hash[key].last['data'][x_point] = y_point
end
end
end
return data_hash
end
|
#linechart(data) ⇒ Object
112
113
114
115
116
117
|
# File 'lib/simplechartkick.rb', line 112
def linechart(data)
self.chart_div("LineChart",data)
end
|
#new_data_callback(name) ⇒ Object
54
55
56
57
|
# File 'lib/simplechartkick.rb', line 54
def new_data_callback(name)
name = translate_name(name)
@metadata[name] = {'length' => 0, 'chart_type' => 'LineChart', 'bincount' => 10, 'series_titles' => []}
end
|
#options_callback(options) ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/simplechartkick.rb', line 59
def options_callback(options)
if options.has_key?("chart_type")
@metadata[@current_name]['chart_type'] = options['chart_type']
end
if options.has_key?('histogram')
if options['histogram']
@metadata[@current_name]['chart_type'] = 'Histogram'
end
end
if options.has_key?('bincount')
@metadata[@current_name]['bincount'] = options['bincount']
end
if options.has_key?('ymin')
@metadata[@current_name]['ymin'] = options['ymin']
end
if options.has_key?('ymax')
@metadata[@current_name]['ymax'] = options['ymax']
end
end
|
#p(content, name = nil) ⇒ Object
93
94
95
96
97
98
99
100
|
# File 'lib/simplechartkick.rb', line 93
def p(content, name = nil)
if name == nil
index = @chart_id
else
index = @chart_names[name]
end
self.write_html("<p>#{content}</p>\n", index)
end
|
#piechart(data) ⇒ Object
119
120
121
122
|
# File 'lib/simplechartkick.rb', line 119
def piechart(data)
self.chart_div("PieChart",data)
end
|
#save ⇒ Object
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
# File 'lib/simplechartkick.rb', line 157
def save
@chart_id = 0
@js_block = ""
self.getMultiSeriesHashes.each_pair do |(chart_name, chart_series)|
if !@metadata.has_key?(chart_name)
@metadata[chart_name] = {'chart_type' => 'LineChart', 'bincount' => 10}
end
type = @metadata[chart_name].has_key?('chart_type') ? @metadata[chart_name]['chart_type'] : 'LineChart'
if type == "PieChart"
chart_series = chart_series[0]['data']
elsif type == "Histogram"
type = 'ColumnChart'
bins = @metadata[chart_name]['bincount']
chart_series.each do |series|
name = series['name']
series_data = series['data']
hist_data = {}
ypoints = []
series_data.each_pair do |(x, y)|
ypoints << y
end
min = @metadata[chart_name].has_key?('ymin') ? @metadata[chart_name]['ymin'] : ypoints.min
max = @metadata[chart_name].has_key?('ymax') ? @metadata[chart_name]['ymax'] : ypoints.max
width = (max.to_f-min.to_f)/bins.to_f
bins.times do |i|
index = (width*i).round(2)
hist_data[index] = 0
ypoints.delete_if do |value|
if value >= width*i && value < width*(i+1)
hist_data[index] += 1
true
else
false
end
end
end
series['data'] = hist_data
end
end
self.chart_div(chart_series, type, chart_name)
if @annotations.has_key?(chart_name)
@annotations[chart_name].each {|content| self.p(content)}
end
end
@html += "<script>$(function (){#{@js_block}});</script></body></html>"
File.open(@filename, "w") do |file|
file.syswrite(@html)
end
end
|
#set_x_callback(data, name, options) ⇒ Object
45
46
47
48
49
50
51
52
|
# File 'lib/simplechartkick.rb', line 45
def set_x_callback(data, name, options)
xmin = data.min
xmax = data.max
@metadata[name]['xmin'] = xmin
@metadata[name]['xmax'] = xmax
@metadata[name]['length'] = (@metadata[name]['length'] < data.size) ? data.size : @metadata[name]['length']
check_title(name, options)
end
|
#write_html(content, index) ⇒ Object
Rendering Functions functions
80
81
82
|
# File 'lib/simplechartkick.rb', line 80
def write_html(content, index)
@html += content
end
|