Skip to content

Commit

Permalink
Merge pull request #49 from Fullscreen/fix-48
Browse files Browse the repository at this point in the history
Don't truncate integer axis values
  • Loading branch information
claudiofullscreen committed Mar 1, 2016
2 parents e8e9631 + ed7d8e7 commit 8bea268
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ For more information about changelogs, check
[Keep a Changelog](http://keepachangelog.com) and
[Vandamme](http://tech-angels.github.io/vandamme).

## 1.3.0 - 2016.03.01

* [ENHANCEMENT] When axis values are integer, ensure they are not truncated (see issue #48)

## 1.2.0 - 2016.01.24

* [ENHANCEMENT] Do not titleize the chart labels (see issue #39)
Expand Down
8 changes: 4 additions & 4 deletions examples/squid/two_axis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
# You can use ..
filename = File.basename(__FILE__).gsub('.rb', '.pdf')
Prawn::ManualBuilder::Example.generate(filename) do
data = {'Earnings' => {2013 => 104_323, 2014 => 27_234, 2015 => 34_123},
'Views' => {2013 => 182, 2014 => 46, 2015 => 88},
'Uniques' => {2013 => 104, 2014 => 27, 2015 => 14}}
chart data, type: :two_axis, height: 150, labels: [true, false, false], formats: [:currency], line_width: 0.5, colors: ['6f3d79', '7d807f']
data = {'Earnings' => {'2011' => 52, '2012' => 71, '2013' => 93, '2014' => 74},
'Views' => {'2011' => 0, '2012' => 4, '2013' => 3, '2014' => 5},
'Uniques' => {'2011' => 1, '2012' => 3, '2013' => 0, '2014' => 1}}
chart data, type: :two_axis, height: 150, labels: [true, false, false], formats: [:currency, :integer], line_width: 0.5, colors: ['6f3d79', '7d807f']
end
11 changes: 10 additions & 1 deletion lib/squid/axis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,16 @@ def min

def max
if @data.any? && values.last && values.last.any?
[values.last.max, @steps].max
closest_step_to values.last.max
end
end

def closest_step_to(value)
if @format == :integer
p "VALUE: #{((value - min) / @steps + 1) * @steps + min}"
((value - min) / @steps + 1) * @steps + min
else
[value, @steps].max
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/squid/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Squid
VERSION = '1.2.0'
VERSION = '1.3.0'
end
15 changes: 11 additions & 4 deletions spec/axis_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
let(:options) { {steps: steps, stack: stack?, format: format} }
let(:steps) { 4 }
let(:stack?) { false }
let(:format) { :integer }
let(:format) { nil }
let(:block) { nil }
let(:series) { [[-1.0, 9.9, 3.0], [nil, 2.0, -50.0]] }

Expand Down Expand Up @@ -44,8 +44,8 @@

describe 'given :integer format' do
let(:format) { :integer }
it 'returns the labels as integers' do
expect(labels).to eq %w(9 -5 -20 -35 -50)
it 'returns the labels as integers, rounded to the closest step' do
expect(labels).to eq %w(14 -2 -18 -34 -50)
end
end

Expand Down Expand Up @@ -86,10 +86,17 @@
it { expect(width).to be_zero }
end

describe 'given no block, returns the maximum value of the block' do
describe 'given non-integer values, returns the maximum value of the block' do
subject(:axis) { Squid::Axis.new series, options, &block }
let(:block) { -> (value) { value.to_i } }
it { expect(width).to eq 9 }
end

describe 'given integer values, returns the maximum value, rounded to the closest step' do
subject(:axis) { Squid::Axis.new series, options, &block }
let(:format) { :integer }
let(:block) { -> (value) { value.to_i } }
it { expect(width).to eq 14 }
end
end
end

0 comments on commit 8bea268

Please sign in to comment.