Class: DYI::Chart::ArrayReader
- Inherits:
-
Object
- Object
- DYI::Chart::ArrayReader
- 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.
Direct Known Subclasses
Class Method Summary collapse
-
.read(array_of_array, options = {}) ⇒ ArrayReader
Create a new instance of ArrayReader, loading array-of-array.
Instance Method Summary collapse
-
#[](i, j) ⇒ Numeric
Returns the value at index.
-
#clear_data ⇒ Object
Clears all records.
-
#each {|record| ... } ⇒ Object
Calls block once for each record, passing that records as a parameter.
-
#has_field?(field_name) ⇒ Bolean
Returns whether the record has the field.
-
#initialize ⇒ ArrayReader
constructor
A new instance of ArrayReader.
-
#members ⇒ Array<Symbol>
Returns an array of the field’s name.
-
#read(array_of_array, options = {}) ⇒ Object
Loads array-of-array and sets data.
-
#records ⇒ Array<Struct>
Returns the array of the records.
-
#records_size ⇒ Integer
Returns number of the records.
-
#series(index) ⇒ Array<Numeric>
Returns an array of values of the specified series.
-
#values_each {|values| ... } ⇒ Object
Calls block once for each record, passing the values that records as a parameter.
-
#values_size ⇒ Integer
Returns number of the values in the record.
Constructor Details
#initialize ⇒ ArrayReader
Returns a new instance of ArrayReader.
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.
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.
290 291 292 |
# File 'lib/dyi/chart/array_reader.rb', line 290 def read(array_of_array, ={}) new.read(array_of_array, ) end |
Instance Method Details
#[](i, j) ⇒ Numeric
Returns the value at index.
84 85 86 |
# File 'lib/dyi/chart/array_reader.rb', line 84 def [](i, j) @records[i].values[j] end |
#clear_data ⇒ Object
Clears all records
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.
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.
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 |
#members ⇒ Array<Symbol>
Returns an array of the field’s name
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.
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, ={}) clear_data row_range = [:row_range] || (0..-1) col_range = [:column_range] || (0..-1) schema = [:schema] || [:value] data_types = [:data_types] || [] # row_proc = options[:row_proc] @schema = record_schema(schema) array_of_array = transpose(array_of_array) if [: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 |
#records ⇒ Array<Struct>
Returns the array of the records.
91 92 93 |
# File 'lib/dyi/chart/array_reader.rb', line 91 def records @records.clone end |
#records_size ⇒ Integer
Returns number of the records.
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.
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.
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_size ⇒ Integer
Returns number of the values in the record
105 106 107 |
# File 'lib/dyi/chart/array_reader.rb', line 105 def values_size @records.first.values.size rescue 0 end |