Class: Dashboards::Chart
- Inherits:
-
Object
- Object
- Dashboards::Chart
- Defined in:
- lib/dashboards/dsl/chart.rb
Constant Summary collapse
- VALID_TYPES =
[:line, :pie, :column, :bar, :area, :scatter]
- DEFAULT_HEIGHT =
'120px'
- DEFAULT_COLOR =
White
'#FFFFFF'
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#initialize(name_or_options, options = {}) ⇒ Chart
constructor
A new instance of Chart.
- #render(context) ⇒ Object
Constructor Details
#initialize(name_or_options, options = {}) ⇒ Chart
Returns a new instance of Chart.
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/dashboards/dsl/chart.rb', line 12 def initialize(, = {}) if .is_a?(Hash) @name = nil = else @name = end @type = .delete(:type) || :line @data = .delete(:data) @options = @options[:height] ||= [:height] || DEFAULT_HEIGHT @options[:color] ||= DEFAULT_COLOR end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
5 6 7 |
# File 'lib/dashboards/dsl/chart.rb', line 5 def data @data end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/dashboards/dsl/chart.rb', line 5 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/dashboards/dsl/chart.rb', line 5 def @options end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
5 6 7 |
# File 'lib/dashboards/dsl/chart.rb', line 5 def type @type end |
Instance Method Details
#render(context) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/dashboards/dsl/chart.rb', line 26 def render(context) data = @data.is_a?(Proc) ? context.instance_exec(&@data) : @data chartkick_method = chartkick_method(@type) = @options.dup [:title] = @name if @name # Apply color to different chart types case @type when :pie, :donut [:colors] ||= [@options[:color]] when :column, :bar [:colors] ||= [@options[:color]] else [:dataset] ||= { borderColor: @options[:color], backgroundColor: @options[:color], pointBackgroundColor: @options[:color], pointBorderColor: @options[:color], pointHoverBackgroundColor: @options[:color], pointHoverBorderColor: @options[:color], hoverBackgroundColor: @options[:color], hoverBorderColor: @options[:color] } end # Apply height [:height] = @options[:height] raise ArgumentError, "Invalid chart type: #{@type}" unless VALID_TYPES.include?(@type) if context.respond_to?(chartkick_method) context.public_send(chartkick_method, data, **) else render_fallback(data) end end |