class Squid::Graph

@private

Public Class Methods

new(document, data = {}, settings = {}) click to toggle source
# File lib/squid/graph.rb, line 18
def initialize(document, data = {}, settings = {})
  @data, @settings = data, settings
  @plot = Plotter.new document, bottom: bottom
  @plot.paddings = {left: left.width, right: right.width} if @data.any?
end

Public Instance Methods

draw() click to toggle source
# File lib/squid/graph.rb, line 24
def draw
  @plot.box(h: height, border: border) { draw_graph if @data.any? }
end

Private Instance Methods

axis(first:, last:) click to toggle source
# File lib/squid/graph.rb, line 97
def axis(first:, last:)
  series = @data.values[first, last].map(&:values)
  options = {steps: steps, stack: stack?, format: formats[first]}
  Axis.new(series, options) {|label| @plot.width_of label}
end
bottom() click to toggle source
# File lib/squid/graph.rb, line 103
def bottom
  baseline ? 20 : 0
end
draw_axis_labels() click to toggle source
# File lib/squid/graph.rb, line 51
def draw_axis_labels
  @plot.axis_labels AxisLabel.for(left, align: :right, height: grid_height)
  @plot.axis_labels AxisLabel.for(right, align: :left, height: grid_height)
end
draw_categories() click to toggle source
# File lib/squid/graph.rb, line 56
def draw_categories
  labels = @data.values.first.keys.map{|key| key.to_s}
  @plot.categories labels, every: every, ticks: ticks
  @plot.horizontal_line 0.0
end
draw_chart(axis, second_axis: false) click to toggle source
# File lib/squid/graph.rb, line 67
def draw_chart(axis, second_axis: false)
  args = {minmax: axis.minmax, height: grid_height, stack: stack?}
  args[:labels] = items_of labels, skip_first_if: second_axis
  args[:formats] = items_of formats, skip_first_if: second_axis
  points = Point.for axis.data, args
  options = {colors: colors, starting_at: (second_axis ? 1: 0)}
  case (second_axis ? :column : type)
    when :point then @plot.points points, options
    when :line, :two_axis then @plot.lines points, options.merge(line_widths: line_widths)
    when :column then @plot.columns points, options
    when :stack then @plot.stacks points, options
  end
end
draw_charts() click to toggle source
# File lib/squid/graph.rb, line 62
def draw_charts
  draw_chart right, second_axis: true
  draw_chart left
end
draw_graph() click to toggle source
# File lib/squid/graph.rb, line 30
def draw_graph
  draw_legend if legend
  draw_gridlines
  draw_axis_labels
  draw_charts if chart
  draw_categories if baseline
end
draw_gridlines() click to toggle source
# File lib/squid/graph.rb, line 44
def draw_gridlines
  options = {height: grid_height, count: steps, skip_baseline: baseline}
  Gridline.for(options).each do |line|
    @plot.horizontal_line line.y, line_width: 0.5, transparency: 0.25
  end
end
draw_legend() click to toggle source
# File lib/squid/graph.rb, line 38
def draw_legend
  labels = @data.keys.reverse.map{|key| key.to_s}
  right = legend.is_a?(Hash) ? legend.fetch(:right, 0) : 0
  @plot.legend labels, right: right, colors: colors, height: legend_height
end
grid_height() click to toggle source
# File lib/squid/graph.rb, line 115
def grid_height
  height - bottom - legend_height - legend_bottom
end
items_of(array, skip_first_if:) click to toggle source
# File lib/squid/graph.rb, line 81
def items_of(array, skip_first_if:)
  if skip_first_if
    array.empty? ? [] : array[1..-1]
  else
    array
  end
end
left() click to toggle source
# File lib/squid/graph.rb, line 89
def left
  @left ||= axis first: 0, last: (two_axis? ? 1 : @data.size)
end
legend_bottom() click to toggle source
# File lib/squid/graph.rb, line 111
def legend_bottom
  legend.is_a?(Hash) ? legend.fetch(:bottom, 15) : 15
end
legend_height() click to toggle source
# File lib/squid/graph.rb, line 107
def legend_height
  legend ? 15 : 0
end
right() click to toggle source
# File lib/squid/graph.rb, line 93
def right
  @right ||= axis first: 1, last: (two_axis? ? @data.size : 0)
end
stack?() click to toggle source
# File lib/squid/graph.rb, line 119
def stack?
  type == :stack
end
two_axis?() click to toggle source
# File lib/squid/graph.rb, line 123
def two_axis?
  type == :two_axis
end