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] 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
|