prawn-graph 0.9.2 → 0.9.3

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: bab79a3dc4ba1072ba72479b83ac9de8a1d38922
4
- data.tar.gz: 6c23d7a89268e42bfff25b1da58bcb49adc7e3bc
3
+ metadata.gz: e781761d7d174b22bde7030b5987e8e59c99a69f
4
+ data.tar.gz: 9102428b3c801fb0c1e32daf04912a0ef7866c65
5
5
  SHA512:
6
- metadata.gz: b6b4482768b7d5470a3a3913698cd8669a391d8947a576c540d45938a76c62b644705f7572fd7474e21a218b2aab72d1a3a7c342d1dcacdbcfddbbcf79050898
7
- data.tar.gz: d4666cd5006ccaef777fe90c882b36da57a170903dd1017588b037ddd9d5070b81533c86db743ce4fb2acab5083926a72888e0a1e07a3f20e5a617699611a4c9
6
+ metadata.gz: 155310ed2dde8d1375d42632af91b7f78c7f9f032f026f795ad437cbb2a6eeba318e62ac5ef6753751ec4e10903576e1b312be2377f0a97fa55fb2b30f86a9e3
7
+ data.tar.gz: d845d5ad0201e5e2b549a022b6ab66170121748f4c5e989e739917b75c541e3098c6f03c76c26b602109bb34b375d4b17790f7fb7266214b6218fb005f9d44f0
data/README.md CHANGED
@@ -77,37 +77,54 @@ adventurous - please add it!
77
77
 
78
78
  ## Using prawn-graph
79
79
 
80
- ```ruby
81
- require 'prawn-graph'
82
-
83
- series = []
84
- series << Prawn::Graph::Series.new([1,2,3,4,5], title: "Some numbers", type: :bar)
80
+ Graphs can be created by calling the `graph` or its alias, `chart` method with an array of
81
+ `Prawn::Graph::Series` objects representing the data you would like to plot and how it should
82
+ be displayed. It will also take a hash of options, and block which will have the graph yeilded
83
+ to it.
85
84
 
86
- Prawn::Document.generate('test.pdf') do
87
- graph series
88
- end
85
+ ```ruby
86
+ graph data, options = {}, &block.
89
87
  ```
90
88
 
91
89
  When called with just a set of data, prawn-graph will do its best to make the graph fit in the
92
90
  available space. For a little more control over how the graphs are rendered in the document
93
- you can pass the following options:
91
+ you can pass the following options to `graph` or `chart`:
94
92
 
95
93
  Option | Data type | Description
96
94
  ----------- | --------- | -----------
97
95
  :at | [integer, integer] | Specify the location on the page you want the graph to appear.
98
96
  :width | integer | Desired width of the graph. Defaults to horizontal space available.
99
97
  :height | integer | Desired height of the graph. Defaults to vertical space available.
98
+ :title | string | The overall title for your chart
99
+ :series_key | boolean | Should we render the series key for multi series graphs? Defaults to true.
100
+
101
+ The `data` passed to `graph` or `chart` should be an `Array` of `Prawn::Graph::Series` objects, which
102
+ themselves are made up of an array of data points to plot, and a series of options.
103
+
104
+ ```ruby
105
+ Prawn::Graph::Series.new [1,2,3,4], options = {}
106
+ ```
107
+
108
+ Valid `options` are:
109
+
110
+ Option | Data type | Description
111
+ ------------- | --------- | -----------
112
+ :mark_average | boolean | Should we mark a line showing the average value of the series? Defaults to false.
113
+ :mark_minimum | boolean | Should we mark the minimum value of the series? Defaults to false.
114
+ :mark_maximum | boolean | Should we mark the maximum value of the series? Defaults to false.
115
+ :title | string | The title of this series, which will be shown in the series key.
116
+ :type | symbol | How this series should be rendered. Defaults to `:bar`, valid options are `:bar`, `:line`.
100
117
 
101
- ### Advanced example
118
+ ### Show me some code!
102
119
 
103
120
  ```ruby
104
121
  require 'prawn-graph'
105
122
 
106
123
  series = []
107
- series << Prawn::Graph::Series.new([4,9,3,2,1,6,2,8,2,3,4,9,2], title: "A label for a series", type: :bar, mark_average: true)
108
- series << Prawn::Graph::Series.new([5,4,3,2,7,9,2,8,7,5,4,9,2].reverse, title: "Another label", type: :line, mark_average: true)
124
+ series << Prawn::Graph::Series.new([4,9,3,2,1,6,2,8,2,3,4,9,2], title: "A label for a series", type: :bar)
125
+ series << Prawn::Graph::Series.new([5,4,3,2,7,9,2,8,7,5,4,9,2].reverse, title: "Another label", type: :line, mark_average: true, mark_minimum: true)
109
126
  series << Prawn::Graph::Series.new([1,2,3,4,5,9,6,4,5,6,3,2,11], title: "Yet another label", type: :bar)
110
- series << Prawn::Graph::Series.new([1,2,3,4,5,12,6,4,5,6,3,2,9].shuffle, title: "One final label", type: :line, mark_average: true)
127
+ series << Prawn::Graph::Series.new([1,2,3,4,5,12,6,4,5,6,3,2,9].shuffle, title: "One final label", type: :line, mark_average: true, mark_maximum: true)
111
128
 
112
129
  Prawn::Document.generate('test.pdf') do
113
130
  graph series, width: 500, height: 200, title: "A Title for the chart", at: [10,700]
@@ -33,6 +33,9 @@ module Prawn
33
33
  prawn.bounding_box [@graph_area.point[0] + 5, @graph_area.point[1] - 20], width: @plot_area_width, height: @plot_area_height do
34
34
  j = 2
35
35
  prawn.save_graphics_state do
36
+ max_marked = false
37
+ min_marked = false
38
+
36
39
  @series.values.each_with_index do |v, i|
37
40
  next if i == 0
38
41
 
@@ -59,6 +62,36 @@ module Prawn
59
62
  prawn.fill_color = @canvas.theme.markers
60
63
  prawn.fill_ellipse([ ( previous_x_offset), previous_y ], 1)
61
64
  prawn.fill_ellipse([ ( this_x_offset), this_y ], 1)
65
+
66
+ if @series.mark_minimum? && min_marked == false && previous_value == @series.min
67
+ prawn.save_graphics_state do
68
+ prawn.fill_color = @canvas.theme.min
69
+ prawn.stroke_color = @canvas.theme.min
70
+ prawn.line_width = 1
71
+
72
+ prawn.dash(2)
73
+ prawn.stroke_line([previous_x_offset, 0], [previous_x_offset, previous_y])
74
+ prawn.undash
75
+
76
+ prawn.fill_ellipse([ ( previous_x_offset), previous_y ], 2)
77
+ min_marked = true
78
+ end
79
+ end
80
+
81
+ if @series.mark_maximum? && max_marked == false && previous_value == @series.max
82
+ prawn.save_graphics_state do
83
+ prawn.fill_color = @canvas.theme.max
84
+ prawn.stroke_color = @canvas.theme.max
85
+ prawn.line_width = 1
86
+
87
+ prawn.dash(2)
88
+ prawn.stroke_line([previous_x_offset, 0], [previous_x_offset, previous_y])
89
+ prawn.undash
90
+
91
+ prawn.fill_ellipse([ ( previous_x_offset), previous_y ], 2)
92
+ max_marked = true
93
+ end
94
+ end
62
95
  j += 1
63
96
  end
64
97
 
@@ -131,6 +164,8 @@ module Prawn
131
164
  num_points = @series[0].size
132
165
  width_per_point = (@plot_area_width / num_points).round(2).to_f
133
166
  width = ((width_per_point * BigDecimal('0.9')) / @series.size).round(2).to_f
167
+ min_marked = false
168
+ max_marked = false
134
169
 
135
170
  num_points.times do |point|
136
171
 
@@ -155,6 +190,36 @@ module Prawn
155
190
  prawn.stroke_line([0, average_y_coordinate], [ @plot_area_width, average_y_coordinate ])
156
191
  prawn.undash
157
192
  end
193
+
194
+ if @series[series_index].mark_minimum? && min_marked == false && @series[series_index].values[point] == @series[series_index].min
195
+ prawn.save_graphics_state do
196
+ prawn.fill_color = @canvas.theme.min
197
+ prawn.stroke_color = @canvas.theme.min
198
+ prawn.line_width = 1
199
+
200
+ prawn.dash(2)
201
+ prawn.stroke_line([x_position, 0], [x_position, y_position])
202
+ prawn.undash
203
+
204
+ prawn.fill_ellipse([x_position, y_position ], 2)
205
+ min_marked = true
206
+ end
207
+ end
208
+
209
+ if @series[series_index].mark_maximum? && max_marked == false && @series[series_index].values[point] == @series[series_index].max
210
+ prawn.save_graphics_state do
211
+ prawn.fill_color = @canvas.theme.max
212
+ prawn.stroke_color = @canvas.theme.max
213
+ prawn.line_width = 1
214
+
215
+ prawn.dash(2)
216
+ prawn.stroke_line([x_position, 0], [x_position, y_position])
217
+ prawn.undash
218
+
219
+ prawn.fill_ellipse([x_position, y_position ], 2)
220
+ max_marked = true
221
+ end
222
+ end
158
223
  end
159
224
 
160
225
  end
@@ -71,6 +71,14 @@ module Prawn
71
71
  options.mark_average == true
72
72
  end
73
73
 
74
+ def mark_minimum?
75
+ options.mark_minimum == true
76
+ end
77
+
78
+ def mark_maximum?
79
+ options.mark_maximum == true
80
+ end
81
+
74
82
  # @deprecated Provided to allow for tempory backwards compatibilty with legacy graph drawing. Do not use.
75
83
  # @return [Array] Series represented as an array in the format [ title, val1, val2... ]
76
84
  #
@@ -1,5 +1,5 @@
1
1
  module Prawn
2
2
  module Graph
3
- VERSION = "0.9.2"
3
+ VERSION = "0.9.3"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prawn-graph
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.2
4
+ version: 0.9.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Stenhouse
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2016-06-14 00:00:00.000000000 Z
12
+ date: 2016-06-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler