prawn-graph 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 48595c4b07bae03905c3f1884ea25c7940793669
4
- data.tar.gz: d64af98709158716e5130bcd278ad5c5b783fdcb
3
+ metadata.gz: 2b4ef08ce19955ddfb4f13cfdd5ea16dc32f43ce
4
+ data.tar.gz: 5596f850626b686869af45159666b45cb92261c3
5
5
  SHA512:
6
- metadata.gz: 3f4551d1a9c008ab92462af6b261c25db9b9ec8fd6d9ba4fd1b0fde0602ae2d67a14e05f2760d1e1b34bf7c8d07727c133e0931a4d40d39444790ac6e739b94a
7
- data.tar.gz: 3da90cd7612b627883d015ca24c2cf4b7b59a4e3274ae2e52f1b3fb6d279b70aa43837059e1ebd18b4bac09c6eee0e53c5d57a1c55ec101f0ea94dbe83801715
6
+ metadata.gz: caa5f449c651cda707d11796dcd688aaa3e4cd3fbcd3220b1c8b8c69301c634dfd39d6a583a3c739312ba4470151c42332f68d007515dd603511a7b5b0744724
7
+ data.tar.gz: 8d5d19afcc85fd66106062fbb2c3db5868699376c315b25a71739bfee1347ec5fa439c3198570a42c2a16fc446af148845c2c0a8d6256471159ef1a87d9f0792
data/README.md CHANGED
@@ -8,11 +8,6 @@
8
8
  [![security](https://hakiri.io/github/HHRy/prawn-graph/master.svg)](https://hakiri.io/github/HHRy/prawn-graph/master)
9
9
  ![Maintained: yes](https://img.shields.io/badge/maintained-yes-brightgreen.png)
10
10
 
11
- **This readme reflects the state of _master_ which is not the released version of prawn-graph.**
12
-
13
- **The code being used to cut gems for release is the `stable` Branch, please make pull requests for**
14
- **bug fixes from that branch.**
15
-
16
11
  An extension for the [prawn pdf library][5] which adds the ability to draw graphs (or charts if
17
12
  you perfer) in PDF documents.
18
13
 
@@ -49,7 +44,6 @@ The `bar_chart`, `line_chart`, `bar_graph`, and `line_graph` methods have been r
49
44
  version of prawn-graph is no-longer backwards compatible. If you _must_ use those old methods, then please
50
45
  use version `0.9.10` and **upgrade your calls to prawn graph to use the new `graph` methods as soon as possible**.
51
46
 
52
-
53
47
  ## Installation
54
48
 
55
49
  To use prawn-graph, you can add the following to your `Gemfile`:
@@ -100,6 +94,7 @@ Option | Data type | Description
100
94
  :height | integer | Desired height of the graph. Defaults to vertical space available.
101
95
  :title | string | The overall title for your chart
102
96
  :series_key | boolean | Should we render the series key for multi series graphs? Defaults to true.
97
+ :theme | Prawn::Graph::Theme | An instance of the Theme object which should be used to style this chart. Default is a greyscale theme for printing.
103
98
 
104
99
  The `data` passed to `graph` or `chart` should be an `Array` of `Prawn::Graph::Series` objects, which
105
100
  themselves are made up of an array of data points to plot, and a series of options.
@@ -136,6 +131,51 @@ Option | Data type | Description
136
131
  end
137
132
  ```
138
133
 
134
+ ### Theming
135
+
136
+ You can pass an instnace of `Prawn::Graph::Theme` using the `:theme` option to the `graph` and `chart` methods. The
137
+ theme expects to be initialised with a Hash in the following format:
138
+
139
+ ```ruby
140
+ {
141
+ series:
142
+ [
143
+ 'EBEDEF',
144
+ 'D6DBDF',
145
+ '85929E',
146
+ '34495E',
147
+ '1B2631'
148
+ ],
149
+ title:'17202A',
150
+ background:'FFFFFF',
151
+ grid:'F2F4F4',
152
+ axes:'17202A',
153
+ markers:'34495E',
154
+ stroke_grid_lines:true,
155
+ default:'333333',
156
+ average:'34495E',
157
+ max:'17202A',
158
+ min:'17202A'
159
+ }
160
+ ```
161
+
162
+ Valid keys are:
163
+
164
+ Option | Data type | Description
165
+ ------------------- | ------------------ | -----------
166
+ :series | Array of Strings | HEX code colours used to draw a series on the chart. Will cycle through these are too many series for colors
167
+ :title | string | HEX color code used to color this part of the chart
168
+ :background | string | HEX color code used to color this part of the chart
169
+ :grid | string | HEX color code used to color this part of the chart
170
+ :axes | string | HEX color code used to color this part of the chart
171
+ :markers | string | HEX color code used to color this part of the chart
172
+ :stroke_grid_lines | boolean | Should the grid lines be stroked?
173
+ :default | string | HEX color code used to color this part of the chart
174
+ :average | string | HEX color code used to color this part of the chart
175
+ :max | string | HEX color code used to color this part of the chart
176
+ :min | string | HEX color code used to color this part of the chart
177
+
178
+
139
179
  ### Output
140
180
  <img src="http://prawn-graph.ryanstenhouse.jp/img/prawn-graph-output.png" alt="Prawn Graph Example Output" width="933" height="420">
141
181
 
@@ -16,7 +16,7 @@ module Prawn
16
16
  end
17
17
  end
18
18
 
19
- def initialize(bounds, attributes = nil, theme = Prawn::Graph::Theme::Default)
19
+ def initialize(bounds, attributes = nil, theme = Prawn::Graph::Theme.default)
20
20
  @bounds = bounds
21
21
  @graph_area = Dimensions.new({ width: 0, height: 0, x: 0, y: 0 })
22
22
  @title_area = Dimensions.new({ width: 0, height: 0, x: 0, y: 0 })
@@ -16,7 +16,7 @@ module Prawn
16
16
  if @series[series_index].mark_average?
17
17
  average_y_coordinate = (point_height_percentage(@series[series_index].avg) * @plot_area_height) - 5
18
18
  prawn.line_width = 1
19
- prawn.stroke_color = @color[series_index]
19
+ prawn.stroke_color = @canvas.theme.color_for(@series[series_index]) # @color[series_index]
20
20
  prawn.dash(2)
21
21
  prawn.stroke_line([0, average_y_coordinate], [ @plot_area_width, average_y_coordinate ])
22
22
  prawn.undash
@@ -53,8 +53,8 @@ module Prawn
53
53
 
54
54
  @series.size.times do |series_index|
55
55
  series_offset = series_index + 1
56
- prawn.fill_color = @color[series_index]
57
- prawn.stroke_color = @color[series_index]
56
+ prawn.fill_color = @canvas.theme.color_for(@series[series_index])
57
+ prawn.stroke_color = @canvas.theme.color_for(@series[series_index])
58
58
  prawn.line_width = width
59
59
 
60
60
  starting = (prawn.bounds.left + (point * width_per_point))
@@ -17,7 +17,7 @@ module Prawn
17
17
  verify_series_are_ok!
18
18
  @options = {xaxis_labels: []}.merge(options.merge({ series_count: series.size }))
19
19
  @prawn = prawn
20
- @theme = Prawn::Graph::Theme::Default
20
+ @theme = options[:theme].nil? ? Prawn::Graph::Theme.default : options[:theme]
21
21
  @layout = Prawn::Graph::Calculations::LayoutCalculator.new([prawn.bounds.width, prawn.bounds.height], @options, @theme).calculate
22
22
 
23
23
  yield self if block_given?
@@ -14,7 +14,7 @@ module Prawn
14
14
  private
15
15
 
16
16
  def apply_marker_to_graph(value_marked, value, x, y, color)
17
- if @series.mark_maximum? && value_marked == false && value != 0 && value == @series.max
17
+ if value_marked == false && value != 0 && value == @series.max
18
18
  draw_marker_point(color, x, y)
19
19
  value_marked = true
20
20
  end
@@ -26,7 +26,7 @@ module Prawn
26
26
  if @series.mark_average?
27
27
  average_y_coordinate = (point_height_percentage(@series.avg) * @plot_area_height) - 5
28
28
  prawn.line_width = 1
29
- prawn.stroke_color = @color
29
+ prawn.stroke_color = @canvas.theme.color_for(@series) #@color
30
30
  prawn.dash(2)
31
31
  prawn.stroke_line([0, average_y_coordinate], [ @plot_area_width, average_y_coordinate ])
32
32
  prawn.undash
@@ -47,8 +47,8 @@ module Prawn
47
47
  spacing = width_per_point
48
48
 
49
49
  prawn.line_width = 2
50
- prawn.fill_color = @color
51
- prawn.stroke_color = @color
50
+ prawn.fill_color = @canvas.theme.color_for(@series)
51
+ prawn.stroke_color = @canvas.theme.color_for(@series)
52
52
 
53
53
  previous_value = @series.values[i - 1]
54
54
  this_value = v
@@ -66,10 +66,15 @@ module Prawn
66
66
  prawn.fill_ellipse([ ( this_x_offset), this_y ], 1)
67
67
  end
68
68
 
69
- min_marked = apply_marker_to_graph(min_marked, previous_value, previous_x_offset, previous_y, @canvas.theme.min)
70
- min_marked = apply_marker_to_graph(min_marked, this_value, this_x_offset, this_y, @canvas.theme.min)
71
- max_marked = apply_marker_to_graph(max_marked, previous_value, previous_x_offset, previous_y, @canvas.theme.max)
72
- max_marked = apply_marker_to_graph(max_marked, this_value, this_x_offset, this_y, @canvas.theme.max)
69
+ if @series.mark_minimum?
70
+ min_marked = apply_marker_to_graph(min_marked, previous_value, previous_x_offset, previous_y, @canvas.theme.min)
71
+ min_marked = apply_marker_to_graph(min_marked, this_value, this_x_offset, this_y, @canvas.theme.min)
72
+ end
73
+
74
+ if @series.mark_maximum?
75
+ max_marked = apply_marker_to_graph(max_marked, previous_value, previous_x_offset, previous_y, @canvas.theme.max)
76
+ max_marked = apply_marker_to_graph(max_marked, this_value, this_x_offset, this_y, @canvas.theme.max)
77
+ end
73
78
 
74
79
  j += 1
75
80
  end
@@ -13,6 +13,7 @@ module Prawn
13
13
  # `:at` - The point from where the graph will be drawn. `[<Integer>x, <Integer>y]`
14
14
  # `:title` - The title for this chart. Must be a string. `<String>`
15
15
  # `:series_key` - Should we render the key to series in this chart? `<Boolean>`
16
+ # `:theme ` - An instance of the theme to be used for styling this graph. `<Prawn::Graph::Theme>`
16
17
  #
17
18
  def graph(series, options = {}, &block)
18
19
  canvas = Prawn::Graph::ChartComponents::Canvas.new(series, self, options, &block)
@@ -5,7 +5,7 @@ module Prawn
5
5
  # on a chart.
6
6
  #
7
7
  class Series
8
- attr_accessor :values, :options
8
+ attr_accessor :values, :options, :uuid
9
9
 
10
10
  DEFAULT_OPTIONS = {
11
11
  title: nil,
@@ -18,6 +18,7 @@ module Prawn
18
18
  def initialize(values = [], options = {})
19
19
  @values = values
20
20
  @options = OpenStruct.new(DEFAULT_OPTIONS.merge(options))
21
+ @uuid = SecureRandom.uuid
21
22
  end
22
23
 
23
24
  # @return [String] The value of +options.title+.
@@ -1,41 +1,61 @@
1
1
  module Prawn
2
2
  module Graph
3
- module Theme
4
- Default = OpenStruct.new({
5
- series: [
6
- 'EBEDEF',
7
- 'D6DBDF',
8
- '85929E',
9
- '34495E',
10
- '1B2631',
11
- 'EBEDEF',
12
- 'D6DBDF',
13
- '85929E',
14
- '34495E',
15
- '1B2631',
16
- 'EBEDEF',
17
- 'D6DBDF',
18
- '85929E',
19
- '34495E',
20
- '1B2631',
21
- ],
22
- title: '17202A',
23
- background: 'FFFFFF',
24
- grid: 'F2F4F4',
25
- axes: '17202A',
26
- markers: '34495E',
27
- stroke_grid_lines: true,
28
- default: '333333',
29
- average: '34495E',
30
- max: '17202A',
31
- min: '17202A',
32
- font_sizes: OpenStruct.new({
33
- default: 8,
34
- main_title: 10,
35
- axis_labels: 5,
36
- series_key: 8,
37
- }),
38
- })
3
+ class Theme
4
+
5
+ attr_reader :theme
6
+
7
+ # The default theme is special, only create one of them.
8
+ #
9
+ def self.default
10
+ @@_default_theme ||= Prawn::Graph::Theme.new(Prawn::Graph::Theme::Default)
11
+ end
12
+
13
+ def initialize(arg)
14
+ @series_map = {}
15
+ @current_series_color = 0
16
+ @theme = OpenStruct.new(arg)
17
+ end
18
+
19
+ def number_of_colors
20
+ theme.series.size
21
+ end
22
+
23
+ def ==(other)
24
+ theme == other.theme
25
+ end
26
+
27
+ def color_for(series)
28
+ @series_map[series.uuid] = cycle_color unless @series_map.has_key?(series.uuid)
29
+ @series_map[series.uuid]
30
+ end
31
+
32
+ def font_sizes
33
+ @font_sizes ||= OpenStruct.new({default: 8, main_title: 10, axis_labels: 5, series_key: 8 })
34
+ end
35
+
36
+ def method_missing(method_name, *arguments)
37
+ if arguments.any?
38
+ @theme.send(method_name, arguments)
39
+ else
40
+ @theme.send(method_name)
41
+ end
42
+ end
43
+
44
+ Default = {series:['EBEDEF', 'D6DBDF', '85929E', '34495E', '1B2631' ], title:'17202A', background:'FFFFFF', grid:'F2F4F4', axes:'17202A', markers:'34495E', stroke_grid_lines:true, default:'333333', average:'34495E', max:'17202A', min:'17202A' }
45
+
46
+ private
47
+
48
+ def cycle_color
49
+ if @series_map.empty?
50
+ @current_series_color = 0
51
+ else
52
+ next_color_index = @current_series_color + 1
53
+ next_color_index = 0 if next_color_index == @theme.series.size
54
+ @current_series_color = next_color_index
55
+ end
56
+ @theme.series[@current_series_color]
57
+ end
58
+
39
59
  end
40
60
  end
41
61
  end
@@ -1,5 +1,5 @@
1
1
  module Prawn
2
2
  module Graph
3
- VERSION = "1.0.4"
3
+ VERSION = "1.0.5"
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: 1.0.4
4
+ version: 1.0.5
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-11-28 00:00:00.000000000 Z
12
+ date: 2016-12-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler