This repository has been archived by the owner on Nov 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd3brush_integration.js
138 lines (115 loc) · 4.34 KB
/
d3brush_integration.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
// this code was adapted from an original piece of code provided by Tamas Foldi.
var CANVAS_SELECTOR, TABLEAU_NULL, convertRowToObject, drawLinks, drawNodes, drawNodesAndLinks, drawSanKeyGraph, errorWrapped, getColumnIndexes, initEditor, makeSanKeyData,
slice = [].slice,
interval,
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
var parms = new Array;
var xAxisField, yAxisField;
var WORKSHEET_NAME = "Hourly selling";
TABLEAU_NULL = '%null%';
errorWrapped = function(context, fn) {
return function() {
var args, err, error;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
try {
return fn.apply(null, args);
} catch (error) {
err = error;
return console.error("Got error during '", context, "' : ", err.message, err.stack);
}
};
};
// ---
// generated by coffee-script 1.9.2
getColumnIndexes = function(table, required_keys) {
var c, colIdxMaps, fn, j, len, ref;
colIdxMaps = {};
ref = table.getColumns();
for (j = 0, len = ref.length; j < len; j++) {
c = ref[j];
fn = c.getFieldName();
if (indexOf.call(required_keys, fn) >= 0) {
colIdxMaps[fn] = c.getIndex();
}
}
return colIdxMaps;
};
//converts
convertRowToObject = function(row, attrs_map) {
var id, name, o;
o = {};
for (name in attrs_map) {
id = attrs_map[name];
o[name] = row[id].value;
}
return o;
};
//this function is the start of it all and calls/references the above functions
initEditor = function() {
var onDataLoadError, onDataLoadOk, tableau, updateEditor;
tableau = TBLib.getTableau();
onDataLoadError = function(err) {
return console.error("Error during Tableau Async request:", err._error.message, err._error.stack);
};
onDataLoadOk = errorWrapped("Getting data from Tableau", function(table) {
var col_indexes, data, row, tableauData;
//we have hardcoded column indexes here, but there is probably a better way
//col_indexes = getColumnIndexes(table, ["Order Date", "AGG(SalesRollingAverage)"]);
col_indexes = getColumnIndexes(table, [xAxisField, yAxisField]);
data = (function() {
var j, len, ref, results;
ref = table.getData();
results = [];
var resultsObj = {};
for (j = 0, len = ref.length; j < len; j++) {
row = ref[j];
var convertedRow = convertRowToObject(row, col_indexes);
// aggregate by date
if (!resultsObj.hasOwnProperty(convertedRow['DAY(Album Purchase DateTime)'])) {
resultsObj[convertedRow['DAY(Album Purchase DateTime)']] = convertedRow;
} else {
resultsObj[convertedRow['DAY(Album Purchase DateTime)']].price += convertedRow['AGG(CNT(Album_Title))'];
}
//results.push(convertRowToObject(row, col_indexes));
}
// convert aggregated obj to array
for (res in resultsObj) {
results.push(resultsObj[res]);
}
// sort by date
results.sort( (a,b) => {
if (a['DAY(Album Purchase DateTime)'] > b['DAY(Album Purchase DateTime)']) return 1;
return -1;
});
return results;
})();
tableauData = data;
$('#htext').val(JSON.stringify(tableauData)); // trying to save this to a hidden object for later use
drawBrush(tableauData); // set up base graph with all data
});
//on initial load we are going to get workbook parameters and read the two field names from them
TBLib.getCurrentViz().getWorkbook().getParametersAsync().then(function(parms) {
//loop through array and set the xAxis and yAxis field names
for(var i =0; i < parms.length; i++){
if(parms[i].getName() === 'xAxisField')
{
xAxisField = parms[i].getCurrentValue().value;
}
else if(parms[i].getName() === 'yAxisField')
{
yAxisField = parms[i].getCurrentValue().value;
}
}
TBLib.getAllWorksheets().get(WORKSHEET_NAME).getSummaryDataAsync({
maxRows: 0,
ignoreSelection: true,
includeAllColumns: true,
ignoreAliases: true
}).then(onDataLoadOk, onDataLoadError);
//add event listener to the viz don't need this for brush only interaction
//return getCurrentViz().addEventListener(tableau.TableauEventName.PARAMETER_VALUE_CHANGE, onParmChange);
});
};
this.appApi = {
initEditor: initEditor
};