class ApexCharts::OptionsBuilder

Attributes

built[R]

Public Class Methods

new(sample, options) click to toggle source
# File lib/apex_charts/options_builder.rb, line 28
def initialize(sample, options)
  @options = camelize_keys(options)
  if options[:plotOptions]&.[](:bar)&.[](:horizontal)
    @ytype = type(sample)
  else
    @xtype = type(sample)
  end
  @built = {}
end

Public Instance Methods

build_annotations() click to toggle source
# File lib/apex_charts/options_builder.rb, line 79
def build_annotations
  annotations = @options.delete :annotations
  @built[:annotations] = (
    if annotations.is_a? Hash
      options_class(:Annotations).check annotations.compact
    end
  )
end
build_chart() click to toggle source
# File lib/apex_charts/options_builder.rb, line 88
def build_chart
  @built[:chart] =
    if target = @options.delete(:brushTarget)
      {
        brush: {enabled: true, target: target.to_s},
        selection: {enabled: true}
      }
    else
      {}
    end

  @built[:chart].merge!({
    id: @options[:chartId] || @options[:id],
    group: @options.delete(:group),
    height: @options.delete(:height) { target ? 180 : 400 },
    width: @options.delete(:width),
    stacked: @options.delete(:stacked),
    animations: enabled(@options.delete(:animations)),
    sparkline: enabled(@options.delete(:sparkline)),
    background: @options.delete(:background),
    foreColor: @options.delete(:foreColor)
  }.compact)

  chart = @options.delete :chart

  return unless chart.is_a? Hash

  @built[:chart].merge! options_class(:Chart).check(chart.compact)
end
build_colors() click to toggle source
# File lib/apex_charts/options_builder.rb, line 118
def build_colors
  colors = @options.delete :colors
  colors &&= Array(colors)
  @built[:colors] = colors
end
build_data_labels() click to toggle source
# File lib/apex_charts/options_builder.rb, line 124
def build_data_labels
  data_labels = @options.delete :dataLabels
  return if data_labels.nil?

  @built[:dataLabels] = if [true, false].include? data_labels
                          {enabled: data_labels}
                        elsif data_labels.is_a? Hash
                          options_class(:DataLabels).check data_labels.compact
                        end
end
build_defer() click to toggle source
# File lib/apex_charts/options_builder.rb, line 135
def build_defer
  defer = @options.delete :defer
  @built[:defer] = defer == true
end
build_div() click to toggle source
# File lib/apex_charts/options_builder.rb, line 70
def build_div
  @built[:div] = {
    id: @options.delete(:id),
    var: @options.delete(:var),
    class: @options.delete(:class),
    style: @options.delete(:style)
  }.compact
end
build_fill() click to toggle source
# File lib/apex_charts/options_builder.rb, line 141
def build_fill
  fill = @options.delete :fill
  @built[:fill] = if fill.is_a? String
                    {type: fill}
                  elsif fill.is_a? Hash
                    options_class(:Fill).check fill.compact
                  end
end
build_general_options() click to toggle source
# File lib/apex_charts/options_builder.rb, line 46
def build_general_options
  build_annotations
  build_colors
  build_data_labels
  build_defer
  build_fill
  build_grid
  build_labels
  build_legend
  build_markers
  build_no_data
  build_plot_options
  build_responsive
  build_states
  build_stroke
  build_subtitle
  build_theme
  build_title
  build_tooltip
  build_xaxis
  build_yaxis
  built.compact
end
build_grid() click to toggle source
# File lib/apex_charts/options_builder.rb, line 150
def build_grid
  grid = @options.delete :grid
  @built[:grid] = if [true, false].include? grid
                    {show: grid}
                  elsif grid.is_a? Hash
                    options_class(:Grid).check grid.compact
                  end
end
build_labels() click to toggle source
# File lib/apex_charts/options_builder.rb, line 159
def build_labels
  labels = @options.delete :labels
  labels &&= Array(labels)
  @built[:labels] = labels
end
build_legend() click to toggle source
# File lib/apex_charts/options_builder.rb, line 165
def build_legend
  legend = @options.delete :legend
  @built[:legend] = if [true, false].include? legend
                      {show: legend}
                    elsif legend.is_a? String
                      {show: true, position: legend}
                    elsif legend.is_a? Hash
                      options_class(:Legend).check legend.compact
                    end
end
build_markers() click to toggle source
# File lib/apex_charts/options_builder.rb, line 176
def build_markers
  markers = @options.delete :markers
  @built[:markers] = if markers.is_a? String
                       {shape: markers}
                     elsif markers.is_a? Hash
                       options_class(:Markers).check markers.compact
                     end
end
build_no_data() click to toggle source
# File lib/apex_charts/options_builder.rb, line 185
def build_no_data
  no_data = @options.delete :noData
  @built[:noData] = if no_data.is_a? String
                      {text: no_data}
                    elsif no_data.is_a? Hash
                      options_class(:NoData).check no_data.compact
                    end
end
build_options() click to toggle source
# File lib/apex_charts/options_builder.rb, line 38
def build_options
  options_class(:Root).check @options

  build_chart
  build_div
  build_general_options
end
build_plot_options() click to toggle source
# File lib/apex_charts/options_builder.rb, line 194
def build_plot_options
  plot_options = @options.delete :plotOptions
  return unless plot_options.is_a? Hash

  @built[:plotOptions] =
    options_class(:Plot).check plot_options.compact
end
build_responsive() click to toggle source
# File lib/apex_charts/options_builder.rb, line 202
def build_responsive
  responsive = @options.delete :responsive
  responsive &&= responsive.is_a?(Hash) ? [responsive] : Array(responsive)
  @built[:responsive] = responsive
end
build_states() click to toggle source
# File lib/apex_charts/options_builder.rb, line 208
def build_states
  @built[:states] = {
    normal: filter_type_hash(@options.delete(:normal)),
    hover: filter_type_hash(@options.delete(:hover)),
    active: filter_type_hash(@options.delete(:active))
  }.compact

  states = @options.delete :states
  @built[:states].merge! options_class(:States).check(states.compact) if states.is_a? Hash

  @built[:states] = nil if @built[:states].empty?
end
build_stroke() click to toggle source
# File lib/apex_charts/options_builder.rb, line 221
def build_stroke
  curve = @options.delete :curve
  @built[:stroke] = {curve: curve}.compact

  stroke = @options.delete :stroke
  if [true, false].include? stroke
    @built[:stroke].merge!(show: stroke)
  elsif stroke.is_a? Hash
    @built[:stroke].merge! options_class(:Stroke).check(stroke.compact)
  end

  @built[:stroke] = nil if @built[:stroke].empty?
end
build_subtitle() click to toggle source
# File lib/apex_charts/options_builder.rb, line 235
def build_subtitle
  subtitle = @options.delete(:subtitle)
  @built[:subtitle] = if subtitle.is_a? String
                        {text: subtitle}
                      elsif subtitle.is_a? Hash
                        options_class(:TitleSubtitle).check subtitle.compact
                      end
end
build_theme() click to toggle source
# File lib/apex_charts/options_builder.rb, line 244
def build_theme
  theme = @options.delete(:theme)
  @built[:theme] = if theme.is_a? String
                     case theme
                     when 'random'
                       resolve_theme(Theme.all_palettes.sample)
                     when 'monochrome'
                       {monochrome: {enabled: true}}
                     else
                       resolve_theme(theme)
                     end
                   elsif theme.is_a? Hash
                     options_class(:Theme).check theme.compact
                   end
end
build_title() click to toggle source
# File lib/apex_charts/options_builder.rb, line 260
def build_title
  title = @options.delete(:title)
  @built[:title] = if title.is_a? String
                     {text: title}
                   elsif title.is_a? Hash
                     options_class(:TitleSubtitle).check title.compact
                   end
end
build_tooltip() click to toggle source
# File lib/apex_charts/options_builder.rb, line 269
def build_tooltip
  tooltip = @options.delete :tooltip
  @built[:tooltip] = if [true, false].include? tooltip
                       {enabled: tooltip}
                     elsif tooltip.is_a? Hash
                       options_class(:Tooltip).check tooltip.compact
                     end
end
build_xaxis() click to toggle source
# File lib/apex_charts/options_builder.rb, line 278
def build_xaxis
  xaxis = @options.delete :xaxis
  @built[:xaxis] = {
    type: @options.delete(:xtype) { @xtype },
    title: {
      text: @options.delete(:xtitle)
    }.compact
  }.compact
  @built[:xaxis].delete(:title) if @built[:xaxis][:title].empty?

  if xaxis.is_a? String
    @built[:xaxis][:title] = {text: xaxis}
  elsif xaxis.is_a? Hash
    options_class(:XAxis).check xaxis
    @built[:xaxis].merge! xaxis
  end

  @built[:xaxis] = nil if @built[:xaxis].empty?
end
build_yaxis() click to toggle source
# File lib/apex_charts/options_builder.rb, line 298
def build_yaxis
  yaxis = @options.delete :yaxis
  @built[:yaxis] = [{
    type: @options.delete(:ytype) { @ytype },
    title: {
      text: @options.delete(:ytitle)
    }.compact
  }.compact]
  @built[:yaxis][0].delete(:title) if @built[:yaxis][0][:title].empty?

  if yaxis.is_a? String
    @built[:yaxis][0][:title] = {text: yaxis}
  elsif yaxis.is_a? Hash
    options_class(:YAxis).check yaxis
    @built[:yaxis][0].merge! yaxis
  end

  @built[:yaxis] = nil if @built[:yaxis].all?(&:empty?)
end

Private Instance Methods

boolean_to_hash(options) { |options| ... } click to toggle source
# File lib/apex_charts/options_builder.rb, line 326
def boolean_to_hash(options)
  return if options.nil?

  if [true, false].include? options
    yield(options)
  elsif options.is_a?(Hash)
    options.compact
  end
end
enabled(options) click to toggle source
# File lib/apex_charts/options_builder.rb, line 320
def enabled(options)
  boolean_to_hash(options) do |opts|
    {enabled: opts}
  end
end
filter_type_hash(state) click to toggle source
# File lib/apex_charts/options_builder.rb, line 336
def filter_type_hash(state)
  if state.is_a? String
    {filter: {type: state}}
  elsif state.is_a? Hash
    state.compact
  end
end
options_class(name) click to toggle source
# File lib/apex_charts/options_builder.rb, line 353
def options_class(name)
  schema = ApexCharts.config.schema
  ApexCharts.const_get "Options::#{schema}::#{name}Options"
end
resolve_theme(theme) click to toggle source
# File lib/apex_charts/options_builder.rb, line 344
def resolve_theme(theme)
  if Theme::PALETTES.include? theme
    {palette: theme}
  elsif Theme.palette_names.include? theme
    @built[:colors] = Theme.get_colors(theme)
    nil
  end
end