Class: RubyPost::GraphData

Inherits:
GraphFile show all
Defined in:
lib/graph.rb

Overview

set the x and y coordinates arrays independently This uses temp files in order to store and create the graph. Graphs created with this type of data need to be compiled directly to postscript using RubyPost::File#compile_to_ps unless you want to copy all of the temporay files too!

Constant Summary collapse

@@graph_data_temp_file =

class variable to store the number of the temporary files used and keep the filenames separate

0

Instance Attribute Summary collapse

Attributes inherited from GraphFile

#filename

Attributes inherited from BaseDrawCommand

#drawable

Instance Method Summary collapse

Methods inherited from BaseGraphData

#add_coords, #add_grid, #add_label, #add_post_command, #add_pre_command, #add_range, #compile_post_commands, #compile_pre_commands

Methods inherited from BaseDrawCommand

#add_option, #color, #colour, #compile_options, #rotate, #scale, #set_drawable, #translate

Constructor Details

#initialize(x = Array.new, y = Array.new) ⇒ GraphData

must make a copy of the arrays as it will be compiled at some latter time and the referenced memory may not be around then!



140
141
142
143
144
145
146
# File 'lib/graph.rb', line 140

def initialize(x=Array.new,y=Array.new)
  super()
  @x = Array.new(x)
  @y = Array.new(y)
  @filename = 'temp_graph_data_' + @@graph_data_temp_file.to_s + '.txt'
  @@graph_data_temp_file = @@graph_data_temp_file+1
end

Instance Attribute Details

#x=(value) ⇒ Object (writeonly)

Sets the attribute x

Parameters:

  • value

    the value to set the attribute x to.



131
132
133
# File 'lib/graph.rb', line 131

def x=(value)
  @x = value
end

#y=(value) ⇒ Object (writeonly)

Sets the attribute y

Parameters:

  • value

    the value to set the attribute y to.



131
132
133
# File 'lib/graph.rb', line 131

def y=(value)
  @y = value
end

Instance Method Details

#compileObject

creates the tempory file with the graph data and send this to the metapost file. Also note that we force scientific notation for the number format as it is ,most compatible with metapost graph.



151
152
153
154
155
156
157
# File 'lib/graph.rb', line 151

def compile
  min = [@x.length, @y.length].min
  IO::File.open(@filename, 'w') do |f|
    min.times { |i| f.puts sprintf("%e", @x[i]) + ' ' + sprintf("%e", @y[i]) }
  end
  "\"" + @filename + "\" " + compile_options + ";"
end