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
75
76
77
78
79
|
# File 'lib/rails-data-explorer/chart/box_plot.rb', line 26
def render
return '' unless render?
ca = compute_chart_attrs
return '' unless ca
%(
<div id="#{ dom_id }" class="rde-chart rde-box-plot">
<svg class="box" style="height: #{ ca[:base_width] }px;"></svg>
<script type="text/javascript">
(function() {
var base_width = #{ ca[:base_width] },
base_height = #{ ca[:base_height] },
margin = { top: 10, right: 50, bottom: 95, left: 50 },
width = base_width - margin.left - margin.right,
height = base_height - margin.top - margin.bottom;
var min = #{ ca[:min] },
max = #{ ca[:max] };
var chart = d3.box()
.whiskers(iqr(1.5))
.width(width)
.height(height)
.tickFormat(#{ ca[:axis_tick_format] });
var data = #{ ca[:values].to_json };
chart.domain([min, max]);
var svg = d3.select("##{ dom_id }").selectAll("svg")
.data(data)
.append("g")
.attr("transform", "rotate(90) translate(" + (width + margin.left) + " -" + (height + margin.bottom) + ")")
.call(chart);
// Function to compute the interquartile range.
function iqr(k) {
return function(d, i) {
var q1 = d.quartiles[0],
q3 = d.quartiles[2],
iqr = (q3 - q1) * k,
i = -1,
j = d.length;
while (d[++i] < q1 - iqr);
while (d[--j] > q3 + iqr);
return [i, j];
};
}
})();
</script>
</div>
)
end
|