Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/charts/bar_chart/bar_chart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def pre_draw

def draw_group_labels
return if options[:group_labels].nil? || group_labels.empty?
raise ArgumentError if group_labels.count != group_count
raise ArgumentError, 'count of group-Labels and bars does not match' if group_labels.count != group_count
group_label_style = {
text_anchor: 'middle',
writing_mode: (vertical? ? 'lr' : 'tb'),
Expand Down
40 changes: 27 additions & 13 deletions lib/charts/chart.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class Charts::Chart
attr_reader :data,
:options,
:prepared_data,
:renderer
:options,
:prepared_data,
:renderer

def initialize(data, opts = {})
validate_arguments(data, opts)
Expand All @@ -17,20 +17,34 @@ def validate_arguments(data, options)
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 options.is_a? Hash
if options[:outer_margin] and !options[:outer_margin].is_a?(Numeric)
raise ArgumentError.new('outer_margin not a number')
if options[:outer_margin] && !options[:outer_margin].is_a?(Numeric)
raise ArgumentError, 'outer_margin not a number'
end
validate_array_and_count(data, options, :colors)
validate_array_and_count(data, options, :labels)
is_an_array?(options, [:labels, :colors])
validate_colors(data, options, :colors)
validate_labels(data, options, :labels)
end

def validate_array_and_count(data, options, key)
def is_an_array?(options, keys)
keys.map do |key|
if options[key] && !options[key].is_a?(Array)
raise ArgumentError, "#{key} not an array"
end
end
end

def validate_colors(data, options, key)
if options[key]
unless options[key].is_a? Array
raise ArgumentError.new("#{ key } not an array")
if options[key].any? && data.count > options[key].count
raise ArgumentError, "number of #{key} is too small"
end
if options[key].any? and data.count > options[key].count
raise ArgumentError.new("not enough #{ key }")
end
end

def validate_labels(data, options, key)
if options[key]
if options[key].any? && (data.count != options[key].count)
raise ArgumentError, "number of #{key} does not match array"
end
end
end
Expand Down Expand Up @@ -91,7 +105,7 @@ def draw_title
renderer.text options[:title], x, y, text_anchor: 'middle', class: 'title'
end

def initialize_instance_variables
def initialize_instance_variables
end

def create_options_methods
Expand Down
6 changes: 3 additions & 3 deletions spec/charts/bar_chart/bar_chart_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@
describe 'too few group_labels' do
let(:group_labels) { ['one', 'two'] }
it 'raises an error' do
expect{ chart.render }.to raise_error(ArgumentError)
expect{ chart.render }.to raise_error(ArgumentError, 'count of group-Labels and bars does not match')
end
end
end
Expand All @@ -255,10 +255,10 @@
labels_texts = svg.css('text.label').map{ |t| t.text.tr("\n", '') }
expect(labels_texts).to eq(labels)
end
describe 'too few labels' do
describe 'count of label and data does not match' do
let(:labels) { ['Alpha', 'Beta'] }
it 'raises an error' do
expect{ chart.render }.to raise_error(ArgumentError)
expect{ chart.render }.to raise_error(ArgumentError, 'number of labels does not match array')
end
end
end
Expand Down
50 changes: 25 additions & 25 deletions spec/charts/count_chart/count_chart_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@
let(:chart) { Charts::CountChart.new [1] }
it 'has a default item-colors' do
expect(chart.colors).to eq([
'#e41a1d',
'#377eb9',
'#4daf4b',
'#984ea4',
'#ff7f01',
'#ffff34',
'#a65629',
'#f781c0',
'#888888'
])
'#e41a1d',
'#377eb9',
'#4daf4b',
'#984ea4',
'#ff7f01',
'#ffff34',
'#a65629',
'#f781c0',
'#888888'
])
end
it 'has a default background-colors' do
expect(chart.background_color).to eq('white')
Expand Down Expand Up @@ -176,23 +176,23 @@
it 'creates the prepared_data for simple keys' do
chart = Charts::CountChart.new([3, 2], colors: ['x', 'o'], columns: 2)
expect(chart.prepared_data).to eq([
['x', 'x'],
['x', 'o'],
['o']
])
['x', 'x'],
['x', 'o'],
['o']
])
end
it 'creates the prepared_data for complex keys' do
chart = Charts::CountChart.new([2, 2], colors: ['#FF0000', '#00FF00'], columns: 2)
expect(chart.prepared_data).to eq([
['#FF0000', '#FF0000'],
['#00FF00', '#00FF00']
])
['#FF0000', '#FF0000'],
['#00FF00', '#00FF00']
])
end
it 'default colors get assigned when no colors are specified' do
chart = Charts::CountChart.new([1, 1, 1])
expect(chart.prepared_data).to eq([
['#e41a1d', '#377eb9', '#4daf4b']
])
['#e41a1d', '#377eb9', '#4daf4b']
])
end
end

Expand Down Expand Up @@ -251,23 +251,23 @@

describe '#height and #width with presence of labels' do
context 'three items with three labels' do
let(:data) { [3] }
let(:data) { [3, 3, 3] }
let(:labels) { ['Cars', 'Buses', 'Bikes'] }
let(:item_height) { 20 }
include_examples 'has a width and height of', 40, 120
include_examples 'has a width and height of', 40, 180
end
context 'five items with five labels' do
let(:data) { [5] }
let(:data) { [5, 5, 5, 5, 5] }
let(:labels) { ['Cars', 'Buses', 'Bikes', 'Planes', 'Ferries'] }
let(:item_height) { 20 }
include_examples 'has a width and height of', 40, 180
include_examples 'has a width and height of', 40, 380
end
context 'five items in three columns with five labels' do
let(:data) { [5] }
let(:data) { [5, 5, 5, 5, 5] }
let(:columns) { 3 }
let(:labels) { ['Cars', 'Buses', 'Bikes', 'Planes', 'Ferries'] }
let(:item_height) { 20 }
include_examples 'has a width and height of', 60, 160
include_examples 'has a width and height of', 60, 300
end
end

Expand Down