Class: Charty::RenderContext

Inherits:
Object
  • Object
show all
Defined in:
lib/charty/plotter.rb

Defined Under Namespace

Classes: Configurator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, **args, &block) ⇒ RenderContext

Returns a new instance of RenderContext


160
161
162
163
164
165
166
# File 'lib/charty/plotter.rb', line 160

def initialize(method, **args, &block)
  @method = method
  configurator = Configurator.new(**args)
  configurator.instance_eval &block
  # TODO: label も外から付けられた方がよさそう
  (@range, @series, @function, @data, @title, @xlabel, @ylabel, @labels) = configurator.to_a
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data


158
159
160
# File 'lib/charty/plotter.rb', line 158

def data
  @data
end

#functionObject (readonly)

Returns the value of attribute function


158
159
160
# File 'lib/charty/plotter.rb', line 158

def function
  @function
end

#labelsObject (readonly)

Returns the value of attribute labels


158
159
160
# File 'lib/charty/plotter.rb', line 158

def labels
  @labels
end

#methodObject (readonly)

Returns the value of attribute method


158
159
160
# File 'lib/charty/plotter.rb', line 158

def method
  @method
end

#rangeObject (readonly)

Returns the value of attribute range


158
159
160
# File 'lib/charty/plotter.rb', line 158

def range
  @range
end

#seriesObject (readonly)

Returns the value of attribute series


158
159
160
# File 'lib/charty/plotter.rb', line 158

def series
  @series
end

#titleObject (readonly)

Returns the value of attribute title


158
159
160
# File 'lib/charty/plotter.rb', line 158

def title
  @title
end

#xlabelObject (readonly)

Returns the value of attribute xlabel


158
159
160
# File 'lib/charty/plotter.rb', line 158

def xlabel
  @xlabel
end

#ylabelObject (readonly)

Returns the value of attribute ylabel


158
159
160
# File 'lib/charty/plotter.rb', line 158

def ylabel
  @ylabel
end

Instance Method Details

#apply(plotter_adapter) ⇒ Object


239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/charty/plotter.rb', line 239

def apply(plotter_adapter)
  case
    when !@series.empty?
      plotter_adapter.series = @series
    when @function
      linspace = Linspace.new(@range[:x], 100)
      # TODO: set label with function
      # TODO: set ys to xs when gruff curve with function
      @series << Series.new(linspace.to_a, linspace.map{|x| @function.call(x) }, label: "function" )
  end

  @plotter_adapter = plotter_adapter
  self
end

#range_xObject


223
224
225
# File 'lib/charty/plotter.rb', line 223

def range_x
  @range[:x]
end

#range_yObject


227
228
229
# File 'lib/charty/plotter.rb', line 227

def range_y
  @range[:y]
end

#render(filename = nil) ⇒ Object


231
232
233
# File 'lib/charty/plotter.rb', line 231

def render(filename=nil)
  @plotter_adapter.render(self, filename)
end

#save(filename = nil) ⇒ Object


235
236
237
# File 'lib/charty/plotter.rb', line 235

def save(filename=nil)
  @plotter_adapter.save(self, filename)
end