mascot-vis 2.0.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (157) hide show
  1. package/README.md +4 -3
  2. package/dist/mascot-es.js +27745 -0
  3. package/dist/mascot-min.js +186 -199
  4. package/dist/mascot-umd.js +27781 -0
  5. package/js/depGraphVis.js +66 -0
  6. package/package.json +23 -15
  7. package/src-new-ts/action/createElement.ts +91 -0
  8. package/src-new-ts/action/encode.js +20 -0
  9. package/src-new-ts/action/repeat.js +128 -0
  10. package/src-new-ts/action/traverseScene.js +41 -0
  11. package/src-new-ts/data/Network.js +2 -0
  12. package/src-new-ts/data/Scope.js +135 -0
  13. package/src-new-ts/data/Table.js +263 -0
  14. package/src-new-ts/data/Tree.js +3 -0
  15. package/src-new-ts/data/field.ts +115 -0
  16. package/src-new-ts/data/import.ts +96 -0
  17. package/src-new-ts/data/predicate.ts +82 -0
  18. package/src-new-ts/depgraph/DepGraph.js +178 -0
  19. package/src-new-ts/depgraph/Edge.js +9 -0
  20. package/src-new-ts/depgraph/SceneGraph2DepGraph.js +110 -0
  21. package/src-new-ts/depgraph/Signal.js +12 -0
  22. package/src-new-ts/depgraph/operator/BoundsEvaluator.js +30 -0
  23. package/src-new-ts/depgraph/operator/Dataflow.js +41 -0
  24. package/src-new-ts/depgraph/operator/DomainBuilder.js +50 -0
  25. package/src-new-ts/depgraph/updateDepGraph.js +45 -0
  26. package/src-new-ts/depgraph/variable/BoundsVar.js +81 -0
  27. package/src-new-ts/depgraph/variable/ChannelVar.js +17 -0
  28. package/src-new-ts/depgraph/variable/DataScopeVar.js +12 -0
  29. package/src-new-ts/depgraph/variable/DomainVar.js +15 -0
  30. package/src-new-ts/depgraph/variable/FieldVar.js +17 -0
  31. package/src-new-ts/depgraph/variable/LayoutParameter.js +8 -0
  32. package/src-new-ts/depgraph/variable/ScaleVar.js +13 -0
  33. package/src-new-ts/depgraph/variable/Variable.js +39 -0
  34. package/src-new-ts/element/gradient/LinearGradient.js +37 -0
  35. package/src-new-ts/element/group/Collection.js +109 -0
  36. package/src-new-ts/element/group/Group.js +307 -0
  37. package/src-new-ts/element/group/Scene.js +98 -0
  38. package/src-new-ts/element/mark/CircleMark.ts +85 -0
  39. package/src-new-ts/element/mark/Mark.ts +233 -0
  40. package/src-new-ts/element/mark/PathMark.js +483 -0
  41. package/src-new-ts/element/mark/Segment.js +29 -0
  42. package/src-new-ts/element/mark/Vertex.js +118 -0
  43. package/src-new-ts/encode/Scale.ts +115 -0
  44. package/src-new-ts/index.ts +19 -0
  45. package/src-new-ts/layout/Layout.ts +3 -0
  46. package/src-new-ts/render/CanvasRenderer.ts +24 -0
  47. package/src-new-ts/render/SVGRenderer.js +316 -0
  48. package/src-new-ts/util.ts +3 -0
  49. package/src-old/action/Classify.js +53 -0
  50. package/src-old/action/Densify.js +199 -0
  51. package/src-old/action/Partition.js +531 -0
  52. package/src-old/action/Repeat.js +106 -0
  53. package/src-old/action/Repopulate.js +44 -0
  54. package/src-old/action/Stratify.js +156 -0
  55. package/src-old/basic/Gradient.js +37 -0
  56. package/src-old/basic/Point.js +51 -0
  57. package/src-old/basic/Rectangle.js +63 -0
  58. package/src-old/bind/bindToAngle.js +56 -0
  59. package/src-old/bind/bindToAreaMark.js +360 -0
  60. package/src-old/bind/bindToColor.js +114 -0
  61. package/src-old/bind/bindToLink.js +81 -0
  62. package/src-old/bind/bindToPosition.js +283 -0
  63. package/src-old/bind/bindToRadialDistance.js +62 -0
  64. package/src-old/bind/bindToSize.js +235 -0
  65. package/src-old/bind/bindToText.js +60 -0
  66. package/src-old/bind/bindToThickness.js +100 -0
  67. package/src-old/constraint/AffixConstraint.js +129 -0
  68. package/src-old/constraint/AlignConstraint.js +58 -0
  69. package/src-old/core/Encoding.js +336 -0
  70. package/src-old/core/Scale.js +322 -0
  71. package/src-old/core/SceneLoader.js +290 -0
  72. package/src-old/core/SceneValidator.js +232 -0
  73. package/src-old/core/SpecExecutor.js +113 -0
  74. package/src-old/core/SpecGenerator.js +350 -0
  75. package/src-old/data/DataImporter.js +64 -0
  76. package/src-old/data/DataScope.js +124 -0
  77. package/src-old/data/DataTable.js +338 -0
  78. package/src-old/data/Network.js +106 -0
  79. package/src-old/data/Tree.js +251 -0
  80. package/src-old/data/transform/Bin.js +46 -0
  81. package/src-old/data/transform/Filter.js +48 -0
  82. package/src-old/data/transform/Groupby.js +18 -0
  83. package/src-old/data/transform/KDE.js +58 -0
  84. package/src-old/data/transform/Sort.js +14 -0
  85. package/src-old/data/transform/Split.js +5 -0
  86. package/src-old/data/transform/partition.js +46 -0
  87. package/src-old/history/UndoRedoStack +0 -0
  88. package/src-old/index.js +271 -0
  89. package/src-old/indexSVG.js +259 -0
  90. package/src-old/interaction/Interaction.js +91 -0
  91. package/src-old/interaction/MouseEvent.js +8 -0
  92. package/src-old/interaction/Selection.js +9 -0
  93. package/src-old/interaction/brush.js +362 -0
  94. package/src-old/item/Segment.js +29 -0
  95. package/src-old/item/Vertex.js +118 -0
  96. package/src-old/item/composite/Collection.js +106 -0
  97. package/src-old/item/composite/Glyph.js +19 -0
  98. package/src-old/item/composite/Group.js +310 -0
  99. package/src-old/item/composite/Scene.js +1251 -0
  100. package/src-old/item/mark/ArcPath.js +181 -0
  101. package/src-old/item/mark/AreaPath.js +78 -0
  102. package/src-old/item/mark/CirclePath.js +102 -0
  103. package/src-old/item/mark/EllipsePath.js +5 -0
  104. package/src-old/item/mark/Image.js +101 -0
  105. package/src-old/item/mark/LinkPath.js +118 -0
  106. package/src-old/item/mark/Mark.js +163 -0
  107. package/src-old/item/mark/Path.js +494 -0
  108. package/src-old/item/mark/PointText.js +201 -0
  109. package/src-old/item/mark/PolygonPath.js +64 -0
  110. package/src-old/item/mark/RectPath.js +88 -0
  111. package/src-old/item/mark/RingPath.js +92 -0
  112. package/src-old/item/refs/Axis.js +362 -0
  113. package/src-old/item/refs/EncodingAxis.js +515 -0
  114. package/src-old/item/refs/Gridlines.js +144 -0
  115. package/src-old/item/refs/LayoutAxis.js +316 -0
  116. package/src-old/item/refs/Legend.js +273 -0
  117. package/src-old/layout/Circular.js +95 -0
  118. package/src-old/layout/Force.js +52 -0
  119. package/src-old/layout/Grid.js +423 -0
  120. package/src-old/layout/Layout.js +13 -0
  121. package/src-old/layout/Packing.js +56 -0
  122. package/src-old/layout/Stack.js +264 -0
  123. package/src-old/layout/Strata.js +88 -0
  124. package/src-old/layout/Sugiyama.js +59 -0
  125. package/src-old/layout/TidyTree.js +105 -0
  126. package/src-old/layout/Treemap.js +87 -0
  127. package/src-old/renderer/SVGInteractionHandler.js +241 -0
  128. package/src-old/renderer/SVGRenderer.js +325 -0
  129. package/src-old/renderer/WebGLRenderer.js +1097 -0
  130. package/src-old/renderer/WebGLRenderer2.js +249 -0
  131. package/src-old/renderer/threejs/Line2.js +18 -0
  132. package/src-old/renderer/threejs/LineGeometry.js +77 -0
  133. package/src-old/renderer/threejs/LineMaterial.js +605 -0
  134. package/src-old/renderer/threejs/LineSegments2.js +281 -0
  135. package/src-old/renderer/threejs/LineSegmentsGeometry.js +226 -0
  136. package/src-old/renderer/threejs/Wireframe.js +51 -0
  137. package/src-old/renderer/threejs/WireframeGeometry2.js +16 -0
  138. package/src-old/scale/areaSize.js +0 -0
  139. package/src-old/scale/domain.js +38 -0
  140. package/src-old/util/Constants.js +180 -0
  141. package/src-old/util/DataUtil.js +35 -0
  142. package/src-old/util/ItemUtil.js +586 -0
  143. package/src-old/util/Numerical.js +33 -0
  144. package/tests/demo-tests/README.md +80 -0
  145. package/tests/demo-tests/SVG2PNG.js +56 -0
  146. package/tests/demo-tests/demos2CanvasPNGs.js +69 -0
  147. package/tests/demo-tests/demos2ScenesSVGs.js +100 -0
  148. package/tests/demo-tests/pathElementWorker.js +91 -0
  149. package/tests/demo-tests/pixelTest.js +62 -0
  150. package/tests/demo-tests/renderDemos.html +132 -0
  151. package/tests/demo-tests/serializationTest.js +36 -0
  152. package/tests/demo-tests/serializeDemos.html +134 -0
  153. package/tests/unit-tests/README.md +4 -0
  154. package/tests/unit-tests/jasmine-browser.json +21 -0
  155. package/tests/unit-tests/jasmine.json +14 -0
  156. package/tests/unit-tests/testSpec.js +274 -0
  157. package/dist/mascot.js +0 -24446
@@ -0,0 +1,283 @@
1
+ import {getParents, getClosestLayout, getCellBoundsInLayout, sameClass, getPeers, getCellIndexInLayout} from "../util/ItemUtil";
2
+ import {DataType, ItemType, LayoutType} from "../util/Constants";
3
+ import Scale from "../core/Scale";
4
+
5
+ export function bindToPosition(encoding){
6
+
7
+ encoding._query = function() {
8
+ this.data = [];
9
+
10
+ let field = this.field, items = this.items;
11
+ let dataScopes = ((this.anyItem.type == "vertex" || this.anyItem.type == "segment") && !this.anyItem.dataScope) ?
12
+ items.map(d => d.parent.dataScope) : items.map(d => d.dataScope);
13
+
14
+ switch (this.datatable.getFieldType(field)) {
15
+ case DataType.Boolean:
16
+ break;
17
+
18
+ case DataType.Date:
19
+ this.data = dataScopes.map(d => d.getFieldValue(field));
20
+ break;
21
+
22
+ case DataType.String:
23
+ try {
24
+ this.data = dataScopes.map(d => d.getFieldValue(field));
25
+ } catch (error) {
26
+ throw new Error("Cannot bind " + this.channel + " to " + field + " : " + error);
27
+ }
28
+ break;
29
+
30
+ default: //integer or number
31
+ this.data = dataScopes.map(d => d.aggregateNumericalField(field, this.aggregator));
32
+ break;
33
+ }
34
+ }
35
+
36
+ encoding._map = function() {
37
+ let channel = this.channel, fieldType = this.datatable.getFieldType(this.field);
38
+
39
+ //to be used for determining the range of scale
40
+ //TODO: need to update cellBounds dynamically for _map() and _apply()
41
+ let extent;
42
+ let layout = getClosestLayout(this.anyItem);
43
+ if (layout && layout.type == LayoutType.Grid) {
44
+ let cellBounds = layout.cellBounds;
45
+ extent = channel == "x" ? cellBounds[0].width : cellBounds[0].height;
46
+ } else if (this.anyItem.type === "vertex" && this.anyItem.parent.type === ItemType.Area) {
47
+ extent = channel == "x" ? this.anyItem.parent.bounds.width : this.anyItem.parent.bounds.height;
48
+ } else {
49
+ let pos = this.items.map(d => d[channel]);
50
+ extent = Math.max(...pos) - Math.min(...pos);
51
+ if (extent < 100) extent = 100;
52
+ else if (extent > 500) extent = 500;
53
+ }
54
+
55
+ let min, max, domain, range;
56
+ switch (fieldType) {
57
+ case DataType.Boolean:
58
+ break;
59
+
60
+ case DataType.Date:
61
+ min = Math.min(...this.data);
62
+ max = Math.max(...this.data);
63
+ domain = [min, max];
64
+ if (this.scale) {
65
+ // let domainValues = domain.concat(this.scale.domain);
66
+ let domainValues = this.data;
67
+ for (let enc of this.scale.encodings) {
68
+ domainValues = domainValues.concat(enc.data);
69
+ }
70
+ domain = [Math.min(...domainValues), Math.max(...domainValues)];
71
+ //extent = Math.abs(this.scale.map(domain[0]) - this.scale.map(domain[1]));
72
+ range = this.scale.range;
73
+ } else {
74
+ this.scale = new Scale("time");
75
+ this.scale.isFlipped = this._flipScale;
76
+ range = [0, extent];
77
+ this.scale._baseItem = this.anyItem;
78
+ }
79
+ break;
80
+
81
+ case DataType.String:
82
+ domain = Array.from(new Set(this.data));
83
+ range = [0, extent];
84
+ if (this.scale) {
85
+ let od = this.scale.domain;
86
+ //the following lines preserve the order of values in existing domain
87
+ for (let d of domain) {
88
+ if (od.indexOf(d) < 0)
89
+ od.push(d);
90
+ }
91
+ domain = od;
92
+ //domain = Array.from(new Set(domain.concat(this.scale.domain)));
93
+ range = this.scale.range;
94
+ } else {
95
+ this.scale = new Scale("point");
96
+ this.scale.isFlipped = this._flipScale;
97
+ range = [0, extent];
98
+ this.scale._baseItem = this.anyItem;
99
+ }
100
+ break;
101
+
102
+ default: //integer or number
103
+ min = Math.min(...this.data);
104
+ max = Math.max(...this.data);
105
+ domain = [min, max];
106
+
107
+ if (this.scale) {
108
+ //where zero is included depends on the existing scale
109
+ let domainValues = this.data;
110
+ for (let enc of this.scale.encodings) {
111
+ domainValues = domainValues.concat(enc.data);
112
+ }
113
+ // let domainValues = domain.concat(this.scale._scale.domain());
114
+ domain = [Math.min(...domainValues), Math.max(...domainValues)];
115
+
116
+ range = this.scale.range;
117
+ } else {
118
+ this.scale = new Scale(this.scaleType);
119
+ this.scale.isFlipped = this._flipScale;
120
+ this.scale.includeZero = this._includeZero;
121
+ range = [0, extent];
122
+ //remember the item that was used to first create this scale,
123
+ //so that when the scale is reused later, we can refer to the base item
124
+ //to get absoluate positions
125
+ this.scale._baseItem = this.anyItem;
126
+ }
127
+ if (domain[0] == domain[1])
128
+ domain[1] = domain[0] * 1.1;
129
+ break;
130
+ }
131
+
132
+ this.scale._scale.domain(domain);
133
+ this.scale._setRange(range);
134
+
135
+ //TODO: need to adjust according to scale type
136
+ this.scale._addEncoding(this);
137
+ }
138
+
139
+ encoding._apply = function() {
140
+ let items = [], channel = this.channel;
141
+ //if the scale is shared across multiple encodings, need to find the offset based on all items using this scale
142
+ //use case: box plot, where lines, rect segments and line vertices share the same scale
143
+ for (let enc of this.scale.encodings)
144
+ items = items.concat(enc.items);
145
+ if (channel == "x") {
146
+ //let layout = getClosestLayout(this.anyItem, "grid");
147
+ //let layout = getTopLevelCollection(this.anyItem) ? getTopLevelCollection(this.anyItem).layout : getClosestLayout(this.anyItem);
148
+ let layout = getClosestLayout(this.anyItem, "grid"), baseLayout = this.scale._baseItem ? getClosestLayout(this.scale._baseItem, "grid"): undefined;
149
+ if (this.scale._baseItem && !sameClass(this.anyItem, this.scale._baseItem) && layout && baseLayout && layout.numRows === baseLayout.numRows && layout.numCols === baseLayout.numCols) {
150
+ let tx = baseLayout.group.bounds.left - layout.group.bounds.left,
151
+ ty = 0;
152
+ layout.group.getScene().translate(layout.group, tx, ty);
153
+ layout._colGap = baseLayout.colGap;
154
+ let cellIndices = this.items.map(d => getCellIndexInLayout(d)),
155
+ baseCellBounds = baseLayout.cellBounds;
156
+ for (let i = 0; i < this.items.length; i++) {
157
+ let itm = this.items[i];
158
+ let dx = baseCellBounds[cellIndices[i]].left + this.scale.map(this.data[i]) - itm[channel],
159
+ dy = 0;
160
+ itm._doTranslate(dx, dy);
161
+ if (itm.type == "vertex" || itm.type == "segment")
162
+ itm.parent._updateBounds();
163
+ }
164
+ this.anyItem.parent.getScene()._updateAncestorBounds(this.anyItem, this.items);
165
+ } else if (layout && layout.type == LayoutType.Grid){
166
+ //do not use scale.offset, use cell bounds
167
+ for (let i = 0; i < this.items.length; i++) {
168
+ let itm = this.items[i], itmCb = getCellBoundsInLayout(itm);
169
+ let dx = itmCb.left + this.scale.map(this.data[i]) - itm[channel],
170
+ dy = 0;
171
+ itm._doTranslate(dx, dy);
172
+ if (itm.type == "vertex" || itm.type == "segment")
173
+ itm.parent._updateBounds();
174
+ }
175
+ } else if (this.anyItem.type === "vertex" && this.anyItem.parent.type === ItemType.Area) {
176
+ //this is to make sure that the encoding of vertex position is within the bounds of an area mark
177
+ let marks = getParents(this.items);
178
+ if (this.scale.offset === undefined)
179
+ this.scale.offset = Math.min(...marks.map(d => d.bounds.left));
180
+ for (let i = 0; i < this.items.length; i++) {
181
+ let peer = this.items[i], parent = peer.parent;
182
+ let dx = parent.bounds.left + this.scale.map(this.data[i]) - peer[channel],
183
+ dy = 0;
184
+ peer._doTranslate(dx, dy);
185
+ peer.parent._updateBounds();
186
+ }
187
+ } else if (this.anyItem.type == "vertex" || this.anyItem.type == "segment") {
188
+ //TODO: right now using the leftmost mark peer as the baseline, which will not work for cases like bullet chart
189
+ //need to check if marks are part of a glyph, and use the leftmose glyph peer.
190
+ let marks = getParents(this.items);
191
+ if (this.scale.offset === undefined)
192
+ this.scale.offset = Math.min(...marks.map(d => d.bounds.left));
193
+ for (let i = 0; i < this.items.length; i++) {
194
+ let peer = this.items[i];
195
+ let dx = this.scale.offset + this.scale.map(this.data[i]) - peer[channel],
196
+ dy = 0;
197
+ peer._doTranslate(dx, dy);
198
+ peer.parent._updateBounds();
199
+ }
200
+ } else {
201
+ if (this.scale.offset === undefined)
202
+ this.scale.offset = Math.min(...items.map(d => d[channel]));
203
+ if (this.items[0].type === ItemType.PointText)
204
+ this.items.forEach(d => d.horzAnchor = "center");
205
+ for (let i = 0; i < this.items.length; i++) {
206
+ let peer = this.items[i];
207
+ let dx = this.scale.offset + this.scale.map(this.data[i]) - peer[channel],
208
+ dy = 0;
209
+ peer._doTranslate(dx, dy);
210
+ }
211
+ }
212
+
213
+ } else {//channel y
214
+ //let layout = getTopLevelCollection(this.anyItem) ? getTopLevelCollection(this.anyItem).layout : getClosestLayout(this.anyItem);
215
+ let layout = getClosestLayout(this.anyItem, "grid"), baseLayout = this.scale._baseItem ? getClosestLayout(this.scale._baseItem, "grid"): undefined;
216
+ if (this.scale._baseItem && !sameClass(this.anyItem, this.scale._baseItem) && layout && baseLayout && layout.numRows === baseLayout.numRows && layout.numCols === baseLayout.numCols) {
217
+ //if (this.scale._baseItem && !sameClass(this.anyItem, this.scale._baseItem) && baseLayout && baseLayout.numRows === 1) {
218
+ //let peers = getPeers(this.scale._baseItem, this.scale._baseItem.parent.getScene());
219
+ let tx = 0,
220
+ ty = baseLayout.group.bounds.top - layout.group.bounds.top;
221
+ layout.group.getScene().translate(layout.group, tx, ty);
222
+ layout._rowGap = baseLayout.rowGap;
223
+ let cellIndices = this.items.map(d => getCellIndexInLayout(d)),
224
+ baseCellBounds = baseLayout.cellBounds;
225
+ for (let i = 0; i < this.items.length; i++) {
226
+ let itm = this.items[i];
227
+ let dx = 0,
228
+ dy = baseCellBounds[cellIndices[i]].bottom - this.scale.map(this.data[i]) - itm[channel];
229
+ itm._doTranslate(dx, dy);
230
+ if (itm.type == "vertex" || itm.type == "segment")
231
+ itm.parent._updateBounds();
232
+ }
233
+ this.anyItem.parent.getScene()._updateAncestorBounds(this.anyItem, this.items);
234
+ } else if (layout && layout.type == LayoutType.Grid){
235
+ let cellBounds = this.items.map(d => getCellBoundsInLayout(d));
236
+ for (let i = 0; i < this.items.length; i++) {
237
+ let itm = this.items[i];
238
+ let dx = 0,
239
+ dy = cellBounds[i].bottom - this.scale.map(this.data[i]) - itm[channel];
240
+ itm._doTranslate(dx, dy);
241
+ if (itm.type == "vertex" || itm.type == "segment")
242
+ itm.parent._updateBounds();
243
+ }
244
+ } else if (this.anyItem.type === "vertex" && this.anyItem.parent.type === ItemType.Area) {
245
+ if (this.scale.offset === undefined)
246
+ this.scale.offset = Math.min(...this.items.map(d => d.y));
247
+ for (let i = 0; i < this.items.length; i++) {
248
+ let peer = this.items[i], parent = peer.parent;
249
+ let dx = 0,
250
+ dy = parent.bounds.top + this.scale.rangeExtent - this.scale.map(this.data[i]) - peer[channel];
251
+ peer._doTranslate(dx, dy);
252
+ peer.parent._updateBounds();
253
+ }
254
+ } else if (this.anyItem.type == "vertex" || this.anyItem.type == "segment") {
255
+ if (this.scale.offset === undefined)
256
+ this.scale.offset = Math.min(...this.items.map(d => d.y));
257
+ for (let i = 0; i < this.items.length; i++) {
258
+ let peer = this.items[i];
259
+ let dx = 0,
260
+ dy = this.scale.offset + this.scale.rangeExtent - this.scale.map(this.data[i]) - peer[channel];
261
+ peer._doTranslate(dx, dy);
262
+ peer.parent._updateBounds();
263
+ }
264
+ } else {
265
+ if (this.scale.offset === undefined)
266
+ this.scale.offset = Math.min(...items.map(d => d.bounds.y));
267
+ if (this.items[0].type === ItemType.PointText)
268
+ this.items.forEach(d => d.vertAnchor = "middle");
269
+ for (let i = 0; i < this.items.length; i++) {
270
+ let peer = this.items[i];
271
+ let dx = 0,
272
+ dy = this.scale.offset + this.scale.rangeExtent - this.scale.map(this.data[i]) - peer.refBounds[channel];
273
+ peer._doTranslate(dx, dy);
274
+ }
275
+ }
276
+ }
277
+ }
278
+
279
+ encoding.run();
280
+ if (encoding.rangeExtent)
281
+ encoding.scale.rangeExtent = encoding.rangeExtent;
282
+ return encoding;
283
+ }
@@ -0,0 +1,62 @@
1
+ import Scale from "../core/Scale";
2
+ import {DataType} from "../util/Constants";
3
+ import {polar2Cartesian} from "../util/ItemUtil";
4
+
5
+ export function bindToRadialDistance(encoding){
6
+
7
+ encoding._query = function() {
8
+ this.data = [];
9
+
10
+ let field = this.field, items = this.items;
11
+ let dataScopes = ((this.anyItem.type == "vertex" || this.anyItem.type == "segment") && !this.anyItem.dataScope) ?
12
+ items.map(d => d.parent.dataScope) : items.map(d => d.dataScope);
13
+
14
+ switch (this.datatable.getFieldType(field)) {
15
+ case DataType.Boolean:
16
+ break;
17
+
18
+ case DataType.Date:
19
+ this.data = dataScopes.map(d => d.getFieldValue(field));
20
+ break;
21
+
22
+ case DataType.String:
23
+ try {
24
+ this.data = dataScopes.map(d => d.getFieldValue(field));
25
+ } catch (error) {
26
+ throw new Error("Cannot bind " + this.channel + " to " + field + " : " + error);
27
+ }
28
+ break;
29
+
30
+ default: //integer or number
31
+ this.data = dataScopes.map(d => d.aggregateNumericalField(field, this.aggregator));
32
+ break;
33
+ }
34
+ }
35
+
36
+
37
+ encoding._map = function() {
38
+ let data = this.data;
39
+ if (!this.scale){
40
+ this.scale = new Scale("linear");
41
+ this.scale.domain = [0, Math.max(...data)];
42
+ this.scale._setRange([0, this.anyItem.parent.radius]);
43
+
44
+ }
45
+ this.scale._addEncoding(this);
46
+ }
47
+
48
+ encoding._apply = function() {
49
+ for (let i = 0; i < this.items.length; i++) {
50
+ let peer = this.items[i], rd = this.scale.map(this.data[i]);
51
+ let coords = polar2Cartesian(this.anyItem.parent.x, this.anyItem.parent.y, rd, peer.polarAngle);
52
+ peer.x = coords[0];
53
+ peer.y = coords[1];
54
+ }
55
+
56
+ //relayout if needed
57
+ this.scene._relayoutAncestors(this.anyItem, this.items);
58
+ }
59
+
60
+ encoding.run();
61
+ return encoding;
62
+ }
@@ -0,0 +1,235 @@
1
+ import Scale from "../core/Scale";
2
+ import {DataType, ItemType, LayoutType} from "../util/Constants";
3
+ import { uniqueNumbers } from "../util/DataUtil";
4
+
5
+ export function bindToSize(encoding){
6
+
7
+ encoding._query = function() {
8
+ this.data = [];
9
+ let field = this.field, items = this.items, aggregator = this.aggregator;
10
+ switch (this.datatable.getFieldType(field)) {
11
+ case DataType.Boolean:
12
+ break;
13
+
14
+ case DataType.Date:
15
+ this.data = items.map(d => d.dataScope.getFieldValue(field));
16
+ break;
17
+
18
+ case DataType.String:
19
+ break;
20
+
21
+ default: //integer or number
22
+ this.data = items.map(d => d.dataScope.aggregateNumericalField(field, aggregator));
23
+ break;
24
+ }
25
+ }
26
+
27
+ encoding._map = function() {
28
+ let items = this.items, data = this.data, channel = this.channel, scale;
29
+
30
+ scale = new Scale(this.scaleType);
31
+
32
+ if (this.data.find(d => d < 0) != undefined && (channel == "width" || channel == "height") && items[0].type.indexOf("rect") == 0) {
33
+ this._rectNegativeValues = true;
34
+ scale.domain = [Math.min(...data), Math.max(...data)];
35
+ if (channel == "width") {
36
+ // let left = Math.min(...(items.map(d => d.bounds.left))),
37
+ // right = Math.max(...(items.map(d => d.bounds.right)));
38
+ // scale._setRange([0, right - left]);
39
+
40
+ //TODO: check if item width has already been encoded with a different field
41
+ //the code below assumes width hasn't been bound to data
42
+ scale._setRange([0, this.rangeExtent ? this.rangeExtent : Math.max(...(items.map(d => d.width)))]);
43
+ } else {
44
+ // let top = Math.min(...(items.map(d => d.bounds.top))),
45
+ // bottom = Math.max(...(items.map(d => d.bounds.bottom)));
46
+ // scale._setRange([0, bottom - top]);
47
+
48
+ //TODO: check if item height has already been encoded with a different field
49
+ //the code below assumes height hasn't been bound to data
50
+ scale._setRange([0, this.rangeExtent ? this.rangeExtent : Math.max(...(items.map(d => d.height)))]);
51
+ }
52
+ } else {
53
+ scale.domain = [0, Math.max(...data)];
54
+ let min, max;
55
+ if (channel == "radius" || channel == "outerRadius" || channel == "innerRadius") {
56
+ min = 0;
57
+ max = Math.max(...(items.map(d => d[channel])));
58
+ if (max < 20) max = 20;
59
+ } else if (channel == "area") {
60
+ min = 0;
61
+ max = Math.max(...(items.map(d => d.bounds.width * d.bounds.height)));
62
+ if (max < 400) max = 400;
63
+ } else if (channel == "fontSize") {
64
+ min = 2;
65
+ max = Math.max(...(items.map(d => parseFloat(d.styles.fontSize))));
66
+ } else if (channel == "strokeWidth") {
67
+ min = 1;
68
+ max = Math.max(...(items.map(d => parseFloat(d.styles.strokeWidth))));
69
+ if (max == min)
70
+ max = min + 5;
71
+ } else {
72
+ min = 0;
73
+ max = Math.max(...(items.map(d => d.bounds[channel])));
74
+ }
75
+ if (this.rangeExtent)
76
+ max = min + this.rangeExtent;
77
+ if (this.range) {
78
+ min = this.range[0];
79
+ max = this.range[1];
80
+ }
81
+ scale._setRange([min, max]);
82
+ }
83
+
84
+ if (this.scale){
85
+ // TODO
86
+ } else {
87
+ this.scale = scale;
88
+ }
89
+
90
+ this.scale._addEncoding(this);
91
+ }
92
+
93
+ encoding._apply = function() {
94
+ if (this.channel == "radius" || this.channel == "outerRadius" || this.channel == "innerRadius") {
95
+ for (let i = 0; i < this.items.length; i++) {
96
+ let peer = this.items[i];
97
+ peer[this.channel] = this.scale.map(this.data[i]);
98
+ }
99
+ this.scene._relayoutAncestors(this.anyItem, this.items);
100
+ } else if (this._rectNegativeValues){
101
+ let dMax = Math.max(...this.data), dMin = Math.min(...this.data),
102
+ base = this.channel === "width" ? Math.min(...this.items.map(d => d.bounds.left)) :
103
+ Math.min(...this.items.map(d => d.bounds.top)) + this.scale.rangeExtent,
104
+ maxSide = this.channel === "width" ? "right" : "top",
105
+ minSide = this.channel === "width" ? "left" : "bottom";
106
+ for (let i = 0; i < this.items.length; i++) {
107
+ let offset, peer = this.items[i];
108
+ if (peer.parent && peer.parent.type === ItemType.Collection) {
109
+ let pp = peer.parent.parent;
110
+ if (pp.type === ItemType.Collection && pp.layout && pp.layout.type === LayoutType.Grid) {
111
+ let idx = pp.children.indexOf(peer.parent);
112
+ offset = pp.layout.cellBounds[idx][minSide];
113
+ } else {
114
+ offset = peer.parent.refBounds[minSide];
115
+ }
116
+ } else {
117
+ offset = base;
118
+ }
119
+ if (this.channel === "width") {
120
+ peer.leftSegment._doTranslate(offset + this.scale.map(0) - peer.leftSegment.vertex1.x, 0);
121
+ peer.rightSegment._doTranslate(offset + this.scale.map(this.data[i]) - peer.rightSegment.vertex1.x, 0);
122
+ } else {
123
+ peer.topSegment._doTranslate(0, offset - this.scale.map(this.data[i]) - peer.topSegment.vertex1.y);
124
+ peer.bottomSegment._doTranslate(0, offset - this.scale.map(0) - peer.bottomSegment.vertex1.y);
125
+ }
126
+
127
+ peer._updateBounds();
128
+ if (this.data[i] > 0) {
129
+ peer.boundsOffsets[maxSide] = this.scale.map(dMax) - this.scale.map(this.data[i]);
130
+ peer.boundsOffsets[minSide] = this.scale.map(0) - this.scale.map(dMin);
131
+ } else {
132
+ peer.boundsOffsets[minSide] = this.scale.map(this.data[i]) - this.scale.map(dMin);
133
+ peer.boundsOffsets[maxSide] = this.scale.map(dMax) - this.scale.map(0);
134
+ }
135
+ }
136
+
137
+
138
+ // if (this.channel == "width") {
139
+ // let base = Math.min(...this.items.map(d => d.bounds.left));
140
+ // for (let i = 0; i < this.items.length; i++) {
141
+ // let offset, peer = this.items[i], left = peer.leftSegment.vertex1.x, right = peer.rightSegment.vertex1.x;
142
+ // if (peer.parent && peer.parent.type == ItemType.Collection) {
143
+ // offset = peer.parent.bounds.left;
144
+ // } else {
145
+ // offset = base;
146
+ // }
147
+ // peer.rightSegment._doTranslate(offset + this.scale.map(this.data[i]) - right, 0);
148
+ // peer.leftSegment._doTranslate(offset + this.scale.map(0) - left, 0);
149
+ // peer._updateBounds();
150
+ // if (this.data[i] > 0) {
151
+ // peer.boundsOffsets.right = this.scale.map(dMax) - this.scale.map(this.data[i]);
152
+ // peer.boundsOffsets.left = this.scale.map(0) - this.scale.map(dMin);
153
+ // } else {
154
+ // peer.boundsOffsets.left = this.scale.map(this.data[i]) - this.scale.map(dMin);
155
+ // peer.boundsOffsets.right = this.scale.map(dMax) - this.scale.map(0);
156
+ // }
157
+ // }
158
+ // } else if (this.channel == "height") {
159
+ // for (let i = 0; i < this.items.length; i++) {
160
+ // let offset, peer = this.items[i], top = peer.topSegment.vertex1.y, btm = peer.bottomSegment.vertex1.y;
161
+ // if (peer.parent && peer.parent.type === ItemType.Collection) {
162
+ // let pp = peer.parent.parent;
163
+ // if (pp.type === ItemType.Collection && pp.layout && pp.layout.type === LayoutType.Grid) {
164
+ // let idx = pp.children.indexOf(peer.parent);
165
+ // offset = pp.layout.cellBounds[idx].bottom;
166
+ // } else {
167
+ // offset = peer.parent.refBounds.bottom;
168
+ // }
169
+ // } else {
170
+ // let base = Math.min(...this.items.map(d => d.bounds.top)) + this.scale.rangeExtent;
171
+ // offset = base;
172
+ // }
173
+ // peer.topSegment._doTranslate(0, offset - this.scale.map(this.data[i]) - top);
174
+ // peer.bottomSegment._doTranslate(0, offset - this.scale.map(0) - btm);
175
+ // peer._updateBounds();
176
+ // if (this.data[i] > 0) {
177
+ // peer.boundsOffsets.top = this.scale.map(dMax) - this.scale.map(this.data[i]);
178
+ // peer.boundsOffsets.bottom = this.scale.map(0) - this.scale.map(dMin);
179
+ // } else {
180
+ // peer.boundsOffsets.bottom = this.scale.map(this.data[i]) - this.scale.map(dMin);
181
+ // peer.boundsOffsets.top = this.scale.map(dMax) - this.scale.map(0);
182
+ // }
183
+ // }
184
+ // }
185
+ } else if (this.channel == "area") {
186
+ for (let i = 0; i < this.items.length; i++) {
187
+ let peer = this.items[i], s = Math.sqrt(this.scale.map(this.data[i]));
188
+ peer.resize(s, s);
189
+ }
190
+ this.scene._relayoutAncestors(this.anyItem, this.items);
191
+ } else if (this.channel == "fontSize") {
192
+ for (let i = 0; i < this.items.length; i++) {
193
+ let peer = this.items[i], s = this.scale.map(this.data[i]);
194
+ peer.styles.fontSize = s+ "px";
195
+ }
196
+ this.scene._relayoutAncestors(this.anyItem, this.items);
197
+ } else if (this.channel == "strokeWidth") {
198
+ for (let i = 0; i < this.items.length; i++) {
199
+ let peer = this.items[i], s = this.scale.map(this.data[i]);
200
+ peer.styles.strokeWidth = s;
201
+ }
202
+ this.scene._relayoutAncestors(this.anyItem, this.items);
203
+ } else {
204
+ let xRef = 'left', yRef = 'bottom';
205
+ //if glyph item, check alignment
206
+ if (this.anyItem.parent && this.anyItem.parent.type == ItemType.Glyph) {
207
+ let children = this.anyItem.parent.children;
208
+ if (uniqueNumbers(children.map(d=>d.bounds.right)).length == 1)
209
+ xRef = 'right';
210
+ if (uniqueNumbers(children.map(d=>d.bounds.top)).length == 1)
211
+ yRef = 'top';
212
+ }
213
+
214
+ if (this.anyItem.type === ItemType.Collection && this.anyItem.layout && this.anyItem.layout[this.channel]) {
215
+ //for cases like treemap barchart, where the collection size is bound to data before dividing
216
+ for (let i = 0; i < this.items.length; i++) {
217
+ let peer = this.items[i], s = this.scale.map(this.data[i]);
218
+ peer.layout[this.channel] = s;
219
+ }
220
+ } else {
221
+ for (let i = 0; i < this.items.length; i++) {
222
+ let peer = this.items[i], s = this.scale.map(this.data[i]);
223
+ let wd = this.channel == "width" ? s : peer.bounds.width,
224
+ ht = this.channel == "height" ? s : peer.bounds.height;
225
+ peer.resize(wd, ht, xRef, yRef);
226
+ }
227
+ }
228
+
229
+ this.scene._relayoutAncestors(this.anyItem, this.items);
230
+ }
231
+ }
232
+
233
+ encoding.run();
234
+ return encoding;
235
+ }
@@ -0,0 +1,60 @@
1
+ import {DataType} from "../util/Constants";
2
+ import DataScope from "../data/DataScope";
3
+ import { nodeId } from "../util/Constants";
4
+ import Scale from "../core/Scale";
5
+
6
+ export function bindToText(encoding){
7
+
8
+ encoding._query = function() {
9
+ this.data = [];
10
+
11
+ let field, items = this.items, dt;
12
+ if (this.field.startsWith("parent.") || this.field.startsWith("child.")) {
13
+ dt = this.datatable.tree.nodeTable;
14
+ field = this.field.split(".")[1];
15
+ let s = this.field.split(".")[0];
16
+ let nodeIds = items.map(d => d.dataScope).map(d => d.getFieldValue(s));
17
+ if (dt.getFieldType(field) == DataType.Integer || dt.getFieldType(field) == DataType.Number)
18
+ this.data = nodeIds.map(d => (new DataScope(dt)).cross(nodeId, d).aggregateNumericalField(field));
19
+ else
20
+ this.data = nodeIds.map(d => (new DataScope(dt)).cross(nodeId, d).getFieldValue(field));
21
+ } else {
22
+ dt = this.datatable;
23
+ field = this.field;
24
+ if (dt.getFieldType(field) == DataType.Integer || dt.getFieldType(field) == DataType.Number)
25
+ this.data = items.map(d => d.dataScope.aggregateNumericalField(field, this.aggregator));
26
+ else
27
+ this.data = items.map(d => d.dataScope.getFieldValue(field));
28
+ }
29
+ }
30
+
31
+ encoding._map = function() {
32
+ if (this.scale) {
33
+ } else {
34
+ switch (this.datatable.getFieldType(this.field)) {
35
+ case DataType.Boolean:
36
+ break;
37
+
38
+ case DataType.Date:
39
+ break;
40
+
41
+ case DataType.String:
42
+ default: //integer or number
43
+ this.scale = new Scale("ordinal");
44
+ this.scale.domain = [...new Set(this.data)];
45
+ this.scale._scale.range(this.scale.domain.map(d => d + ""));
46
+ break;
47
+ }
48
+ }
49
+ }
50
+
51
+ encoding._apply = function() {
52
+ for (let i = 0; i < this.items.length; i++) {
53
+ let peer = this.items[i], value = this.scale.map(this.data[i]);
54
+ peer.text = value;
55
+ }
56
+ }
57
+
58
+ encoding.run();
59
+ return encoding;
60
+ }