Class: DYI::Chart::ArrayReader

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/dyi/chart/array_reader.rb

Overview

ArrayReader converts the ruby array into a readable format for the chart object of DYI.

If any ruby object or something (file, database, etc…) is used as the data source of DYI’s chart, the object of the inheritance class of ArrayReader avails. For example, using a CSV data, CsvReader class avails.

Basic Usage

Using PieChart and ArrayReader (or sub class of ArrayReader), you can create the pie chart as the following:

require 'rubygems'
require 'dyi'

# Nominal GDP of Asian Countries (2010)
chart_data = [['China', 5878],
              ['Japan', 5459],
              ['India', 1538],
              ['South Koria', 1007],
              ['Other Countries', 2863]]
reader = DYI::Chart::ArrayReader.read(chart_data, :schema => [:name, :value])

# Creates the Pie Chart
chart = DYI::Chart::PieChart.new(450,250)
chart.load_data(reader)
chart.save('asian_gdp.svg')

Creating the instance, you should not call new method but ArrayReader.read method.

The optional argument :schema means a field name. The field name :value is the particular name, that is to say, the chart object generate a chart using a value of the field named :value. If :schema option is not specified, the ArrayReader object looks upon all feilds as :vlaue field. The field names other than :name are used in the format string and so on, as following:

# Nominal GDP of Asian Countries (2010)
chart_data = [['China',       'People\'s Republic of China', 5878, 'red'],
              ['Japan',       'Japan',                       5459, 'blue'],
              ['India',       'Republic of India',           1538, 'yellow'],
              ['South Koria', 'Republic of Korea',           1007, 'green'],
              ['Others',      'Other Asian Countries',       2863, 'gray']]
reader = DYI::Chart::ArrayReader.read(chart_data,
                                      :schema => [:name, :long, :value, :color])

# Creates the Pie Chart
chart = DYI::Chart::PieChart.new(450,250,
                                 :legend_format => '{?long}')
chart.load_data(reader)
chart.save('asian_gdp.svg')

See ArrayReader.read for other optional arguments.

Since:

  • 0.0.0

Direct Known Subclasses

CsvReader, ExcelReader

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeArrayReader

Returns a new instance of ArrayReader.

Since:

  • 0.0.0



152
153
154
# File 'lib/dyi/chart/array_reader.rb', line 152

def initialize
  @records = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object (private)

Makes the instance respond to xxx_values method.

Examples:

data = ArrayReader.read([['Smith', 20, 3432], ['Thomas', 25, 9721]],
                        :schema => [:name, :age, :value])
data.name_vlaues  # => ['Smith', 'Thomas']
data.age_values   # => [20, 25]

Since:

  • 1.0.0



256
257
258
259
260
261
262
263
# File 'lib/dyi/chart/array_reader.rb', line 256

def method_missing(name, *args)
  if args.size == 0 && name.to_s =~ /_values\z/ &&
      @schema.members.include?(RUBY_VERSION >= '1.9' ? $`.to_sym : $`)
    @records.map{|r| r.__send__($`)}
  else
    super
  end
end

Class Method Details

.read(array_of_array, options = {}) ⇒ ArrayReader

Create a new instance of ArrayReader, loading array-of-array.

Examples:

# example of using :row_range option
chart_data = [['Country', 'Nominal GDP'],
              ['China', 5878],
              ['Japan', 5459],
              ['India', 1538],
              ['South Koria', 1007],
              ['Other Countries', 2863]]
# skips the first row
reader = DYI::Chart::ArrayReader.read(chart_data,
                                      :schema => [:name, :value],
                                      :row_range => (1..-1))
# example of using :transposed option
chart_data = [['China', 'Japan', 'India', 'South Koria', 'Other Countries'],
              [5878, 5459, 1538, 1007, 2863]]
# transposes the rows and the columns
reader = DYI::Chart::ArrayReader.read(chart_data,
                                        :schema => [:name, :value],
                                        :transposed => true)

Parameters:

  • array_of_array (Array<Array>)

    two dimensional array

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :row_range (Range)

    a range of rows

  • :column_range (Range)

    a range of columns

  • :schema (Array<Symbol>)

    array of field names. see Overview of DYI::Chart::ArrayReader.

  • :transposed (Boolean)

    whether the array-of-array is transposed

Returns:

Since:

  • 0.0.0



290
291
292
# File 'lib/dyi/chart/array_reader.rb', line 290

def read(array_of_array, options={})
  new.read(array_of_array, options)
end

Instance Method Details

#[](i, j) ⇒ Numeric

Returns the value at index.

Parameters:

  • i (Integer)

    the index of records

  • j (Integer)

    the index of series

Returns:

Since:

  • 0.0.0



84
85
86
# File 'lib/dyi/chart/array_reader.rb', line 84

def [](i, j)
  @records[i].values[j]
end

#clear_dataObject

Clears all records

Since:

  • 0.0.0



110
111
112
# File 'lib/dyi/chart/array_reader.rb', line 110

def clear_data
  @records.clear
end

#each {|record| ... } ⇒ Object

Calls block once for each record, passing that records as a parameter.

Yields:

  • (record)

    iteration block

Yield Parameters:

  • record (Struct)

    the record in self

Since:

  • 1.0.0



147
148
149
# File 'lib/dyi/chart/array_reader.rb', line 147

def each(&block)
  @records.each(&block)
end

#has_field?(field_name) ⇒ Bolean

Returns whether the record has the field.

Parameters:

  • field_name (Symbol, String)

    field name

Returns:

  • (Bolean)

    true if the record has the field, false otherwise

Since:

  • 1.0.0



139
140
141
# File 'lib/dyi/chart/array_reader.rb', line 139

def has_field?(field_name)
  @schema.members.include?(RUBY_VERSION >= '1.9' ? field_name.to_sym : field_name.to_s)
end

#membersArray<Symbol>

Returns an array of the field’s name

Returns:

  • (Array<Symbol>)

    an array of the field’s name

Since:

  • 1.1.0



199
200
201
# File 'lib/dyi/chart/array_reader.rb', line 199

def members
  @schema.members.map{|name| name.to_sym}
end

#read(array_of_array, options = {}) ⇒ Object

Loads array-of-array and sets data.

Parameters:

  • array_of_array (Array<Array>)

    two dimensional array

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :row_range (Range)

    a range of rows

  • :column_range (Range)

    a range of columns

  • :schema (Array<Symbol>)

    array of field names. see Overview of DYI::Chart::ArrayReader.

  • :transposed (Boolean)

    whether the array-of-array is transposed

Since:

  • 0.0.0



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
# File 'lib/dyi/chart/array_reader.rb', line 164

def read(array_of_array, options={})
  clear_data
  row_range = options[:row_range] || (0..-1)
  col_range = options[:column_range] || (0..-1)
  schema = options[:schema] || [:value]
  data_types = options[:data_types] || []
#        row_proc = options[:row_proc]
  @schema = record_schema(schema)
  array_of_array = transpose(array_of_array) if options[:transposed]

  array_of_array[row_range].each do |row|
    record_source = []
    values = []
    has_set_value = false
    row[col_range].each_with_index do |cell, i|
      cell = primitive_value(cell, data_types[i])
      if schema[i].nil? || schema[i].to_sym == :value
        unless has_set_value
          record_source << cell
          has_set_value = true
        end
        values << cell
      else
        record_source << cell
      end
    end
    record_source << values
    @records << @schema.new(*record_source)
  end
  self
end

#recordsArray<Struct>

Returns the array of the records.

Returns:

  • (Array<Struct>)

    the array of the records

Since:

  • 1.0.0



91
92
93
# File 'lib/dyi/chart/array_reader.rb', line 91

def records
  @records.clone
end

#records_sizeInteger

Returns number of the records.

Returns:

  • (Integer)

    number of the records

Since:

  • 1.0.0



98
99
100
# File 'lib/dyi/chart/array_reader.rb', line 98

def records_size
  @records.size
end

#series(index) ⇒ Array<Numeric>

Returns an array of values of the specified series.

Parameters:

  • index (Integer)

    an index of the series

Returns:

  • (Array<Numeric>)

    an array of values

Since:

  • 1.0.0



129
130
131
132
133
# File 'lib/dyi/chart/array_reader.rb', line 129

def series(index)
  @records.map do |record|
    record.values[index]
  end
end

#values_each {|values| ... } ⇒ Object

Calls block once for each record, passing the values that records as a parameter.

Yields:

  • (values)

    iteration block

Yield Parameters:

  • values (Array<Numeric>)

    the values that the record has

Since:

  • 1.0.0



119
120
121
122
123
# File 'lib/dyi/chart/array_reader.rb', line 119

def values_each(&block)
  @records.each do |record|
    yield record.values
  end
end

#values_sizeInteger

Returns number of the values in the record

Returns:

  • (Integer)

    number of the values

Since:

  • 1.0.0



105
106
107
# File 'lib/dyi/chart/array_reader.rb', line 105

def values_size
  @records.first.values.size rescue 0
end