Sha256: bb6e47a29e5b6ad72725c6b22005984c438b4183e8ed9c936555e750648bffcc

Contents?: true

Size: 1.73 KB

Versions: 3

Compression:

Stored size: 1.73 KB

Contents

class Plasticine::Builder::Line < Plasticine::Builder::Base
  attr_reader :lines

  def initialize(id, options={})
    super

    @visual.merge! lines: [], nature: 'line', axis_x_format: :string, axis_y_format: :number, axis_y_tick_count: 10, chart_layout: 'line', order: nil, quarter_start_month: 1
    @lines = {}
  end


  def axis_x_format=(format)
    @visual[:axis_x_format] = format
  end

  def axis_y_format=(format)
    @visual[:axis_y_format] = format
  end

  def axis_y_tick_count=(tick_count)
    @visual[:axis_y_tick_count] = tick_count
  end

  def chart_layout=(clayout)
    @visual[:chart_layout] = clayout
  end

  def quarter_start_month=(month)
    @visual[:quarter_start_month] = month
  end

  def order=(direction)
    @visual[:order] = direction # asc or desc
  end

  def add_line(id, label)
    @lines[id] = { label: label, dots: [], total_y: 0 } if not @lines[id]
  end

  def add_dot(line_id, x, y)
    @lines[line_id][:dots] << { x: x, y: y }
    @lines[line_id][:total_y] += y
  end

  def close_visual
    super

    #@chart[:legend] = (@chart[:lines].length > 1) if @chart[:legend].nil?
    set_max_y
    @visual[:lines] = @lines.each_value.map { |d| d }

    if @visual[:order]
      @visual[:lines] = @visual[:lines].sort_by{ |l| l[:total_y] }
      @visual[:lines].reverse! if @visual[:order] == 'asc'
    end

  end

  def set_max_y
    stacks = {}
    max_y = 0

    @lines.each_value do |line|
      line[:dots].each do |dot|
        key = dot[:x].to_s
        stacks[key] = 0 if not stacks[key]
        stacks[key] += dot[:y]
        max_y = dot[:y] if max_y < dot[:y]
      end
    end

    @visual[:max_y_stack] = stacks.max_by{|k,v| v}[1] # Return the highest stacked value for a specific X
    @visual[:max_y] = max_y
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
plasticine-1.2.3 lib/plasticine/builder/line.rb
plasticine-1.2.2 lib/plasticine/builder/line.rb
plasticine-1.2.1 lib/plasticine/builder/line.rb