svg-lib 0.0.6 → 0.0.7

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
  SHA256:
3
- metadata.gz: 1838ec9557c2332e4ca7a8daa07b3d3d64ae0aebccd8d373f88bcbec7a2ee5fd
4
- data.tar.gz: f5b7bdf0371fd15426349d87e9cf80204aae66d4c719f26088830c400f22bf27
3
+ metadata.gz: c8f8207432872d10688057afbacf6cc999b9ff5faa8d00b7275544fe9849e2d1
4
+ data.tar.gz: ed17273e56404503fb201f50508cdcdef727f84e003e3bd51ae82e76be157282
5
5
  SHA512:
6
- metadata.gz: 1ec3399c2bb0cdf2a8da8acd9c83513d2e7563a4c0db4a81a3c74b9d587417edec65a34fec9671b18a07818e1e34766fb01bffff7a74c5768ab624cebe27e5b2
7
- data.tar.gz: fea5449f4ccf671806db77cc29cfc9e48c53063324e6f20d5be2799fd9b627bb788cfee0564e3b94a76b5d5cd2398dbbdb009ea8db3c238b9b90f776b9079faa
6
+ metadata.gz: 93f86a3d95912ce0368ea653dde8b0290bbbbf6005dc237916fadad8989d98fd2702b4b606d5fe17bbdfb39b3becf3f9fa02cf6d6c2d65c0b1f5cfe00b7ca2b1
7
+ data.tar.gz: b47ee1352fd8e04ee36a5e3b44166b0d3f951102cef8f2d9636f4be0fe7fe4bc9220dd6c6222771f02028312535dfa22808c5592da67bfca29147980e8738922
data/bin/eplot CHANGED
@@ -28,6 +28,17 @@ opts[:width] = 800
28
28
  opts[:height] = 800
29
29
  opts[:noaxis] = false
30
30
 
31
+ opts[:lpad] = 100
32
+ opts[:rpad] = 50
33
+ opts[:tpad] = 100
34
+ opts[:bpad] = 100
35
+ opts[:xaxfmt] = "%-0.4f"
36
+ opts[:yaxfmt] = "%-0.4f"
37
+
38
+ opts[:xrng] = nil
39
+
40
+ opts[:legendfontsize] = 14
41
+
31
42
  progname = File.basename($PROGRAM_NAME,File.extname($PROGRAM_NAME))
32
43
  parser = OptionParser.new do |parser|
33
44
  parser.banner = <<~banner
@@ -106,6 +117,39 @@ parser = OptionParser.new do |parser|
106
117
  opts[:labels] = list.split(",")
107
118
  end
108
119
 
120
+ parser.on("--noaxis") do
121
+ opts[:noaxis] = true
122
+ end
123
+
124
+ parser.on("--lpad=INT", "left padding in px") do |int|
125
+ opts[:lpad] = int.to_i
126
+ end
127
+ parser.on("--rpad=INT", "right padding in px") do |int|
128
+ opts[:rpad] = int.to_i
129
+ end
130
+ parser.on("--tpad=INT", "top padding in px") do |int|
131
+ opts[:tpad] = int.to_i
132
+ end
133
+ parser.on("--bpad=INT", "bottom padding in px") do |int|
134
+ opts[:bpad] = int.to_i
135
+ end
136
+
137
+ parser.on("--legendfontsize=INT" "legend font size in px") do |int|
138
+ opts[:legendfontsize] = int.to_i
139
+ end
140
+
141
+ parser.on("--xaxfmt=STR", "format for x axis labels") do |str|
142
+ opts[:xaxfmt] = str
143
+ end
144
+
145
+ parser.on("--yaxfmt=STR", "format for y axis labels") do |str|
146
+ opts[:yaxfmt] = str
147
+ end
148
+
149
+ parser.on("--xrng=\"FLOAT,FLOAT\"", "range for x labels when only one column is given") do |str|
150
+ b,e = str.split(",").map{|e| e.to_f}
151
+ opts[:xrng] = (b..e)
152
+ end
109
153
 
110
154
  parser.on("-h","--help","shows this message") do
111
155
  STDERR.puts parser
@@ -129,7 +173,7 @@ end
129
173
 
130
174
  # MAIN PROGRAM ================================================================
131
175
 
132
- SVG.new(x: 0.0, y: 0.0, width: opts[:width], height: opts[:height],font_family: "monospaced") do |svg|
176
+ SVG.new(x: 0.0, y: 0.0, width: opts[:width], height: opts[:height],) do |svg|
133
177
  plt = SVG::Plot.new(svg)
134
178
  plt.title = opts[:title];
135
179
  plt.xlabel = opts[:xlabel];
@@ -139,6 +183,14 @@ SVG.new(x: 0.0, y: 0.0, width: opts[:width], height: opts[:height],font_family:
139
183
  plt.miny = opts[:miny] || plt.miny
140
184
  plt.maxy = opts[:maxy] || plt.maxy
141
185
 
186
+ plt.padleft = opts[:lpad]
187
+ plt.padright = opts[:rpad]
188
+ plt.padtop = opts[:tpad]
189
+ plt.padbottom = opts[:bpad]
190
+ plt.legendfontsize = opts[:legendfontsize]
191
+
192
+ plt.xlabelformat = opts[:xaxfmt]
193
+ plt.ylabelformat = opts[:yaxfmt]
142
194
 
143
195
  datavar = []
144
196
  data = []
@@ -161,7 +213,9 @@ SVG.new(x: 0.0, y: 0.0, width: opts[:width], height: opts[:height],font_family:
161
213
  x = data.map{|e| e[0]}
162
214
  data = data.map{|e| e[1]}
163
215
  else
164
- x = (1 .. data.size).to_a.map{|e| e.to_f}
216
+ rng = opts[:xrng] || (0...data.size)
217
+ rngstep = (rng.end - rng.begin).to_f/(data.size-1)
218
+ x = rng.step(rngstep).to_a.map{|e| e}
165
219
  data = data.map{|e| e[0]}
166
220
  end
167
221
 
@@ -172,23 +226,25 @@ SVG.new(x: 0.0, y: 0.0, width: opts[:width], height: opts[:height],font_family:
172
226
  plt.minmax(rawx,rawdata,xlog: opts[:xlog], ylog: opts[:ylog])
173
227
 
174
228
 
175
- yticks,ylabels = plt.yticks(log: opts[:ylog])
229
+ yticks,ylabels = plt.yticks
176
230
  if opts[:boxplot]
177
231
  plt.boxplot(rawdata,ylog: opts[:ylog],labels: opts[:labels])
178
232
  xticks,xlabels = plt.boxplotticks(rawdata.size)
179
233
  xlabels = ["",*opts[:labels],""]
180
234
  plt.axis(xticks,xlabels,stroke: "black",fill: "black",flip: true,rotang: -15) unless opts[:noaxis]
181
235
  else
182
- xticks,xlabels = plt.xticks(log: opts[:xlog])
236
+ xticks,xlabels = plt.xticks
183
237
  rawdata.each_with_index do |y,i|
184
238
  if opts[:scatterplot]
185
239
  plt.scatter(rawx[i],y,
186
240
  ylog: opts[:ylog],
241
+ xlog: opts[:xlog],
187
242
  label: opts[:labels][i],
188
243
  marker: opts[:markers][i])
189
244
  else
190
245
  plt.plot(rawx[i],y,
191
246
  ylog: opts[:ylog],
247
+ xlog: opts[:xlog],
192
248
  label: opts[:labels][i],
193
249
  marker: opts[:markers][i],
194
250
  linestyle: opts[:linestyles][i])
@@ -18,8 +18,8 @@ class SVG
18
18
  return @@markers
19
19
  end
20
20
  @@linestyles = [
21
- "-","--","-.",":","3","10 1","10 5","5 10","5 2 2 5","2 10",
22
- "2 10","5 2 2 5","5 10","10 5","10 1","3",":","-.","--","-"
21
+ "-","--","-.",":","3","10 1","10 5","5 10","5 2 2 5","3 10",
22
+ "3 10","5 2 2 5","5 10","10 5","10 1","3",":","-.","--","-"
23
23
  ] * 2
24
24
  def self.linestyles
25
25
  return @@linestyles
@@ -27,32 +27,13 @@ class SVG
27
27
 
28
28
  attr_accessor :minx,:maxx,:miny,:maxy
29
29
 
30
- def minmax(x,y,xlog: false,ylog: false)
31
- miny,maxy = y.flatten.minmax
32
- @miny = miny unless (@miny && @miny.finite?)
33
- @maxy = maxy unless (@maxy && @maxy.finite?)
34
-
35
- if ylog
36
- @miny = Math.log([1e-12,@miny].max,10)
37
- @maxy = Math.log(@maxy,10)
38
- end
39
- minx,maxx = x.flatten.minmax
40
- @minx = minx unless (@minx && @minx.finite?)
41
- @maxx = maxx unless (@maxx && @maxx.finite?)
42
-
43
- if xlog
44
- @minx = Math.log([1e-12,@minx].max,10)
45
- @maxx = Math.log(@maxx,10)
46
- end
47
- end
48
-
49
30
  attr_accessor :padleft, :padright,:padtop,:padbottom,:colors,:labels,:markers,
50
31
  :linestyles,:opacities,:linewidth
51
32
 
52
33
 
53
34
  attr_accessor :markernum,:markersize
54
35
  attr_accessor :xlabel,:ylabel,:axcolor,:axlinewidth,:axlabelstrokewidth,
55
- :axlabelfontsize
36
+ :axlabelfontsize,:xlabelformat,:ylabelformat
56
37
  attr_accessor :ticksnum,:tickslen,:tickstextlen,:ticksfontsize,:ticksstrokewidth
57
38
  attr_accessor :legendfontsize,:legendbgcolor,:legendbgopacity,:legendfgcolor,
58
39
  :legendlinelen,:legendheight,:legendpad
@@ -64,7 +45,7 @@ class SVG
64
45
  @padleft = 100
65
46
  @padright = 50
66
47
  @padtop = 50
67
- @padbottom = 90
48
+ @padbottom = 100
68
49
  @colors = []
69
50
  @labels = []
70
51
  @markers = []
@@ -115,21 +96,57 @@ class SVG
115
96
  @legendfgcolor = "white"
116
97
  end
117
98
 
99
+ def minmax(x,y,xlog: false,ylog: false)
100
+ y = y.flatten
101
+ if ylog
102
+ y = y
103
+ .map{|e|begin Math.log(e,10) rescue -1.0/0.0 end }
104
+ .reject{|e| e.infinite?}
105
+ end
106
+
107
+ miny,maxy = y.minmax
108
+ @miny = miny unless (@miny && @miny.finite?)
109
+ @maxy = maxy unless (@maxy && @maxy.finite?)
110
+
111
+ x = x.flatten
112
+ if xlog
113
+ x = x
114
+ .map{|e|begin Math.log(e,10) rescue -1.0/0.0 end }
115
+ .reject{|e| e.infinite?}
116
+ end
117
+ minx,maxx = x.minmax
118
+ @minx = minx unless (@minx && @minx.finite?)
119
+ @maxx = maxx unless (@maxx && @maxx.finite?)
120
+ end
121
+
122
+
123
+
118
124
  def self.mapxcoords(x,minx,maxx,w,padleft,padright,log)
119
- begin x = log ? Math.log(x,10) : x rescue x = 1e-12 end
125
+ begin
126
+ x = log ? Math.log(x,10) : x
127
+ rescue
128
+ STDERR.puts "WARNING error execption Math.log(#{x},10)"
129
+ x = -1.0/0.0
130
+ end
120
131
  xnew = padleft + ((x - minx)/((maxx-minx) + 1e-100))*(w-(padleft + padright))
121
132
  end
122
133
 
123
134
  def self.mapycoords(y,miny,maxy,h,padtop,padbottom,log)
124
- begin y = log ? Math.log(y,10) : y rescue y = 1e-12 end
135
+ begin
136
+ y = log ? Math.log(y,10) : y
137
+ rescue
138
+ STDERR.puts "WARNING error execption Math.log(#{y},10)"
139
+ y = -1.0/0.0
140
+ end
125
141
  ynew = padtop + ((maxy - y)/((maxy-miny) + 1e-100))*(h-(padtop + padbottom))
126
142
  end
127
143
 
128
144
  def map_and_zip(xraw,yraw,minx,maxx,miny,maxy,xlog,ylog)
129
145
  xp = xraw.map{|v| Plot.mapxcoords(v,minx,maxx,@svg.width,@padleft,@padright,xlog)}
130
146
  yp = yraw.map{|v| Plot.mapycoords(v,miny,maxy,@svg.height,@padtop,@padbottom,ylog)}
131
-
132
- return xp.zip(yp)
147
+ pts = xp.zip(yp)
148
+ pts = pts.reject{|p| p[0].infinite? || p[1].infinite?}
149
+ return pts
133
150
  end
134
151
 
135
152
  def set_plot_style(label,color,marker,linestyle,opacity)
@@ -149,19 +166,19 @@ class SVG
149
166
  def title
150
167
  @svg.g(id: "title",text_anchor: "middle") do |g|
151
168
  g.text(
152
- x: (@svg.width).to_i / 2, y: @titlefontsize,
169
+ x: @svg.width / 2, y: @titlefontsize,
153
170
  text: @title,
154
171
  font_size: @titlefontsize)
155
172
  end
156
173
  end
157
174
 
158
175
  def legend
159
- legendtextwidth = @legendfontsize*@labels.map{|e| e.size}.max / 2
176
+ legendtextwidth = @legendfontsize*0.75*@labels.map{|e| e.size}.max
160
177
  legendwidth = @legendlinelen + 3*@legendpad + legendtextwidth
161
178
  legendheight = 2*@legendpad + @labels.size*2*@legendfontsize
162
179
 
163
180
  t = SVG.transform do |t|
164
- t.translate(@svg.width-legendwidth-@padright,0)
181
+ t.translate(@svg.width-legendwidth-@padright,@padtop)
165
182
  end
166
183
 
167
184
  @svg.g(id: "legend",transform: t) do |g|
@@ -202,7 +219,11 @@ class SVG
202
219
  def axis(ticks,labels, flip: false, rotang: 0,**args)
203
220
  min,max = ticks[0], ticks[-1]
204
221
  # Dibujar el eje
205
- @svg.g(id: "axis#{flip ? @xlabel : @ylabel}", text_anchor: "middle",stroke: @axcolor, fill: @axcolor,**args) do |axis|
222
+ @svg.g(id: "axis#{flip ? @xlabel : @ylabel}",
223
+ text_anchor: "middle",
224
+ stroke: @axcolor,
225
+ stroke_width: @axlinewidth,
226
+ fill: @axcolor,**args) do |axis|
206
227
  axis.path() do |path|
207
228
  path.m(min[0],min[1])
208
229
  path.l(max[0],max[1])
@@ -211,9 +232,9 @@ class SVG
211
232
  # Mostrar nombre
212
233
  t = SVG.transform do |t|
213
234
  if flip
214
- t.translate(@svg.width/2,@svg.height - @axlabelfontsize/2)
235
+ t.translate(@svg.width/2,@svg.height - @axlabelfontsize)
215
236
  else
216
- t.translate(@axlabelfontsize/2,@svg.height/2)
237
+ t.translate(@axlabelfontsize,@svg.height/2)
217
238
  t.rotate(-90,0,0)
218
239
  end
219
240
  end
@@ -250,18 +271,18 @@ class SVG
250
271
  end
251
272
  end
252
273
 
253
- def yticks(min: @miny,max: @maxy,frmt: @ylabelformat, ticksnum: @ticksnum,log: false)
274
+ def yticks(min: @miny,max: @maxy,frmt: @ylabelformat, ticksnum: @ticksnum)
254
275
  ticks = []
255
276
  labels = []
256
277
 
257
- ticks.push([@padleft,Plot.mapycoords(min,min,max,@svg.height,@padtop,@padbottom,log)])
278
+ ticks.push([@padleft,Plot.mapycoords(min,min,max,@svg.height,@padtop,@padbottom,false)])
258
279
  labels.push(frmt % min)
259
280
  ticksnum.times do |i|
260
281
  v = min + ((max - min) / (ticksnum+1)) * (i+1)
261
- ticks.push([@padleft,Plot.mapycoords(v,min,max,@svg.height,@padtop,@padbottom,log)])
282
+ ticks.push([@padleft,Plot.mapycoords(v,min,max,@svg.height,@padtop,@padbottom,false)])
262
283
  labels.push(frmt % v)
263
284
  end
264
- ticks.push([@padleft,Plot.mapycoords(max,min,max,@svg.height,@padtop,@padbottom,log)])
285
+ ticks.push([@padleft,Plot.mapycoords(max,min,max,@svg.height,@padtop,@padbottom,false)])
265
286
  labels.push(frmt % max)
266
287
  return ticks,labels
267
288
  end
@@ -286,18 +307,18 @@ class SVG
286
307
  return ticks,labels
287
308
  end
288
309
 
289
- def xticks(min: @minx, max: @maxy, ticksnum: @ticksnum, frmt: @xlabelformat,log: false)
310
+ def xticks(min: @minx, max: @maxx, ticksnum: @ticksnum, frmt: @xlabelformat)
290
311
  ticks = []
291
312
  labels = []
292
313
 
293
- ticks.push([Plot.mapxcoords(min,min,max,@svg.height,@padleft,@padright,log),@svg.height-@padbottom])
314
+ ticks.push([Plot.mapxcoords(min,min,max,@svg.height,@padleft,@padright,false),@svg.height-@padbottom])
294
315
  labels.push(frmt % min)
295
316
  ticksnum.times do |i|
296
317
  v = min + ((max - min) / (ticksnum+1)) * (i+1)
297
- ticks.push([Plot.mapxcoords(v,min,max,@svg.height,@padleft,@padright,log),@svg.height-@padbottom])
318
+ ticks.push([Plot.mapxcoords(v,min,max,@svg.height,@padleft,@padright,false),@svg.height-@padbottom])
298
319
  labels.push(frmt % v)
299
320
  end
300
- ticks.push([Plot.mapxcoords(max,min,max,@svg.height,@padleft,@padright,log),@svg.height-@padbottom])
321
+ ticks.push([Plot.mapxcoords(max,min,max,@svg.height,@padleft,@padright,false),@svg.height-@padbottom])
301
322
  labels.push(frmt % max)
302
323
  return ticks,labels
303
324
 
@@ -20,7 +20,7 @@ class SVG
20
20
  stroke_width: @linewidth,
21
21
  opacity: @opacities.last) do |g|
22
22
 
23
- g.path(title: @labels.last,fill: "none",stroke_dasharray: self.linestyle(linestyle)) do |path|
23
+ g.path(title: @labels.last,fill: "none",stroke_dasharray: self.linestyle(@linestyles.last)) do |path|
24
24
  missing = true
25
25
  points.each do |(x,y)|
26
26
  if !x.finite? || !y.finite?
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: svg-lib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ricardo Nieto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-17 00:00:00.000000000 Z
11
+ date: 2020-10-19 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: basic svg-lib ruby implementation
14
14
  email: nifr91@gmail.com