-
Notifications
You must be signed in to change notification settings - Fork 0
/
HistogramModule.js
51 lines (43 loc) · 1.47 KB
/
HistogramModule.js
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
// HistogramModule.js
var HistogramModule = function(bins, canvas_width, canvas_height) {
// Create the elements
// Create the tag:
var canvas_tag = "<canvas width='" + canvas_width + "' height='" + canvas_height + "' ";
canvas_tag += "style='border:1px dotted'></canvas>";
// Append it to body:
var canvas = $(canvas_tag)[0];
$("#elements").append(canvas);
// Create the context and the drawing controller:
var context = canvas.getContext("2d");
// Prep the chart properties and series:
var datasets = [{
label: "Distance of Stored food from nearest safe spot.",
backgroundColor: ["rgba(0,129,255,0.5)"],
borderColor: ["rgba(0,127,255,0.8)"],
//backgroundFill: "rgba(0,127,255,0.75)",
//highlightStroke: "rgba(0,127,255,1)",
data: []
}];
// Add a zero value for each bin
for (var i in bins)
datasets[0].data.push(0);
var data = {
labels: bins,
datasets: datasets
};
var options = {
scaleBeginsAtZero: true
};
// Create the chart object
var chart = new Chart(context,{'type': 'bar', 'data': data, 'options': options});
// Now what?
this.render = function(data) {
for (var i in data)
chart.data.datasets[0].data[i] = data[i];
chart.update();
};
this.reset = function() {
chart.destroy();
chart = new Chart(context,{'type': 'bar', 'data': data, 'options': options});
};
};