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