Sha256: 7fad44fc90989ee07093c019c784544379903f41231cc4bed5cf99b211cd3032

Contents?: true

Size: 1.43 KB

Versions: 2

Compression:

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

    private
      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

2 entries across 2 versions & 1 rubygems

Version Path
highcharts-js-rails-0.0.3 lib/highcharts/axis.rb
highcharts-js-rails-0.0.2 lib/highcharts/axis.rb