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, #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.



22
23
24
25
26
27
28
29
30
31
32
# 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



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

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

#barchart(data) ⇒ Object



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

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

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



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

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



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

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



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

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

#div(content, name = nil) ⇒ Object



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

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

#getMultiSeriesHashesObject



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

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



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

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



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

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

#options_callback(options) ⇒ Object



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

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



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

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



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

def piechart(data)
  #does not accept multiple series

  self.chart_div("PieChart",data)
end

#saveObject



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
206
207
208
# File 'lib/simplechartkick.rb', line 156

def save
  @chart_id = 0
  @js_block = ""
  
  self.getMultiSeriesHashes.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



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

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



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

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