Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

extended gc4r to take sparkline fill areas

  • Loading branch information...
commit 061a94fbb6a0690a51748524bb60cf652fa83cc4 1 parent daceb74
@datawrangling authored
View
11 app/models/page.rb
@@ -6,7 +6,7 @@ def normed_daily_pageviews
@pageviews = JSON.parse(self.daily_timeline.pageviews)
@dates = JSON.parse(self.daily_timeline.dates)
maxval = @pageviews.max
- normed_values = @pageviews.collect { |x| x * (120.0 / maxval)}
+ normed_values = @pageviews.collect { |x| x * (110.0 / maxval)}
date_view_hash = {}
@dates.each_with_index do |date, index|
date_view_hash[date] = normed_values[index]
@@ -16,8 +16,13 @@ def normed_daily_pageviews
return sorted_pageviews
end
- def sparkline
- dataset = GC4R::API::GoogleChartDataset.new :data => self.normed_daily_pageviews, :color => '0000FF'
+ def sparkline( fillcolor='76A4FB' )
+ dataset = GC4R::API::GoogleChartDataset.new :data => self.normed_daily_pageviews,
+ :color => '000000', :fill => ['B', fillcolor ,'0','0','0']
+ # red => FF0000
+ # lightblue => 76A4FB
+ # green => 33FF00
+ # darkblue => 0000FF
data = GC4R::API::GoogleChartData.new :datasets => dataset , :min => 0, :max => 120
# @chart = GoogleBarChart.new :width => 120, :height => 12
@chart = GC4R::API::GoogleSparklinesChart.new :width => 120, :height => 12
View
2  app/views/pages/index.html.erb
@@ -21,7 +21,7 @@
<tr>
<td><%= index + 1 + APP_CONFIG['articles_per_page']*(params[:page].to_i - 1) %></td>
<td><%= number_with_delimiter(page.total_pageviews, ',') %></td>
- <td><%= link_to page.title, page %><BR><%= image_tag page.sparkline.to_url %> </td>
+ <td><%= link_to page.title, page %><BR><%= image_tag page.sparkline('76A4FB').to_url %> </td>
<td><%= link_to 'http://en.wikipedia.org/wiki/' + page.url, 'http://en.wikipedia.org/wiki/' + page.url %></td>
</tr>
<% end %>
View
335 lib/gc4r.rb
@@ -1,335 +0,0 @@
-module GC4R
-
- module GoogleChartsModule #:nodoc:
- def self.included(mod)
- mod.extend(ClassMethods)
- end
- module ClassMethods
- def use_google_charts
- include GC4R::API
- end
- end
- end # GoogleCharts
-
-
- module GoogleChartsObject #:nodoc:
- def create
- get_param + '=' + get_value
- end
- def get_param
- end
- def get_value
- end
- def valid?
- end
- def expand_multiple data, delimiter = "|"
- if data.is_a?(Array)
- data.join delimiter
- else
- data
- end
- end
- def validates value
- !value.nil?
- end
- end
- module GoogleChartContainer
- def create
- uri = []
- @params.each do |param|
- if param.valid?
- uri << param.create
- end
- end
- uri.join "&"
- end
- def add(param)
- @params ||= []
- @params << param
- end
- def valid? # if at least on child is valid
- valid = false;
- @params.each do |param|
- valid ||= param.valid?
- end
- valid
- end
- end
- class ChartContainer
- include GoogleChartContainer
- end
-
-# objects
- class Chart < Struct.new :chart_type
- include GoogleChartsObject
- def get_param
- 'cht'
- end
- def get_value
- chart_type
- end
- def valid?
- validates chart_type
- end
- end
- class ChartSize < Struct.new :width, :height
- include GoogleChartsObject
- def get_param
- 'chs'
- end
- def get_value
- width.to_s + 'x' + height.to_s
- end
- def valid?
- !width.nil? and !height.nil?
- end
- end
-
- # data
- class ChartData < Struct.new :datasets, :min, :max
- include GoogleChartsObject
- def get_param
- 'chd'
- end
- def get_value
- if datasets[0].is_a?(Array)
- 't:' + datasets.map{ |value| mangle(value).join(",") }.join("|")
- else
- 't:' + mangle(datasets).join(",")
- end
- end
- def valid?
- !datasets.nil? && !datasets.empty?
- end
- private
- def mangle dataset
- dataset.map do |value|
- if value.nil?
- -1
- else
- value
- end
- end
- end
- end
- class ChartDataScale < Struct.new :min, :max
- include GoogleChartsObject
- def get_param
- "chds"
- end
- def get_value
- min.to_s + ',' + max.to_s
- end
- def valid?
- !min.nil? && !max.nil?
- end
- end
- class ChartDataColor < Struct.new :colors
- include GoogleChartsObject
- def get_param
- "chco"
- end
- def get_value
- expand_multiple colors, ","
- end
- def valid?
- !colors.nil? && !colors.empty?
- end
- end
- class ChartDataLegend < Struct.new :names
- include GoogleChartsObject
- def get_param
- 'chdl'
- end
- def get_value
- expand_multiple names
- end
- def valid?
- # TODO hack
- !names.nil? && !names.empty?
- end
- end
- class ChartDataPieLabels < Struct.new :names #TODO hack, hack hack
- include GoogleChartsObject
- def get_param
- 'chl'
- end
- def get_value
- expand_multiple names
- end
- def valid?
- # TODO hack
- !names.nil? && !names.empty?
- end
- end
- class ChartAxis < Struct.new :axis
- include GoogleChartsObject
- def get_param
- 'chxt'
- end
- def get_value
- expand_multiple axis, ","
- end
- def valid?
- validates axis
- end
- end
- class ChartAxisLabels < Struct.new :labels
- include GoogleChartsObject
- def get_param
- 'chxl'
- end
- def get_value
- labels
- end
- def valid?
- validates labels
- end
- end
- # title
- class ChartTitleSize < Struct.new :color, :fontsize
- include GoogleChartsObject
- def get_param
- 'chts'
- end
- def get_value
- color + ',' + fontsize.to_s
- end
- def valid?
- !color.nil? and !fontsize.nil?
- end
- end
- class ChartTitleName < Struct.new :title
- include GoogleChartsObject
- def get_param
- 'chtt'
- end
- def get_value
- expand_multiple title
- end
- def valid?
- validates title
- end
- end
- class ChartTitle < ChartContainer
- def initialize options = {}
- add ChartTitleName.new(options[:title])
- add ChartTitleSize.new(options[:color], options[:fontsize])
- end
- end
-
-#
-# GC4R exported API
-#
- module API #:nodoc:
- # base chart
- class GoogleChart < GC4R::ChartContainer
- GOOGLE_CHART_URL = 'http://chart.apis.google.com/chart?'
- HELLO_WORLD = "cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World"
- def initialize options={}
- add Chart.new(options[:chart_type])
- add ChartTitle.new(options)
- add ChartSize.new(options[:width], options[:height])
- end
- def to_url
- uri = create
- if uri.nil? || uri.empty?
- uri = HELLO_WORLD
- end
- url = GOOGLE_CHART_URL + uri
- puts "Google Chart: #{url}"
- url
- end
- def data=(d)
- add d
- end
- def axis=(a)
- add a
- end
- end
-
- # chart types
- class GoogleLineChart < GoogleChart
- SIMPLE = 'lc'
- XY = 'lxy'
- def initialize options={}
- options[:chart_type] ||= SIMPLE
- super
- end
- end
- class GoogleSparklinesChart < GoogleChart
- def initialize options = {}
- options[:chart_type] = "ls"
- super
- end
- end
- class GoogleBarChart < GoogleChart
- VERTICAL = 'bvs'
- HORIZONTAL = 'bhs'
- def initialize options = {}
- options[:chart_type] ||= VERTICAL
- super
- end
- end
- class GooglePieChart < GoogleChart
- PIE_2D = 'p'
- PIE_3D = 'p3'
- def initialize options = {}
- options[:chart_type] ||= PIE_2D
- super
- end
- end
- class GoogleScatterPlotsChart < GoogleChart
- def initialize options = {}
- options[:chart_type] = "s"
- super
- end
- end
-
- # data and dataset
- class GoogleChartDataset
- attr_accessor :data, :color, :title
- def initialize options={}
- @data = options[:data]
- @color = options[:color]
- @title = options[:title]
- end
- end
- class GoogleChartData < ChartContainer
- def initialize options = {}
- datasets = []
- colors = []
- legend = []
- if options[:datasets].is_a?(Array)
- options[:datasets].each do |dataset|
- datasets << dataset.data
- colors << dataset.color
- legend << dataset.title
- end
- else
- datasets << options[:datasets].data
- colors << options[:datasets].color
- legend << options[:datasets].title
- end
- add ChartData.new(datasets.compact)
- add ChartDataColor.new(colors.compact)
- add ChartDataLegend.new(legend.compact)
- add ChartDataPieLabels.new(legend.compact)
- add ChartDataScale.new(options[:min], options[:max])
- end
- end
- # axis
- class GoogleChartAxis < ChartContainer
- LEFT = 'y'
- BOTTOM = 'x'
- RIGHT = 'r'
- TOP = 't'
- def initialize options={}
- add ChartAxis.new options[:axis]
- end
- end
- end
-
-
-end
-
View
19 vendor/plugins/svn/lib/gc4r.rb
@@ -124,6 +124,18 @@ def valid?
!min.nil? && !max.nil?
end
end
+ class ChartFillArea < Struct.new :fill
+ include GoogleChartsObject
+ def get_param
+ "chm"
+ end
+ def get_value
+ expand_multiple fill, ","
+ end
+ def valid?
+ !fill.nil? && !fill.empty?
+ end
+ end
class ChartDataColor < Struct.new :colors
include GoogleChartsObject
def get_param
@@ -288,11 +300,12 @@ def initialize options = {}
# data and dataset
class GoogleChartDataset
- attr_accessor :data, :color, :title
+ attr_accessor :data, :color, :title, :fill
def initialize options={}
@data = options[:data]
@color = options[:color]
@title = options[:title]
+ @fill = options[:fill]
end
end
class GoogleChartData < ChartContainer
@@ -300,19 +313,23 @@ def initialize options = {}
datasets = []
colors = []
legend = []
+ fill = []
if options[:datasets].is_a?(Array)
options[:datasets].each do |dataset|
datasets << dataset.data
colors << dataset.color
+ fill << dataset.fill
legend << dataset.title
end
else
datasets << options[:datasets].data
colors << options[:datasets].color
+ fill << options[:datasets].fill
legend << options[:datasets].title
end
add ChartData.new(datasets.compact)
add ChartDataColor.new(colors.compact)
+ add ChartFillArea.new(fill.compact)
add ChartDataLegend.new(legend.compact)
add ChartDataPieLabels.new(legend.compact)
add ChartDataScale.new(options[:min], options[:max])
Please sign in to comment.
Something went wrong with that request. Please try again.