class PiCharts::Base

The Base class is responsible for providing the boiler plate code for all of the different types of PiCharts that someone can create with this gem. That's its job.

Attributes

cdn[RW]
config[RW]

Public Class Methods

new(args = {}) click to toggle source

Every class has a config and, depending on the type the create method will help with setting up the config for that specific type of chart.

# File lib/pi_charts/base.rb, line 12
def initialize(args = {})
  @config = Config.new
  create
end

Public Instance Methods

create() click to toggle source

The create() method helps with the main boiler-plate buisness that goes behind the Base class.

# File lib/pi_charts/base.rb, line 75
def create
  # helps with boilerplateness
end
hover(args = {}) click to toggle source

The hover() method provides an interface +that is currently incomplete+ to the hover options for a chart.

# File lib/pi_charts/base.rb, line 54
def hover(args = {})
  config.data[:options][:hover] = {}
  if args.keys.empty?
    config.data[:options][:hover][:mode] = 'nearest'
    config.data[:options][:hover][:intersect] = true
  else
    config.data[:options][:hover][:mode] = 'nearest' if args[:nearest]
    config.data[:options][:hover][:intersect] = true if args[:intersect]
    config.data[:options][:hover][:animationDuration] = args[:animation] if args[:animation]
  end
  # TODO: add support for onHover configuration option
  true
end
html(args = {}) click to toggle source

The html() method helps build the relevant html for the chart.

# File lib/pi_charts/base.rb, line 80
def html(args = {})
  id     = SecureRandom.uuid
  width  = args[:width]  || "50"
  config = args[:config] || @config.json
  type   = @config.type

  "<div id=\"canvas-holder\" style=\"width:#{width}%\">
    <canvas id=\"#{id}\" />
  </div>
  <script>
    var config = #{config}
    window.onload = function() {
      var ctx = document.getElementById(\"#{id}\").getContext(\"2d\");
      new Chart(ctx, config);
    };
  </script>" 
end
legend(args = {}) click to toggle source

The legend() provides an interface to set options for the legend for any chart.

# File lib/pi_charts/base.rb, line 39
def legend(args = {})
  config.data[:options][:legend] = {} unless config.data[:options][:legend]
  config.data[:options][:legend][:position]  = args[:position] if args [:position]
  config.data[:options][:legend][:display]   = true  if args[:display]
  config.data[:options][:legend][:display]   = false if args[:hidden]
  config.data[:options][:legend][:fullWidth] = true  if args[:full]
  config.data[:options][:legend][:reverse]   = true  if args[:reverse]
  # legend labels
  config.data[:options][:legend][:labels][:boxWidth]  = args[:width]   if args[:width]
  config.data[:options][:legend][:labels][:fontSize]  = args[:size]    if args[:size]
  config.data[:options][:legend][:labels][:fontStyle] = args[:style]   if args[:style]
  config.data[:options][:legend][:labels][:padding]   = args[:padding] if args[:padding]
end
random_color() click to toggle source

Sometimes you just need a random color.

# File lib/pi_charts/base.rb, line 69
def random_color
  "##{SecureRandom.hex(3)}"
end
responsive(responsive = true) click to toggle source

The responsive() method will make any chart responsive.

# File lib/pi_charts/base.rb, line 18
def responsive(responsive = true)
  if responsive
    config.data[:options][:responsive] = true
  else
    config.data[:options][:responsive] = false
  end
end