-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchart.js
More file actions
194 lines (164 loc) · 5.64 KB
/
chart.js
File metadata and controls
194 lines (164 loc) · 5.64 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Load the Visualization API and the controls package.
jQuery(document).ready(function($){
// внутри этой функции $ будет работать как jQuery
google.charts.load('current', {
'packages': ['corechart', 'controls']
});
google.charts.setOnLoadCallback(onGoogleChartsLoaded);
function onGoogleChartsLoaded() {
$('.google-chart-entity').each(function () {
var entity = $(this);
var opt = {
tableId: entity.data('table-id'),
visualisationType: entity.data('visualization-type'),
title: entity.data('title'),
hTitle: entity.data('h-title'),
vTitle: entity.data('v-title'),
filters: entity.data('filters-cols').split(',')
}
if (opt.filters.length > 0) {
entity.prepend('<div id="select-container-' + entity.attr("id") + '" class="chart-selects-container"></div>');
opt.selectContainer = "select-container-" + entity.attr("id");
};
console.log(opt);
var tableURL = 'https://docs.google.com/spreadsheets/d/' + opt.tableId + '/gviz/tq?sheet=Sheet1&headers=1';
drawSheetName('SELECT *', fillFilter);
function drawSheetName(queryString, callbackName) {
var queryString = encodeURIComponent(queryString);
var query = new google.visualization.Query(tableURL + '&tq=' + queryString);
query.send(callbackName);
}
function handleSampleDataQueryResponse(response) {
if (response.isError()) {
console.error('Error in qsuery: ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}
var data = response.getDataTable();
//var chart = new google.visualization.ColumnChart(document.getElementById('chart'));
var chart = new google.visualization[opt.visualisationType](entity.find('.chart')[0]);
//var chart = new google.visualization.PieChart(document.getElementById('chart'));
var options = {
height: 400,
title: opt.title,
//colors: ['#9575cd', '#33ac71'],
hAxis: {
title: opt.hTitle
},
vAxis: {
title: opt.vTitle
}
};
chart.draw(data, options);
}
function fillFilter(response) {
console.log(response);
$(function () {
var sortColArr = opt.filters;
var sortColObjArr = [];
var sortColId = '';
var colPos = '';
var drownCols = [];
$.each(response.J.ng, function (key, val) {
if (sortColArr.indexOf(val.label) >= 0) {
sortColObjArr.push({
col_pos: key * 1,
col_id: val.id,
col_label: val.label
});
} else {
drownCols.push(val.id);
}
});
function getColIdFromSortColObjArrByLabel(label) {
var res = '';
$.each(sortColObjArr, function (key, value) {
if (opt.selectContainer + '-' + value.col_label === label) {
res = value.col_id;
};
});
return res;
}
var selectsContainer = $('#' + opt.selectContainer);
$.each(sortColObjArr, function (key, value) {
selectsContainer.append('<select id="' + opt.selectContainer + '-' + value.col_label + '" ></select>');
value.optionsList = [];
$.each(response.J.og, function (key, val) {
value.optionsList.push(val.c[value.col_pos]['v']);
});
value.optionsList = value.optionsList.getUnique();
$('#' + opt.selectContainer + '-' + value.col_label).append('<option value="0" >' + value.col_label + ' (Все)</option>');
$.each(value.optionsList, function (k, v) {
$('#' + opt.selectContainer + '-' + value.col_label).append('<option value="' + v + '" >' + v + '</option>');
});
});
function drawChart(query) {
if (query) {
var query = "select " + drownCols.join(", ") + " " + query;
} else {
var query = "select " + drownCols.join(", ");
}
drawSheetName(query, handleSampleDataQueryResponse);
}
var selectsVals = [];
selectsContainer.find('select').off().change(function () {
var currentSelect = $(this);
var query = false;
var isExist = false;
$.each(selectsVals, function (key, value) {
if (value.sort_col == currentSelect.attr('id') && getColIdFromSortColObjArrByLabel(currentSelect.attr('id')) == value.sort_col_id) {
isExist = true;
}
});
if (!isExist) { //Еще нет
selectsVals.push({
value: currentSelect.val(),
sort_col: $(this).attr('id'),
sort_col_id: getColIdFromSortColObjArrByLabel(currentSelect.attr('id')),
});
} else { // уже есть
selectsVals.forEach(function (value, key) {
if (value.sort_col == currentSelect.attr('id') && getColIdFromSortColObjArrByLabel(currentSelect.attr('id')) == value.sort_col_id) {
if (currentSelect.val() == 0) {
selectsVals.splice(key, 1);
return;
} else {
selectsVals[key]['value'] = currentSelect.val();
}
}
});
}
var selectsValsLength = selectsVals.length;
if (selectsValsLength > 0) {
if (selectsValsLength > 1) {
query = query = " where ";
$.each(selectsVals, function (key, value) {
if (key < selectsValsLength - 1) {
query += value.sort_col_id + " = '" + value.value + "' AND ";
} else {
query += value.sort_col_id + " = '" + value.value + "'";
}
});
} else {
query = " where " + selectsVals[0]['sort_col_id'] + " = " + "'" + selectsVals[0]['value'] + "'";
}
}
drawChart(query);
});
drawChart();
});
}
});
Array.prototype.getUnique = function () {
var o = {},
a = [],
i, e;
for (i = 0; e = this[i]; i++) {
o[e] = 1
};
for (e in o) {
a.push(e)
};
return a;
}
}
});