Sha256: 364b98d50cab37117ce5afc217d9d53382ba2928cafcba5f21d5ff6bd2942990

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

module Highcharts
  class Axis < Base
    
    attr_accessor :title, :categories, :tickInterval, :min, :max, :labels, :opposite
    
    def to_s
      "{" +
        [render_title, render_categories, render_tickInterval, render_options(:objects => 'min max opposite'), render_labels].flatten.compact.join(',') +
      "}"
    end
    
    def render_title
      title.present? ? "title: {text: '#{title}'}" : nil
    end
    
    def render_categories
      return nil unless categories.present?
      "categories: [#{categories.collect {|c| "'#{format_category(c)}'"}.join(', ')}]" # need to encapsulate each category in quotes and format it before joining them with a comma
    end
    
    def render_tickInterval
      return tickInterval if tickInterval.present?
      return nil unless categories.present?
      "tickInterval: #{(Math.sqrt(categories.length) < 5 ? 1 : Math.sqrt(categories.length)).floor}"
    end
    
    def format_category(category)
      case category
      when Date
        category.strftime("%b. %d")
      when Time
        category.strftime("%H:%M")
      when DateTime
        category.strftime("%b. %d, %H:%M")
      else
        category.to_s
      end
    end
    
    def render_labels
      return nil unless labels.present?
      "labels: {" +
        "formatter: function(){ return '#{labels[:prepend]}' + this.value + '#{labels[:append]}'; }" +
      "}"
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
highcharts-js-rails-0.0.1 lib/highcharts/axis.rb