Class: DYI::Chart::CsvReader
- Inherits:
-
ArrayReader
- Object
- ArrayReader
- DYI::Chart::CsvReader
- Defined in:
- lib/dyi/chart/csv_reader.rb,
lib/ironruby.rb
Overview
CsvReader
class provides a interface to CSV file and data for a chart object.
Basic Usage
Using PieChart
and CsvReader, you can create the pie chart as the following:
# contents of asian_gdp.csv
# -------------------------
# China,5878
# Japan,5459
# India,1538
# South Koria,1007
# Other Countries, 2863
require 'rubygems'
require 'dyi'
reader = DYI::Chart::CsvReader.read('asian_gdp.csv',
:schema => [:name, :value],
:data_types => [:string, :number])
# 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 CsvReader.read method.
The optional argument :schema
means a field name. See ArrayReader for :schema
option. The optional argument :data_types
means data types of the field. Using this option, CsvReader
converts text data that the CSV data included into a appropriate ruby object. (:data_types
option has been implemented in ArrayReader
, although ArrayReader
ignores this option.) If :data_types
option is not specified, the values of all the fields are converted into instances of Float
. :data_types
option can specify the following values: :string
, :decimal
, :number
(synonym of :decimal
), :float
, :integer
, :date
, :datetime
, :boolean
See CsvReader.read for other optional arguments.
Class Method Summary collapse
-
.parse(csv, options = {}) ⇒ CsvReader
Parses CSV data and creates instance of CsvReader.
-
.read(path, options = {}) ⇒ Object
Parses CSV file and creates instance of CsvReader.
Instance Method Summary collapse
- #__org_read__ ⇒ Object
-
#parse(csv, options = {}) ⇒ Object
Parses CSV data and sets data.
-
#read(path, options = {}) ⇒ Object
Parses CSV file and sets data.
Methods inherited from ArrayReader
#[], #clear_data, #each, #has_field?, #initialize, #members, #records, #records_size, #series, #values_each, #values_size
Constructor Details
This class inherits a constructor from DYI::Chart::ArrayReader
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class DYI::Chart::ArrayReader
Class Method Details
.parse(csv, options = {}) ⇒ CsvReader
Parses CSV data and creates instance of CsvReader.
174 175 176 |
# File 'lib/dyi/chart/csv_reader.rb', line 174 def parse(csv, ={}) new.parse(csv, ) end |
.read(path, options = {}) ⇒ Object
Parses CSV file and creates instance of CsvReader.
163 164 165 |
# File 'lib/dyi/chart/csv_reader.rb', line 163 def read(path, ={}) new.read(path, ) end |
Instance Method Details
#__org_read__ ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/dyi/chart/csv_reader.rb', line 75 def read(path, ={}) = .dup @date_format = .delete(:date_format) @datetime_format = .delete(:datetime_format) encode = case ([:encode] || :utf8).to_sym when :utf8 then 'UTF-8' when :sjis then 'Shift_JIS' when :euc then 'EUC-JP' when :jis then 'iso-2022-jp' when :utf16 then 'UTF-16' end parser = Microsoft::VisualBasic::FileIO::TextFieldParser.new( path, System::Text::Encoding.get_encoding(encode)) parser.set_delimiters([:col_sep] || ',') parsed_array = [] while !parser.end_of_data parsed_array << parser.read_fields.map {|v| v.to_s} end super(parsed_array, ) end |
#parse(csv, options = {}) ⇒ Object
Parses CSV data and sets data.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/dyi/chart/csv_reader.rb', line 96 def parse(csv, ={}) = .clone @date_format = .delete(:date_format) @datetime_format = .delete(:datetime_format) = case ([:encode] || :utf8).to_sym when :utf8 then nil when :sjis then '-w -S -m0 -x --cp932' when :euc then '-w -E -m0 -x --cp932' when :jis then '-w -J -m0 -x' when :utf16 then '-w -W16 -m0 -x' else raise ArgumentError,"Unknown encode: `#{@encode}'" end parsed_array = if RUBY_VERSION >= '1.9' CSV.parse( ? NKF.nkf(, csv) : csv, :col_sep => [:col_sep] || ',', :row_sep => [:row_sep] || :auto) else CSV.parse( ? NKF.nkf(, csv) : csv, [:col_sep], [:row_sep]) end __org_read__(parsed_array, ) end |
#read(path, options = {}) ⇒ Object
Parses CSV file and sets data.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/dyi/chart/csv_reader.rb', line 123 def read(path, ={}) = .dup @date_format = .delete(:date_format) @datetime_format = .delete(:datetime_format) encode = case ([:encode] || :utf8).to_sym when :utf8 then 'UTF-8' when :sjis then 'Shift_JIS' when :euc then 'EUC-JP' when :jis then 'iso-2022-jp' when :utf16 then 'UTF-16' end parser = Microsoft::VisualBasic::FileIO::TextFieldParser.new( path, System::Text::Encoding.get_encoding(encode)) parser.set_delimiters([:col_sep] || ',') parsed_array = [] while !parser.end_of_data parsed_array << parser.read_fields.map {|v| v.to_s} end super(parsed_array, ) end |