Class: Chartd::Chart
- Inherits:
-
Object
- Object
- Chartd::Chart
- Defined in:
- lib/chartd/chart.rb
Constant Summary collapse
- ERR_BAD_DATASET =
'Dataset has to be an array of Fixnums and/or Floats.'.freeze
- ERR_TOO_MANY_DATASETS =
'Too many datasets supplied, the maximum is 5.'.freeze
Instance Attribute Summary collapse
-
#dataset ⇒ Object
readonly
Returns the value of attribute dataset.
-
#max ⇒ Object
allow min and max to be changed after instantiating a Chart.
-
#min ⇒ Object
allow min and max to be changed after instantiating a Chart.
Instance Method Summary collapse
-
#initialize(dataset = [], min: nil, max: nil, ylabels: true, options: {}) ⇒ Chart
constructor
A new instance of Chart.
- #url ⇒ Object
Constructor Details
#initialize(dataset = [], min: nil, max: nil, ylabels: true, options: {}) ⇒ Chart
Returns a new instance of Chart.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/chartd/chart.rb', line 16 def initialize(dataset = [], min: nil, max: nil, ylabels: true, options: {}) raise ERR_BAD_DATASET unless dataset.is_a?(Array) # Check if dataset is multidimensional and if so, use it as is. # Otherwise make it multidimensional. if dataset[0].is_a?(Array) raise ERR_TOO_MANY_DATASETS if dataset.count > 5 @dataset = dataset else @dataset = [dataset] end # calculate min and max across the entire dataset but only if they are # not set explicitly. @min = min || dataset.flatten.min @max = max || dataset.flatten.max @ylabels = ylabels = .merge() end |
Instance Attribute Details
#dataset ⇒ Object (readonly)
Returns the value of attribute dataset.
11 12 13 |
# File 'lib/chartd/chart.rb', line 11 def dataset @dataset end |
#max ⇒ Object
allow min and max to be changed after instantiating a Chart
14 15 16 |
# File 'lib/chartd/chart.rb', line 14 def max @max end |
#min ⇒ Object
allow min and max to be changed after instantiating a Chart
14 15 16 |
# File 'lib/chartd/chart.rb', line 14 def min @min end |
Instance Method Details
#url ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/chartd/chart.rb', line 38 def url u = BASE_URL.dup # encode each piece of data and append it to URL as params encoded_data = @dataset.each_with_index.map do |d, i| ["d#{i}", Encoder.encode(d, min: @min, max: @max)] end # set labels for y axis when they’re enabled (which they are by default) if @ylabels [:ymin] ||= @min [:ymax] ||= @max end u.query = URI.encode_www_form( .merge(encoded_data.to_h) ) u.to_s.force_encoding('utf-8') end |