-
Notifications
You must be signed in to change notification settings - Fork 0
/
british-food-attitudes.js
85 lines (68 loc) · 2.13 KB
/
british-food-attitudes.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
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
function BritishFoodAttitudes() {
// Name for the visualisation to appear in the menu bar.
this.name = 'British Food Attitudes';
// Each visualisation must have a unique ID with no special
// characters.
this.id = 'british-food-attitudes';
// Property to represent whether data has been loaded.
this.loaded = false;
// Preload the data. This function is called automatically by the
// gallery when a visualisation is added.
this.preload = function() {
var self = this;
this.data = loadTable(
'./data/food/attitudes-transposed.csv', 'csv', 'header',
// Callback function to set the value
// this.loaded to true.
function(table) {
self.loaded = true;
});
};
this.setup = function() {
if (!this.loaded) {
console.log('Data not yet loaded');
return;
}
// Create a select DOM element.
this.select = createSelect();
this.select.position(350, 500);
// Fill the options with all company names.
var questions = this.data.columns;
// First entry is empty.
for (var i = 1; i < questions.length; i++) {
this.select.option(questions[i]);
}
};
this.destroy = function() {
this.select.remove();
};
// Create a new pie chart object.
this.pie = new PieChart(width / 2, height / 2, width * 0.4);
this.draw = function() {
if (!this.loaded) {
console.log('Data not yet loaded');
return;
}
// Get the value of the company we're interested in from the
// select item.
var question = this.select.value();
// Get the column of raw data for question.
var col = this.data.getColumn(question);
// Convert all data strings to numbers.
col = stringsToNumbers(col);
// Copy the row labels from the table (the first item of each row).
var labels = this.data.getColumn(0);
// Colour to use for each category.
var colours = [
color(0,204,0),
color(51,255,51),
color(255,255,51),
color(255,153,51),
color(255,51,51)
];
// Make a title.
var title = question;
// Draw the pie chart!
this.pie.draw(col, labels, colours, title);
};
}