class Fusioncharts::Chart

Attributes

dataFormat[R]
dataSource[R]
height[R]
jsonUrl[R]
options[RW]
renderAt[R]
type[R]
width[R]
xmlUrl[R]

Public Class Methods

new(options=nil) click to toggle source

Constructor

# File lib/fusioncharts/rails/chart.rb, line 11
def initialize(options=nil)
  if options.nil?
    @options = {}
  else
    @options = options
    parse_options
  end
end

Public Instance Methods

dataFormat=(format) click to toggle source

Sets the dataformat for a chart. Valid values are: json / xml

# File lib/fusioncharts/rails/chart.rb, line 43
def dataFormat=(format)
  @dataFormat = format

  setOption('dataFormat', @dataFormat)
end
dataSource=(dataSource) click to toggle source

Set the datasource for the chart. It can take the followinf formats

  1. Ruby Hash

  2. XML string

  3. JSON string

# File lib/fusioncharts/rails/chart.rb, line 60
def dataSource=(dataSource)
  @dataSource = dataSource
  parse_datasource_json
end
height=(height) click to toggle source

Sets the height for a chart

# File lib/fusioncharts/rails/chart.rb, line 28
def height=(height)
  @height = height.to_s

  setOption('height', @height)
end
jsonUrl=(url) click to toggle source

Set the JSON url where data needs to be loaded

# File lib/fusioncharts/rails/chart.rb, line 66
def jsonUrl=(url)
  @jsonUrl = url
end
jsonUrl?() click to toggle source

Returns where the chart needs to load JSON data from a url

# File lib/fusioncharts/rails/chart.rb, line 81
def jsonUrl?
  self.jsonUrl ? true : false
end
render() click to toggle source

Render the chart

# File lib/fusioncharts/rails/chart.rb, line 86
def render
  config = json_escape JSON.generate(self.options)
  dataUrlFormat = self.jsonUrl? ? "json" : ( self.xmlUrl ? "xml" : nil )
  template = File.read(File.expand_path("../../../templates/chart.erb", __FILE__))
  renderer = ERB.new(template)
  raw renderer.result(binding)
end
renderAt=(id) click to toggle source

Set the DOM id where the chart needs to be rendered

# File lib/fusioncharts/rails/chart.rb, line 50
def renderAt=(id)
  @renderAt = id

  setOption('renderAt',  @renderAt)
end
type=(type) click to toggle source

Set the type for a chart

# File lib/fusioncharts/rails/chart.rb, line 35
def type=(type)
  @type = type

  setOption('type', @type)
end
width=(width) click to toggle source

Sets the width for a chart

# File lib/fusioncharts/rails/chart.rb, line 21
def width=(width)
  @width = width.to_s

  setOption('width', @width)
end
xmlUrl=(url) click to toggle source

Set the XML url where data needs to be loaded

# File lib/fusioncharts/rails/chart.rb, line 71
def xmlUrl=(url)
  @xmlUrl = url
end
xmlUrl?() click to toggle source

Returns where the chart needs to load XML data from a url

# File lib/fusioncharts/rails/chart.rb, line 76
def xmlUrl?
  self.xmlUrl ? true : false
end

Private Instance Methods

json_escape(str) click to toggle source

Escape tags in json, if avoided might be vulnerable to XSS

# File lib/fusioncharts/rails/chart.rb, line 122
def json_escape(str)
  str.to_s.gsub('/', '\/')
end
parse_datasource_json() click to toggle source

Helper method to convert json string to Ruby hash

# File lib/fusioncharts/rails/chart.rb, line 103
def parse_datasource_json
  @dataFormat = "json" unless defined? @dataFormat

  if !xmlUrl? or !jsonUrl?
    @dataSource = JSON.parse(@dataSource) if @dataSource.is_a? String and @dataFormat == "json"
  end

  setOption('dataSource', @dataSource)
end
parse_options() click to toggle source

Helper method that converts the constructor params into instance variables

# File lib/fusioncharts/rails/chart.rb, line 114
def parse_options
  keys = @options.keys

  keys.each{ |k| instance_variable_set "@#{k}".to_sym, @options[k] if self.respond_to? k }
  parse_datasource_json
end
setOption(key, value) click to toggle source

Helper method to add property to the options hash

# File lib/fusioncharts/rails/chart.rb, line 96
def setOption(key, value)
  self.options[key] = value

  return self
end