Sha256: f08ca58063d323f203cea97b1c365315a7399b9e16b7ff0d65a7bc745f5ca787

Contents?: true

Size: 1.74 KB

Versions: 1

Compression:

Stored size: 1.74 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

1 entries across 1 versions & 1 rubygems

Version Path
plasticine-1.2.0 lib/plasticine/builder/line.rb