class DYI::Chart::Base

Base class of all the chart classes. @abstract @since 0.0.0

Constants

DEFAULT_CHART_COLOR

Attributes

canvas[R]

Returns the canvas of the image body. @return [Canvas] the canvas of the image body

data[R]

Returns the data for the chart. @return [Chart::ArrayReader] the data for the chart

options[R]

@private

Public Class Methods

new(width, height, options={}) click to toggle source

@param [Length] width width of the chart image @param [Length] height height of the chart image @param [Hash{Symbol => Object}] options the options to creat the chart

image. See <em>Instance Attribute</em> of the each chart class
({Base}, {PieChart}, {LineChart}, etc...).
# File lib/dyi/chart/base.rb, line 254
def initialize(width, height, options={})
  @canvas = Canvas.new(width, height)
  @options = {}
  options.each do |key, value|
    __send__("#{key}=", value) if respond_to?("#{key}=")
  end
end

Public Instance Methods

clear_real_size() click to toggle source

Clears real size of the chart image, and sets chart size as values of width and height properties. See {#set_real_size}, {#width}, {#height}, {Canvas#real_width} and {Canvas#real_height}.

# File lib/dyi/chart/base.rb, line 297
def clear_real_size
  @canvas.real_width = nil
  @canvas.real_height = nil
end
height() click to toggle source

Returns height of the chart image on user unit. @return [Length] height of the chart image on user unit

# File lib/dyi/chart/base.rb, line 276
def height
  @canvas.height
end
height=(height) click to toggle source

Sets height of the chart image on user unit. @param [Length] height height of the chart image on user unit

# File lib/dyi/chart/base.rb, line 282
def height=(height)
  @canvas.height = height
end
load_data(reader) click to toggle source

Loads the data, and creates chart image. @param [ArrayReader] reader the ArrayReader or its sub class that has

the data of the chart
# File lib/dyi/chart/base.rb, line 305
def load_data(reader)
  @data = reader
  create_vector_image
end
puts_in_io(format=nil, io=$>, options={}) click to toggle source

Outputs the chart image to IO stream. @param [Symbol] format the file format. Supports the following formats:

[+:svg+] SVG (Scalable Vector Graphics). If +format+ equals nil,
         output SVG format.
[+:eps+] EPS (Encapsulated Post Script).
[+:xaml+] XAML (Extensible Application Markup Language).
[+:emf+] EMF (Enhanced Metafile). Using _IronRuby_ only.
[+:png+] PNG (Portable Network Graphics). _librsvg_ must have been
         installed on the system.

@param [IO] io the io which the chart image is outputed to @option options [Boolean] :inline_mode true if outputs the inlime-mode, false

otherwise. _SVG_ format only.
# File lib/dyi/chart/base.rb, line 339
def puts_in_io(format=nil, io=$>, options={})
  @canvas.puts_in_io(format, io, options)
end
save(file_name, format=nil, options={}) click to toggle source

Save the chart image as a file. @param [String] file_name the file name which is saved the chart image

as

@param [Symbol] format the file format. Supports the following formats:

[+:svg+] SVG (Scalable Vector Graphics). If +format+ equals nil,
         output SVG format.
[+:eps+] EPS (Encapsulated Post Script).
[+:xaml+] XAML (Extensible Application Markup Language).
[+:emf+] EMF (Enhanced Metafile). Using _IronRuby_ only.
[+:png+] PNG (Portable Network Graphics). _librsvg_ must have been
         installed on the system.

@option options [Boolean] :inline_mode true if outputs the inlime-mode, false

otherwise. _SVG_ format only.
# File lib/dyi/chart/base.rb, line 323
def save(file_name, format=nil, options={})
  @canvas.save(file_name, format, options)
end
set_real_size(width, height) click to toggle source

Sets size of the chart image. @param [Length] width width of the chart image @param [Length] height height of the chart image

# File lib/dyi/chart/base.rb, line 289
def set_real_size(width, height)
  @canvas.real_width = Length.new(width)
  @canvas.real_height = Length.new(height)
end
string(format=nil, options={}) click to toggle source

Outputs the chart image as a String (binary). @param [Symbol] format the file format. Supports the following formats:

[+:svg+] SVG (Scalable Vector Graphics). If +format+ equals nil,
         output SVG format.
[+:eps+] EPS (Encapsulated Post Script).
[+:xaml+] XAML (Extensible Application Markup Language).
[+:emf+] EMF (Enhanced Metafile). Using _IronRuby_ only.
[+:png+] PNG (Portable Network Graphics). _librsvg_ must have been
         installed on the system.

@option options [Boolean] :inline_mode true if outputs the inlime-mode, false

otherwise. _SVG_ format only.
# File lib/dyi/chart/base.rb, line 354
def string(format=nil, options={})
  @canvas.string(format, options)
end
width() click to toggle source

Returns width of the chart image on user unit. @return [Length] width of the chart image on user unit

# File lib/dyi/chart/base.rb, line 264
def width
  @canvas.width
end
width=(width) click to toggle source

Sets width of the chart image on user unit. @param [Length] width width of the chart image on user unit

# File lib/dyi/chart/base.rb, line 270
def width=(width)
  @canvas.width = width
end

Private Instance Methods

chart_color(index) click to toggle source
# File lib/dyi/chart/base.rb, line 364
def chart_color(index)
  if data.has_field?(:color)
    color = Color.new_or_nil(data.records[index].color)
  end
  if color.nil? && respond_to?(:chart_colors) && chart_colors
    color = chart_colors[index]
  end
  color || Color.new(DEFAULT_CHART_COLOR[index % DEFAULT_CHART_COLOR.size])
end
create_vector_image() click to toggle source

@since 1.0.0

# File lib/dyi/chart/base.rb, line 375
def create_vector_image
  @canvas.add_css_class(canvas_css_class) if canvas_css_class && !canvas_css_class.empty?
  @canvas.add_script(script_body) if script_body && !script_body.empty?
  @canvas.add_stylesheet(css_body) if css_body && !css_body.empty?
  @canvas.metadata = data if output_chart_data?
  script_files && script_files.each do |script_file|
    @canvas.reference_script_file(script_file)
  end
  css_files && css_files.each do |css_file|
    @canvas.reference_stylesheet_file(css_file)
  end
  xsl_files && xsl_files.each do |xsl_file|
    @canvas.reference_stylesheet_file(xsl_file, 'text/xsl')
  end
  brush = Drawing::Brush.new
  brush.opacity = background_image_opacity if background_image_opacity != 1.0
  if background_image_url
    brush.import_image(canvas, [0, 0], width, height, background_image_url)
  end 
  if background_image_file[:path]
    brush.draw_image(canvas, [0, 0], width, height, background_image_file[:path], :content_type=>background_image_file[:content_type])
  end
end