-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathChartRendering.js
55 lines (51 loc) · 1.72 KB
/
ChartRendering.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
// Declare variables for R input
var col = "orange";
var coord = [];
var binding = new Shiny.OutputBinding();
binding.find = function(scope) {
return $(scope).find(".Dragable");
};
binding.renderValue = function(el, data) {
var $el = $(el);
var boxWidth = 600;
var boxHeight = 400;
dataArray = data.data
col = data.col
var box = d3.select(el)
.append('svg')
.attr('class', 'box')
.attr('width', boxWidth)
.attr('height', boxHeight);
var drag = d3.behavior.drag()
.on('dragstart', function(d, i) {
box.select("circle:nth-child(" + (i + 1) + ")")
.style('fill', 'red');
})
.on('drag', function(d, i) {
box.select("circle:nth-child(" + (i + 1) + ")")
.attr('cx', d3.event.x)
.attr('cy', d3.event.y);
})
.on('dragend', function(d, i) {
circle.style('fill', col);
coord = []
d3.range(1, (dataArray.length + 1)).forEach(function(entry) {
sel = box.select("circle:nth-child(" + (entry) + ")")
coord = d3.merge([coord, [sel.attr("cx"), sel.attr("cy")]])
})
console.log(coord)
Shiny.onInputChange("JsData", coord);
});
var circle = box.selectAll('.draggableCircle')
.data(dataArray)
.enter()
.append('svg:circle')
.attr('class', 'draggableCircle')
.attr('cx', function(d) { return d.x; })
.attr('cy', function(d) { return d.y; })
.attr('r', function(d) { return d.r; })
.call(drag)
.style('fill', col);
};
// Regsiter new Shiny binding
Shiny.outputBindings.register(binding, "shiny.Dragable");