squid 1.0.0.beta2 → 1.0.0.beta3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 802e428b541dc6eaba5d9274733688abd8c47666
4
- data.tar.gz: cf142a3795699066ebde7c1ba686749a1785184f
3
+ metadata.gz: a30136182bd1690282984a45aa989c76048d2323
4
+ data.tar.gz: 4fbcc2b4a29298c6f6c5548d96e07dc211913334
5
5
  SHA512:
6
- metadata.gz: c64d9edd048a94b238f389d408e6df4b1906531214aae2e4d162323cf5e66213d9eab0c25db673eafba0e0e2703dff7917106cef87089c5f8ab5fab115139a5d
7
- data.tar.gz: b3db0636c20220d14f8ca97f4200f3d97f31b78a5c1ff3d76ec752ab1c643971d2fe6059b14e42806de108d0b1828c9bb5986008ccee48fa41f96af30f77f44d
6
+ metadata.gz: 8cd3102e934345601ef5177af3a7a85cb395a4fc262ab45cc025cb4a7011364d9157aa65ce094ff37fc4c01866824410393d559fcf09a14312ee8e01030ef741
7
+ data.tar.gz: 9470e71ffb9c6197b210b4f6f050630c3c1c330450bc011164fb878a605e674f8928cbbada38e590bc5a8da3dbaed378e9369816fc284b34ca70fdebc35cbc22
data/README.md CHANGED
@@ -40,11 +40,11 @@ Multiple options can be combined. Here is a comprehensive list.
40
40
 
41
41
  ![02-type-point](https://raw.githubusercontent.com/fullscreen/squid/master/examples/screenshots/readme_02.png "chart(data, type: :point)")
42
42
 
43
- ##### `:line_widths` changes the line width (default: `[3]`, only applies to line graphs).
43
+ ##### `:line_width` changes the line width (default: `3`, only applies to line graphs).
44
44
 
45
- ![03-type-line](https://raw.githubusercontent.com/fullscreen/squid/master/examples/screenshots/readme_03.png "chart(data, type: :line, line_widths: [10])")
45
+ ![03-type-line](https://raw.githubusercontent.com/fullscreen/squid/master/examples/screenshots/readme_03.png "chart(data, type: :line, line_width: 10)")
46
46
 
47
- ##### `:colors` changes the colors of the chart (default: `%w(2e578c 5d9648 e7a13d bc2d30 6f3d79 7d807f)`).
47
+ ##### `:color` changes the color of the chart (default: `'2e578c'`).
48
48
 
49
49
  ![04-colors](https://raw.githubusercontent.com/fullscreen/squid/master/examples/screenshots/readme_04.png "chart(data, colors: ['5d9648'])")
50
50
 
@@ -68,17 +68,17 @@ Multiple options can be combined. Here is a comprehensive list.
68
68
 
69
69
  ![09-legend](https://raw.githubusercontent.com/fullscreen/squid/master/examples/screenshots/readme_09.png "chart(data, legend: false)")
70
70
 
71
- ##### `:legend` can also set the distance of the legend from the right margin (default: `{offset: 0}`).
71
+ ##### `:legend` can also set the right and bottom margins of the legend (default: `{right: 0, bottom: 15}`).
72
72
 
73
- ![10-legend-offset](https://raw.githubusercontent.com/fullscreen/squid/master/examples/screenshots/readme_10.png "chart(data, legend: {offset: 50})")
73
+ ![10-legend-offset](https://raw.githubusercontent.com/fullscreen/squid/master/examples/screenshots/readme_10.png "chart(data, legend: {right: 50})")
74
74
 
75
- ##### `:format` changes the format of the label values. Can be `:integer` (default), `:float`, `:percentage`, `:currency`, or `:seconds`.
75
+ ##### `:format` changes the format of the axis labels. Can be `:integer` (default), `:float`, `:percentage`, `:currency`, or `:seconds`.
76
76
 
77
77
  ![11-format](https://raw.githubusercontent.com/fullscreen/squid/master/examples/screenshots/readme_11.png "chart(data, format: :percentage)")
78
78
 
79
- ##### `:labels` shows/hides the value for each point in the graph (default: `false`).
79
+ ##### `:label` shows/hides the value for each point in the graph (default: `false`).
80
80
 
81
- ![12-labels](https://raw.githubusercontent.com/fullscreen/squid/master/examples/screenshots/readme_12.png "chart(data, labels: true)")
81
+ ![12-label](https://raw.githubusercontent.com/fullscreen/squid/master/examples/screenshots/readme_12.png "chart(data, label: true)")
82
82
 
83
83
  ##### `:border` shows/hides a border around the graph (default: `false`).
84
84
 
@@ -101,12 +101,21 @@ data = {safari: {2013 => 43.2, 2014 => 46.1, 2015 => 50.7},
101
101
  chart data, labels: true, format: :percentage
102
102
  ```
103
103
 
104
- ![15-multiple-columns](https://raw.githubusercontent.com/fullscreen/squid/master/examples/screenshots/readme_15.png "chart(data, labels: true, format: :percentage)")
104
+ ![15-multiple-columns](https://raw.githubusercontent.com/fullscreen/squid/master/examples/screenshots/readme_15.png "chart(data, label: true, format: :percentage)")
105
105
 
106
106
  When plotting multiple series, the option `type: :stack` can be set to display stacked columns:
107
107
 
108
108
  ![16-multiple-stacks](https://raw.githubusercontent.com/fullscreen/squid/master/examples/screenshots/readme_16.png "chart(data, type: :stack, format: :percentage)")
109
109
 
110
+ Any value set for the `:color`, `:format`, `:line_width` and `:label` options will be applied to the first series only:
111
+
112
+ ![17-singular-options](https://raw.githubusercontent.com/fullscreen/squid/master/examples/screenshots/readme_17.png "chart data, type: :line, color: '6f3d79', format: :percentage, line_width: 0.5, label: true")
113
+
114
+ To customize each series, use the _plural_ options `:colors`, `:formats`, `:line_widths` and `:labels` instead:
115
+
116
+ ![18-plural-options](https://raw.githubusercontent.com/fullscreen/squid/master/examples/screenshots/readme_18.png "chart data, type: :line, colors: ['6f3d79', '7d807f'], formats: [:percentage, :percentage], line_widths: [0.5, 6], labels: [true, true]")
117
+
118
+
110
119
  Finally, the option `type: :two_axis` can be set to display two separate axes in your series belong to two different domains.
111
120
 
112
121
  For instance, the following code generates the graph below:
@@ -117,7 +126,7 @@ data = {views: {2013 => 182, 2014 => 46, 2015 => 88},
117
126
  chart data, type: :two_axis
118
127
  ```
119
128
 
120
- ![17-two-axis](https://raw.githubusercontent.com/fullscreen/squid/master/examples/screenshots/readme_17.png "chart(data, type: :two_axis)")
129
+ ![19-two-axis](https://raw.githubusercontent.com/fullscreen/squid/master/examples/screenshots/readme_19.png "chart(data, type: :two_axis)")
121
130
 
122
131
  How to install
123
132
  ==============
data/Rakefile CHANGED
@@ -12,6 +12,7 @@ task :readme do
12
12
  print 'Building readme... '
13
13
  require_relative 'examples/readme'
14
14
  print 'Extracting screenshots...'
15
+ # Note: images 17, 18 and 19 need to be cropped at 309 (not 280) because they have more code
15
16
  `convert -density 175 -colorspace sRGB readme.pdf -resize 50% -quality 100 -crop 744x320+0+280 examples/screenshots/readme_%02d.png`
16
17
  puts 'DONE!'
17
18
  end
@@ -1,5 +1,5 @@
1
1
  filename = File.basename(__FILE__).gsub('.rb', '.pdf')
2
2
  Prawn::ManualBuilder::Example.generate(filename) do
3
3
  data = {views: {2013 => 182, 2014 => 46, 2015 => 134}}
4
- chart data, type: :line, line_widths: [10]
4
+ chart data, type: :line, line_width: 10
5
5
  end
@@ -1,5 +1,5 @@
1
1
  filename = File.basename(__FILE__).gsub('.rb', '.pdf')
2
2
  Prawn::ManualBuilder::Example.generate(filename) do
3
3
  data = {views: {2013 => 182, 2014 => 46, 2015 => 134}}
4
- chart data, legend: {offset: 50}
4
+ chart data, legend: {right: 50, bottom: 50}
5
5
  end
@@ -1,5 +1,5 @@
1
1
  filename = File.basename(__FILE__).gsub('.rb', '.pdf')
2
2
  Prawn::ManualBuilder::Example.generate(filename) do
3
3
  data = {views: {2013 => 182, 2014 => 46, 2015 => 134}}
4
- chart data, labels: true
4
+ chart data, label: true
5
5
  end
@@ -1,5 +1,5 @@
1
1
  filename = File.basename(__FILE__).gsub('.rb', '.pdf')
2
2
  Prawn::ManualBuilder::Example.generate(filename) do
3
3
  data = {safari: {2013 => 43.2, 2014 => 46.1, 2015 => 50.7}, firefox: {2013 => 56.8, 2014 => 53.9, 2015 => 49.3}}
4
- chart data, labels: true, format: :percentage
4
+ chart data, label: true, format: :percentage
5
5
  end
@@ -0,0 +1,5 @@
1
+ filename = File.basename(__FILE__).gsub('.rb', '.pdf')
2
+ Prawn::ManualBuilder::Example.generate(filename) do
3
+ data = {safari: {2013 => 53.2, 2014 => 56.1, 2015 => 60.7}, firefox: {2013 => 46.8, 2014 => 43.9, 2015 => 39.3}}
4
+ chart data, type: :line, color: '6f3d79', format: :percentage, line_width: 0.5, label: true
5
+ end
@@ -0,0 +1,5 @@
1
+ filename = File.basename(__FILE__).gsub('.rb', '.pdf')
2
+ Prawn::ManualBuilder::Example.generate(filename) do
3
+ data = {safari: {2013 => 53.2, 2014 => 56.1, 2015 => 60.7}, firefox: {2013 => 46.8, 2014 => 43.9, 2015 => 39.3}}
4
+ chart data, type: :line, colors: ['6f3d79', '7d807f'], formats: [:percentage, :percentage], line_widths: [0.5, 6], labels: [true, true]
5
+ end
@@ -0,0 +1,5 @@
1
+ filename = File.basename(__FILE__).gsub('.rb', '.pdf')
2
+ Prawn::ManualBuilder::Example.generate(filename) do
3
+ data = {earnings: {2013 => 104_323, 2014 => 27_234, 2015 => 74_123}, views: {2013 => 182, 2014 => 46, 2015 => 88}, uniques: {2013 => 153, 2014 => 36, 2015 => 78} }
4
+ chart data, type: :two_axis
5
+ end
@@ -22,7 +22,9 @@ Prawn::ManualBuilder::Example.generate 'readme.pdf' do
22
22
  s.example '14-height'
23
23
  s.example '15-multiple-columns'
24
24
  s.example '16-multiple-stacks'
25
- s.example '17-two-axis'
25
+ s.example '17-singular-options'
26
+ s.example '18-plural-options'
27
+ s.example '19-two-axis'
26
28
  end
27
29
  end
28
30
  end
Binary file
Binary file
Binary file
@@ -4,6 +4,6 @@
4
4
  #
5
5
  filename = File.basename(__FILE__).gsub('.rb', '.pdf')
6
6
  Prawn::ManualBuilder::Example.generate(filename) do
7
- data = {views: {2013 => 182, 2014 => 46, 2015 => 802000000000000000}}
8
- chart data, baseline: false, format: :float
7
+ data = {earnings: {2013 => 182, 2014 => 46, 2015 => 8020}}
8
+ chart data, baseline: false, formats: [:currency]
9
9
  end
@@ -0,0 +1,9 @@
1
+ # By default, <code>chart</code> generates charts with default colors.
2
+ #
3
+ # You can use the <code>:color</code> option to manually set the color.
4
+ #
5
+ filename = File.basename(__FILE__).gsub('.rb', '.pdf')
6
+ Prawn::ManualBuilder::Example.generate(filename) do
7
+ data = {views: {2013 => 18.2, 2014 => 4.6, 2015 => 10.2}}
8
+ chart data, color: '6f3d79'
9
+ end
@@ -5,5 +5,5 @@ filename = File.basename(__FILE__).gsub('.rb', '.pdf')
5
5
  Prawn::ManualBuilder::Example.generate(filename) do
6
6
  data = {views: {2013 => 182, 2014 => 46, 2015 => 88},
7
7
  uniques: {2013 => 104, 2014 => -27, 2015 => 14}}
8
- chart data, labels: true
8
+ chart data, label: true, format: :percentage
9
9
  end
@@ -1,9 +1,9 @@
1
1
  # By default, <code>chart</code> does not write value labels on the chart.
2
2
  #
3
- # You can use the <code>:labels</code> option to enable this behavior.
3
+ # You can use the <code>:label</code> option to enable this behavior.
4
4
  #
5
5
  filename = File.basename(__FILE__).gsub('.rb', '.pdf')
6
6
  Prawn::ManualBuilder::Example.generate(filename) do
7
7
  data = {a_very_long_label_to_test_padding_with_labels: {2013 => 182000, 2014 => -182000, 2015 => 182000}}
8
- chart data, labels: true
8
+ chart data, label: true
9
9
  end
@@ -5,5 +5,5 @@
5
5
  filename = File.basename(__FILE__).gsub('.rb', '.pdf')
6
6
  Prawn::ManualBuilder::Example.generate(filename) do
7
7
  data = {views: {2013 => 182, 2014 => 46}, uniques: {2013 => 94, 2014 => 27}}
8
- chart data, legend: {offset: 50}
8
+ chart data, legend: {right: 50, bottom: 20}
9
9
  end
@@ -5,5 +5,5 @@
5
5
  filename = File.basename(__FILE__).gsub('.rb', '.pdf')
6
6
  Prawn::ManualBuilder::Example.generate(filename) do
7
7
  data = {views: {Safari: 45.20001, Firefox: 63.3999, Chrome: 21.4}}
8
- chart data, type: :line
8
+ chart data, type: :line, line_width: 0.25
9
9
  end
@@ -5,7 +5,7 @@ filename = File.basename(__FILE__).gsub('.rb', '.pdf')
5
5
  Prawn::ManualBuilder::Example.generate(filename) do
6
6
  data = {views: {2013 => 182, 2014 => -46, 2015 => 88},
7
7
  uniques: {2013 => 104, 2014 => 27, 2015 => 14}}
8
- chart data, type: :line, labels: true
8
+ chart data, type: :line, labels: [false, true], formats: %i(percentage percentage), line_widths: [0.5]
9
9
  end
10
10
 
11
11
 
@@ -5,5 +5,5 @@ filename = File.basename(__FILE__).gsub('.rb', '.pdf')
5
5
  Prawn::ManualBuilder::Example.generate(filename) do
6
6
  data = {views: {2013 => 182, 2014 => 46, 2015 => 88},
7
7
  uniques: {2013 => -104, 2014 => 27, 2015 => 14}}
8
- chart data, type: :point, labels: true
8
+ chart data, type: :point, labels: [false, true], formats: []
9
9
  end
@@ -18,14 +18,15 @@ Prawn::ManualBuilder::Example.generate 'squid.pdf' do
18
18
  s.example 'basic'
19
19
  s.example 'legend'
20
20
  end
21
-
21
+
22
22
  p.section 'Chart types' do |s|
23
23
  s.example 'point'
24
24
  s.example 'line'
25
25
  end
26
-
26
+
27
27
  p.section 'Styling' do |s|
28
28
  s.example 'height'
29
+ s.example 'color'
29
30
  s.example 'baseline'
30
31
  s.example 'ticks'
31
32
  s.example 'every'
@@ -36,7 +37,7 @@ Prawn::ManualBuilder::Example.generate 'squid.pdf' do
36
37
  s.example 'line_width'
37
38
  s.example 'legend_offset'
38
39
  end
39
-
40
+
40
41
  p.section 'Multiple series' do |s|
41
42
  s.example 'columns'
42
43
  s.example 'lines'
@@ -6,5 +6,5 @@ Prawn::ManualBuilder::Example.generate(filename) do
6
6
  data = {views: {2013 => 196, 2014 => 66, 2015 => -282},
7
7
  hits: {2013 => 100, 2014 => -12, 2015 => 82},
8
8
  uniques: {2013 => -114, 2014 => 47, 2015 => -14}}
9
- chart data, type: :stack, labels: true
9
+ chart data, type: :stack, labels: [true, false, true], formats: [:currency, :float, :percentage]
10
10
  end
@@ -3,8 +3,8 @@
3
3
  # You can use ..
4
4
  filename = File.basename(__FILE__).gsub('.rb', '.pdf')
5
5
  Prawn::ManualBuilder::Example.generate(filename) do
6
-
7
- data = {views: {2013 => 182, 2014 => 46, 2015 => 88},
8
- earnings: {2013 => 104_323, 2014 => 27_234, 2015 => 14_123}}
9
- chart data, type: :two_axis, border: true
6
+ data = {earnings: {2013 => 104_323, 2014 => 27_234, 2015 => 34_123},
7
+ views: {2013 => 182, 2014 => 46, 2015 => 88},
8
+ uniques: {2013 => 104, 2014 => 27, 2015 => 14}}
9
+ chart data, type: :two_axis, height: 150, labels: [true, false, false], formats: [:currency], line_width: 0.5, colors: ['6f3d79', '7d807f']
10
10
  end
@@ -28,12 +28,10 @@ module Squid
28
28
  # ENV['SQUID_GRIDLINES'] = '4'
29
29
  #
30
30
  class Configuration < OpenStruct
31
- COLORS = '2e578c 5d9648 e7a13d bc2d30 6f3d79 7d807f'
32
-
33
31
  def self.boolean
34
32
  -> (value) { %w(1 t T true TRUE).include? value }
35
33
  end
36
-
34
+
37
35
  def self.integer
38
36
  -> (value) { value.to_i }
39
37
  end
@@ -45,25 +43,25 @@ module Squid
45
43
  def self.float
46
44
  -> (value) { value.to_f }
47
45
  end
48
-
49
- def self.array
50
- -> (value) { value.split }
46
+
47
+ def self.array(proc = nil)
48
+ -> (values) { values.split.map{|value| proc ? proc.call(value) : value} }
51
49
  end
52
50
 
53
51
  ATTRIBUTES = {
54
- baseline: {default: 'true', as: boolean},
55
- border: {default: 'false', as: boolean},
56
- chart: {default: 'true', as: boolean},
57
- colors: {default: COLORS, as: array},
58
- every: {default: '1', as: integer},
59
- format: {default: 'integer', as: symbol},
60
- height: {default: '250', as: float},
61
- labels: {default: 'false', as: boolean},
62
- legend: {default: 'true', as: boolean},
63
- line_widths: {default: '3', as: integer},
64
- steps: {default: '4', as: integer},
65
- ticks: {default: 'true', as: boolean},
66
- type: {default: 'column', as: symbol},
52
+ baseline: {as: boolean, default: 'true'},
53
+ border: {as: boolean, default: 'false'},
54
+ chart: {as: boolean, default: 'true'},
55
+ colors: {as: array},
56
+ every: {as: integer, default: '1'},
57
+ formats: {as: array(symbol)},
58
+ height: {as: float, default: '250'},
59
+ labels: {as: array(boolean)},
60
+ legend: {as: boolean, default: 'true'},
61
+ line_widths: {as: array(float)},
62
+ steps: {as: integer, default: '4'},
63
+ ticks: {as: boolean, default: 'true'},
64
+ type: {as: symbol, default: 'column'},
67
65
  }
68
66
 
69
67
  attr_accessor *ATTRIBUTES.keys
@@ -72,7 +70,7 @@ module Squid
72
70
  def initialize
73
71
  ATTRIBUTES.each do |key, options|
74
72
  var = "squid_#{key}".upcase
75
- value = ENV.fetch var, options[:default]
73
+ value = ENV.fetch var, options.fetch(:default, '')
76
74
  public_send "#{key}=", options[:as].call(value)
77
75
  end
78
76
  end
data/lib/squid/format.rb CHANGED
@@ -10,13 +10,19 @@ module Squid
10
10
  when :percentage then number_to_percentage value, precision: 1
11
11
  when :currency then number_to_currency value
12
12
  when :seconds then number_to_minutes_and_seconds value
13
- when :float then number_to_delimited value
13
+ when :float then number_to_float value
14
14
  else number_to_delimited value.to_i
15
15
  end.to_s
16
16
  end
17
17
 
18
18
  def number_to_minutes_and_seconds(value)
19
- "#{value.round / 60}:#{(value.round % 60).to_s.rjust 2, '0'}"
19
+ signum = '-' if value < 0
20
+ "#{signum}#{value.abs.round/60}:#{(value.abs.round%60).to_s.rjust 2, '0'}"
21
+ end
22
+
23
+ def number_to_float(value)
24
+ float = number_to_rounded value, significant: true, precision: 2
25
+ number_to_delimited float
20
26
  end
21
27
  end
22
28
  end
data/lib/squid/graph.rb CHANGED
@@ -12,7 +12,7 @@ module Squid
12
12
  # @private
13
13
  class Graph
14
14
  extend Settings
15
- has_settings :baseline, :border, :chart, :colors, :every, :format, :height
15
+ has_settings :baseline, :border, :chart, :colors, :every, :formats, :height
16
16
  has_settings :legend, :line_widths, :steps, :ticks, :type, :labels
17
17
 
18
18
  def initialize(document, data = {}, settings = {})
@@ -37,8 +37,8 @@ module Squid
37
37
 
38
38
  def draw_legend
39
39
  labels = @data.keys.reverse.map{|key| key.to_s.titleize}
40
- offset = legend.is_a?(Hash) ? legend.fetch(:offset, 0) : 0
41
- @plot.legend labels, offset: offset, colors: colors, height: legend_height
40
+ right = legend.is_a?(Hash) ? legend.fetch(:right, 0) : 0
41
+ @plot.legend labels, right: right, colors: colors, height: legend_height
42
42
  end
43
43
 
44
44
  def draw_gridlines
@@ -60,14 +60,17 @@ module Squid
60
60
  end
61
61
 
62
62
  def draw_charts
63
- draw_chart right, type: :column, colors: colors[1..-1]
64
- draw_chart left, colors: colors
63
+ draw_chart right, second_axis: true
64
+ draw_chart left
65
65
  end
66
66
 
67
- def draw_chart(axis, options = {})
68
- args = {minmax: axis.minmax, height: grid_height, stack: stack?, labels: labels, format: format}
67
+ def draw_chart(axis, second_axis: false)
68
+ args = {minmax: axis.minmax, height: grid_height, stack: stack?}
69
+ args[:labels] = items_of labels, skip_first_if: second_axis
70
+ args[:formats] = items_of formats, skip_first_if: second_axis
69
71
  points = Point.for axis.data, args
70
- case options.delete(:type) {type}
72
+ options = {colors: colors, starting_at: (second_axis ? 1: 0)}
73
+ case (second_axis ? :column : type)
71
74
  when :point then @plot.points points, options
72
75
  when :line, :two_axis then @plot.lines points, options.merge(line_widths: line_widths)
73
76
  when :column then @plot.columns points, options
@@ -75,19 +78,26 @@ module Squid
75
78
  end
76
79
  end
77
80
 
81
+ def items_of(array, skip_first_if:)
82
+ if skip_first_if
83
+ array.empty? ? [] : array[1..-1]
84
+ else
85
+ array
86
+ end
87
+ end
88
+
78
89
  def left
79
90
  @left ||= axis first: 0, last: (two_axis? ? 1 : @data.size)
80
91
  end
81
92
 
82
93
  def right
83
- @right ||= axis first: 1, last: (two_axis? ? 1 : 0)
94
+ @right ||= axis first: 1, last: (two_axis? ? @data.size : 0)
84
95
  end
85
96
 
86
97
  def axis(first:, last:)
87
98
  series = @data.values[first, last].map(&:values)
88
- Axis.new series, steps: steps, stack: stack?, format: format do |label|
89
- @plot.width_of label
90
- end
99
+ options = {steps: steps, stack: stack?, format: formats[first]}
100
+ Axis.new(series, options) {|label| @plot.width_of label}
91
101
  end
92
102
 
93
103
  def bottom
@@ -95,11 +105,15 @@ module Squid
95
105
  end
96
106
 
97
107
  def legend_height
98
- 15
108
+ legend ? 15 : 0
109
+ end
110
+
111
+ def legend_bottom
112
+ legend.is_a?(Hash) ? legend.fetch(:bottom, 15) : 15
99
113
  end
100
114
 
101
115
  def grid_height
102
- height - bottom - legend_height * (legend ? 2 : 1)
116
+ height - bottom - legend_height - legend_bottom
103
117
  end
104
118
 
105
119
  def stack?
data/lib/squid/plotter.rb CHANGED
@@ -22,18 +22,25 @@ module Squid
22
22
 
23
23
  # Draws the graph legend with the given labels.
24
24
  # @param [Array<LegendItem>] The labels to write as part of the legend.
25
- def legend(labels, height:, offset: 0, colors: [])
25
+ def legend(labels, height:, right: 0, colors: [])
26
26
  left = @pdf.bounds.width/2
27
27
  box(x: left, y: @pdf.bounds.top, w: left, h: height) do
28
- x = @pdf.bounds.right - offset
28
+ x = @pdf.bounds.right - right
29
29
  options = {size: 7, height: @pdf.bounds.height, valign: :center}
30
30
  labels.each.with_index do |label, i|
31
- color = Array.wrap(colors[labels.size - 1 - i]).first
31
+ index = labels.size - 1 - i
32
+ series_color = colors.fetch index, series_colors(index)
33
+ color = Array.wrap(series_color).first
32
34
  x = legend_item label, x, color, options
33
35
  end
34
36
  end
35
37
  end
36
38
 
39
+ def series_colors(index)
40
+ default_colors = %w(2e578c 5d9648 e7a13d bc2d30 6f3d79 7d807f)
41
+ default_colors.fetch(index % default_colors.size)
42
+ end
43
+
37
44
  # Draws a horizontal line.
38
45
  def horizontal_line(y, options = {})
39
46
  with options do
@@ -66,34 +73,35 @@ module Squid
66
73
  end
67
74
  end
68
75
 
69
- def points(series, colors: [])
70
- items(series, colors: colors) do |point, w, i, padding|
76
+ def points(series, options = {})
77
+ items(series, options) do |point, w, i, padding|
71
78
  x, y = (point.index + 0.5)*w + left, point.y + @bottom
72
79
  @pdf.fill_circle [x, y], 5
73
80
  end
74
81
  end
75
82
 
76
- def lines(series, colors: [], line_widths: [])
83
+ def lines(series, options = {})
77
84
  x, y = nil, nil
78
- items(series, colors: colors) do |point, w, i, padding|
85
+ line_widths = options.delete(:line_widths) { [] }
86
+ items(series, options) do |point, w, i, padding|
79
87
  prev_x, prev_y = x, y
80
88
  x, y = (point.index + 0.5)*w + left, point.y + @bottom
81
- line_width = Array.wrap(line_widths).fetch(i, 1)
89
+ line_width = line_widths.fetch i, 3
82
90
  with line_width: line_width, cap_style: :round do
83
91
  @pdf.line [prev_x, prev_y], [x,y] unless point.index.zero? || prev_y.nil? || prev_x > x
84
92
  end
85
93
  end
86
94
  end
87
95
 
88
- def stacks(series, colors: [])
89
- items(series, colors: colors, fill: true) do |point, w, i, padding|
96
+ def stacks(series, options = {})
97
+ items(series, options.merge(fill: true)) do |point, w, i, padding|
90
98
  x, y = point.index*w + padding + left, point.y + @bottom
91
99
  @pdf.fill_rectangle [x, y], w - 2*padding, point.height
92
100
  end
93
101
  end
94
102
 
95
- def columns(series, colors: [])
96
- items(series, colors: colors, fill: true, count: series.size) do |point, w, i, padding|
103
+ def columns(series, options = {})
104
+ items(series, options.merge(fill: true, count: series.size)) do |point, w, i, padding|
97
105
  item_w = (w - 2 * padding)/ series.size
98
106
  x, y = point.index*w + padding + left + i*item_w, point.y + @bottom
99
107
  @pdf.fill_rectangle [x, y], item_w, point.height
@@ -127,13 +135,15 @@ module Squid
127
135
  options
128
136
  end
129
137
 
130
- def items(series, colors: [], fill: false, count: 1, &block)
138
+ def items(series, colors: [], fill: false, count: 1, starting_at: 0, &block)
131
139
  series.reverse_each.with_index do |points, reverse_index|
132
140
  index = series.size - reverse_index - 1
141
+ color_index = index + starting_at
133
142
  w = width / points.size.to_f
134
- series_colors = Array.wrap(colors[index]).cycle
143
+ series_color = colors.fetch color_index, series_colors(color_index)
144
+ item_color = Array.wrap(series_color).cycle
135
145
  points.select(&:y).each do |point|
136
- item point, series_colors.next, w, fill, index, count, &block
146
+ item point, item_color.next, w, fill, index, count, &block
137
147
  end
138
148
  end
139
149
  end
data/lib/squid/point.rb CHANGED
@@ -4,17 +4,17 @@ module Squid
4
4
  class Point
5
5
  extend Format
6
6
 
7
- def self.for(series, minmax:, height:, labels:, stack:, format:)
7
+ def self.for(series, minmax:, height:, labels:, stack:, formats:)
8
8
  @min = Hash.new 0
9
9
  @max = Hash.new 0
10
10
  min, max = minmax
11
11
  offset = -> (value) { value * height.to_f / (max-min) }
12
- series.map do |values|
12
+ series.map.with_index do |values, series_i|
13
13
  values.map.with_index do |value, i|
14
14
  h = y_for value, index: i, stack: false, &offset if value
15
15
  y = y_for value, index: i, stack: stack, &offset if value
16
16
  y = y - offset.call([min, 0].min) if value
17
- label = format_for(value, format) if labels
17
+ label = format_for value, formats[series_i] if labels[series_i]
18
18
  new y: y, height: h, index: i, label: label, negative: value.to_f < 0
19
19
  end
20
20
  end
@@ -3,12 +3,25 @@ require 'squid/config'
3
3
  module Squid
4
4
  # @private
5
5
  module Settings
6
+ # For each key, create an attribute reader with a settings value.
7
+ # First, check if an option with the key exists.
8
+ # For example: {formats: [:currency]} ->> [:currency]
9
+ # Then, check is an option with the singular version of the key exists.
10
+ # For example: {format: :currency} ->> [:currency]
11
+ # Finally, check whether the key has a value in Squid configuration.
12
+ # For example: config.formats = [:currency] ->> [:currency]
6
13
  def has_settings(*keys)
7
14
  keys.each do |key|
8
- define_method key do
9
- @settings.fetch key, Squid.configuration.public_send(key)
15
+ define_method(key) do
16
+ singular_key = key.to_s.singularize.to_sym
17
+ if @settings.key? key
18
+ @settings[key]
19
+ elsif @settings.key? singular_key
20
+ [@settings[singular_key]]
21
+ else
22
+ Squid.configuration.public_send key
23
+ end
10
24
  end
11
- private key
12
25
  end
13
26
  end
14
27
  end
data/lib/squid/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Squid
2
- VERSION = '1.0.0.beta2'
2
+ VERSION = '1.0.0.beta3'
3
3
  end
data/lib/squid.rb CHANGED
@@ -7,7 +7,7 @@ module Squid
7
7
  module Interface
8
8
  # Plots a graph the current document.
9
9
  #
10
- # @param [Hash<#to_s, [Hash<#to_s, Numeric>]>] data the series to plot in
10
+ # @param [Hash<#to_s, [Hash<#to_s, Numeric>]>] data the series to plot in
11
11
  # the graph. Each key is the name of the series, and each value contains
12
12
  # the values for the series. Values are also a hash, where each key is
13
13
  # a category, and each value the numerical value for that category.
@@ -21,8 +21,8 @@ module Squid
21
21
  # @option settings [Numeric] :height (250) the full height of the graph.
22
22
  # @option settings [Boolean] :labels (false) whether to plot value labels.
23
23
  # @option settings [<Boolean, Hash>] :legend (true) whether to plot the
24
- # legend. If a Hash is provided, the `:offset` option can be set to
25
- # specify the offset between the legend and the right margin.
24
+ # legend. If a Hash is provided, the `:right` and `:bottom` options can be
25
+ # set to specify the right and bottom margin of the legend.
26
26
  # @option settings [Numeric] :line_width (3) the line width for line graphs.
27
27
  # @option settings [Numeric] :steps (4) the number of gridlines.
28
28
  # @option settings [Boolean] :ticks (true) whether to plot the ticks.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: squid
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.beta2
4
+ version: 1.0.0.beta3
5
5
  platform: ruby
6
6
  authors:
7
7
  - claudiob
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-08-26 00:00:00.000000000 Z
11
+ date: 2015-08-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: prawn
@@ -157,7 +157,9 @@ files:
157
157
  - examples/readme/14-height.rb
158
158
  - examples/readme/15-multiple-columns.rb
159
159
  - examples/readme/16-multiple-stacks.rb
160
- - examples/readme/17-two-axis.rb
160
+ - examples/readme/17-singular-options.rb
161
+ - examples/readme/18-plural-options.rb
162
+ - examples/readme/19-two-axis.rb
161
163
  - examples/readme/readme.rb
162
164
  - examples/screenshots/readme_00.png
163
165
  - examples/screenshots/readme_01.png
@@ -177,9 +179,12 @@ files:
177
179
  - examples/screenshots/readme_15.png
178
180
  - examples/screenshots/readme_16.png
179
181
  - examples/screenshots/readme_17.png
182
+ - examples/screenshots/readme_18.png
183
+ - examples/screenshots/readme_19.png
180
184
  - examples/squid/baseline.rb
181
185
  - examples/squid/basic.rb
182
186
  - examples/squid/border.rb
187
+ - examples/squid/color.rb
183
188
  - examples/squid/columns.rb
184
189
  - examples/squid/every.rb
185
190
  - examples/squid/format.rb
@@ -1,7 +0,0 @@
1
- filename = File.basename(__FILE__).gsub('.rb', '.pdf')
2
- Prawn::ManualBuilder::Example.generate(filename) do
3
- data = {views: {2013 => 182, 2014 => 46, 2015 => 88}, earnings: {2013 => 104_323, 2014 => 27_234, 2015 => 14_123}}
4
- chart data, type: :two_axis
5
- end
6
-
7
-