Class: Charts::CountChart
- Inherits:
-
Chart
- Object
- Chart
- Charts::CountChart
show all
- Defined in:
- lib/charts/count_chart/count_chart.rb
Instance Attribute Summary
Attributes inherited from Chart
#data, #options, #prepared_data, #renderer
Instance Method Summary
collapse
Methods inherited from Chart
#create_options_methods, #draw_background, #draw_title, #initialize, #initialize_instance_variables, #post_draw, #pre_draw, #render, #validate_array_and_count
Constructor Details
This class inherits a constructor from Charts::Chart
Instance Method Details
#default_options ⇒ Object
2
3
4
5
6
7
8
9
|
# File 'lib/charts/count_chart/count_chart.rb', line 2
def default_options
super.merge(
columns: 10,
inner_margin: 2,
item_width: 20,
item_height: 20
)
end
|
#draw ⇒ Object
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/charts/count_chart/count_chart.rb', line 25
def draw
prepared_data.each_with_index do |row, row_count|
row.each_with_index do |color, column_count|
x = offset_x(column_count) + inner_margin + outer_margin
y = offset_y(row_count) + inner_margin + outer_margin
draw_item(x, y, color)
end
end
draw_labels
end
|
#draw_item(_x, _y, _color) ⇒ Object
68
69
70
|
# File 'lib/charts/count_chart/count_chart.rb', line 68
def draw_item(_x, _y, _color)
raise NotImplementedError
end
|
#draw_label_text(x, y, label) ⇒ Object
46
47
48
49
50
|
# File 'lib/charts/count_chart/count_chart.rb', line 46
def draw_label_text(x, y, label)
x = x + item_width + inner_margin
y = y + item_height / 2 + 2 * renderer.font_size / 5
renderer.text(label, x, y, class: 'label_text')
end
|
#draw_labels ⇒ Object
36
37
38
39
40
41
42
43
44
|
# File 'lib/charts/count_chart/count_chart.rb', line 36
def draw_labels
return if labels.empty?
data.each_with_index do |data, index|
x = inner_margin + outer_margin
y = offset_y(prepared_data.count + (index + 1)) + inner_margin + outer_margin
draw_item(x, y, colors[index])
draw_label_text(x, y, labels[index]) end
end
|
#height ⇒ Object
76
77
78
|
# File 'lib/charts/count_chart/count_chart.rb', line 76
def height
(prepared_data.count + label_count) * outer_item_height + (2 * outer_margin)
end
|
#label_count ⇒ Object
80
81
82
|
# File 'lib/charts/count_chart/count_chart.rb', line 80
def label_count
labels.any? ? (labels.count + 1) : 0
end
|
#offset_x(column_count) ⇒ Object
52
53
54
|
# File 'lib/charts/count_chart/count_chart.rb', line 52
def offset_x(column_count)
column_count * outer_item_width
end
|
#offset_y(row_count) ⇒ Object
56
57
58
|
# File 'lib/charts/count_chart/count_chart.rb', line 56
def offset_y(row_count)
row_count * outer_item_height
end
|
#outer_item_height ⇒ Object
64
65
66
|
# File 'lib/charts/count_chart/count_chart.rb', line 64
def outer_item_height
item_height + 2 * inner_margin
end
|
#outer_item_width ⇒ Object
60
61
62
|
# File 'lib/charts/count_chart/count_chart.rb', line 60
def outer_item_width
item_width + 2 * inner_margin
end
|
#prepare_data ⇒ Object
17
18
19
20
21
22
23
|
# File 'lib/charts/count_chart/count_chart.rb', line 17
def prepare_data
prepared_data = []
data.each_with_index do |value, index|
value.to_i.times { prepared_data << colors[index].to_s }
end
prepared_data.each_slice(columns).to_a
end
|
#validate_arguments(data, options) ⇒ Object
11
12
13
14
15
|
# File 'lib/charts/count_chart/count_chart.rb', line 11
def validate_arguments(data, options)
super(data, options)
raise ArgumentError if options[:inner_margin] and !options[:inner_margin].is_a?(Numeric)
raise ArgumentError unless data.all? { |x| Integer(x) }
end
|
#width ⇒ Object
72
73
74
|
# File 'lib/charts/count_chart/count_chart.rb', line 72
def width
prepared_data.first.count * outer_item_width + (2 * outer_margin) end
|