Skip to content

Commit f20ffd3

Browse files
committed
Add :col_max_length setting
Allows for a maximum column width to be specified for charts of type `:column` or `:stack`.
1 parent bd2e988 commit f20ffd3

File tree

7 files changed

+46
-4
lines changed

7 files changed

+46
-4
lines changed

examples/squid/col_max_width.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# By default, <code>chart</code> maximizes the width of columns.
2+
#
3+
# You can use the <code>:col_max_width</code> option to limit the maximum width of columns in the chart (measured in points; 72 points per inch).
4+
#
5+
filename = File.basename(__FILE__).gsub('.rb', '.pdf')
6+
Prawn::ManualBuilder::Example.generate(filename) do
7+
data = {views: {2013 => 182, 2014 => 46, 2015 => 134}}
8+
chart data, col_max_width: 20
9+
end

examples/squid/squid.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
p.section 'Basics' do |s|
1818
s.example 'basic'
1919
s.example 'legend'
20+
s.example 'col_max_width'
2021
end
2122

2223
p.section 'Chart types' do |s|

lib/squid/configuration.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def self.array(proc = nil)
5959
labels: {as: array(boolean)},
6060
legend: {as: boolean, default: 'true'},
6161
line_widths: {as: array(float)},
62+
col_max_width: {as: integer, default: '0'},
6263
steps: {as: integer, default: '4'},
6364
ticks: {as: boolean, default: 'true'},
6465
type: {as: symbol, default: 'column'},

lib/squid/graph.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ module Squid
1313
class Graph
1414
extend Settings
1515
has_settings :baseline, :border, :chart, :colors, :every, :formats, :height
16-
has_settings :legend, :line_widths, :steps, :ticks, :type, :labels
16+
has_settings :legend, :line_widths, :col_max_width, :steps, :ticks, :type, :labels
1717

1818
def initialize(document, data = {}, settings = {})
1919
@data, @settings = data, settings
20-
@plot = Plotter.new document, bottom: bottom
20+
@plot = Plotter.new document, bottom: bottom, col_max_width: settings[:col_max_width]
2121
@plot.paddings = {left: left.width, right: right.width} if @data.any?
2222
end
2323

lib/squid/plotter.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ module Squid
77
class Plotter
88
attr_accessor :paddings
99
# @param [Prawn::Document] a PDF document to wrap in a Plotter instance.
10-
def initialize(pdf, bottom:)
10+
def initialize(pdf, bottom:, col_max_width: 0)
1111
@pdf = pdf
1212
@bottom = bottom
13+
@col_max_width = col_max_width || 0
1314
end
1415

1516
# Draws a bounding box of the given height, rendering the block inside it.
@@ -96,14 +97,23 @@ def lines(series, options = {})
9697
def stacks(series, options = {})
9798
items(series, options.merge(fill: true)) do |point, w, i, padding|
9899
x, y = point.index*w + padding + left, point.y + @bottom
99-
@pdf.fill_rectangle [x, y], w - 2*padding, point.height
100+
w -= 2 * padding
101+
if @col_max_width > 0 && @col_max_width < w
102+
x += (w - @col_max_width) / 2
103+
w = @col_max_width
104+
end
105+
@pdf.fill_rectangle [x, y], w, point.height
100106
end
101107
end
102108

103109
def columns(series, options = {})
104110
items(series, options.merge(fill: true, count: series.size)) do |point, w, i, padding|
105111
item_w = (w - 2 * padding)/ series.size
106112
x, y = point.index*w + padding + left + i*item_w, point.y + @bottom
113+
if @col_max_width > 0 && @col_max_width < item_w
114+
x += (item_w - @col_max_width) / 2
115+
item_w = @col_max_width
116+
end
107117
@pdf.fill_rectangle [x, y], item_w, point.height
108118
end
109119
end

spec/configuration_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
it_behaves_like 'a configurable setting', method: 'labels', env: 'SQUID_LABELS', default: [], sample_value: sample_true
2929
it_behaves_like 'a configurable setting', method: 'legend', env: 'SQUID_LEGEND', default: true, sample_value: sample_false
3030
it_behaves_like 'a configurable setting', method: 'line_widths', env: 'SQUID_LINE_WIDTHS', default: [] , sample_value: '4'
31+
it_behaves_like 'a configurable setting', method: 'col_max_width', env: 'SQUID_COL_MAX_WIDTH', default: 0, sample_value: '20'
3132
it_behaves_like 'a configurable setting', method: 'steps', env: 'SQUID_STEPS', default: 4, sample_value: '0'
3233
it_behaves_like 'a configurable setting', method: 'ticks', env: 'SQUID_TICKS', default: true, sample_value: sample_false
3334
it_behaves_like 'a configurable setting', method: 'type', env: 'SQUID_TYPE', default: :column, sample_value: 'line'

spec/squid_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@
5555
end
5656
end
5757

58+
context 'given the :col_max_width is set to a value greater than zero' do
59+
let(:maximum) { 20 }
60+
let(:settings) { options.merge col_max_width: maximum }
61+
62+
it 'creates the columns within the specified width' do
63+
widths = rectangles_of(chart).map{ |r| r[:width] }
64+
expect(widths[0]).to eq maximum
65+
end
66+
end
67+
5868
context 'given the series has nil values' do
5969
let(:values) { {2013 => -50, 2014 => nil, 2015 => 20} }
6070

@@ -144,6 +154,16 @@
144154
it 'includes as many stacks as the number of values' do
145155
expect(rectangles_of(chart).map{|r| r[:point].first}.uniq.size).to be 3
146156
end
157+
158+
context 'given the :col_max_width is set to a value greater than zero' do
159+
let(:maximum) { 20 }
160+
let(:settings) { options.merge type: :stack, col_max_width: maximum }
161+
162+
it 'creates the columns within the specified width' do
163+
widths = rectangles_of(chart).map{ |r| r[:width] }
164+
expect(widths[0]).to eq maximum
165+
end
166+
end
147167
end
148168

149169
context 'given multiple options are provided' do

0 commit comments

Comments
 (0)