/
CommonFunnel.ts
290 lines (239 loc) · 10.9 KB
/
CommonFunnel.ts
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import { ITooltip } from "@hpcc-js/api";
import { HTMLWidget } from "@hpcc-js/common";
import "amcharts3/amcharts/funnel";
import { min as d3Min } from "d3-array";
import { select as d3Select } from "d3-selection";
declare const require: any;
const AmCharts = (window as any).AmCharts;
export class CommonFunnel extends HTMLWidget {
_chart: any = {};
_selected = null;
_selections = [];
_dataUpdated = 0;
_prevDataUpdated = -1;
_columnsUpdated = 0;
_prevColumnsUpdated = -1;
d3LabelSelection;
titles;
baloon;
constructor() {
super();
this._tag = "div";
}
updateChartOptions() {
this._chart.startDuration = this.startDuration();
this._chart.rotate = this.flip();
this._chart.pullOutOnlyOne = this.selectionMode() === "simple";
this._chart.color = this.fontColor();
this._chart.colorField = "sliceColor";
this._chart.fontSize = this.fontSize();
this._chart.fontFamily = this.fontFamily();
this._chart.labelPosition = this.labelPosition();
this.titles = [];
this.baloon = {};
this._chart.titleField = this.columns()[0];
this._chart.valueField = this.columns()[1];
this._chart.depth3D = this.Depth3D();
this._chart.angle = this.Angle3D();
let sortingMethod = function (a, b) { return a[1] > b[1] ? 1 : -1; };
if (this.reverseDataSorting()) {
sortingMethod = function (a, b) { return a[1] < b[1] ? 1 : -1; };
}
this.data().sort(sortingMethod);
// DataProvider
if (this._dataUpdated > this._prevDataUpdated || this._columnsUpdated > this._prevColumnsUpdated) {
this._chart.dataProvider = this.formatData(this.data());
}
this._prevDataUpdated = this._dataUpdated;
this._prevColumnsUpdated = this._columnsUpdated;
// Color Palette
this._chart.colors = this.data().map(function (row) {
return this._palette(row[0]);
}, this);
// Scroll Bar
if (this.showScrollbar()) {
this._chart.chartScrollbar.enabled = true;
} else {
this._chart.chartScrollbar.enabled = false;
}
return this._chart;
}
formatData(dataArr) {
const dataObjArr = [];
const context = this;
dataArr.forEach(function (dataRow) {
const dataObj = {};
context.columns().forEach(function (colName, cIdx) {
dataObj[colName] = dataRow[cIdx];
});
dataObjArr.push(dataObj);
});
return dataObjArr;
}
enter(domNode, element) {
HTMLWidget.prototype.enter.apply(this, arguments);
const context = this;
const initObj: any = {
type: "funnel",
addClassNames: true,
autoResize: true,
autoMargins: true,
chartScrollbar: {}
};
if (typeof (window as any).define === "function" && (window as any).define.amd) {
initObj.pathToImages = require && require.toUrl ? require.toUrl("amchartsImg") : ".";
}
this._chart = AmCharts.makeChart(domNode, initObj);
this._chart.addListener("clickSlice", function (e) {
const field = e.chart.colorField;
const data = e.dataItem.dataContext;
if (data[field] !== null && data[field] !== undefined) {
delete data[field];
if (context.selectionMode() === "simple") {
if (context._selected !== null) {
delete context._selected.data[context._selected.field];
}
context._selected = null;
}
} else {
data[field] = context.selectionColor();
if (context.selectionMode() === "simple") {
if (context._selected !== null) {
delete context._selected.data[context._selected.field];
}
context._selected = {
field,
data,
cIdx: 1,
dIdx: e.dataItem.index
};
context._selections.push(context._selected);
}
}
e.chart.validateData();
context.click(context.rowToObj(context.data()[e.dataItem.index]), context.columns()[1], context._selected !== null);
});
}
update(domNode, element) {
HTMLWidget.prototype.update.apply(this, arguments);
domNode.style.width = this.size().width + "px";
domNode.style.height = this.size().height + "px";
this._palette = this._palette.switch(this.paletteID());
if (this.useClonedPalette()) {
this._palette = this._palette.cloneNotExists(this.paletteID() + "_" + this.id());
}
this.updateChartOptions();
this._chart.validateNow();
this._chart.validateData();
}
render(callback) {
return HTMLWidget.prototype.render.apply(this, arguments);
}
postUpdate(domNode, element) {
const context = this;
if (this.labelPosition() !== "center") {
const containerBoundingClientRect = context._element.select("svg").node().getBoundingClientRect(); // might need to change this selection (could get a bit more accurate results?)
this.d3LabelSelection = element.selectAll(".amcharts-funnel-item");
const left = [];
const right = [];
this.d3LabelSelection.each(function (d, i) {
const boundingRect = d3Select(this).node().getBoundingClientRect();
const labelRightPos = boundingRect.right;
if (labelRightPos > containerBoundingClientRect.right) {
right.push(containerBoundingClientRect.right - labelRightPos);
}
const labelLeftPos = boundingRect.left;
if (labelLeftPos < containerBoundingClientRect.left) {
left.push(labelLeftPos - containerBoundingClientRect.left);
}
});
const rightOffset = right.length ? d3Min(right) : 0;
const leftOffset = left.length ? d3Min(left) : 0;
let smallerOffset = 0;
if (rightOffset < 0) {
smallerOffset += rightOffset;
}
if (leftOffset < 0) {
smallerOffset += leftOffset;
}
switch (this.labelPosition()) {
case "right":
this._chart.marginRight = Math.abs(smallerOffset);
this._chart.marginLeft = 0;
break;
case "left":
this._chart.marginLeft = Math.abs(smallerOffset);
this._chart.marginRight = 0;
break;
}
this._chart.validateNow();
}
}
data(_?) {
if (arguments.length) {
this._dataUpdated++;
}
return HTMLWidget.prototype.data.apply(this, arguments);
}
fontSize: { (): number; (_: number): CommonFunnel };
fontSize_exists: () => boolean;
fontFamily: { (): string; (_: string): CommonFunnel };
fontFamily_exists: () => boolean;
fontColor: { (): string; (_: string): CommonFunnel };
fontColor_exists: () => boolean;
flip: { (): boolean; (_: boolean): CommonFunnel };
flip_exists: () => boolean;
reverseDataSorting: { (): boolean; (_: boolean): CommonFunnel };
reverseDataSorting_exists: () => boolean;
labelPosition: { (): string; (_: string): CommonFunnel };
labelPosition_exists: () => boolean;
showScrollbar: { (): boolean; (_: boolean): CommonFunnel };
showScrollbar_exists: () => boolean;
startDuration: { (): number; (_: number): CommonFunnel };
startDuration_exists: () => boolean;
Depth3D: { (): number; (_: number): CommonFunnel };
Depth3D_exists: () => boolean;
Angle3D: { (): number; (_: number): CommonFunnel };
Angle3D_exists: () => boolean;
useClonedPalette: { (): boolean; (_: boolean): CommonFunnel };
useClonedPalette_exists: () => boolean;
selectionMode: { (): string; (_: string): CommonFunnel };
selectionMode_exists: () => boolean;
selectionColor: { (): string; (_: string): CommonFunnel };
selectionColor_exists: () => boolean;
// I2DChart
_palette;
paletteID: { (): string; (_: string): CommonFunnel };
click: (row, column, selected) => void;
dblclick: (row, column, selected) => void;
// ITooltip ---
tooltipStyle: { (): string; (_: string): ITooltip };
tooltipStyle_exists: () => boolean;
tooltipValueFormat: { (): string; (_: string): ITooltip };
tooltipValueFormat_exists: () => boolean;
tooltipSeriesColor: { (): string; (_: string): ITooltip };
tooltipSeriesColor_exists: () => boolean;
tooltipLabelColor: { (): string; (_: string): ITooltip };
tooltipLabelColor_exists: () => boolean;
tooltipValueColor: { (): string; (_: string): ITooltip };
tooltipValueColor_exists: () => boolean;
tooltipTick: { (): boolean; (_: boolean): ITooltip };
tooltipTick_exists: () => boolean;
tooltipOffset: { (): number; (_: number): ITooltip };
tooltipOffset_exists: () => boolean;
}
CommonFunnel.prototype._class += " amchart_CommonFunnel";
CommonFunnel.prototype.implements(ITooltip.prototype);
CommonFunnel.prototype.publish("fontSize", 11, "number", "Font Size", null, { tags: ["Basic", "Shared"] });
CommonFunnel.prototype.publish("fontFamily", "Verdana", "string", "Font Name", null, { tags: ["Basic", "Shared", "Shared"] });
CommonFunnel.prototype.publish("fontColor", "#000000", "html-color", "Font Color", null, { tags: ["Basic", "Shared"] });
CommonFunnel.prototype.publish("flip", true, "boolean", "Flip Chart", null, { tags: ["Intermediate"] });
CommonFunnel.prototype.publish("reverseDataSorting", false, "boolean", "Reverse Data Sorting", null, { tags: ["Intermediate"] });
CommonFunnel.prototype.publish("labelPosition", "center", "set", "Label Position", ["left", "right", "center"], { tags: ["Intermediate"] });
CommonFunnel.prototype.publish("showScrollbar", false, "boolean", "Show Chart Scrollbar", null, { tags: ["Intermediate"] });
CommonFunnel.prototype.publish("startDuration", 0.3, "number", "Start Duration (sec)", null, { tags: ["Private"] });
CommonFunnel.prototype.publish("Depth3D", 0, "number", "3D Depth (px)", null, { tags: ["Basic"] });
CommonFunnel.prototype.publish("Angle3D", 0, "number", "3D Angle (Deg)", null, { tags: ["Basic"] });
CommonFunnel.prototype.publish("useClonedPalette", false, "boolean", "Enable or disable using a cloned palette", null, { tags: ["Intermediate", "Shared"] });
CommonFunnel.prototype.publish("selectionMode", "simple", "set", "Selection Mode", ["simple", "multi"], { tags: ["Intermediate"] });
CommonFunnel.prototype.publish("selectionColor", "#f00", "html-color", "Font Color", null, { tags: ["Basic"] });