Class: SimpleChartkick

Inherits:
SimpleOutput::SimpleOutputPlugin show all
Defined in:
lib/simplechartkick.rb

Overview

SimpleChartkick

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

Methods inherited from SimpleOutput::SimpleOutputPlugin

#advance_series, #annotate, #append_callback, #append_hash, #append_points, #append_series_name, #append_xy, #get_data_as_points, #get_data_as_xy, #get_series_hashes, #new_data, #new_data_check, #set_hash, #set_options, #set_points, #set_x_data, #set_xy, #set_y_callback, #set_y_data, #translate_name

Constructor Details

#initialize(filename = "results.html", title = "Html Results Page", javascript_path = "../include") ⇒ SimpleChartkick

Returns a new instance of SimpleChartkick.



22
23
24
25
26
27
28
29
30
31
# File 'lib/simplechartkick.rb', line 22

def initialize(filename="results.html", title="Html Results Page", javascript_path="../include")
  super()
  @filename = filename
   = {}
  @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



130
131
132
# File 'lib/simplechartkick.rb', line 130

def areachart(data)
  self.chart_div("AreaChart", data)
end

#barchart(data) ⇒ Object



126
127
128
# File 'lib/simplechartkick.rb', line 126

def barchart(data)
  self.chart_div("BarChart",data)
end

#chart_div(data, type = "LineChart", name = nil) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/simplechartkick.rb', line 100

def chart_div(data, type="LineChart", name=nil)
  #Convert data to pairs

  @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



33
34
35
36
37
38
39
40
# File 'lib/simplechartkick.rb', line 33

def check_title(name, options)
  if options.has_key?('series')
    [name]['series_titles'] << options['series']
  else
    [name]['series_titles'] << "set-#{@series_next}"
    @series_next += 1
  end
end

#columnchart(data) ⇒ Object



122
123
124
# File 'lib/simplechartkick.rb', line 122

def columnchart(data)
 self.chart_div("ColumnChart",data)
end

#div(content, name = nil) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/simplechartkick.rb', line 82

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

#get_multiseries_hashesObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/simplechartkick.rb', line 134

def get_multiseries_hashes
  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



110
111
112
113
114
115
# File 'lib/simplechartkick.rb', line 110

def linechart(data)
  #Accepts a name:value hash {"Football" => 10, "Basketball" => 5}

  #or a array of pairs [["Football", 10], ["Basketball", 5]]

  #may also nest series with a hash {:name => "Trial1", :data => trial1_data},...

  self.chart_div("LineChart",data)
end

#new_data_callback(name) ⇒ Object



52
53
54
55
# File 'lib/simplechartkick.rb', line 52

def new_data_callback(name)
  name = translate_name(name)
  [name] = {'length' => 0, 'chart_type' => 'LineChart', 'bincount' => 10, 'series_titles' => []}
end

#options_callback(options) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/simplechartkick.rb', line 57

def options_callback(options)
  if options.has_key?("chart_type")
    [@current_name]['chart_type'] = options['chart_type']
  end
  if options.has_key?('histogram')
    if options['histogram']
      [@current_name]['chart_type'] = 'Histogram'
    end
  end
  if options.has_key?('bincount')
    [@current_name]['bincount'] = options['bincount']
  end
  if options.has_key?('ymin')
    [@current_name]['ymin'] = options['ymin']
  end
  if options.has_key?('ymax')
    [@current_name]['ymax'] = options['ymax']
  end
end

#p(content, name = nil) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/simplechartkick.rb', line 91

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



117
118
119
120
# File 'lib/simplechartkick.rb', line 117

def piechart(data)
  #does not accept multiple series

  self.chart_div("PieChart",data)
end

#saveObject



153
154
155
156
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
# File 'lib/simplechartkick.rb', line 153

def save
  @chart_id = 0
  @js_block = ""
  
  self.get_multiseries_hashes.each_pair do |(chart_name, chart_series)|
    if !.has_key?(chart_name)
      [chart_name] = {'chart_type' => 'LineChart', 'bincount' => 10}
    end
    type = [chart_name].has_key?('chart_type') ? [chart_name]['chart_type'] : 'LineChart' 
    if type == "PieChart"
      chart_series = chart_series[0]['data']
    elsif type == "Histogram"
      type = 'ColumnChart'
      bins =  [chart_name]['bincount']
      #Reorder data

      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 = [chart_name].has_key?('ymin') ? [chart_name]['ymin'] : ypoints.min
        max = [chart_name].has_key?('ymax') ? [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



43
44
45
46
47
48
49
50
# File 'lib/simplechartkick.rb', line 43

def set_x_callback(data, name, options)
  xmin = data.min
  xmax = data.max
  [name]['xmin'] = xmin
  [name]['xmax'] = xmax
  [name]['length'] = ([name]['length'] < data.size) ? data.size : [name]['length']
  check_title(name, options)
end

#write_html(content, index) ⇒ Object

Rendering Functions functions



78
79
80
# File 'lib/simplechartkick.rb', line 78

def write_html(content, index)
  @html += content
end