-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCustomControl.js
87 lines (73 loc) · 2.46 KB
/
CustomControl.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
sap.ui.define([
"sap/ui/core/Control",
"sap/ui/core/HTML",
"sap/ui/core/ResizeHandler",
"sap/ui/thirdparty/d3"
], function(Control, HTMLControl, ResizeHandler) {
return Control.extend("CustomControl", {
metadata : {
aggregations : {
_html : {
type : "sap.ui.core.HTML",
multiple : false,
visibility : "hidden"
},
data : {
type : "sap.ui.base.ManagedObject",
multiple : true
}
}
},
_iWidth : null,
_sContainerId : null,
_sResizeHandlerId : null,
init : function() {
this._sContainerId = this.getId() + "--container";
this.setAggregation("_html", new HTMLControl({
content : "<svg id=\"" + this._sContainerId + "\" width=\"100%\"></svg>"
}));
},
_onResize : function(oEvent) {
this._updateSVG(oEvent.size.width);
},
_updateSVG : function(iWidth) {
var aData = this.getBinding("data").getContexts().map(function(oContext) {
return oContext.getObject();
});
var selContainer = d3.select("#" + this._sContainerId);
var selRects = selContainer.selectAll("rect").data(aData);
selRects.enter().append("rect");
var iNumDataPoints = aData.length;
var iNumSpaces = iNumDataPoints - 1;
var iSpaceRelativeDoDataPoint = 0.25;
var iBarWidth = iWidth / (iNumDataPoints + iNumSpaces * iSpaceRelativeDoDataPoint);
var iSpaceWidth = iBarWidth * iSpaceRelativeDoDataPoint;
selRects
.attr("width", iBarWidth)
.attr("height", function(d) {
return d.v;
})
.attr("x", function(d, i) {
return i * (iSpaceWidth + iBarWidth);
});
},
renderer : function(oRm, oControl) {
oRm.write("<div");
oRm.writeControlData(oControl);
oRm.write(">");
oRm.write("<h1>Responsive D3.js Custom Control</h1>");
oRm.write("<p>Change the size of the window and see how the bars adjust</p>");
oRm.renderControl(oControl.getAggregation("_html"));
oRm.write("</div>");
},
onBeforeRendering : function() { ResizeHandler.deregister(this._sResizeHandlerId);
},
onAfterRendering : function() {
this._sResizeHandlerId = ResizeHandler.register(this, jQuery.proxy(this._onResize, this));
var $control = this.$();
if ($control) {
this._updateSVG($control.rect().width);
}
}
});
}, true);