Skip to content

Commit f5cfae2

Browse files
committed
added support for block canvas and staircase plot, switch to relative requires
1 parent 6272855 commit f5cfae2

File tree

6 files changed

+81
-32
lines changed

6 files changed

+81
-32
lines changed

example/ex_line_plot.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/env ruby
2+
require_relative "../lib/unicode_plot"
3+
4+
UnicodePlot.lineplot([1, 2, 7], [9, -6, 8], title: "Default Lineplot").render
5+
UnicodePlot.lineplot([1, 2, 7], [9, -6, 8], title: "Ascii Lineplot", canvas: :ascii).render
6+
UnicodePlot.lineplot([1, 2, 7], [9, -6, 8], title: "Dot Lineplot", canvas: :dot).render
7+
UnicodePlot.lineplot([1, 2, 7], [9, -6, 8], title: "Block Lineplot", canvas: :block).render

example/ex_staircase_plot.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212

1313
# Two plots at a time.
1414
# Using an explicit limit because data for the 2nd plot is outside the bounds from the 1st plot
15-
plot = UnicodePlot.stairs([1, 2, 4, 7, 8], [1, 3, 4, 2, 7], title: "Two Staircases", xlim: [0,10] )
15+
plot = UnicodePlot.stairs([1, 2, 4, 7, 8], [1, 3, 4, 2, 7], title: "Two Staircases", xlim: [1,10] )
16+
UnicodePlot.stairs!(plot, [0, 3, 5, 6, 9], [2, 5, 3, 4, 0])
17+
plot.render
18+
19+
plot = UnicodePlot.stairs([1, 2, 4, 7, 8], [1, 3, 4, 2, 7], title: "Two Staircases", xlim: [1,10] , canvas: :block)
1620
UnicodePlot.stairs!(plot, [0, 3, 5, 6, 9], [2, 5, 3, 4, 0])
1721
plot.render
1822

example/staircase_plot.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/env ruby
2+
require_relative "../lib/unicode_plot"
3+
4+
# single plot at a time
5+
UnicodePlot.stairs([1, 2, 4, 7, 8], [1, 3, 4, 2, 7], style: :post).render
6+
7+
# pre: style
8+
UnicodePlot.stairs([1, 2, 4, 7, 8], [1, 3, 4, 2, 7], style: :pre).render
9+
10+
# Another single plot with title
11+
UnicodePlot.stairs([2, 3, 5, 6, 9], [2, 5, 3, 4, 2], title: "My Staircase Plot").render
12+
13+
# Two plots at a time.
14+
# Using an explicit limit because data for the 2nd plot is outside the bounds from the 1st plot
15+
plot = UnicodePlot.stairs([1, 2, 4, 7, 8], [1, 3, 4, 2, 7], title: "Two Staircases", xlim: [0,10] )
16+
UnicodePlot.stairs!(plot, [0, 3, 5, 6, 9], [2, 5, 3, 4, 0])
17+
plot.render
18+
19+

lib/unicode_plot.rb

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
require 'stringio'
22

3-
require 'unicode_plot/version'
3+
require_relative './unicode_plot/version'
44

5-
require 'unicode_plot/utils'
6-
require 'unicode_plot/styled_printer'
7-
require 'unicode_plot/value_transformer'
8-
require 'unicode_plot/renderer'
5+
require_relative './unicode_plot/utils'
6+
require_relative './unicode_plot/styled_printer'
7+
require_relative './unicode_plot/value_transformer'
8+
require_relative './unicode_plot/renderer'
99

10-
require 'unicode_plot/canvas'
11-
require 'unicode_plot/braille_canvas'
12-
require 'unicode_plot/density_canvas'
13-
require 'unicode_plot/lookup_canvas'
14-
require 'unicode_plot/ascii_canvas'
15-
require 'unicode_plot/dot_canvas'
10+
require_relative './unicode_plot/canvas'
11+
require_relative './unicode_plot/braille_canvas'
12+
require_relative './unicode_plot/density_canvas'
13+
require_relative './unicode_plot/lookup_canvas'
14+
require_relative './unicode_plot/ascii_canvas'
15+
require_relative './unicode_plot/dot_canvas'
16+
require_relative './unicode_plot/block_canvas'
1617

17-
require 'unicode_plot/plot'
18-
require 'unicode_plot/grid_plot'
18+
require_relative './unicode_plot/plot'
19+
require_relative './unicode_plot/grid_plot'
20+
21+
require_relative './unicode_plot/barplot'
22+
require_relative './unicode_plot/boxplot'
23+
require_relative './unicode_plot/densityplot'
24+
require_relative './unicode_plot/lineplot'
25+
require_relative './unicode_plot/histogram'
26+
require_relative './unicode_plot/scatterplot'
27+
28+
# new!
29+
require_relative './unicode_plot/stairs'
1930

20-
require 'unicode_plot/barplot'
21-
require 'unicode_plot/boxplot'
22-
require 'unicode_plot/densityplot'
23-
require 'unicode_plot/lineplot'
24-
require 'unicode_plot/histogram'
25-
require 'unicode_plot/scatterplot'

lib/unicode_plot/block_canvas.rb

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# coding: utf-8
2+
23
=begin
34
The `BlockCanvas` is also Unicode-based.
45
It has half the resolution of the `BrailleCanvas`.
@@ -9,18 +10,29 @@
910
using binary operations.
1011
=end
1112

12-
class BlockCanvas < LookupCanvas
13-
14-
BLOCK_SIGNS = [0b1000, 0b0010,
15-
0b0100, 0b0001]
13+
module UnicodePlot
14+
class BlockCanvas < LookupCanvas
15+
X_PIXEL_PER_CHAR = 2
16+
Y_PIXEL_PER_CHAR = 2
17+
18+
def initialize(width, height, fill_char=0, **kw)
19+
super(width, height,
20+
X_PIXEL_PER_CHAR,
21+
Y_PIXEL_PER_CHAR,
22+
fill_char,
23+
**kw)
24+
end
25+
26+
BLOCK_SIGNS = [ [0b1000, 0b0010].freeze,
27+
[0b0100, 0b0001].freeze
28+
].freeze
1629

17-
BLOCK_DECODE = [' ', '▗', '▖', '▄',
18-
'▝', '▐', '▞', '▟',
19-
'▘', '▚', '▌', '▙',
20-
'▀', '▜', '▛', '█' ]
30+
BLOCK_DECODE = [' ', '▗', '▖', '▄',
31+
'▝', '▐', '▞', '▟',
32+
'▘', '▚', '▌', '▙',
33+
'▀', '▜', '▛', '█' ].freeze
2134

22-
def x_pixel_per_char ; 2 ; end
23-
def y_pixel_per_char ; 2 ; end
24-
def lookup_encode ; BLOCK_SIGNS ; end
25-
def lookup_decode ; BLOCK_DECODE ; end
35+
def lookup_encode(x,y) ; BLOCK_SIGNS[x][y] ; end
36+
def lookup_decode(x) ; BLOCK_DECODE[x] ; end
37+
end
2638
end

lib/unicode_plot/canvas.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ def self.create(canvas_type, width, height, **kw)
1010
BrailleCanvas.new(width, height, **kw)
1111
when :density
1212
DensityCanvas.new(width, height, **kw)
13+
when :block
14+
BlockCanvas.new(width, height, **kw)
1315
when :dot
1416
DotCanvas.new(width, height, **kw)
1517
else

0 commit comments

Comments
 (0)