Class: HighChart

Inherits:
Object
  • Object
show all
Defined in:
lib/high_chart.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(series, options = {}) ⇒ HighChart

Returns a new instance of HighChart.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/high_chart.rb', line 10

def initialize(series, options={})
  unless series.kind_of?(Array)
    raise HighChartError, "series must be an Array"
  end

  if options[:x_axis_labels] == nil
    raise HighChartError, "X-Axis labels are required"
  end

  if options[:colors] != nil && !options[:colors].kind_of?(Array)
    raise HighChartError, "Colors must be an Array"
  end

  if options[:config_file]
    read_defaults(options[:config_file])
  end

  options.each do |k, v|
    if self.respond_to?(k.to_sym)
      merged_value = merge_defaults(k, v)
      self.instance_variable_set(("@#{k.to_s}").to_sym, merged_value)
    end
  end

  @series = series
end

Instance Attribute Details

#chartObject

Returns the value of attribute chart.



6
7
8
# File 'lib/high_chart.rb', line 6

def chart
  @chart
end

#chart_divObject

Returns the value of attribute chart_div.



6
7
8
# File 'lib/high_chart.rb', line 6

def chart_div
  @chart_div
end

#chart_typeObject

Returns the value of attribute chart_type.



6
7
8
# File 'lib/high_chart.rb', line 6

def chart_type
  @chart_type
end

#colorsObject

Returns the value of attribute colors.



6
7
8
# File 'lib/high_chart.rb', line 6

def colors
  @colors
end

#creditsObject

Returns the value of attribute credits.



6
7
8
# File 'lib/high_chart.rb', line 6

def credits
  @credits
end

#globalObject

Returns the value of attribute global.



6
7
8
# File 'lib/high_chart.rb', line 6

def global
  @global
end

#labelsObject

Returns the value of attribute labels.



6
7
8
# File 'lib/high_chart.rb', line 6

def labels
  @labels
end

#langObject

Returns the value of attribute lang.



6
7
8
# File 'lib/high_chart.rb', line 6

def lang
  @lang
end

#legendObject

Returns the value of attribute legend.



6
7
8
# File 'lib/high_chart.rb', line 6

def legend
  @legend
end

#loadingObject

Returns the value of attribute loading.



6
7
8
# File 'lib/high_chart.rb', line 6

def loading
  @loading
end

#plot_optionsObject

Returns the value of attribute plot_options.



6
7
8
# File 'lib/high_chart.rb', line 6

def plot_options
  @plot_options
end

#pointObject

Returns the value of attribute point.



6
7
8
# File 'lib/high_chart.rb', line 6

def point
  @point
end

#seriesObject

Returns the value of attribute series.



6
7
8
# File 'lib/high_chart.rb', line 6

def series
  @series
end

#subtitleObject

Returns the value of attribute subtitle.



6
7
8
# File 'lib/high_chart.rb', line 6

def subtitle
  @subtitle
end

#symbolsObject

Returns the value of attribute symbols.



6
7
8
# File 'lib/high_chart.rb', line 6

def symbols
  @symbols
end

#titleObject

Returns the value of attribute title.



6
7
8
# File 'lib/high_chart.rb', line 6

def title
  @title
end

#toolbarObject

Returns the value of attribute toolbar.



6
7
8
# File 'lib/high_chart.rb', line 6

def toolbar
  @toolbar
end

#tooltipObject

Returns the value of attribute tooltip.



6
7
8
# File 'lib/high_chart.rb', line 6

def tooltip
  @tooltip
end

#widthObject

Returns the value of attribute width.



6
7
8
# File 'lib/high_chart.rb', line 6

def width
  @width
end

#x_axisObject

Returns the value of attribute x_axis.



6
7
8
# File 'lib/high_chart.rb', line 6

def x_axis
  @x_axis
end

#x_axis_labelsObject

Returns the value of attribute x_axis_labels.



6
7
8
# File 'lib/high_chart.rb', line 6

def x_axis_labels
  @x_axis_labels
end

#y_axisObject

Returns the value of attribute y_axis.



6
7
8
# File 'lib/high_chart.rb', line 6

def y_axis
  @y_axis
end

Instance Method Details

#merge_defaults(key, value) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/high_chart.rb', line 37

def merge_defaults(key, value)
  default_value = self.send(key.to_s.to_sym)
  if default_value.kind_of?(Hash) && value.kind_of?(Hash)
    default_value.stringify_keys!.merge!(value.stringify_keys!)
  else
    default_value = value
  end

  return default_value
end

#read_defaults(config_file) ⇒ Object

read a yaml file of defaults and write out the options



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/high_chart.rb', line 49

def read_defaults(config_file)
  begin
    defaults = YAML.load_file(config_file)

    unless defaults == false
      defaults.each do |k, v|
        if self.respond_to?(k.to_sym)
          self.instance_variable_set(("@" + k.to_s).to_sym, v)
        end
      end
    end
  rescue Exception => e
    puts e.message
    puts e.backtrace
    raise e
  end
end

#to_jsonObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/high_chart.rb', line 104

def to_json
  json_hash = {
          :chart => chart,
          :colors => colors,
          :credits => credits,
          :global => global,
          :labels => labels,
          :lang => lang,
          :legend => legend,
          :loading => loading,
          :plotOptions => plot_options,
          :point => point,
          :subtitle => subtitle,
          :symbols => symbols,
          :title => title,
          :toolbar => toolbar,
          :tooltip => tooltip,
          :xAxis => x_axis,
          :yAxis => y_axis,
          :series => series,
          :width => width
  }

  clean_json_hash = json_hash.reject do |key, value|
    value == nil
  end

  return clean_json_hash.to_json
end