-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.html.erb
221 lines (211 loc) · 7.35 KB
/
script.html.erb
1
2
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
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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<script>
var data_ctx = document.getElementById("chart_canvas").getContext("2d");
var overlay_canvas = document.getElementById("overlay_canvas");
var overlay_ctx = overlay_canvas.getContext("2d");
var ticks_ctx = document.getElementById("ticks_canvas").getContext("2d");
<% if meta[:display_legend] %>
var legend_ctx = document.getElementById("legend_canvas").getContext("2d");
<% end %>
// This is the actual data being plotted.
// The only relevant bits are the background colors and the data values
data = {
datasets: [
{
label: "Score",
data: [<%= chart_data[:values].join(',') %>],
backgroundColor: [<%= chart_data[:colors].map { |l| "\"#{l}\""}.join(',') %>],
borderColor: [<%= chart_data[:colors].map { |_| "\"#000000\""}.join(',') %>],
borderWidth: 1
}
],
labels: [<%= chart_data[:labels].map { |l| "\"#{l}\""}.join(',') %>],
true_values: [<%= chart_data[:values].join(',') %>]
};
options = {
scale: {
display: false,
gridLines: {
lineWidth: <%= meta.fetch(:main_line_width, 3) %>,
drawBorder: true,
drawOnChartArea: true
},
layout: {
padding: {
left: 0,
right: 0,
top: 2,
bottom: 2
}
},
ticks: {
beginAtZero: true,
drawOnChartArea: true,
min: 0,
max: <%= meta[:max_score] %>,
stepSize: <%= meta.fetch(:step_size, 1) %>,
callback: function (value, index, values) {
return "";
}
}
},
legend: {
display: false
},
tooltips: {
callbacks: {
label: function (tooltipItem, data) {
var label = data.labels[tooltipItem.index] || '';
return label + ": " + Math.round(data.true_values[tooltipItem.index]);
}
}
}
};
<% if meta[:display_legend] %>
// This layer provides the legend
legendData = {
datasets: [
{
label: "Score",
data: [<%= chart_data[:ticks].join(',') %>],
backgroundColor: [<%= chart_data[:colors].map { |c| "\"#{c}\""}.join(',') %>],
borderColor: [<%= chart_data[:colors].map { |_| "\"rgba(255,0,0,0.0)\""}.join(',') %>]
}
],
labels: [<%= chart_data[:labels].map { |l| "\"#{l}\""}.join(',') %>],
true_values: [<%= chart_data[:values].join(',') %>]
};
legendOptions = {
animation: false,
scale: {
display: false
},
legend: {
display: <%= meta.fetch(:display_legend, true).to_s %>,
position: 'left',
labels : {
fontSize: 14,
fontFamily: "'muli', 'Myriad Pro', Helvetica, Arial, sans-serif"
}
},
tooltips: {
callbacks: {
label: function (tooltipItem, data) {
var label = data.labels[tooltipItem.index] || '';
return label + ": " + Math.round(data.true_values[tooltipItem.index]);
}
}
}
};
<% end %>
// This provides layer gives us the dividing border lines.
// The data values for this are all set to 1.0 and the background
// color is transparent so that the colors from the content layer
// can show through.
///
// this layer is drawn last.
overlayData = {
datasets: [
{
label: "Score",
data: [<%= chart_data[:ranges].join(',') %>],
backgroundColor: [<%= chart_data[:colors].map { |_| "\"rgba(255,0,0,0.0)\""}.join(',') %>],
borderWidth: 2,
borderColor: [<%= chart_data[:colors].map { |_| "\"#000000\""}.join(',') %>],
}
],
labels: [<%= chart_data[:labels].map { |l| "\"#{l}\""}.join(',') %>],
true_values: [<%= chart_data[:values].join(',') %>]
};
overlayOptions = {
animation: false,
scale: {
display: false
},
legend: {
display: false
},
layout: {
padding: {
left: 0,
right: 0,
top: 2,
bottom: 2
}
},
tooltips: {
displayColors: false,
callbacks: {
label: function (tooltipItem, data) {
var label = data.labels[tooltipItem.index] || '';
return label + ": " + Math.round(data.true_values[tooltipItem.index] * 100) / 100;
}
}
}
};
// This layer provides the tick marks, it's drawn between the content and
// the overlay. The data values are all set to zero so that we get tick
// marks all the way to the center, even where there's data
//
ticksData = {
datasets: [
{
label: "Score",
data: [<%= chart_data[:ticks].join(',') %>],
}
],
labels: [<%= chart_data[:labels].map { |l| "\"#{l}\""}.join(',') %>],
true_values: [<%= chart_data[:values].join(',') %>]
};
ticksOptions = {
animation: false,
scale: {
display: <%= meta.fetch(:display_ticks_scale, true).to_s %>,
gridLines: {
lineWidth: <%= meta.fetch(:tick_line_width, 3) %>,
drawBorder: false,
},
ticks: {
beginAtZero: true,
min: 0,
max: <%= meta[:max_score] %>,
stepSize: <%= meta.fetch(:tick_size, 1) %>,
callback: function (value, index, values) {
return "";
}
}
},
legend: {
display: false
},
layout: {
padding: {
left: 0,
right: 0,
top: 2,
bottom: 2
}
},
tooltips: {
callbacks: {
label: function (tooltipItem, data) {
var label = data.labels[tooltipItem.index] || '';
return label + ": " + Math.round(data.true_values[tooltipItem.index]);
}
}
}
};
new Chart(data_ctx, { data: data, type: "polarArea", options: options });
var overlay_chart = new Chart(overlay_ctx, { data: overlayData, type: "polarArea", options: overlayOptions });
new Chart(ticks_ctx, { data: ticksData, type: "polarArea", options: ticksOptions });
<% if meta[:display_legend] %>
new Chart(legend_ctx, { data: legendData, type: "polarArea", options: legendOptions });
<% end %>
<% if chart_data.fetch(:use_nav, false) %>
overlay_canvas.onclick = function (e) {
var slice = overlay_chart.getElementAtEvent(e);
if (!slice.length) return; // return if not clicked on slice
var label = slice[0]._model.label.replace(/(-|\/|:| )/g, '');
window.location = "<%= chart_data.fetch(:url_prefix, '')%>" + label + ".html";
}
<% end %>
</script>