Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

* Allow Line XY charts with differing data series length.

  • Loading branch information...
commit 4d7b5832cf28e32666cb5fd520d60968643d5765 1 parent 815570f
@donv donv authored
Showing with 15 additions and 12 deletions.
  1. +13 −12 lib/gruff/line.rb
  2. +2 −0  test/test_line.rb
View
25 lib/gruff/line.rb
@@ -97,7 +97,7 @@ def dataxy(name, x_data_points=[], y_data_points=[], color=nil)
x_data_points = Array(x_data_points) # make sure it's an array
#append the x data to the last entry that was just added in the @data member
lastElem = @data.length()-1
- @data[lastElem] << x_data_points
+ @data[lastElem][DATA_VALUES_X_INDEX] = x_data_points
# Update the global min/max values for the x data
x_data_points.each_with_index do |x_data_point, index|
@@ -123,7 +123,7 @@ def draw
# Check to see if more than one datapoint was given. NaN can result otherwise.
@x_increment = (@column_count > 1) ? (@graph_width / (@column_count - 1).to_f) : @graph_width
-
+
#normalize the x data if it is specified
@data.each_with_index do |data_row, index|
norm_x_data_points = []
@@ -135,7 +135,7 @@ def draw
@norm_data[index] << norm_x_data_points
end
end
-
+
if (defined?(@norm_baseline)) then
level = @graph_top + (@graph_height - @norm_baseline * @graph_height)
@d = @d.push
@@ -147,24 +147,22 @@ def draw
@d = @d.pop
end
- @norm_data.each_with_index do |data_row, dr_index|
+ @norm_data.each_with_index do |data_row, dr_index|
prev_x = prev_y = nil
@one_point = contains_one_point_only?(data_row)
data_row[DATA_VALUES_INDEX].each_with_index do |data_point, index|
+ next if data_point.nil?
+
x_data = data_row[DATA_VALUES_X_INDEX]
if (x_data == nil)
#use the old method: equally spaced points along the x-axis
new_x = @graph_left + (@x_increment * index)
else
- x_data_row = data_row[DATA_VALUES_X_INDEX]
- x_data_point = x_data_row[index]
- new_x = getXCoord(x_data_point, @graph_width, @graph_left)
+ new_x = getXCoord(x_data[index], @graph_width, @graph_left)
end
- next if data_point.nil?
-
draw_label(new_x, index)
new_y = @graph_top + (@graph_height - data_point * @graph_height)
@@ -203,6 +201,10 @@ def normalize
@norm_baseline = (@baseline_value.to_f / @maximum_value.to_f) if @baseline_value
end
+ def sort_norm_data
+ super unless @data.any?{|d| d[DATA_VALUES_X_INDEX]}
+ end
+
def getXCoord(x_data_point, width, offset)
return(x_data_point * width + offset)
end
@@ -215,10 +217,9 @@ def contains_one_point_only?(data_row)
if one_point
# more than one point, bail
return false
- else
- # there is at least one data point
- return true
end
+ # there is at least one data point
+ one_point = true
end
end
return one_point
View
2  test/test_line.rb
@@ -465,6 +465,8 @@ def test_xy_data_pairs
g.dataxy("Apples", [[1,1], [3,2],[4,3],[5,4],[6,4],[10,3]])
g.dataxy("Bapples", [[1,1],[3,1],[4,2],[5,2],[7,3],[9,3]])
g.data("Capples", [1, 1, 2, 2, 3, 3])
+ g.dataxy("Dapples", [[1,1],[2,3],[5,8],[13,21]])
+ g.dataxy("Eapples", [[1,1],[2,3],[5,8],[13,21],[13,8],[5,3],[2,1],[1,1]])
g.labels = {0 => '2003', 2 => '2004', 4 => '2005'}
g.write('test/output/data_xy_pairs.png')
end
Please sign in to comment.
Something went wrong with that request. Please try again.