Class: Charts::Chart
- Inherits:
-
Object
- Object
- Charts::Chart
- Defined in:
- lib/charts/chart.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#prepared_data ⇒ Object
readonly
Returns the value of attribute prepared_data.
-
#renderer ⇒ Object
readonly
Returns the value of attribute renderer.
Instance Method Summary collapse
- #create_options_methods ⇒ Object
- #default_options ⇒ Object
- #draw ⇒ Object
- #draw_background ⇒ Object
- #draw_title ⇒ Object
-
#initialize(data, opts = {}) ⇒ Chart
constructor
A new instance of Chart.
- #initialize_instance_variables ⇒ Object
- #post_draw ⇒ Object
- #pre_draw ⇒ Object
- #prepare_data ⇒ Object
- #render ⇒ Object
- #validate_arguments(data, options) ⇒ Object
- #validate_array_and_count(data, options, key) ⇒ Object
Constructor Details
#initialize(data, opts = {}) ⇒ Chart
Returns a new instance of Chart.
7 8 9 10 11 12 13 14 |
# File 'lib/charts/chart.rb', line 7 def initialize(data, opts = {}) validate_arguments(data, opts) @data = data @options = .merge opts initialize_instance_variables @prepared_data = prepare_data end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
2 3 4 |
# File 'lib/charts/chart.rb', line 2 def data @data end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
2 3 4 |
# File 'lib/charts/chart.rb', line 2 def @options end |
#prepared_data ⇒ Object (readonly)
Returns the value of attribute prepared_data.
2 3 4 |
# File 'lib/charts/chart.rb', line 2 def prepared_data @prepared_data end |
#renderer ⇒ Object (readonly)
Returns the value of attribute renderer.
2 3 4 |
# File 'lib/charts/chart.rb', line 2 def renderer @renderer end |
Instance Method Details
#create_options_methods ⇒ Object
97 98 99 100 101 |
# File 'lib/charts/chart.rb', line 97 def .each do |key, value| define_singleton_method key, proc { value } end end |
#default_options ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/charts/chart.rb', line 38 def { title: nil, type: :svg, outer_margin: 30, background_color: 'white', labels: [], colors: [ '#e41a1d', '#377eb9', '#4daf4b', '#984ea4', '#ff7f01', '#ffff34', '#a65629', '#f781c0', '#888888' ] } end |
#draw ⇒ Object
75 76 77 |
# File 'lib/charts/chart.rb', line 75 def draw raise NotImplementedError end |
#draw_background ⇒ Object
83 84 85 |
# File 'lib/charts/chart.rb', line 83 def draw_background renderer.rect 0, 0, width, height, fill: background_color, class: 'background_color' end |
#draw_title ⇒ Object
87 88 89 90 91 92 |
# File 'lib/charts/chart.rb', line 87 def draw_title return unless [:title] x = width / 2 y = outer_margin / 2 + 2 * renderer.font_size / 5 renderer.text [:title], x, y, text_anchor: 'middle', class: 'title' end |
#initialize_instance_variables ⇒ Object
94 95 |
# File 'lib/charts/chart.rb', line 94 def initialize_instance_variables end |
#post_draw ⇒ Object
79 80 81 |
# File 'lib/charts/chart.rb', line 79 def post_draw renderer.post_draw end |
#pre_draw ⇒ Object
69 70 71 72 73 |
# File 'lib/charts/chart.rb', line 69 def pre_draw @renderer = Charts::Renderer.new(self) draw_background draw_title end |
#prepare_data ⇒ Object
59 60 61 |
# File 'lib/charts/chart.rb', line 59 def prepare_data data end |
#render ⇒ Object
63 64 65 66 67 |
# File 'lib/charts/chart.rb', line 63 def render pre_draw draw post_draw end |
#validate_arguments(data, options) ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/charts/chart.rb', line 16 def validate_arguments(data, ) raise ArgumentError.new('Data missing') if data.empty? raise ArgumentError.new('Data not an array') unless data.is_a? Array raise ArgumentError.new('Options missing') unless .is_a? Hash if [:outer_margin] and ![:outer_margin].is_a?(Numeric) raise ArgumentError.new('outer_margin not a number') end validate_array_and_count(data, , :colors) validate_array_and_count(data, , :labels) end |
#validate_array_and_count(data, options, key) ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/charts/chart.rb', line 27 def validate_array_and_count(data, , key) if [key] unless [key].is_a? Array raise ArgumentError.new("#{ key } not an array") end if [key].any? and data.count > [key].count raise ArgumentError.new("not enough #{ key }") end end end |