Class: SimpleChartkick

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

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

Methods inherited from SimpleOutput::SimpleOutputPlugin

#advance_series, #annotate, #appendHash, #appendPoints, #appendXY, #append_callback, #append_series_name, #getDataAsPoints, #getDataAsXY, #getSeriesHashes, #newData, #setHash, #setOptions, #setPoints, #setXData, #setXY, #setYData, #set_x_callback, #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
# File 'lib/simplechartkick.rb', line 23

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



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

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

#barchart(data) ⇒ Object



106
107
108
# File 'lib/simplechartkick.rb', line 106

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

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



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

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

#columnchart(data) ⇒ Object



102
103
104
# File 'lib/simplechartkick.rb', line 102

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

#div(content, name = nil) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/simplechartkick.rb', line 62

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



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/simplechartkick.rb', line 114

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



90
91
92
93
94
95
# File 'lib/simplechartkick.rb', line 90

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



33
34
35
# File 'lib/simplechartkick.rb', line 33

def new_data_callback(name)
  @metadata[name] = {'chart_type' => 'LineChart', 'bincount' => 10}
end

#options_callback(options) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/simplechartkick.rb', line 37

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



71
72
73
74
75
76
77
78
# File 'lib/simplechartkick.rb', line 71

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



97
98
99
100
# File 'lib/simplechartkick.rb', line 97

def piechart(data)
  #does not accept multiple series
  self.chart_div("PieChart",data)
end

#saveObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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
# File 'lib/simplechartkick.rb', line 135

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']
      #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 = @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

#write_html(content, index) ⇒ Object

Rendering Functions functions



58
59
60
# File 'lib/simplechartkick.rb', line 58

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