Class: DYI::Chart::CsvReader

Inherits:
ArrayReader show all
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.

See Also:

Since:

  • 0.0.0

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • csv (String)

    CSV data

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

    a customizable set of options

Options Hash (options):

  • :data_types (Array<Symbol>)

    array of field data types

  • :date_format (String)

    date format string of CSV data, parsing a date string in CSV at Date#strptime

  • :datetime_format (String)

    date-time format string of CSV data, parsing a date-time string in CSV at DateTime#strptime

  • :encode (Symbol)

    encoding of CSV data as the following: :utf8 (default), :sjis, :euc, :jis (ISO-2022-JP), :utf16 (UTF-16BE)

  • :col_sep (String)

    a separator of columns, default to ","

  • :row_sep (String)

    a separator of rows, default to :auto, which means that a separetor is "\r\n", "\n", or "\r" sequence

  • :row_range (Range)

    a range of rows

  • :column_range (Range)

    a range of columns

  • :schema (Array<Symbol>)

    array of field names. see Overview of ArrayReader.

  • :transposed (Boolean)

    whether the array-of-array is transposed

Returns:

  • (CsvReader)

    a new instance of CsvReader

Raises:

  • (ArgumentError)

    unknown encode is given for :encode

See Also:

Since:

  • 1.1.1



174
175
176
# File 'lib/dyi/chart/csv_reader.rb', line 174

def parse(csv, options={})
  new.parse(csv, options)
end

.read(path, options = {}) ⇒ Object

Parses CSV file and creates instance of CsvReader.

Parameters:

  • path (String)

    a path of the CSV file

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

    a customizable set of options

Options Hash (options):

  • :data_types (Array<Symbol>)

    array of field data types

  • :date_format (String)

    date format string of CSV data, parsing a date string in CSV at Date#strptime

  • :datetime_format (String)

    date-time format string of CSV data, parsing a date-time string in CSV at DateTime#strptime

  • :encode (Symbol)

    encoding of CSV data as the following: :utf8 (default), :sjis, :euc, :jis (ISO-2022-JP), :utf16 (UTF-16BE)

  • :col_sep (String)

    a separator of columns, default to ","

  • :row_sep (String)

    a separator of rows, default to :auto, which means that a separetor is "\r\n", "\n", or "\r" sequence

  • :row_range (Range)

    a range of rows

  • :column_range (Range)

    a range of columns

  • :schema (Array<Symbol>)

    array of field names. see Overview of ArrayReader.

  • :transposed (Boolean)

    whether the array-of-array is transposed

Raises:

  • (ArgumentError)

    unknown encode is given for :encode

See Also:

Since:

  • 0.0.0



163
164
165
# File 'lib/dyi/chart/csv_reader.rb', line 163

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

Instance Method Details

#__org_read__Object

Since:

  • 0.0.0



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, options={})
  options = options.dup
  @date_format = options.delete(:date_format)
  @datetime_format = options.delete(:datetime_format)

  encode = 
    case (options[: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(options[:col_sep] || ',')

  parsed_array = []
  while !parser.end_of_data
    parsed_array << parser.read_fields.map {|v| v.to_s}
  end
  super(parsed_array, options)
end

#parse(csv, options = {}) ⇒ Object

Parses CSV data and sets data.

Parameters:

  • csv (String)

    CSV data

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

    a customizable set of options

Options Hash (options):

  • :data_types (Array<Symbol>)

    array of field data types

  • :date_format (String)

    date format string of CSV data, parsing a date string in CSV at Date#strptime

  • :datetime_format (String)

    date-time format string of CSV data, parsing a date-time string in CSV at DateTime#strptime

  • :encode (Symbol)

    encoding of CSV data as the following: :utf8 (default), :sjis, :euc, :jis (ISO-2022-JP), :utf16 (UTF-16BE)

  • :col_sep (String)

    a separator of columns, default to ","

  • :row_sep (String)

    a separator of rows, default to :auto, which means that a separetor is "\r\n", "\n", or "\r" sequence

  • :row_range (Range)

    a range of rows

  • :column_range (Range)

    a range of columns

  • :schema (Array<Symbol>)

    array of field names. see Overview of ArrayReader.

  • :transposed (Boolean)

    whether the array-of-array is transposed

Raises:

  • (ArgumentError)

    unknown encode is given for :encode

See Also:

Since:

  • 1.1.1



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, options={})
  options = options.clone
  @date_format = options.delete(:date_format)
  @datetime_format = options.delete(:datetime_format)
  nkf_options =
    case (options[: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_options ? NKF.nkf(nkf_options, csv) : csv, :col_sep => options[:col_sep] || ',', :row_sep => options[:row_sep] || :auto)
    else
      CSV.parse(nkf_options ? NKF.nkf(nkf_options, csv) : csv, options[:col_sep], options[:row_sep])
    end
  __org_read__(parsed_array, options)
end

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

Parses CSV file and sets data.

Parameters:

  • path (String)

    a path of the CSV file

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

    a customizable set of options

Options Hash (options):

  • :data_types (Array<Symbol>)

    array of field data types

  • :date_format (String)

    date format string of CSV data, parsing a date string in CSV at Date#strptime

  • :datetime_format (String)

    date-time format string of CSV data, parsing a date-time string in CSV at DateTime#strptime

  • :encode (Symbol)

    encoding of CSV data as the following: :utf8 (default), :sjis, :euc, :jis (ISO-2022-JP), :utf16 (UTF-16BE)

  • :col_sep (String)

    a separator of columns, default to ","

  • :row_sep (String)

    a separator of rows, default to :auto, which means that a separetor is "\r\n", "\n", or "\r" sequence

  • :row_range (Range)

    a range of rows

  • :column_range (Range)

    a range of columns

  • :schema (Array<Symbol>)

    array of field names. see Overview of ArrayReader.

  • :transposed (Boolean)

    whether the array-of-array is transposed

Raises:

  • (ArgumentError)

    unknown encode is given for :encode

See Also:

Since:

  • 0.0.0



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, options={})
  options = options.dup
  @date_format = options.delete(:date_format)
  @datetime_format = options.delete(:datetime_format)

  encode = 
    case (options[: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(options[:col_sep] || ',')

  parsed_array = []
  while !parser.end_of_data
    parsed_array << parser.read_fields.map {|v| v.to_s}
  end
  super(parsed_array, options)
end