1 | var { DataView } = require('vis-data');
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | function Filter (dataGroup, column, graph) {
|
11 | this.dataGroup = dataGroup;
|
12 | this.column = column;
|
13 | this.graph = graph;
|
14 |
|
15 | this.index = undefined;
|
16 | this.value = undefined;
|
17 |
|
18 |
|
19 | this.values = dataGroup.getDistinctValues(this.column);
|
20 |
|
21 | if (this.values.length > 0) {
|
22 | this.selectValue(0);
|
23 | }
|
24 |
|
25 |
|
26 | this.dataPoints = [];
|
27 |
|
28 | this.loaded = false;
|
29 | this.onLoadCallback = undefined;
|
30 |
|
31 | if (graph.animationPreload) {
|
32 | this.loaded = false;
|
33 | this.loadInBackground();
|
34 | }
|
35 | else {
|
36 | this.loaded = true;
|
37 | }
|
38 | }
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | Filter.prototype.isLoaded = function() {
|
46 | return this.loaded;
|
47 | };
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | Filter.prototype.getLoadedProgress = function() {
|
55 | var len = this.values.length;
|
56 |
|
57 | var i = 0;
|
58 | while (this.dataPoints[i]) {
|
59 | i++;
|
60 | }
|
61 |
|
62 | return Math.round(i / len * 100);
|
63 | };
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | Filter.prototype.getLabel = function() {
|
71 | return this.graph.filterLabel;
|
72 | };
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | Filter.prototype.getColumn = function() {
|
80 | return this.column;
|
81 | };
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | Filter.prototype.getSelectedValue = function() {
|
88 | if (this.index === undefined)
|
89 | return undefined;
|
90 |
|
91 | return this.values[this.index];
|
92 | };
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | Filter.prototype.getValues = function() {
|
99 | return this.values;
|
100 | };
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 | Filter.prototype.getValue = function(index) {
|
108 | if (index >= this.values.length)
|
109 | throw new Error('Index out of range');
|
110 |
|
111 | return this.values[index];
|
112 | };
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | Filter.prototype._getDataPoints = function(index) {
|
121 | if (index === undefined)
|
122 | index = this.index;
|
123 |
|
124 | if (index === undefined)
|
125 | return [];
|
126 |
|
127 | var dataPoints;
|
128 | if (this.dataPoints[index]) {
|
129 | dataPoints = this.dataPoints[index];
|
130 | }
|
131 | else {
|
132 | var f = {};
|
133 | f.column = this.column;
|
134 | f.value = this.values[index];
|
135 |
|
136 | var dataView = new DataView(this.dataGroup.getDataSet(), {filter: function (item) {return (item[f.column] == f.value);}}).get();
|
137 | dataPoints = this.dataGroup._getDataPoints(dataView);
|
138 |
|
139 | this.dataPoints[index] = dataPoints;
|
140 | }
|
141 |
|
142 | return dataPoints;
|
143 | };
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 | Filter.prototype.setOnLoadCallback = function(callback) {
|
153 | this.onLoadCallback = callback;
|
154 | };
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 | Filter.prototype.selectValue = function(index) {
|
163 | if (index >= this.values.length)
|
164 | throw new Error('Index out of range');
|
165 |
|
166 | this.index = index;
|
167 | this.value = this.values[index];
|
168 | };
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 | Filter.prototype.loadInBackground = function(index) {
|
177 | if (index === undefined)
|
178 | index = 0;
|
179 |
|
180 | var frame = this.graph.frame;
|
181 |
|
182 | if (index < this.values.length) {
|
183 |
|
184 | if (frame.progress === undefined) {
|
185 | frame.progress = document.createElement('DIV');
|
186 | frame.progress.style.position = 'absolute';
|
187 | frame.progress.style.color = 'gray';
|
188 | frame.appendChild(frame.progress);
|
189 | }
|
190 | var progress = this.getLoadedProgress();
|
191 | frame.progress.innerHTML = 'Loading animation... ' + progress + '%';
|
192 |
|
193 | frame.progress.style.bottom = 60 + 'px';
|
194 | frame.progress.style.left = 10 + 'px';
|
195 |
|
196 | var me = this;
|
197 | setTimeout(function() {me.loadInBackground(index+1);}, 10);
|
198 | this.loaded = false;
|
199 | }
|
200 | else {
|
201 | this.loaded = true;
|
202 |
|
203 |
|
204 | if (frame.progress !== undefined) {
|
205 | frame.removeChild(frame.progress);
|
206 | frame.progress = undefined;
|
207 | }
|
208 |
|
209 | if (this.onLoadCallback)
|
210 | this.onLoadCallback();
|
211 | }
|
212 | };
|
213 |
|
214 | module.exports = Filter;
|