Class: Appoxy::UI::Visualizations

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

Instance Method Summary collapse

Instance Method Details

#add_chart_startObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/ui/visualizations.rb', line 128

def add_chart_start
  "<script type=\"text/javascript\">
    var #{@id}_chart;
    $(document).ready(function() {
 #{@id}_chart = new Highcharts.Chart({
    chart: {
       renderTo: '#{@div_id}',
       plotBackgroundColor: null,
       plotBorderWidth: null,
       plotShadow: false,
       defaultSeriesType: '#{@type}'
    },
"
end

#add_data(data) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ui/visualizations.rb', line 97

def add_data(data)
  s    = ""
#        data ||= @options[:data]
  if data.is_a?(Array)
    data.each_with_index do |d, i|
      if d.is_a?(Array)
        s << "['#{d[0]}', #{d[1]}]"
      elsif d.is_a?(Hash)
        s << "
            {
                   name: '#{d[:name]}',
                   y: #{d[:y]}
                   // sliced: #{d[:sliced]},
                   // selected: #{d[:selected]}
                }"
      else
        raise "Expected array or hash for data series elements."
      end
      if i < data.size - 1
        s << ",\n"
      end
    end
  end
  s
end

#add_divObject



124
125
126
# File 'lib/ui/visualizations.rb', line 124

def add_div
  "<div id=\"#{@div_id}\" style=\"width:#{@width}; #{@height ? 'height:' + @height : ""}\"></div>"
end

#add_seriesObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ui/visualizations.rb', line 76

def add_series
  s      = "series: [
"
  series = @options[:series]
  series.each_with_index do |ser, i|
    # each series is a hash
    s << "{
       type: '#{@type}',
       name: '#{ser[:name]}',
       data: ["
    s << add_data(ser[:data])
    s << "
    }
    "
    if i < series.size - 1
      s << ",\n"
    end
  end
  s
end

#add_titleObject



143
144
145
146
147
148
149
150
151
# File 'lib/ui/visualizations.rb', line 143

def add_title
  s= ""
  if @options[:title]
    s << "title: {
       text: '#{@options[:title]}'
    },"
  end
  ""
end

#add_tooltipObject



6
7
8
9
10
11
12
13
14
# File 'lib/ui/visualizations.rb', line 6

def add_tooltip
  "
    tooltip: {
       formatter: function() {
          return '<b>'+ this.point.name +'</b>: '+ this.y;
       }
    },
    "
end

#common_values(options) ⇒ Object



153
154
155
156
157
158
# File 'lib/ui/visualizations.rb', line 153

def common_values(options)
  @id     = options[:title].underscore
  @div_id = "#{@id}_div" || "#{@type}_div"
  @width  = options[:width] || "100%"
  @height = options[:height] || nil
end

#pie(options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ui/visualizations.rb', line 16

def pie(options={})

  @type    = 'pie'

  @options = options
  common_values(options)

  s = ""
  s << add_div()
  s << add_chart_start()
  s << "
  plotOptions: {
       pie: {
          allowPointSelect: true,
          cursor: 'pointer',
          dataLabels: {
             enabled: false,
             color: '#000000',
             connectorColor: '#000000',
             formatter: function() {
                return '<b>'+ this.point.name +'</b>: '+ this.y +' %';
             }
          },
          showInLegend: true
       }
    },
  "
  s << add_title

  s << add_tooltip()

  unless @options[:series]
    # make the default series
    serieses = [{:name=>options[:title, :data=>options[:data]]}]
    @options[:series] = serieses
  end

  if @options[:series]
    s << add_series
  else
    # todo: DELETE THIS SECTION
    # Single set of data
#          s << "
#           series: [{
#             type: '#{@type}',
#             name: '#{options[:title]}',
#             data: [
#    "
#          s << add_data
#          s << "]
#          }]"
  end
  s << "
 });
    });
    </script>\n"
  s.html_safe

end