Skip to content

Commit c47d643

Browse files
committed
adding support for bubble plots
1 parent f781310 commit c47d643

4 files changed

Lines changed: 53 additions & 5 deletions

File tree

lib/rubyplot/artist/axes.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ def area! *args, &block
146146
end
147147

148148
def bubble! *args, &block
149-
add_plot "Bubble", *args, &block
149+
plot = Rubyplot::Artist::Plot::Bubble.new self
150+
yield(plot) if block_given?
151+
@plots << plot
150152
end
151153

152154
def dot! *args, &block

lib/rubyplot/artist/circle.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@ module Rubyplot
22
module Artist
33
class Circle < Base
44
def initialize(owner, abs_x:, abs_y:, radius: , stroke_opacity: 0.0,
5-
color: '#000000', stroke_width:)
5+
color: :default, stroke_width:)
66
super(owner.backend, abs_x, abs_y)
77
@owner = owner
88
@radius = radius
99
@stroke_width = stroke_width
1010
@stroke_opacity = stroke_opacity
1111
@color = color
12-
@backend = @owner.backend
1312
end
1413

1514
def draw
1615
@backend.draw_circle(
17-
x: @abs_x, y: @abs_y, radius: @radius, stroke_opacity: @stroke_opacity,
18-
stroke_width: @stroke_width, color: @color
16+
x: @abs_x,
17+
y: @abs_y,
18+
radius: @radius,
19+
stroke_opacity: @stroke_opacity,
20+
stroke_width: @stroke_width,
21+
color: Rubyplot::Color::COLOR_INDEX[@color]
1922
)
2023
end
2124
end # class Circle

lib/rubyplot/artist/plot.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
require_relative 'plot/line'
44
require_relative 'plot/bar'
55
require_relative 'plot/multi_bars'
6+
require_relative 'plot/bubble'

lib/rubyplot/artist/plot/bubble.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module Rubyplot
2+
module Artist
3+
module Plot
4+
class Bubble < Artist::Plot::Base
5+
# Width in pixels of the border of each bubble.
6+
attr_reader :stroke_width
7+
attr_reader :z_max, :z_min
8+
9+
def initialize(*)
10+
super
11+
@bubbles = []
12+
@stroke_width = 1.0
13+
end
14+
15+
def data x_values, y_values, z_values
16+
super(x_values, y_values)
17+
@data[:z_values] = z_values
18+
@z_max = @data[:z_values].max
19+
@z_min = @data[:z_values].min
20+
end
21+
22+
def draw
23+
@normalized_data[:y_values].each_with_index do |iy, idx_y|
24+
ix = @normalized_data[:x_values][idx_y]
25+
iz = @data[:z_values][idx_y]
26+
abs_x = ix * @axes.x_axis.length + @axes.abs_x + @axes.y_axis_margin
27+
abs_y = (@axes.y_axis.length - iy * @axes.y_axis.length) + @axes.abs_y
28+
@bubbles << Rubyplot::Artist::Circle.new(
29+
self,
30+
abs_x: abs_x,
31+
abs_y: abs_y,
32+
radius: iz,
33+
color: @data[:color],
34+
stroke_width: @stroke_width
35+
)
36+
end
37+
@bubbles.each(&:draw)
38+
end
39+
end # class Bubble
40+
end # module Plot
41+
end # module Artist
42+
end # module Rubyplot

0 commit comments

Comments
 (0)