Module: Chartx::Helper

Defined in:
lib/chartx/helper.rb

Instance Method Summary collapse

Instance Method Details

#bullet_chart(data_source, options = {}) ⇒ Object



39
40
41
# File 'lib/chartx/helper.rb', line 39

def bullet_chart(data_source, options = {})
  chartx "bullet", data_source, options
end

#chartx(chart_type, data_source, options, &block) ⇒ 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/chartx/helper.rb', line 43

def chartx(chart_type, data_source, options, &block)
  
  options = options.dup
  @chartx_id ||= 0
  
  height = options.delete(:height) || "600"
  width = options.delete(:width) || "600"
  elem_id = options.delete(:id) || "chart-#{@chartx_id += 1}"
  
  x_name = options.delete(:x_name) || "label"
  y_name = options.delete(:y_name) || "value"
  
  stagger_labels = options.delete(:stagger_labels) || true
  show_tooltips = options.delete(:show_tooltips) || false
  show_values = options.delete(:show_values) || false
  show_labels = options.delete(:show_labels) || true
  color = options.delete(:color) || "category10"
  show_controls = options.delete(:show_controls) || false
  
  show_dist_x = options.delete(:show_x_dist) || true
  show_dist_y = options.delete(:show_y_dist) || true
  
  x_axis_format = options.delete(:x_axis_format) || ',f'
  y_axis_format = options.delete(:y_axis_format) || ',.2f'
  y2_axis_format = options.delete(:y2_axis_format) || '.2f'
  
  clip_edge = options.delete(:clip_edge) || true
  duration = options.delete(:duration) || 500
  
  case chart_type
    when "discrete_bar"
      chart_str = "var chart = nv.models.discreteBarChart()
        .x(function(d) { return d.#{x_name} })
        .y(function(d) { return d.#{y_name} })
        .staggerLabels(#{stagger_labels})
        .tooltips(#{show_tooltips})
        .showValues(#{show_values})
        .width(#{width})
        .height(#{height});"
      
    when "pie"      
      data_source = [data_source] # bug of nvd3
      chart_str = "var chart = nv.models.pieChart()
        .x(function(d) { return d.#{x_name} })
        .y(function(d) { return d.#{y_name} })
        .values(function(d) { return d })  
        .color(d3.scale.#{color}().range())
        .width(#{width})
        .height(#{height});"

    when "multi_bar"    
      chart_str = "var chart = nv.models.multiBarChart()
        .x(function(d) { return d.#{x_name} })
        .y(function(d) { return d.#{y_name} })
        .width(#{width})
        .height(#{height})
        .staggerLabels(#{stagger_labels})
        .tooltips(#{show_tooltips})
        .showValues(#{show_values});
        chart.xAxis.tickFormat(d3.format('#{x_axis_format}'));
        chart.yAxis.tickFormat(d3.format('#{y_axis_format}'));" 

    when "line"
      chart_str = "var chart = nv.models.lineChart()
        .x(function(d) { return d.#{x_name} })
        .y(function(d) { return d.#{y_name} })
        .width(#{width})
        .height(#{height});
        chart.xAxis.tickFormat(d3.format('#{x_axis_format}'));
        chart.yAxis.tickFormat(d3.format('#{y_axis_format}'));"    
  
    when "multi_bar_horizontal"      
      chart_str = "var chart = nv.models.multiBarHorizontalChart()
        .x(function(d) { return d.#{x_name} })
        .y(function(d) { return d.#{y_name} })
        .showValues(#{show_values})
        .tooltips(#{show_tooltips})
        .showControls(#{show_controls})
        .width(#{width})
        .height(#{height});
        chart.yAxis.tickFormat(d3.format('#{x_axis_format}'));"
  
    when  "line_with_focus"       
      chart_str = "var chart = nv.models.lineWithFocusChart()
        .x(function(d) { return d.#{x_name} })
        .y(function(d) { return d.#{y_name} })
        .width(#{width})
        .height(#{height});
        chart.xAxis.tickFormat(d3.format('#{x_axis_format}'));
        chart.yAxis.tickFormat(d3.format('#{y_axis_format}'));
        chart.y2Axis.tickFormat(d3.format('#{y2_axis_format}'));"   
        
    when "scatter"       
      chart_str = "chart = nv.models.scatterChart()
        .x(function(d) { return d.#{x_name} })
        .y(function(d) { return d.#{y_name} })
        .showDistX(#{show_dist_x})
        .showDistY(#{show_dist_y})
        .useVoronoi(true)
        .color(d3.scale.#{color}.range())
        .width(#{width})
        .height(#{height});
        chart.xAxis.tickFormat(d3.format('#{x_axis_format}'));
        chart.yAxis.tickFormat(d3.format('#{y_axis_format}'));"
        
    when "stacked_area"       
      chart_str = "var chart = nv.models.stackedAreaChart()
        .x(function(d) { return d.#{x_name} })
        .y(function(d) { return d.#{y_name} })
        .clipEdge(#{clip_edge})
        .width(#{width})
        .height(#{height});
        chart.xAxis.tickFormat(d3.format('#{x_axis_format}'));
        chart.yAxis.tickFormat(d3.format('#{y_axis_format}'));"
  
    when "bullet"
      chart_str = "var chart = nv.models.bulletChart();"
  end
  
  html = <<-HTML
  <div id="#{ERB::Util.html_escape(elem_id)}" style="height: #{ERB::Util.html_escape(height)}px;">
  <svg></svg>
  </div>
  HTML

  js = <<-JS
  <script type="text/javascript">
    nv.addGraph(function() {
    #{chart_str}

    d3.select("##{elem_id} svg")
        .datum(#{data_source.to_json})
        .transition().duration(#{duration})
        .call(chart);

    return chart;
  });
  </script>
  JS

  if options[:content_for]
    content_for(options[:content_for]) { js.respond_to?(:html_safe) ? js.html_safe : js }
  else
    html += js
  end

  html.respond_to?(:html_safe) ? html.html_safe : html
  
end

#discrete_bar_chart(data_source, options = {}) ⇒ Object



11
12
13
# File 'lib/chartx/helper.rb', line 11

def discrete_bar_chart(data_source, options = {})
  chartx "discrete_bar", data_source, options
end

#line_chart(data_source, options = {}) ⇒ Object



23
24
25
# File 'lib/chartx/helper.rb', line 23

def line_chart(data_source, options = {}) 
  chartx "line", data_source, options
end

#line_with_focus_chart(data_source, options = {}) ⇒ Object



27
28
29
# File 'lib/chartx/helper.rb', line 27

def line_with_focus_chart(data_source, options = {})
  chartx "line_with_focus", data_source, options
end

#multi_bar_chart(data_source, options = {}) ⇒ Object



15
16
17
# File 'lib/chartx/helper.rb', line 15

def multi_bar_chart(data_source, options = {})
  chartx "multi_bar", data_source, options
end

#multi_bar_horizontal_chart(data_source, options = {}) ⇒ Object



19
20
21
# File 'lib/chartx/helper.rb', line 19

def multi_bar_horizontal_chart(data_source, options = {})
  chartx "multi_bar_horizontal", data_source, options
end

#pie_chart(data_source, options = {}) ⇒ Object



7
8
9
# File 'lib/chartx/helper.rb', line 7

def pie_chart(data_source, options = {})
  chartx "pie", data_source, options
end

#scatter_chart(data_source, options = {}) ⇒ Object



31
32
33
# File 'lib/chartx/helper.rb', line 31

def scatter_chart(data_source, options = {})
  chartx "scatter", data_source, options
end

#stacked_area_chart(data_source, options = {}) ⇒ Object



35
36
37
# File 'lib/chartx/helper.rb', line 35

def stacked_area_chart(data_source, options = {})
  chartx "stacked_area", data_source, options
end