Sha256: e5ac32618a948c2c6aaf4afe146296224e837c04708ffdab4aedfe93aa47a414

Contents?: true

Size: 1.94 KB

Versions: 5

Compression:

Stored size: 1.94 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', max_y_ratio: 1, 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 max_y_ratio=(max_y_ratio)
    @visual[:max_y_ratio] = max_y_ratio
  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

    if max_y == 0
      @visual[:max_y_stack] = 0
      @visual[:max_y] = 0
    else
      @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 * @visual[:max_y_ratio]
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
plasticine-1.2.8 lib/plasticine/builder/line.rb
plasticine-1.2.7 lib/plasticine/builder/line.rb
plasticine-1.2.6 lib/plasticine/builder/line.rb
plasticine-1.2.5 lib/plasticine/builder/line.rb
plasticine-1.2.4 lib/plasticine/builder/line.rb