Class: Ziya::Charts::Base

Inherits:
Object
  • Object
show all
Includes:
Helpers::BaseHelper
Defined in:
lib/ziya/charts/base.rb

Overview

The mother ship of all charts. This class figures out how to generate the correct xml to render the chart on the client side. This class handles loading style sheets and chart helper for the helper directory specified during initialization.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::BaseHelper

#chart, #chart_url, #clazz, #component, #dial, #dials, #drawing, #gauge, #indent

Methods included from Utils::Text

#camelize, #classify, #demodulize, #underscore

Methods included from Helper

#block_is_within_action_view?, #capture_block, #chart_path, #charts_swf, #class_id, #codebase, #composite_url, #content_tag, #content_tag_string, #escape_chars, #escape_once, #escape_url, #gauge_path, #gauges_swf, #gen_composite_path, #gen_swf_path, #generate_flash_tag, #generate_old_style_flash_tag, #mime, #plugin_url, #setup_movie_size, #setup_wmode, #tag, #tag_options, #ziya_chart, #ziya_chart_js, #ziya_gauge, #ziya_javascript_include_tag

Constructor Details

#initialize(license = nil, chart_id = nil) ⇒ Base

create a new chart.

:license

the XML/SWF charts license

:chart_id

the name of the chart style sheet.

NOTE: If chart_id is specified the framework will attempt to load the chart styles from public/themes/theme_name/chart_id.yml



53
54
55
56
57
58
59
60
61
62
# File 'lib/ziya/charts/base.rb', line 53

def initialize( license=nil, chart_id=nil ) 
  @id          = chart_id
  @license     = license
  @options     = {}
  @series_desc = []
  @theme       = default_theme
  @render_mode = Base.mode_reset
  initialize_components
  load_helpers( Ziya.helpers_dir ) if Ziya.helpers_dir
end

Instance Attribute Details

#idObject

:nodoc:



45
46
47
# File 'lib/ziya/charts/base.rb', line 45

def id
  @id
end

#licenseObject

:nodoc:



45
46
47
# File 'lib/ziya/charts/base.rb', line 45

def license
  @license
end

#optionsObject

:nodoc:



45
46
47
# File 'lib/ziya/charts/base.rb', line 45

def options
  @options
end

#sizeObject

:nodoc:



45
46
47
# File 'lib/ziya/charts/base.rb', line 45

def size
  @size
end

#themeObject

:nodoc:



45
46
47
# File 'lib/ziya/charts/base.rb', line 45

def theme
  @theme
end

#typeObject (readonly)

:nodoc:



46
47
48
# File 'lib/ziya/charts/base.rb', line 46

def type
  @type
end

Class Method Details

.componentsObject

class component accessor…



65
66
67
# File 'lib/ziya/charts/base.rb', line 65

def self.components # :nodoc:
  @components
end

.mode_dataObject

don’t render stylesheets just gen code for chart stylesheet and data



70
# File 'lib/ziya/charts/base.rb', line 70

def self.mode_data() 1; end

.mode_resetObject

renders everything



73
# File 'lib/ziya/charts/base.rb', line 73

def self.mode_reset() 0; end

Instance Method Details

#add(*args) ⇒ Object

Add chart components such as x and y axis labels, data points and chart labels.

Example:

my_chart = Ziya::Charts::Bar.new
my_chart.add( :axis_category_text, ['2004', '2005', '2006'] ) 
my_chart.add( :series, 'series A', [ 10, 20, 30], [ '10 dogs', '20 cats', '30 rats'] )
my_chart.add( :axis_value_label, [ 'my dogs', 'my cats', 'my rats'] )
my_chart.add( :user_data, :mykey, "Fred" )

This will display a bar chart with x axis ticks my dogs, my cats, my fox and
y axis values 2004, 2005, 2006. The labels on the bars will read 10 dogs, 
20 cats, 30 rats

The args must contain certain keys for the chart to be display correctly. The keys are defined as follows:

:axis_category_text

Array of strings representing the x/y axis ticks dependending on the chart type. This value is required.

:axis_category_label

Array of strings representing the x axis labels. This is supported only for Scatter and Bubble charts. This value is optional. Specify nil for no label change.

:series

Specifies the series name and chart data points. The series name will be used to display chart legends. You must have at least one of these tag defined. You may also specify an array of strings to identifies the custom labels that will be used on top of the chart elements

:axis_value_label

Array of strings representing the ticks on the x/y axis depending on the chart type. This is symmetrical to the axis_category_label tag for the opposite chart axis. Specify nil for no label change.

:user_data

Used to make user data available to the ERB templates in the chart stylesheet yaml file. You must specify a key symbol and an ad-hoc value. The key will be used with the @options hash to access the user data.

:composites

Embeds multiple charts within the given chart via the draw image component.

:chart_types

Specify the chart types per series. This option should

only be used with Mixed Charts !!

:theme

Specify the use of a given theme



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/ziya/charts/base.rb', line 143

def add( *args )
  # TODO Validation categories = series, series = labels, etc...
  directive = args.shift
  case directive
    when :axis_category_text
      categories = args.first.is_a?(Array) ? args.shift : []
      raise ArgumentError, "Must specify an array of categories" if categories.empty?
      categories.insert( 0, nil )
      @options[directive] = categories
    when :axis_category_label
      labels = args.first.is_a?(Array) ? args.shift : []
      raise ArgumentError, "Must specify an array of category labels" if labels.empty?
      @options[directive] = labels          
    when :composites
      composites = args.first.is_a?(Array) ? args.shift: []
      raise ArgumentError, "Must specify an array of urls for the composite chart(s)" if composites.empty?
      @options[directive] = composites
    when :axis_value_label
      values = args.first.is_a?(Array) ? args.shift : []
      raise ArgumentError, "Must specify an array of values" if values.empty?
      @options[directive] = values
    when :series
      legend = args.first.is_a?(String) ? args.shift : ""
      if args.first.is_a?( Array )
        points = args.shift || []
        raise ArgumentError, "Must specify an array of data points" if points.empty?
        points.insert( 0, legend )
        @series_desc << points
      else
        raise ArgumentError, "Must specify an array of data points"
      end
    when :user_data
      key = args.first.is_a?(Symbol) ? args.shift : ""
      raise ArgumentError, "Must specify a key" if key.to_s.empty?
      value = args.shift
      # raise ArgumentError, "Must specify a value" if value.empty?
      @options[key] = value
    when :styles
      styles = args.first.is_a?(String) ? args.shift : ""
      raise ArgumentError, "Must specify a set of styles" if styles.to_s.empty?          
      @options[directive] = styles   
    when :chart_types
      types = args.first.is_a?(Array) ? args.shift : []
      raise ArgumentError, "Must specify a set of chart types" if types.to_s.empty?          
      @options[directive] = types                        
    when :theme
      theme = args.first.is_a?(String) ? args.shift : ""
      raise ArgumentError, "Must specify a theme name" if theme.to_s.empty?          
      @theme = "#{Ziya.themes_dir}/#{theme}"
    when :mode
      @render_mode = args.first.is_a?(Integer) ? args.shift : -1
      raise ArgumentError, "Must specify a valid generation mode" if @render_mode == -1          
    else raise ArgumentError, "Invalid directive must be one of " + 
                             ":axis_category_text, :axis_value, :series, :user_data"
  end 
end

#default_themeObject


Default ZiYa theme



77
78
79
# File 'lib/ziya/charts/base.rb', line 77

def default_theme # :nodoc:
  File.join( Ziya.themes_dir, %w[default] )
end

#load_helpers(helper_dir) ⇒ Object


Load up ERB style helpers



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ziya/charts/base.rb', line 83

def load_helpers( helper_dir ) # :nodoc:    
  Dir.foreach(helper_dir) do |helper_file| 
    next unless helper_file =~ /^([a-z][a-z_]*_helper).rb$/
    Ziya.logger.debug( ">>> ZiYa loading custom helper `#{$1}" )        
    # check rails env for autoloader ?
    if defined?(RAILS_ROOT) 
      require_dependency File.join(helper_dir, $1) 
    else
      require File.join(helper_dir, $1)
    end
    helper_module_name = "Ziya::" + $1.gsub(/(^|_)(.)/) { $2.upcase }        
    # helper_module_name = $1.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
    # if Ziya::Helpers.const_defined?(helper_module_name)
    Ziya.logger.debug( "Include module #{helper_module_name}")
    Ziya::Charts::Base.class_eval("include #{helper_module_name}") 
    # end
  end
end

#to_s(options = {}) ⇒ Object Also known as: to_xml

spews the graph specification to a string

:partial

You can specify this option to only update parts of the charts that have actually changed. This is useful for live update and link update where you may not need to redraw the whole chart.



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/ziya/charts/base.rb', line 204

def to_s( options={} )
  @partial = options[:partial] || false
  @xml     = Builder::XmlMarkup.new
  # Forces utf8 encoding on xml stream
  @xml.instruct! :xml, :version => "1.0", :encoding => "UTF-8"
  @xml.chart do
    @xml.license( @license ) unless @license.nil?
    if render_parents?
      if !@type.nil?
        @xml.chart_type( @type )              
      elsif @options[:chart_types].is_a? Array and ! @options[:chart_types].empty?
        @xml.chart_type do   
          @options[:chart_types].each { |type| @xml.string( type ) }   
        end
      end
    end
    setup_lnf
    setup_series
  end
  @xml.to_s.gsub( /<to_s\/>/, '' )
end