Module: ChartHelper

Defined in:
app/helpers/chart_helper.rb

Instance Method Summary collapse

Instance Method Details

#bar(id = nil, size = nil, data = nil) ⇒ Object

Chartjs



94
95
96
97
98
99
100
101
102
103
104
# File 'app/helpers/chart_helper.rb', line 94

def bar id=nil, size=nil, data=nil
  html  = "<canvas id=\"bar_#{id}\" height=\"#{size[:height]}\" width=\"#{size[:width]}\"></canvas>".html_safe
  script = javascript_tag do
    <<-END.html_safe
      var barChartData = #{data}
      var ctx_#{id} = document.getElementById("bar_#{id}").getContext("2d");
      window.myBar_#{id} = new Chart(ctx_#{id}).Bar(barChartData, {});
    END
  end
  return html + script
end

#boxplot(id = nil, size = nil, data = nil, options = nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/helpers/chart_helper.rb', line 68

def boxplot id=nil, size=nil, data=nil, options=nil
  html = "<div style=\"width: #{size[:width]}px; height: #{size[:height]}px;\" class=\"gallery\" id=\"boxplot_#{id}\"><svg></svg></div>".html_safe
  script = javascript_tag do
    <<-END.html_safe
      nv.addGraph(function() {
        var chart = nv.models.boxPlotChart()
            .x(function(d) { return d.label })
            .y(function(d) { return d.values.Q3 })
            .staggerLabels(true)
            .maxBoxWidth(10) // prevent boxes from being incredibly wide
            .yDomain([0, 700]);
        d3.select('#boxplot_#{id} svg')
            .datum(#{data})
            .call(chart);
        nv.utils.windowResize(chart.update);
        return chart;
      });
    END
  end

  return html + script
end

#discrete_bar(id = nil, size = nil, data = nil, options = nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/helpers/chart_helper.rb', line 43

def discrete_bar id=nil, size=nil, data=nil, options=nil
  html = "<div style=\"width: #{size[:width]}px; height: #{size[:height]}px;\" id=\"discrete_bar_#{id}\"><svg></svg></div>".html_safe
  script = javascript_tag do
    <<-END.html_safe
      nv.addGraph(function() {
          var chart = nv.models.discreteBarChart()
              .x(function(d) { return d.label })
              .y(function(d) { return d.value })
              .staggerLabels(true)
              //.staggerLabels(historicalBarChart[0].values.length > 8)
              .showValues(true)
              //.duration(250);

          d3.select('#discrete_bar_#{id} svg')
              .datum(#{data})
              .call(chart);
          nv.utils.windowResize(chart.update);
          return chart;
      });
  END
  end

  return html + script
end

#horizontal_grouped_bar(id = nil, size = nil, data = nil, options = nil) ⇒ Object



3
4
5
6
7
8
9
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
36
37
38
39
40
41
# File 'app/helpers/chart_helper.rb', line 3

def horizontal_grouped_bar id=nil, size=nil, data=nil, options=nil
  html = "<div style=\"width: #{size[:width]}px; height: #{size[:height]}px;\" id=\"horizontal_grouped_bar_#{id}\"><svg></svg></div>".html_safe
  script = javascript_tag do
    <<-END.html_safe
    var chart;
    nv.addGraph(function() {
        chart = nv.models.multiBarHorizontalChart()
            .x(function(d) { return d.label })
            .y(function(d) { return d.value })
            //.yErr(function(d) { return [-Math.abs(d.value * Math.random() * 0.3), Math.abs(d.value * Math.random() * 0.3)] })
            //.barColor(d3.scale.category20().range())
            //.duration(250)
            .showValues(true)
            //.margin({left: 100})
            .stacked(false);

        chart.yAxis.tickFormat(d3.format(',.2f'));

        chart.yAxis.axisLabel('Y Axis');
        chart.xAxis.axisLabel('X Axis').axisLabelDistance(100);

        d3.select('#horizontal_grouped_bar_#{id} svg')
            .datum(#{data})
            .call(chart);

        nv.utils.windowResize(chart.update);

        chart.dispatch.on('stateChange', function(e) { nv.log('New State:', JSON.stringify(e)); });
        chart.state.dispatch.on('change', function(state){
            nv.log('state', JSON.stringify(state));
        });
        return chart;
    });

  END
  end

  return html + script
end

#line(id = nil, size = nil, data = nil) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
# File 'app/helpers/chart_helper.rb', line 106

def line id=nil, size=nil, data=nil
  html  = "<canvas id=\"line_#{id}\" height=\"#{size[:height]}\" width=\"#{size[:width]}\"></canvas>".html_safe
  script = javascript_tag do
    <<-END.html_safe
      var lineChartData = #{data}
      var ctx_#{id} = document.getElementById("line_#{id}").getContext("2d");
      window.myLine_#{id} = new Chart(ctx_#{id}).Line(lineChartData, {});
    END
  end
  return html + script
end

#pie(id = nil, size = nil, data = nil) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
# File 'app/helpers/chart_helper.rb', line 118

def pie id=nil, size=nil, data=nil
  html  = "<canvas id=\"pie_#{id}\" height=\"#{size[:height]}\" width=\"#{size[:width]}\"></canvas>".html_safe
  script = javascript_tag do
    <<-END.html_safe
      var pieData = #{data}
		var ctx_#{id} = document.getElementById("pie_#{id}").getContext("2d");
		window.myPie_#{id} = new Chart(ctx_#{id}).Pie(pieData);
    END
  end
  return html + script
end

#radar(id = nil, size = nil, data = nil) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
# File 'app/helpers/chart_helper.rb', line 130

def radar id=nil, size=nil, data=nil
  html  = "<canvas id=\"radar_#{id}\" height=\"#{size[:height]}\" width=\"#{size[:width]}\"></canvas>".html_safe
  script = javascript_tag do
    <<-END.html_safe
      var radarChartData = #{data}
      window.myRadar_#{id} = new Chart(document.getElementById("radar_#{id}").getContext("2d")).Radar(radarChartData, {
      });
    END
  end
  return html + script
end