chart.js
1.65 KB
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
var chartColors = {
grey: 'rgb(201, 203, 207)',
red: 'rgb(255, 99, 132)',
orange: 'rgb(255, 159, 64)',
yellow: 'rgb(255, 205, 86)',
green: 'rgb(75, 192, 192)',
purple: 'rgb(153, 102, 255)',
blue: 'rgb(54, 162, 235)'
};
var config = {
type: 'line',
data: {},
options: {
tooltips: {
mode: 'index',
intersect: false,
},
hover: {
mode: 'nearest',
intersect: true
},
scales: {
xAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: $('#format').val()
}
}],
yAxes: [{
display: true,
ticks: {
min: 0
}
}]
}
}
}
function load () {
var url = $('#chart').attr('url');
$.ajax({
url: url + $('#format').val()
}).done(function (json) {
var colorNames = Object.keys(chartColors);
for (var i = 0; i < json.datasets.length; i++) {
var colorName = colorNames.pop();
var newColor = chartColors[colorName];
json.datasets[i].backgroundColor = newColor;
json.datasets[i].borderColor = newColor;
json.datasets[i].fill = false;
config.data = json;
}
config.options.scales.xAxes[0].scaleLabel.labelString = $('#format').val();
chart.update();
})
}
function chart () {
var can = $('#chart');
var ctx = can.get(0).getContext("2d");
var container = can.parent().parent(); // get width from proper parent
var $container = jQuery(container);
can.attr('width', $container.width()); //max width
can.attr('height', $container.height()); //max height
window.chart = new Chart(ctx, config);
load();
}
$('#format').on('change', function () {
load();
})