-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathchart.js
155 lines (135 loc) · 3.82 KB
/
chart.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
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
/*global H5P*/
/**
* Graph Cake module
* @external {jQuery} $ H5P.jQuery
* @external {EventDispatcher} EventDispatcher H5P.EventDispatcher
*/
H5P.Chart = (function ($, EventDispatcher) {
/**
* Initialize module.
*
* @class
* @param {Object} params Behavior settings
* @param {Number} id Content identification
*/
function Chart(params) {
var self = this;
// Inheritance
EventDispatcher.call(self);
// Set params and filter data set to make sure it's valid
self.params = params;
if (self.params.listOfTypes) {
Chart.filterData(self.params.listOfTypes);
}
else {
self.params.listOfTypes = [];
}
// Add example/default behavior
if (!self.params.listOfTypes.length) {
self.params.listOfTypes = [
{
text: 'Cat',
value: 1,
color: '#D3D3D3',
fontColor: '#000'
},
{
text: 'Dog',
value: 2,
color: '#ADD8E6',
fontColor: '#000'
},
{
text: 'Mouse',
value: 3,
color: '#90EE90',
fontColor: '#000'
}
];
}
// Set the figure definition for readspeakers if it doesn't exist
if (!self.params.figureDefinition) {
self.params.figureDefinition = "Chart";
}
// Keep track of type.
self.type = (self.params.graphMode === 'pieChart' ? 'Pie' : 'Bar');
}
// Inheritance
Chart.prototype = Object.create(EventDispatcher.prototype);
Chart.prototype.constructor = Chart;
/**
* Make sure the data set has set the required text and value properties.
*
* @param {Array} dataSet
*/
Chart.filterData = function (dataSet) {
// Cycle through data set
for (var i = 0; i < dataSet.length; i++) {
var row = dataSet[i];
if (row.text === undefined || row.value === undefined) {
// Remove invalid data
dataSet.splice(i, 1);
i--;
continue;
}
row.text = this.htmlDecode(row.text.trim());
row.value = parseFloat(row.value);
if (row.text === '' || isNaN(row.value)) {
// Remove invalid data
dataSet.splice(i, 1);
i--;
continue;
}
}
};
/**
* Retrieve true string from HTML encoded string.
* @param {string} input Input string.
* @return {string} Output string.
*/
Chart.htmlDecode = function (input) {
const dparser = new DOMParser().parseFromString(input, 'text/html');
const div = document.createElement('div');
div.innerHTML = dparser.documentElement.textContent;
return div.textContent || div.innerText || '';
}
/**
* Append field to wrapper.
*
* @param {H5P.jQuery} $container
*/
Chart.prototype.attach = function ($container) {
var self = this;
// Create chart on first attach
if (self.$wrapper === undefined) {
self.$wrapper = $('<div/>', {
'class': 'h5p-chart-chart h5p-chart-' + self.type.toLowerCase()
});
self.chart = new H5P.Chart[self.type + 'Chart'](self.params, self.$wrapper);
}
// Prepare container
self.$container = $container.html('').addClass('h5p-chart').append(self.$wrapper);
const $defgroup = $('<div/>', {
'class': 'hidden-but-read',
'html': self.params.figureDefinition,
});
// Add aria-labels for the data
self.params.listOfTypes.forEach(function(type) {
var ariaLabel = $('<div/>', {
'class': 'hidden-but-read',
'html': type.text + ': ' + type.value + ''
});
$defgroup.append(ariaLabel);
});
self.$container.append($defgroup);
// Handle resizing
self.on('resize', function () {
if (!self.$container.is(':visible')) {
return; // Only handle if visible
}
// Resize existing chart
self.chart.resize();
});
};
return Chart;
})(H5P.jQuery, H5P.EventDispatcher);