Module: ChartsHelper

Defined in:
app/helpers/charts_helper.rb

Overview

License

Ekylibre - Simple agricultural ERP Copyright (C) 2008-2013 Brice Texier

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see <www.gnu.org/licenses/>.

Constant Summary collapse

OPTIONS =
[:colors, :credits, :exporting, :labels, :legend, :loading, :navigation, :pane, :plot_options, :series, :subtitle, :title, :tooltip, :x_axis, :y_axis].inject({}) do |hash, name|
  hash[name] = name.to_s.tr('_', '-') # camelize(:lower)
  hash
end.freeze
TYPES =
[:line, :spline, :area, :area_spline, :column, :bar, :pie, :scatter, :area_range, :area_spline_range, :column_range, :waterfall].inject({}) do |hash, name|
  hash[name] = name.to_s.delete('_')
  hash
end.freeze

Instance Method Summary collapse

Instance Method Details

#cartesian_chart(options = {}) ⇒ Object

Permits to draw a linear chart (line, spline, bar) Values are represented with regular interval

series:    (Array of) Hash for series
  name:       ID
  values:     Array of numeric values
  label:      Label for the legend
  ordinate:   Name of the used ordinate
  type:       One of: line, spline, bar
  area:       Boolean
  style:      Styles
abscissa:   X axis details
  label:      Label for the X axis
  values:     Array of labels used for indexes
ordinates: (Array of) Hash for Y axes
  name:       ID
  label:      Name of the Y axis
engine:     Engine for rendering. c3 by default.

119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/helpers/charts_helper.rb', line 119

def cartesian_chart(options = {})
  html_options = options.slice!(:series, :abscissa, :ordinates, :engine)
  options[:type] = :time
  # TODO: Check options validity
  options[:series] = [options[:series]] unless options[:series].is_a?(Array)
  options[:series].each do |serie|
    serie[:values].each do |coordinates|
      coordinates[0] = coordinates[0].utc.l(format: '%Y-%m-%dT%H:%M:%S')
    end
  end
  html_options[:class] ||= 'chart'
  html_options.deep_merge!(data: { chart: options.to_json })
  (:div, nil, html_options)
end

#category_chart(options = {}) ⇒ Object

Permits to draw a nonlinear chart (line, spline) Values are represented with given abscissa for each value

:abscissa
:ordinates
engine:     Engine for rendering. c3 by default.

93
94
95
96
97
98
99
100
# File 'app/helpers/charts_helper.rb', line 93

def category_chart(options = {})
  html_options = options.slice!(:series, :abscissa, :ordinates, :engine)
  options[:type] = :nonlinear
  # TODO: Check options validity
  html_options[:class] ||= 'chart'
  html_options.deep_merge!(data: { chart: options.to_json })
  (:div, nil, html_options)
end

#distribution_chart(_options = {}) ⇒ Object

Permit to produce pie or gauge Values are represented relatively to all

engine:     Engine for rendering. c3 by default.

Raises:

  • (NotImplemented)

84
85
86
# File 'app/helpers/charts_helper.rb', line 84

def distribution_chart(_options = {})
  raise NotImplemented
end

#ligthen(color, rate) ⇒ Object


30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/helpers/charts_helper.rb', line 30

def ligthen(color, rate)
  r = color[1..2].to_i(16)
  g = color[3..4].to_i(16)
  b = color[5..6].to_i(16)
  r *= (1 + rate)
  g *= (1 + rate)
  b *= (1 + rate)
  r = 255 if r > 255
  g = 255 if g > 255
  b = 255 if b > 255
  '#' + r.to_i.to_s(16).rjust(2, '0') + g.to_i.to_s(16).rjust(2, '0') + b.to_i.to_s(16).rjust(2, '0')
end

#normalize_serie(values, x_values, default = 0.0) ⇒ Object


75
76
77
78
79
# File 'app/helpers/charts_helper.rb', line 75

def normalize_serie(values, x_values, default = 0.0)
  x_values.map do |x|
    (values[x] || default).to_s.to_f
  end
end

#tree_distribution_chart(_options = {}) ⇒ Object

Permit to produce pie or gauge Values are represented relatively to all

engine:     Engine for rendering. c3 by default.

Raises:

  • (NotImplemented)

137
138
139
# File 'app/helpers/charts_helper.rb', line 137

def tree_distribution_chart(_options = {})
  raise NotImplemented
end