Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/gruff/line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def data(name, data_points = [], color = nil)
# @rbs x_data_points: Array[nil | Float | Integer] | Array[[nil | Float | Integer, nil | Float | Integer]] | nil
# @rbs y_data_points: Array[nil | Float | Integer] | nil | String
# @rbs color: String
def dataxy(name, x_data_points = [], y_data_points = [], color = nil)
def dataxy(name, x_data_points = [], y_data_points = [], color = nil, line_width = nil)
# make sure it's an array
x_data_points = Array(x_data_points)

Expand All @@ -174,7 +174,7 @@ def dataxy(name, x_data_points = [], y_data_points = [], color = nil)
raise ArgumentError, 'x_data_points.length != y_data_points.length!' if x_data_points.length != y_data_points.length # steep:ignore

# call the existing data routine for the x/y data.
store.add(name, x_data_points, y_data_points, color)
store.add(name, x_data_points, y_data_points, color, line_width)
end

private
Expand Down Expand Up @@ -231,6 +231,8 @@ def draw_graph
circle_radius = @dot_radius || clip_value_if_greater_than(@columns / (store.norm_data.first.y_points.size * 2.5), 5.0)

store.norm_data.each do |data_row|
data_row_stroke_width = stroke_width
data_row_stroke_width = data_row.line_width if data_row.respond_to?(:line_width) && data_row.line_width
prev_x = prev_y = nil
data_row.coordinates.each_with_index do |(x_data, y_data), index|
new_x = begin
Expand All @@ -253,13 +255,13 @@ def draw_graph
new_y = @graph_top + (@graph_height - (y_data * @graph_height))

if contains_one_point_only?(data_row) || !@hide_dots
Gruff::Renderer::Dot.new(renderer, @dot_style, color: data_row.color, width: stroke_width).render(new_x, new_y, circle_radius)
Gruff::Renderer::Dot.new(renderer, @dot_style, color: data_row.color, width: data_row_stroke_width).render(new_x, new_y, circle_radius)
end
if !@hide_lines && prev_x && prev_y
# Renderer::Polyline may cause unknown lines to be drawn with complex graphs.
# Probably it is related to ImageMagick behavior.
# To avoid this problem, we use the Renderer::Line instead.
Gruff::Renderer::Line.new(renderer, color: data_row.color, width: stroke_width)
Gruff::Renderer::Line.new(renderer, color: data_row.color, width: data_row_stroke_width)
.render(prev_x, prev_y, new_x, new_y)
end
prev_x = new_x
Expand Down
7 changes: 5 additions & 2 deletions lib/gruff/store/xy_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ class XYData
attr_accessor :x_points #: Array[nil | Float | Integer]
attr_accessor :y_points #: Array[nil | Float | Integer]
attr_accessor :color #: String
attr_accessor :line_width #: Integer

# @rbs label: String | Symbol
# @rbs x_points: Array[nil | Float | Integer] | nil
# @rbs y_points: Array[nil | Float | Integer] | nil
# @rbs color: String
def initialize(label, x_points, y_points, color)
# @rbs line_width: Integer
def initialize(label, x_points, y_points, color, line_width = nil)
y_points = Array(y_points)
x_points = x_points ? Array(x_points) : Array.new(y_points.length)
raise ArgumentError, 'x_points.length != y_points.length!' if x_points.length != y_points.length
Expand All @@ -24,6 +26,7 @@ def initialize(label, x_points, y_points, color)
@x_points = x_points
@y_points = y_points
@color = color
@line_width = line_width
end

alias points y_points
Expand Down Expand Up @@ -82,7 +85,7 @@ def normalize(minimum_x:, minimum_y:, spread_x:, spread_y:)
y.nil? ? nil : (y.to_f - minimum_y.to_f) / spread_y
end

self.class.new(label, norm_x_points, norm_y_points, color)
self.class.new(label, norm_x_points, norm_y_points, color, line_width)
end
end
end
Expand Down
Loading