0
@@ -22,6 +22,10 @@ class Gruff::Line < Gruff::Base
0
# Hide parts of the graph to fit more datapoints, or for a different appearance.
0
attr_accessor :hide_dots, :hide_lines
0
+ #accessors for support of xy data
0
+ attr_accessor :minimum_x_value
0
+ attr_accessor :maximum_x_value
0
# Call with target pixel width of graph (800, 400, 300), and/or 'false' to omit lines (points only).
0
@@ -43,6 +47,65 @@ class Gruff::Line < Gruff::Base
0
@hide_dots = @hide_lines = false
0
@baseline_color = 'red'
0
+ @maximum_x_value = nil
0
+ @minimum_x_value = nil
0
+ # This method allows one to plot a dataset with both X and Y data.
0
+ # Parameters are as follows:
0
+ # name: string, the title of the dataset
0
+ # x_data_points: an array containing the x data points for the graph
0
+ # y_data_points: an array containing the y data points for the graph
0
+ # color: hex number indicating the line color as an RGB triplet
0
+ # -if (x_data_points.length != y_data_points.length) an error is
0
+ # -if the color argument is nil, the next color from the default theme will
0
+ # -if you want to use a preset theme, you must set it before calling
0
+ # g.title = "X/Y Dataset"
0
+ # g.dataxy("Apples", [1,3,4,5,6,10], [1, 2, 3, 4, 4, 3])
0
+ # g.dataxy("Bapples", [1,3,4,5,7,9], [1, 1, 2, 2, 3, 3])
0
+ # #you can still use the old data method too if you want:
0
+ # g.data("Capples", [1, 1, 2, 2, 3, 3])
0
+ # #labels will be drawn at the x locations of the 1st dataset that you
0
+ # #passed in. In this example the lables are drawn at x locations 1,4,6
0
+ # g.labels = {0 => '2003', 2 => '2004', 4 => '2005'} #labels
0
+ def dataxy(name, x_data_points=[], y_data_points=[], color=nil)
0
+ raise ArgumentError, "x_data_points is nil!" if x_data_points.length == 0
0
+ raise ArgumentError, "x_data_points.length != y_data_points.length!" if x_data_points.length != y_data_points.length
0
+ #call the existing data routine for the y data.
0
+ self.data(name, y_data_points, color)
0
+ x_data_points = Array(x_data_points) # make sure it's an array
0
+ #append the x data to the last entry that was just added in the @data member
0
+ lastElem = @data.length()-1
0
+ @data[lastElem] << x_data_points
0
+ # Update the global min/max values for the x data
0
+ x_data_points.each_with_index do |x_data_point, index|
0
+ next if x_data_point.nil?
0
+ # Setup max/min so spread starts at the low end of the data points
0
+ if @maximum_x_value.nil? && @minimum_x_value.nil?
0
+ @maximum_x_value = @minimum_x_value = x_data_point
0
+ @maximum_x_value = (x_data_point > @maximum_x_value) ?
0
+ x_data_point : @maximum_x_value
0
+ @minimum_x_value = (x_data_point < @minimum_x_value) ?
0
+ x_data_point : @minimum_x_value
0
@@ -50,9 +113,20 @@ class Gruff::Line < Gruff::Base
0
return unless @has_data
0
- # Check to see if more than one datapoint was given. NaN can result otherwise.
0
@x_increment = (@column_count > 1) ? (@graph_width / (@column_count - 1).to_f) : @graph_width
0
+ #normalize the x data if it is specified
0
+ @data.each_with_index do |data_row, index|
0
+ norm_x_data_points = []
0
+ if (data_row[DATA_VALUES_X_INDEX] != nil)
0
+ data_row[DATA_VALUES_X_INDEX].each do |x_data_point|
0
+ norm_x_data_points << ( (x_data_point.to_f - @minimum_x_value.to_f ) /
0
+ (@maximum_x_value.to_f - @minimum_x_value.to_f) )
0
+ @norm_data[index] << norm_x_data_points
0
if (defined?(@norm_baseline)) then
0
level = @graph_top + (@graph_height - @norm_baseline * @graph_height)
0
@@ -64,11 +138,20 @@ class Gruff::Line < Gruff::Base
0
- @norm_data.each
do |data_row|
0
+ @norm_data.each
_with_index do |data_row, dr_index|
0
- data_row[1].each_with_index do |data_point, index|
0
- new_x = @graph_left + (@x_increment * index)
0
+ data_row[DATA_VALUES_INDEX].each_with_index do |data_point, index|
0
+ x_data = data_row[DATA_VALUES_X_INDEX]
0
+ #use the old method: equally spaced points along the x-axis
0
+ new_x = @graph_left + (@x_increment * index)
0
+ x_data_row = data_row[DATA_VALUES_X_INDEX]
0
+ x_data_point = x_data_row[index]
0
+ new_x = getXCoord(x_data_point, @graph_width, @graph_left)
0
next if data_point.nil?
0
draw_label(new_x, index)
0
@@ -102,4 +185,8 @@ class Gruff::Line < Gruff::Base
0
@norm_baseline = (@baseline_value.to_f / @maximum_value.to_f) if @baseline_value
0
+ def getXCoord(x_data_point, width, offset)
0
+ return(x_data_point * width + offset)
Comments
No one has commented yet.