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
|
$(function(){
Chart.defaults.global.defaultFontSize = 16;
var setUpLabelsForChart = function(chart){
var $parent = $(chart.chart.canvas).parent();
var xGutterInPixels = 30;
var lasty = 0;
$.each(chart.config.data.datasets, function(datasetIndex, dataset){
var $label = $('.label[data-datasetIndex="' + datasetIndex + '"]', $parent);
var latestPoint = chart.getDatasetMeta(datasetIndex).data[ dataset.data.length - 1 ];
var y = latestPoint._model.y;
if (y < lasty) {
y = lasty;
}
$label.css({
top: y,
left: latestPoint._model.x + xGutterInPixels
});
lasty = y + $label.height() + 8;
});
};
// Returns an array `numberOfPoints` long, where the final item
// is `radius`, and all the other items are 0.
var pointRadiusFinalDot = function(numberOfPoints, radius){
var pointRadius = [];
for (var i=1; i < numberOfPoints; i++) {
pointRadius.push(0);
}
pointRadius.push(radius);
return pointRadius;
};
var makeSparkline = function makeSparkline($el, valuesStr, color, title){
var values = [];
var labels = [];
$.each(valuesStr.split(' '), function(key, value){
values.push(Number(value));
labels.push('');
});
var spread = Math.max.apply(null, values) - Math.min.apply(null, values);
return new Chart($el, {
type: 'line',
data: {
labels: labels,
datasets: [{
data: values,
pointRadius: pointRadiusFinalDot(values.length, 4),
pointBackgroundColor: color,
borderColor: color,
lineTension: 0
}]
},
options: {
layout: {
padding: {
top: 0,
right: 5,
bottom: 0,
left: 2
}
},
scales: {
xAxes: [{
type: "category",
display: false
}],
yAxes: [{
type: "linear",
display: false,
ticks: {
min: Math.min.apply(null, values) - (spread * 0.3),
max: Math.max.apply(null, values) + (spread * 0.3)
}
}]
}
}
});
};
$('.labelled-sparkline canvas').each(function(){
makeSparkline(
$(this),
$(this).data('values'),
$(this).data('color')
);
});
var $allReports = $('#chart-all-reports'),
labels = $allReports.data('labels'),
data0 = $allReports.data('values-reports'),
data1 = $allReports.data('values-fixed');
window.chartAllReports = new Chart($allReports, {
type: 'line',
data: {
labels: labels,
datasets: [{
data: data0,
pointRadius: pointRadiusFinalDot(data0.length, 4),
pointBackgroundColor: '#F4A140',
borderColor: '#F4A140'
}, {
data: data1,
pointRadius: pointRadiusFinalDot(data1.length, 4),
pointBackgroundColor: '#62B356',
borderColor: '#62B356'
}]
},
options: {
animation: {
onComplete: function(){
setUpLabelsForChart(this);
}
},
layout: {
padding: {
top: 4
}
},
scales: {
xAxes: [{
type: 'category',
gridLines: {
display: false
}
}],
yAxes: [{
type: "linear",
ticks: {
display: false
}
}]
},
onResize: function(chart, size){
setUpLabelsForChart(chart);
}
}
});
});
|