Class: RailsDataExplorer::DataSeries

Inherits:
Object
  • Object
show all
Defined in:
lib/rails-data-explorer/data_series.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_name, _values, options = {}) ⇒ DataSeries

options: :chart_roles, :data_type (all optional)



25
26
27
28
29
30
31
# File 'lib/rails-data-explorer/data_series.rb', line 25

def initialize(_name, _values, options={})
  options = { chart_roles: [], data_type: nil }.merge(options)
  @name = _name
  @values = _values
  @data_type = init_data_type(options[:data_type])
  @chart_roles = init_chart_roles(options[:chart_roles]) # after data_type!
end

Instance Attribute Details

#chart_rolesObject (readonly)

TODO: Add concept of significant figures for rounding values when displaying them en.wikipedia.org/wiki/Significant_figures



7
8
9
# File 'lib/rails-data-explorer/data_series.rb', line 7

def chart_roles
  @chart_roles
end

#data_typeObject (readonly)

TODO: Add concept of significant figures for rounding values when displaying them en.wikipedia.org/wiki/Significant_figures



7
8
9
# File 'lib/rails-data-explorer/data_series.rb', line 7

def data_type
  @data_type
end

#nameObject (readonly)

TODO: Add concept of significant figures for rounding values when displaying them en.wikipedia.org/wiki/Significant_figures



7
8
9
# File 'lib/rails-data-explorer/data_series.rb', line 7

def name
  @name
end

#valuesObject (readonly)

TODO: Add concept of significant figures for rounding values when displaying them en.wikipedia.org/wiki/Significant_figures



7
8
9
# File 'lib/rails-data-explorer/data_series.rb', line 7

def values
  @values
end

Class Method Details

.large_dynamic_range_thresholdObject

Any data series with a dynamic range greater than this is considered having a large dynamic range We consider dynamic range the ratio between the largest and the smallest value.



14
15
16
# File 'lib/rails-data-explorer/data_series.rb', line 14

def self.large_dynamic_range_threshold
  10000.0
end

.many_uniq_vals_thresholdObject

Any data series with more than this uniq vals is considered having many uniq values.



20
21
22
# File 'lib/rails-data-explorer/data_series.rb', line 20

def self.many_uniq_vals_threshold
  30
end

Instance Method Details

#axis_scale(d3_or_vega) ⇒ Object

@param d3_or_vega :d3 or :vega



74
75
76
# File 'lib/rails-data-explorer/data_series.rb', line 74

def axis_scale(d3_or_vega)
  data_type.axis_scale(self, d3_or_vega)
end

#axis_tick_formatObject



69
70
71
# File 'lib/rails-data-explorer/data_series.rb', line 69

def axis_tick_format
  data_type.axis_tick_format(values)
end

#descriptive_statisticsObject

Returns descriptive_statistics as a flat Array



34
35
36
# File 'lib/rails-data-explorer/data_series.rb', line 34

def descriptive_statistics
  @data_type.descriptive_statistics(values)
end

#descriptive_statistics_tableObject

Returns descriptive_statistics as a renderable table structure



39
40
41
# File 'lib/rails-data-explorer/data_series.rb', line 39

def descriptive_statistics_table
  @data_type.descriptive_statistics_table(values)
end

#dynamic_rangeObject



99
100
101
102
# File 'lib/rails-data-explorer/data_series.rb', line 99

def dynamic_range
  # TODO: avoid division by zero
  max_val / [min_val, max_val].min.to_f
end

#has_large_dynamic_range?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/rails-data-explorer/data_series.rb', line 104

def has_large_dynamic_range?
  dynamic_range > self.class.large_dynamic_range_threshold
end

#has_many_uniq_vals?Boolean

Used to decide whether we can render certain chart types

Returns:

  • (Boolean)


95
96
97
# File 'lib/rails-data-explorer/data_series.rb', line 95

def has_many_uniq_vals?
  uniq_vals_count > self.class.many_uniq_vals_threshold
end

#inspect(indent = 1, recursive = 1000) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rails-data-explorer/data_series.rb', line 55

def inspect(indent=1, recursive=1000)
  r = %(#<#{ self.class.to_s }\n)
  r << [
    "@name=#{ name.inspect }",
    "@data_type=#{ data_type.inspect }",
    "@chart_roles=#{ chart_roles.inspect }",
    "@values=<count: #{ values.count }, items: #{ values_summary }>",
  ].map { |e| "#{ '  ' * indent }#{ e }\n"}.join
  if recursive > 0
    # nothing to recurse
  end
  r << %(#{ '  ' * (indent-1) }>\n)
end

#label_sorter(label_val_key, value_sorter) ⇒ Object



108
109
110
# File 'lib/rails-data-explorer/data_series.rb', line 108

def label_sorter(label_val_key, value_sorter)
  data_type.label_sorter(label_val_key, self, value_sorter)
end

#max_valObject



90
91
92
# File 'lib/rails-data-explorer/data_series.rb', line 90

def max_val
  @max_val ||= values.compact.max
end

#min_valObject



86
87
88
# File 'lib/rails-data-explorer/data_series.rb', line 86

def min_val
  @min_val ||= values.compact.min
end

#number_of_valuesObject



43
44
45
# File 'lib/rails-data-explorer/data_series.rb', line 43

def number_of_values
  values.length
end

#uniq_valsObject



78
79
80
# File 'lib/rails-data-explorer/data_series.rb', line 78

def uniq_vals
  @uniq_vals ||= values.uniq
end

#uniq_vals_countObject



82
83
84
# File 'lib/rails-data-explorer/data_series.rb', line 82

def uniq_vals_count
  @uniq_vals_count ||= uniq_vals.length
end

#values_summaryObject



47
48
49
50
51
52
53
# File 'lib/rails-data-explorer/data_series.rb', line 47

def values_summary
  if values.length < 3 || values.inspect.length < 80
    values.inspect
  else
    "[#{ values.first } ... #{ values.last }]"
  end
end