class ChartBibz::ViewComponents::ChartViewComponent

Generate the chart view through the render method

Public Class Methods

new(data = {}, options = {}, html_options = {}) click to toggle source

Need of CanvasViewComponent to work

@example

ChartBibz::ViewComponents::ChartViewComponent.new(data, options, html_options)

@param data [Hash] the chartjs data @param options [Hash] the chartjs options @option options [Symbol] :type The chart type [:bar, :line, …]

@param html_options [Hash] the html_options of the canvas @option html_options [String] :id The id of the canvas @option html_options [String] :class The class of the canvas

@return [void]

@api public

# File lib/chart_bibz/view_components/chart_view_component.rb, line 26
def initialize(data = {}, options = {}, html_options = {})
  @data = data
  @options = options
  @html_options = html_options
end

Public Instance Methods

canvas() click to toggle source

Get the Canvas with new html_options

@example

ChartBibz::ViewComponents::ChartViewComponent.new(data, options, html_options).canvas

@see CanvasViewComponent @return [Object] The canvas object

@api public

# File lib/chart_bibz/view_components/chart_view_component.rb, line 52
def canvas
  @canvas ||= CanvasViewComponent.new(new_html_options)
end

Private Instance Methods

aria_label() click to toggle source

Create an aria lable

@return [String]

@api private

# File lib/chart_bibz/view_components/chart_view_component.rb, line 76
def aria_label
  @options[:title].nil? ? 'Chart' : "Chart of #{@options[:title]}"
end
new_html_options() click to toggle source

Update html_options

@return [Hash] The new html options with the data attributes

@api private

# File lib/chart_bibz/view_components/chart_view_component.rb, line 63
def new_html_options
  @html_options['data-cb-data'] = @data.to_json
  @html_options['data-cb-type'] = @options.delete(:type) || :bar
  @html_options['data-cb-options'] = @options.to_json
  @html_options['aria-label'] = aria_label
  @html_options
end