Skip to content

Line graph: reset previous point between dataxy series to prevent unwanted poly‑line connection#665

Merged
Watson1978 merged 3 commits intotopfunky:masterfrom
katty0324:codex/find-code-to-separate-series-in-line-graph
Jul 23, 2025
Merged

Line graph: reset previous point between dataxy series to prevent unwanted poly‑line connection#665
Watson1978 merged 3 commits intotopfunky:masterfrom
katty0324:codex/find-code-to-separate-series-in-line-graph

Conversation

@katty0324
Copy link
Copy Markdown
Contributor

What problem does this PR solve?

When a Gruff::Line graph is built with multiple dataxy series, the last
point of series N is drawn as a straight line to the first point of
series N + 1.
The result is a single, continuous poly‑line instead of visually separated
series, which is misleading for end‑users. :contentReference[oaicite:0]{index=0}

How to reproduce

require 'gruff'

g = Gruff::Line.new(600)
g.title = 'Gruff Line #dataxy connection bug demo'

# Series‑1: rising line
g.dataxy('Series 1', [0, 1, 2, 3], [1, 2, 3, 4])

# Series‑2: falling line – should start a new poly‑line,
# but is currently connected to the last point of Series‑1.
g.dataxy('Series 2', [0, 1, 2, 3], [3, 2, 1, 0])

g.write('before_fix.png')
line_series_connection_1

Current output:
A “V”‑shaped line connecting (3,4) → (0,3) instead of two independent lines.
Expected output:
Two distinct polylines that do not share a segment.

line_series_connection_2

Root cause

Inside Gruff::Line#draw_graph, the helper variables prev_x and prev_y
cache the coordinates of the previously plotted point.
They are initialised once before the outer loop that iterates over each
data_row (series).
Consequently, the starting point of the next series inherits the last point of
the previous series.

What does this PR change?

A single line resets the cache at the top of the loop:

 store.norm_data.each do |data_row|
+  prev_x = prev_y = nil
   data_row.coordinates.each_with_index do |(x_data, y_data), index|

By setting prev_x and prev_y to nil for every new series, the drawing
logic starts a fresh poly‑line and no longer links series together.

Impact / backwards compatibility

  • Only affects Gruff::Line with the dataxy API.
  • No public API changes.

Thanks for maintaining Gruff! Any feedback is welcome.

@Watson1978
Copy link
Copy Markdown
Collaborator

Thanks

@Watson1978 Watson1978 merged commit e51f38c into topfunky:master Jul 23, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants