Skip to content

Commit efe870b

Browse files
committed
finished separated line plots
1 parent 2c86f52 commit efe870b

9 files changed

Lines changed: 29 additions & 13 deletions

File tree

lib/rubyplot.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require 'rmagick'
44

55
require 'rubyplot/color'
6+
require 'rubyplot/utils'
67
require 'rubyplot/version'
78
require 'rubyplot/themes'
89
require 'rubyplot/artist'

lib/rubyplot/artist/axis/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def initialize axes, title, min_val, max_val
1616

1717
def draw
1818
Rubyplot::Artist::Line2D.new(
19-
self, x1: @x1, y1: @y1, x2: @x2, y2: @y2, stroke_width: 2.0)
19+
self, x1: @x1, y1: @y1, x2: @x2, y2: @y2, stroke_width: 2.0).draw
2020
end
2121
end # class Base
2222
end # class Axis

lib/rubyplot/artist/axis/x_axis.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def populate_major_x_ticks
3131
@axes,
3232
x: count * @major_ticks_distance + @x1,
3333
y: @y1,
34-
label: (count * value_distance).to_s,
34+
label: (count * value_distance),
3535
length: 6,
3636
label_distance: 10
3737
)

lib/rubyplot/artist/line2d.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ def initialize(owner,x1:,y1:,x2:,y2:,color: '#000000',
99
@x2 = x2
1010
@y2 = y2
1111
@color = color
12+
@stroke_opacity = stroke_opacity
13+
@stroke_width = stroke_width
1214
@backend = @owner.backend
1315
end
1416

1517
def draw
16-
@backend.draw_line(x1: @x1, y1: @y1, x2: @x2, y2: @y2, color: @color)
18+
@backend.draw_line(x1: @x1, y1: @y1, x2: @x2, y2: @y2, color: @color,
19+
stroke_opacity: @stroke_opacity, stroke_width: @stroke_width)
1720
end
1821
end # class Line2D
1922
end # class Artist

lib/rubyplot/artist/plot/line.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ class Line < Artist::Plot::Base
77

88
def initialize(*)
99
super
10-
@hide_lines = false
11-
10+
@hide_lines = false
1211
end
1312

1413
def data x_values, y_values=[]
@@ -33,13 +32,14 @@ def draw_single_point
3332
end
3433

3534
def draw_lines
35+
prev_x = prev_y = nil
3636
@normalized_data[:x_values].each_with_index do |ix, idx_ix|
37-
prev_x = prev_y = nil
3837
iy = @normalized_data[:y_values][idx_ix]
3938
new_x = ix * @axes.graph_width + @axes.graph_left
4039
new_y = @axes.graph_top + (@axes.graph_height - iy * @axes.graph_height)
41-
unless prev_x.nil? && prev_y.nil?
40+
if !(prev_x.nil? && prev_y.nil?)
4241
Rubyplot::Artist::Line2D.new(
42+
self,
4343
x1: prev_x,
4444
y1: prev_y,
4545
x2: new_x,

lib/rubyplot/artist/tick/base.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ class Base
1717
# @param label_distance [Integer] Distance between the label and tick.
1818
# @param tick_opacity [Float] Number describing the opacity of the tick drawn. 0-1.0.
1919
def initialize(owner,x:,y:,length:,label:,label_distance:,
20-
tick_opacity: 1.0,tick_width: 3.0)
20+
tick_opacity: 1.0,tick_width: 1.0)
2121
@owner = owner
2222
@x = x
2323
@y = y
2424
@length = length
25-
@label_text = label
25+
@label_text = Rubyplot::Utils.format_label label
2626
@label_distance = label_distance
2727
@tick_opacity = tick_opacity
2828
@tick_width = tick_width

lib/rubyplot/magick_wrapper/artist.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ def draw_line_markers!
444444
(0..@geometry.marker_count).each do |index|
445445
y = @graph_top + @graph_height - index.to_f * @geometry.increment_scaled
446446
y_next = @graph_top + @graph_height - (index.to_f + 1) * @geometry.increment_scaled
447-
@d = @d.fill(@marker_color)
447+
@d = @d.fill(@marker_color)
448448
@d = @d.line(@graph_left, y, @graph_right, y) if
449449
!@geometry.hide_line_markers || (index == 0)
450450
# If the user specified a marker shadow color, draw a shadow just below it

lib/rubyplot/utils.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module Rubyplot
2+
module Utils
3+
THOUSAND_SEPARATOR = ','
4+
class << self
5+
def format_label label
6+
if label.is_a? Float
7+
format('%0.2f', label)
8+
end
9+
end
10+
end
11+
end # module Utils
12+
end # module Rubyplot

spec/axes_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,11 @@
275275

276276
end
277277

278-
it "makes a simple line plot", focus: true do
278+
it "makes a simple line plot" do
279279
fig = Rubyplot::Figure.new
280280
axes = fig.add_subplot 0,0
281281
axes.line! do |p|
282-
p.data [20, 23, 19, 8]
282+
p.data [5, 8, 13, 15]
283283
p.label = "Marco"
284284
p.color = :blue
285285
end
@@ -652,7 +652,7 @@
652652
# FileUtils.rm_rf SPEC_ROOT + "temp/scatter"
653653
end
654654

655-
it "adds a simple scatter plot." do
655+
it "adds a simple scatter plot.", focus: true do
656656
fig = Rubyplot::Figure.new
657657
axes = fig.add_subplot 0,0
658658
axes.scatter!(400) do |p|

0 commit comments

Comments
 (0)