comparison src/DML/MainVisBundle/Resources/assets/lib/vega/vega.filter.centroid.js @ 0:493bcb69166c

added public content
author Daniel Wolff
date Tue, 09 Feb 2016 20:54:02 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:493bcb69166c
1 (function() {
2 /**
3 * centroid function in vega expression
4 */
5 if (vg) {
6 var Transform = vg.transforms.bin.prototype.__proto__.constructor;
7
8 function Centroid(graph) {
9 Transform.prototype.init.call(this, graph);
10 Transform.addParameters(this, {
11 field: {type: 'field'}
12 });
13
14 this._output = {centroid_x: 'centroid_x', centroid_y: 'centroid_y'};
15 return this.mutates(true);
16 }
17
18 var prototype = (Centroid.prototype = Object.create(Transform.prototype));
19 prototype.constructor = Centroid;
20
21 prototype.transform = function(input) {
22 //log.debug(input, ['generating a centroid']);
23
24 var output_x = this._output.centroid_x,
25 output_y = this._output.centroid_y,
26 get = this.param('path').accessor;
27
28 var svg = d3.select("body").append("svg")
29 //.remove()
30 .attr("width", 400)
31 .attr("height", 400);
32
33 var p = svg.append("path");
34 function update(d) {
35 p.attr("d", d["layout_path"]);
36 var bbox = p.node().getBBox();
37 var x = Math.floor(bbox.x + bbox.width/2.0);
38 var y = Math.floor(bbox.y + bbox.height/2.0);
39 vg.dataflow.Tuple.set(d, output_x, x);
40 vg.dataflow.Tuple.set(d, output_y, y);
41 }
42 input.add.forEach(update);
43 input.mod.forEach(update);
44 input.rem.forEach(update);
45
46 input.fields[output_x] = 0;
47 input.fields[output_y] = 0;
48 return input;
49 };
50
51 //module.exports = Bin;
52
53 Centroid.schema = {
54 "$schema": "http://json-schema.org/draft-04/schema#",
55 "title": "Bin transform",
56 "description": "Bins values into quantitative bins (e.g., for a histogram).",
57 "type": "object",
58 "properties": {
59 "type": {"enum": ["bin"]},
60 "field": {
61 "oneOf": [{"type": "string"}, {"$ref": "#/refs/signal"}],
62 "description": "The name of the field to calculate centroids from values from."
63 },
64 "output": {
65 "type": "object",
66 "description": "Rename the output data fields",
67 "properties": {
68 "bin": {"type": "string", "default": "bin"}
69 },
70 "additionalProperties": false
71 }
72 },
73 "additionalProperties": false,
74 "required":["field"]
75 };
76
77 vg.transforms.centroid = Centroid;
78 console.log(vg);
79 } else {
80 //console.error("Can't register centroid filter in vega expression");
81 }
82 }());