prawn-graph 1.0.4 → 1.0.5
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 +4 -4
- data/README.md +46 -6
- data/lib/prawn/graph/calculations/layout_calculator.rb +1 -1
- data/lib/prawn/graph/chart_components/bar_chart_renderer.rb +3 -3
- data/lib/prawn/graph/chart_components/canvas.rb +1 -1
- data/lib/prawn/graph/chart_components/line_chart_renderer.rb +13 -8
- data/lib/prawn/graph/extension.rb +1 -0
- data/lib/prawn/graph/series.rb +2 -1
- data/lib/prawn/graph/theme.rb +56 -36
- data/lib/prawn/graph/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2b4ef08ce19955ddfb4f13cfdd5ea16dc32f43ce
|
4
|
+
data.tar.gz: 5596f850626b686869af45159666b45cb92261c3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: caa5f449c651cda707d11796dcd688aaa3e4cd3fbcd3220b1c8b8c69301c634dfd39d6a583a3c739312ba4470151c42332f68d007515dd603511a7b5b0744724
|
7
|
+
data.tar.gz: 8d5d19afcc85fd66106062fbb2c3db5868699376c315b25a71739bfee1347ec5fa439c3198570a42c2a16fc446af148845c2c0a8d6256471159ef1a87d9f0792
|
data/README.md
CHANGED
@@ -8,11 +8,6 @@
|
|
8
8
|
[](https://hakiri.io/github/HHRy/prawn-graph/master)
|
9
9
|

|
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
|
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 = @
|
57
|
-
prawn.stroke_color = @
|
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
|
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
|
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 =
|
51
|
-
prawn.stroke_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
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
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)
|
data/lib/prawn/graph/series.rb
CHANGED
@@ -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+.
|
data/lib/prawn/graph/theme.rb
CHANGED
@@ -1,41 +1,61 @@
|
|
1
1
|
module Prawn
|
2
2
|
module Graph
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
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
|
data/lib/prawn/graph/version.rb
CHANGED
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
|
+
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-
|
12
|
+
date: 2016-12-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|