svg_plot_gen 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +3 -5
- data/lib/svg_plot_gen.rb +46 -30
- data/svg_plot_gen.gemspec +1 -1
- metadata +8 -8
data/README.md
CHANGED
@@ -16,14 +16,12 @@ Another rationale for using this is that you don't want to waste processing powe
|
|
16
16
|
|
17
17
|
**svg-plot-gen** is written in [ruby](http://www.ruby-lang.org/) and uses [nokogiri](http://nokogiri.org/) to output SVG.
|
18
18
|
|
19
|
-
You'll need to install [ruby](http://www.ruby-lang.org/) and [
|
19
|
+
You'll need to install [ruby](http://www.ruby-lang.org/) and [ruby gems](http://rubygems.org/) if you haven't already got them installed. Then it should just be a case of running
|
20
20
|
|
21
21
|
```
|
22
|
-
|
22
|
+
gem install svg_plot_gen
|
23
23
|
```
|
24
24
|
|
25
|
-
to get all the required gems.
|
26
|
-
|
27
25
|
## What it does
|
28
26
|
|
29
27
|
It outputs an SVG file that looks somewhat like [this](example.svg) to stdout.
|
@@ -35,7 +33,7 @@ It outputs an SVG file that looks somewhat like [this](example.svg) to stdout.
|
|
35
33
|
Run
|
36
34
|
|
37
35
|
```
|
38
|
-
|
36
|
+
svg_plot_gen --help
|
39
37
|
```
|
40
38
|
|
41
39
|
to see details of all the command line options.
|
data/lib/svg_plot_gen.rb
CHANGED
@@ -12,6 +12,12 @@ class Svg_Plot_Gen
|
|
12
12
|
opt :template, "Output a template SVG file that contains [SPLIT] markers where a path and display attribute can be inserted. See README.md", :default => false
|
13
13
|
opt :width, "The width of the output plot in pixels", :default => 960
|
14
14
|
opt :height, "The height of the output plot in pixels", :default => 380
|
15
|
+
opt :y_first, "Sets the value at which the y axis starts", :default => 0
|
16
|
+
opt :y_last, "Set the value at which the y axis ends", :default => 5
|
17
|
+
opt :log, "Sets the y axis to a logarithmic scale. `y_first` and `y_last` now refer to powers of 10", :default => false
|
18
|
+
opt :x_text, "The x-axis label", :default => 'x-axis'
|
19
|
+
opt :y_text, "The y-axis label", :default => 'y-axis'
|
20
|
+
opt :cursor_line, "Adds a vertical line that follows the cursor to the graph", :default => false
|
15
21
|
opt :x_margin, "The distance that is left between the edge of the image and the x-axis", :default => 70
|
16
22
|
opt :y_margin, "The distance that is left between the edge of the image and the y-axis", :default => 100
|
17
23
|
opt :margins, "The distance that is left between the edge of the image and the plot on sides that do not have labels", :default => 25
|
@@ -19,9 +25,6 @@ class Svg_Plot_Gen
|
|
19
25
|
opt :short_tick_len, "The length of the short ticks on the graph", :default => 5
|
20
26
|
opt :font_size, "The size of font used for the plot", :default => 12
|
21
27
|
opt :label_font_size, "The size of font used for the axis labels", :default => 14
|
22
|
-
opt :scale, "The scale that is going to be used on the y axis", :default => 'em'
|
23
|
-
opt :x_text, "The x-axis label", :default => 'x-axis'
|
24
|
-
opt :y_text, "The y-axis label", :default => 'y-axis'
|
25
28
|
end
|
26
29
|
|
27
30
|
axis_padding = 6
|
@@ -55,10 +58,12 @@ class Svg_Plot_Gen
|
|
55
58
|
# Read in the CSS for our font
|
56
59
|
font_css = File.read(File.dirname(__FILE__) + '/font.css')
|
57
60
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
61
|
+
if opts.cursor_line
|
62
|
+
# Load in the javascript for the cursor line script
|
63
|
+
cursor_line_js = File.read(File.dirname(__FILE__) + '/cursor_line.js')
|
64
|
+
# Replace the [XSTART] and [XEND] placeholders in the javascript
|
65
|
+
cursor_line_js = cursor_line_js.gsub(/\[XSTART\]/, xstart.to_s).gsub(/\[XEND\]/, xend.to_s)
|
66
|
+
end
|
62
67
|
|
63
68
|
# Set the template marker if specified
|
64
69
|
if opts.template
|
@@ -77,27 +82,36 @@ class Svg_Plot_Gen
|
|
77
82
|
xlabels = (0..24).step(2).map { |hour| [['00', (hour%24).to_s].join[-2..-1], ':00'].join }.zip(xlines.select { |k, v| v }.map { |k, v| k })
|
78
83
|
|
79
84
|
# Generates the values for a logarithmic scale
|
80
|
-
if opts.
|
85
|
+
if opts.log
|
81
86
|
# Work out the y-positions of the horizonal lines and ticks
|
82
|
-
ylines = Hash[(
|
87
|
+
ylines = Hash[(opts.y_first..opts.y_last)
|
88
|
+
.map { |y| (y < opts.y_last ? [1, 2.5, 5, 7.5] : [1])
|
89
|
+
.map { |m| [Math.log10((10**y)*m), m==1] } }
|
90
|
+
.flatten(1)
|
91
|
+
.map { |y| [(yend - (y[0] - opts.y_first)*(ylen.to_f/(opts.y_last - opts.y_first))).round(1), y[1]] }]
|
83
92
|
# Build up our y-axis labels
|
84
|
-
ylabels =
|
85
|
-
|
86
|
-
|
87
|
-
|
93
|
+
ylabels = (opts.y_first..opts.y_last)
|
94
|
+
.map{ |i| 10**i }
|
95
|
+
.map{ |i| i <= 10000 ? ("%d" % i) : ("%.0e" % i) }
|
96
|
+
.zip(ylines.select { |k ,v| v }.map { |k, v| k })
|
97
|
+
else # Generate the values for a linear scale
|
88
98
|
# Work out the y-positions of the horizonal lines and ticks
|
89
|
-
ylines = Hash[(
|
99
|
+
ylines = Hash[(opts.y_first..opts.y_last)
|
100
|
+
.map { |y| (y < opts.y_last ? [0, 0.25, 0.5, 0.75] : [0])
|
101
|
+
.map { |m| [y+m, m==0] } }
|
102
|
+
.flatten(1)
|
103
|
+
.map { |y| [(yend - (y[0] - opts.y_first)*(ylen.to_f/(opts.y_last - opts.y_first))).round(1), y[1]] }]
|
90
104
|
# Build up our y-axis labels
|
91
|
-
ylabels = (
|
105
|
+
ylabels = (opts.y_first..opts.y_last).zip(ylines.select { |k ,v| v }.map { |k, v| k })
|
92
106
|
end
|
93
107
|
|
94
108
|
# Output some useful info about the plot we're generating to stderr
|
95
|
-
$stderr.puts ["X Origin
|
96
|
-
$stderr.puts ["Y Origin
|
97
|
-
$stderr.puts ["X Length
|
98
|
-
$stderr.puts ["Y Length
|
99
|
-
$stderr.puts ["Px per
|
100
|
-
$stderr.puts ["Px per
|
109
|
+
$stderr.puts ["X Origin ", xstart].join
|
110
|
+
$stderr.puts ["Y Origin ", yend].join
|
111
|
+
$stderr.puts ["X Length ", xlen].join
|
112
|
+
$stderr.puts ["Y Length ", ylen].join
|
113
|
+
$stderr.puts ["Px per x division ", (xlen.to_f/12).round(2)].join
|
114
|
+
$stderr.puts ["Px per y division ", (ylen.to_f/(opts.y_last - opts.y_first)).round(2)].join
|
101
115
|
|
102
116
|
# Create the XML object
|
103
117
|
builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
|
@@ -119,16 +133,18 @@ class Svg_Plot_Gen
|
|
119
133
|
xml.cdata(font_css)
|
120
134
|
end
|
121
135
|
end
|
122
|
-
|
123
|
-
|
124
|
-
xml.
|
136
|
+
if opts.cursor_line
|
137
|
+
# Cursor Line Script
|
138
|
+
xml.script(:type => 'text/javascript') do
|
139
|
+
xml.cdata(cursor_line_js)
|
140
|
+
end
|
141
|
+
# The cursor line itself
|
142
|
+
xml.path(:id => 'cursor_line',
|
143
|
+
:d => ['M0,', ystart, 'l0,', ylen].join,
|
144
|
+
:stroke => orange.html,
|
145
|
+
'stroke-width' => '1.5',
|
146
|
+
:display => 'none')
|
125
147
|
end
|
126
|
-
# The cursor line itself
|
127
|
-
xml.path(:id => 'cursor_line',
|
128
|
-
:d => ['M0,', ystart, 'l0,', ylen].join,
|
129
|
-
:stroke => orange.html,
|
130
|
-
'stroke-width' => '1.5',
|
131
|
-
:display => 'none')
|
132
148
|
# X-Axis - Vertical Lines
|
133
149
|
xml.g(:style => "color:grey",
|
134
150
|
:stroke => "currentColor",
|
data/svg_plot_gen.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "svg_plot_gen"
|
3
3
|
s.summary = "Creates the axes and gridlines for a graph in SVG. Perfect for creating a static background to which live data can be added."
|
4
|
-
s.version = "0.0.
|
4
|
+
s.version = "0.0.4"
|
5
5
|
s.date = Time.now.strftime('%Y-%m-%d')
|
6
6
|
s.author = "Richard Meadows"
|
7
7
|
s.homepage = "https://github.com/richardeoin/svg-plot-gen"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: svg_plot_gen
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2013-03-16 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: trollop
|
16
|
-
requirement: &
|
16
|
+
requirement: &15819420 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '2'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *15819420
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: nokogiri
|
27
|
-
requirement: &
|
27
|
+
requirement: &15817620 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '1.5'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *15817620
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: color
|
38
|
-
requirement: &
|
38
|
+
requirement: &15833660 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '1.4'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *15833660
|
47
47
|
description:
|
48
48
|
email:
|
49
49
|
executables:
|
@@ -55,12 +55,12 @@ files:
|
|
55
55
|
- Gemfile.lock
|
56
56
|
- LICENSE.txt
|
57
57
|
- README.md
|
58
|
+
- bin/svg_plot_gen
|
58
59
|
- example.svg
|
59
60
|
- lib/cursor_line.js
|
60
61
|
- lib/font.css
|
61
62
|
- lib/svg_plot_gen.rb
|
62
63
|
- svg_plot_gen.gemspec
|
63
|
-
- bin/svg_plot_gen
|
64
64
|
homepage: https://github.com/richardeoin/svg-plot-gen
|
65
65
|
licenses:
|
66
66
|
- MIT
|