From ed7d8e7517b14b52dc5089dc234928829a80d764 Mon Sep 17 00:00:00 2001 From: claudiob Date: Tue, 1 Mar 2016 14:10:15 -0800 Subject: [PATCH] Don't truncate integer axis values Closes #48 --- CHANGELOG.md | 4 ++++ examples/squid/two_axis.rb | 8 ++++---- lib/squid/axis.rb | 11 ++++++++++- lib/squid/version.rb | 2 +- spec/axis_spec.rb | 15 +++++++++++---- 5 files changed, 30 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc0f693..18f7cdc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/examples/squid/two_axis.rb b/examples/squid/two_axis.rb index d94a81d..fe2e66c 100644 --- a/examples/squid/two_axis.rb +++ b/examples/squid/two_axis.rb @@ -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 diff --git a/lib/squid/axis.rb b/lib/squid/axis.rb index 45a5a5d..8599420 100644 --- a/lib/squid/axis.rb +++ b/lib/squid/axis.rb @@ -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 diff --git a/lib/squid/version.rb b/lib/squid/version.rb index ecdf13d..719fdc0 100644 --- a/lib/squid/version.rb +++ b/lib/squid/version.rb @@ -1,3 +1,3 @@ module Squid - VERSION = '1.2.0' + VERSION = '1.3.0' end diff --git a/spec/axis_spec.rb b/spec/axis_spec.rb index c727863..754b22f 100644 --- a/spec/axis_spec.rb +++ b/spec/axis_spec.rb @@ -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]] } @@ -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 @@ -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