gruffy 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,67 @@
1
+
2
+ require File.dirname(__FILE__) + '/base'
3
+ require File.dirname(__FILE__) + '/stacked_mixin'
4
+
5
+ class Gruffy::StackedArea < Gruffy::Base
6
+ include StackedMixin
7
+ attr_accessor :last_series_goes_on_bottom
8
+
9
+ def draw
10
+ get_maximum_by_stack
11
+ super
12
+
13
+ return unless @has_data
14
+
15
+ @x_increment = @graph_width / (@column_count - 1).to_f
16
+ @d = @d.stroke 'transparent'
17
+
18
+ height = Array.new(@column_count, 0)
19
+
20
+ data_points = nil
21
+ iterator = last_series_goes_on_bottom ? :reverse_each : :each
22
+ @norm_data.send(iterator) do |data_row|
23
+ prev_data_points = data_points
24
+ data_points = Array.new
25
+
26
+ @d = @d.fill data_row[DATA_COLOR_INDEX]
27
+
28
+ data_row[DATA_VALUES_INDEX].each_with_index do |data_point, index|
29
+ # Use incremented x and scaled y
30
+ new_x = @graph_left + (@x_increment * index)
31
+ new_y = @graph_top + (@graph_height - data_point * @graph_height - height[index])
32
+
33
+ height[index] += (data_point * @graph_height)
34
+
35
+ data_points << new_x
36
+ data_points << new_y
37
+
38
+ draw_label(new_x, index)
39
+
40
+ end
41
+
42
+ if prev_data_points
43
+ poly_points = data_points.dup
44
+ (prev_data_points.length/2 - 1).downto(0) do |i|
45
+ poly_points << prev_data_points[2*i]
46
+ poly_points << prev_data_points[2*i+1]
47
+ end
48
+ poly_points << data_points[0]
49
+ poly_points << data_points[1]
50
+ else
51
+ poly_points = data_points.dup
52
+ poly_points << @graph_right
53
+ poly_points << @graph_bottom - 1
54
+ poly_points << @graph_left
55
+ poly_points << @graph_bottom - 1
56
+ poly_points << data_points[0]
57
+ poly_points << data_points[1]
58
+ end
59
+ @d = @d.polyline(*poly_points)
60
+
61
+ end
62
+
63
+ @d.draw(@base_image)
64
+ end
65
+
66
+
67
+ end
@@ -0,0 +1,61 @@
1
+
2
+ require File.dirname(__FILE__) + '/base'
3
+ require File.dirname(__FILE__) + '/stacked_mixin'
4
+
5
+ class Gruffy::StackedBar < Gruffy::Base
6
+ include StackedMixin
7
+
8
+ # Spacing factor applied between bars
9
+ attr_accessor :bar_spacing
10
+
11
+ # Number of pixels between bar segments
12
+ attr_accessor :segment_spacing
13
+
14
+ # Draws a bar graph, but multiple sets are stacked on top of each other.
15
+ def draw
16
+ get_maximum_by_stack
17
+ super
18
+ return unless @has_data
19
+
20
+ # Setup spacing.
21
+ #
22
+ # Columns sit stacked.
23
+ @bar_spacing ||= 0.9
24
+ @segment_spacing ||= 1
25
+ @bar_width = @graph_width / @column_count.to_f
26
+ padding = (@bar_width * (1 - @bar_spacing)) / 2
27
+
28
+ @d = @d.stroke_opacity 0.0
29
+
30
+ height = Array.new(@column_count, 0)
31
+
32
+ @norm_data.each_with_index do |data_row, row_index|
33
+ data_row[DATA_VALUES_INDEX].each_with_index do |data_point, point_index|
34
+ @d = @d.fill data_row[DATA_COLOR_INDEX]
35
+
36
+ # Calculate center based on bar_width and current row
37
+ label_center = @graph_left + (@bar_width * point_index) + (@bar_width * @bar_spacing / 2.0)
38
+ draw_label(label_center, point_index)
39
+
40
+ next if (data_point == 0)
41
+ # Use incremented x and scaled y
42
+ left_x = @graph_left + (@bar_width * point_index) + padding
43
+ left_y = @graph_top + (@graph_height -
44
+ data_point * @graph_height -
45
+ height[point_index]) + @segment_spacing
46
+ right_x = left_x + @bar_width * @bar_spacing
47
+ right_y = @graph_top + @graph_height - height[point_index] - @segment_spacing
48
+
49
+ # update the total height of the current stacked bar
50
+ height[point_index] += (data_point * @graph_height )
51
+
52
+ @d = @d.rectangle(left_x, left_y, right_x, right_y)
53
+
54
+ end
55
+
56
+ end
57
+
58
+ @d.draw(@base_image)
59
+ end
60
+
61
+ end
@@ -0,0 +1,23 @@
1
+
2
+ module Gruffy::Base::StackedMixin
3
+ # Used by StackedBar and child classes.
4
+ #
5
+ # tsal: moved from Base 03 FEB 2007
6
+ DATA_VALUES_INDEX = Gruffy::Base::DATA_VALUES_INDEX
7
+ def get_maximum_by_stack
8
+ # Get sum of each stack
9
+ max_hash = {}
10
+ @data.each do |data_set|
11
+ data_set[DATA_VALUES_INDEX].each_with_index do |data_point, i|
12
+ max_hash[i] = 0.0 unless max_hash[i]
13
+ max_hash[i] += data_point.to_f
14
+ end
15
+ end
16
+
17
+ # @maximum_value = 0
18
+ max_hash.keys.each do |key|
19
+ @maximum_value = max_hash[key] if max_hash[key] > @maximum_value
20
+ end
21
+ @minimum_value = 0
22
+ end
23
+ end
@@ -0,0 +1,102 @@
1
+ module Gruffy
2
+ module Themes
3
+
4
+ # A color scheme similar to the popular presentation software.
5
+ KEYNOTE = {
6
+ :colors => [
7
+ '#FDD84E', # yellow
8
+ '#6886B4', # blue
9
+ '#72AE6E', # green
10
+ '#D1695E', # red
11
+ '#8A6EAF', # purple
12
+ '#EFAA43', # orange
13
+ 'white'
14
+ ],
15
+ :marker_color => 'white',
16
+ :font_color => 'white',
17
+ :background_colors => %w(black #4a465a)
18
+ }
19
+
20
+ # A color scheme plucked from the colors on the popular usability blog.
21
+ THIRTYSEVEN_SIGNALS = {
22
+ :colors => [
23
+ '#FFF804', # yellow
24
+ '#336699', # blue
25
+ '#339933', # green
26
+ '#ff0000', # red
27
+ '#cc99cc', # purple
28
+ '#cf5910', # orange
29
+ 'black'
30
+ ],
31
+ :marker_color => 'black',
32
+ :font_color => 'black',
33
+ :background_colors => %w(#d1edf5 white)
34
+ }
35
+
36
+ # A color scheme from the colors used on the 2005 Rails keynote
37
+ # presentation at RubyConf.
38
+ RAILS_KEYNOTE = {
39
+ :colors => [
40
+ '#00ff00', # green
41
+ '#333333', # grey
42
+ '#ff5d00', # orange
43
+ '#f61100', # red
44
+ 'white',
45
+ '#999999', # light grey
46
+ 'black'
47
+ ],
48
+ :marker_color => 'white',
49
+ :font_color => 'white',
50
+ :background_colors => %w(#0083a3 #0083a3)
51
+ }
52
+
53
+ # A color scheme similar to that used on the popular podcast site.
54
+ ODEO = {
55
+ :colors => [
56
+ '#202020', # grey
57
+ 'white',
58
+ '#3a5b87', # dark blue
59
+ '#a21764', # dark pink
60
+ '#8ab438', # green
61
+ '#999999', # light grey
62
+ 'black'
63
+ ],
64
+ :marker_color => 'white',
65
+ :font_color => 'white',
66
+ :background_colors => %w(#ff47a4 #ff1f81)
67
+ }
68
+
69
+ # A pastel theme
70
+ PASTEL = {
71
+ :colors => [
72
+ '#a9dada', # blue
73
+ '#aedaa9', # green
74
+ '#daaea9', # peach
75
+ '#dadaa9', # yellow
76
+ '#a9a9da', # dk purple
77
+ '#daaeda', # purple
78
+ '#dadada' # grey
79
+ ],
80
+ :marker_color => '#aea9a9', # Grey
81
+ :font_color => 'black',
82
+ :background_colors => 'white'
83
+ }
84
+
85
+ # A greyscale theme
86
+ GREYSCALE = {
87
+ :colors => [
88
+ '#282828', #
89
+ '#383838', #
90
+ '#686868', #
91
+ '#989898', #
92
+ '#c8c8c8', #
93
+ '#e8e8e8', #
94
+ ],
95
+ :marker_color => '#aea9a9', # Grey
96
+ :font_color => 'black',
97
+ :background_colors => 'white'
98
+ }
99
+
100
+ end
101
+ end
102
+
@@ -0,0 +1,3 @@
1
+ module Gruffy
2
+ VERSION = '0.0.3'
3
+ end
@@ -0,0 +1,63 @@
1
+ class GruffyGenerator < Rails::Generator::NamedBase
2
+
3
+ attr_reader :controller_name,
4
+ :controller_class_path,
5
+ :controller_file_path,
6
+ :controller_class_nesting,
7
+ :controller_class_nesting_depth,
8
+ :controller_class_name,
9
+ :controller_singular_name,
10
+ :controller_plural_name,
11
+ :parent_folder_for_require
12
+ alias_method :controller_file_name, :controller_singular_name
13
+ alias_method :controller_table_name, :controller_plural_name
14
+
15
+ def initialize(runtime_args, runtime_options = {})
16
+ super
17
+
18
+ # Take controller name from the next argument.
19
+ @controller_name = runtime_args.shift
20
+
21
+ base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name)
22
+ @controller_class_name_without_nesting, @controller_singular_name, @controller_plural_name = inflect_names(base_name)
23
+
24
+ if @controller_class_nesting.empty?
25
+ @controller_class_name = @controller_class_name_without_nesting
26
+ else
27
+ @controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}"
28
+ end
29
+ end
30
+
31
+ def manifest
32
+ record do |m|
33
+ # Check for class naming collisions.
34
+ m.class_collisions controller_class_path, "#{controller_class_name}Controller",
35
+ "#{controller_class_name}ControllerTest"
36
+
37
+ # Controller, helper, views, and test directories.
38
+ m.directory File.join('app/controllers', controller_class_path)
39
+ m.directory File.join('test/functional', controller_class_path)
40
+
41
+ m.template 'controller.rb',
42
+ File.join('app/controllers',
43
+ controller_class_path,
44
+ "#{controller_file_name}_controller.rb")
45
+
46
+ # For some reason this doesn't take effect if done in initialize()
47
+ @parent_folder_for_require = @controller_class_path.join('/').gsub(%r%app/controllers/?%, '')
48
+ @parent_folder_for_require += @parent_folder_for_require.blank? ? '' : '/'
49
+
50
+ m.template 'functional_test.rb',
51
+ File.join('test/functional',
52
+ controller_class_path,
53
+ "#{controller_file_name}_controller_test.rb")
54
+
55
+ end
56
+ end
57
+
58
+ protected
59
+ # Override with your own usage banner.
60
+ def banner
61
+ "Usage: #{$0} gruffy ControllerName"
62
+ end
63
+ end
@@ -0,0 +1,32 @@
1
+ class <%= controller_class_name %>Controller < ApplicationController
2
+
3
+ # To make caching easier, add a line like this to config/routes.rb:
4
+ # map.graph "graph/:action/:id/image.png", :controller => "graph"
5
+ #
6
+ # Then reference it with the named route:
7
+ # image_tag graph_url(:action => 'show', :id => 42)
8
+
9
+ def show
10
+ g = Gruffy::Line.new
11
+ # Uncomment to use your own theme or font
12
+ # See http://colourlovers.com or http://www.firewheeldesign.com/widgets/ for color ideas
13
+ # g.theme = {
14
+ # :colors => ['#663366', '#cccc99', '#cc6633', '#cc9966', '#99cc99'],
15
+ # :marker_color => 'white',
16
+ # :background_colors => ['black', '#333333']
17
+ # }
18
+ # g.font = File.expand_path('artwork/fonts/VeraBd.ttf', RAILS_ROOT)
19
+
20
+ g.title = "Gruffy-o-Rama"
21
+
22
+ g.data("Apples", [1, 2, 3, 4, 4, 3])
23
+ g.data("Oranges", [4, 8, 7, 9, 8, 9])
24
+ g.data("Watermelon", [2, 3, 1, 5, 6, 8])
25
+ g.data("Peaches", [9, 9, 10, 8, 7, 9])
26
+
27
+ g.labels = {0 => '2004', 2 => '2005', 4 => '2006'}
28
+
29
+ send_data(g.to_blob, :disposition => 'inline', :type => 'image/png', :filename => "gruffy.png")
30
+ end
31
+
32
+ end
@@ -0,0 +1,24 @@
1
+ require File.dirname(__FILE__) + '<%= '/..' * controller_class_name.split("::").length %>/test_helper'
2
+ require '<%= parent_folder_for_require %><%= controller_file_name %>_controller'
3
+
4
+ # Re-raise errors caught by the controller.
5
+ class <%= controller_class_name %>Controller; def rescue_action(e) raise e end; end
6
+
7
+ class <%= controller_class_name %>ControllerTest < Test::Unit::TestCase
8
+
9
+ #fixtures :data
10
+
11
+ def setup
12
+ @controller = <%= controller_class_name %>Controller.new
13
+ @request = ActionController::TestRequest.new
14
+ @response = ActionController::TestResponse.new
15
+ end
16
+
17
+ # TODO Replace this with your actual tests
18
+ def test_show
19
+ get :show
20
+ assert_response :success
21
+ assert_equal 'image/png', @response.headers['Content-Type']
22
+ end
23
+
24
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gruffy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wuttikrai Limsakul
@@ -64,7 +64,38 @@ email:
64
64
  executables: []
65
65
  extensions: []
66
66
  extra_rdoc_files: []
67
- files: []
67
+ files:
68
+ - lib/gruffy.rb
69
+ - lib/gruffy/accumulator_bar.rb
70
+ - lib/gruffy/area.rb
71
+ - lib/gruffy/bar.rb
72
+ - lib/gruffy/bar_conversion.rb
73
+ - lib/gruffy/base.rb
74
+ - lib/gruffy/bezier.rb
75
+ - lib/gruffy/bullet.rb
76
+ - lib/gruffy/deprecated.rb
77
+ - lib/gruffy/dot.rb
78
+ - lib/gruffy/line.rb
79
+ - lib/gruffy/mini/bar.rb
80
+ - lib/gruffy/mini/legend.rb
81
+ - lib/gruffy/mini/pie.rb
82
+ - lib/gruffy/mini/side_bar.rb
83
+ - lib/gruffy/net.rb
84
+ - lib/gruffy/photo_bar.rb
85
+ - lib/gruffy/pie.rb
86
+ - lib/gruffy/scatter.rb
87
+ - lib/gruffy/scene.rb
88
+ - lib/gruffy/side_bar.rb
89
+ - lib/gruffy/side_stacked_bar.rb
90
+ - lib/gruffy/spider.rb
91
+ - lib/gruffy/stacked_area.rb
92
+ - lib/gruffy/stacked_bar.rb
93
+ - lib/gruffy/stacked_mixin.rb
94
+ - lib/gruffy/themes.rb
95
+ - lib/gruffy/version.rb
96
+ - rails_generators/gruffy/gruffy_generator.rb
97
+ - rails_generators/gruffy/templates/controller.rb
98
+ - rails_generators/gruffy/templates/functional_test.rb
68
99
  homepage: https://rubygems.org/gems/gruffy
69
100
  licenses:
70
101
  - MIT