More Related Content Similar to D3 svg & angular (20) D3 svg & angular1. Adam Klein
- 500Tech - Frontend Experts
- Passionate developer & speaker
- I used to have a jewfro
CTO @
D3, SVG & ANGULAR
11. Make an Arc - SVG
<path stroke="black" fill="black" thickness="2"
d="M13.753288904374106,9.992349288972044A17,17 0 0,1
-13.753288904374106,9.992349288972045L-12.135254915624
213,8.816778784387099A15,15 0 0,0
12.135254915624213,8.816778784387097Z"></path>
12. Make an Arc - D3
let arc = d3.svg.arc()
.outerRadius(17)
.innerRadius(15)
.startAngle(0.7 * Math.PI)
.endAngle(1.3 * Math.PI);
d3.select("svg")
.append("path")
.attr("d", arc());
13. D3 Path Data Generators
+ Angular
arc() {
return d3.svg.arc()
.outerRadius(17)
.innerRadius(15)
.startAngle(0.7 * Math.PI)
.endAngle(1.3 * Math.PI)();
}
<path d={{ $ctrl.arc() }}></path>
19. drag
var drag = d3.behavior.drag()
.on("drag", dragged);
function dragged(d) {
d3.select(this)
.attr("cx", d.x = d3.event.x)
.attr("cy", d.y = d3.event.y);
}
var dot = d3.select("g")
.selectAll("circle")
.data(dots)
.enter().append("circle")
.attr("r", 5)
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
.call(drag);
20. drag
Define
behaviour
var drag = d3.behavior.drag()
.on("drag", dragged);
function dragged(d) {
d3.select(this)
.attr("cx", d.x = d3.event.x)
.attr("cy", d.y = d3.event.y);
}
var dot = d3.select("g")
.selectAll("circle")
.data(dots)
.enter().append("circle")
.attr("r", 5)
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
.call(drag);
21. drag
var drag = d3.behavior.drag()
.on("drag", dragged);
function dragged(d) {
d3.select(this)
.attr("cx", d.x = d3.event.x)
.attr("cy", d.y = d3.event.y);
}
var dot = d3.select("g")
.selectAll("circle")
.data(dots)
.enter().append("circle")
.attr("r", 5)
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
.call(drag);
Create elements
and bind to data
22. drag
var drag = d3.behavior.drag()
.on("drag", dragged);
function dragged(d) {
d3.select(this)
.attr("cx", d.x = d3.event.x)
.attr("cy", d.y = d3.event.y);
}
var dot = d3.select("g")
.selectAll("circle")
.data(dots)
.enter().append("circle")
.attr("r", 5)
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
.call(drag);
attach behaviour to
element
23. drag
Change DOM
on drag
var drag = d3.behavior.drag()
.on("drag", dragged);
function dragged(d) {
d3.select(this)
.attr("cx", d.x = d3.event.x)
.attr("cy", d.y = d3.event.y);
}
var dot = d3.select("g")
.selectAll("circle")
.data(dots)
.enter().append("circle")
.attr("r", 5)
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
.call(drag);
24. d3 drag + angular
<circle
ng-attr-cx="{{ item.x }}"
ng-attr-cy="{{ item.y }}">
</circle>
25. const drag = d3.behavior.drag()
.on(‘drag', () => {
this.item.x += d3.event.dx;
this.item.y += d3.event.dy;
$scope.$digest();
});
d3.select($element[0]).call(drag);
Define behaviour
d3 drag + angular
26. const drag = d3.behavior.drag()
.on(‘drag', () => {
this.item.x += d3.event.dx;
this.item.y += d3.event.dy;
$scope.$digest();
});
d3.select($element[0]).call(drag);
Change data on
drag
wrap with directive
27. wrap with directive
const drag = d3.behavior.drag()
.on(‘drag', () => {
this.item.x += d3.event.dx;
this.item.y += d3.event.dy;
$scope.$digest();
});
d3.select($element[0]).call(drag);
Attach behaviour
to element
34. D3 force layout + Angular
const force = d3.layout.force()
.charge(-60)
.gravity(0.02)
.linkStrength(0.1)
.linkDistance(260)
.size([450,450])
.nodes(this.nodes)
.links(this.links)
.start();
force.on("tick", () => {
$scope.$digest();
});
Define
layout
35. const force = d3.layout.force()
.charge(-60)
.gravity(0.02)
.linkStrength(0.1)
.linkDistance(260)
.size([450,450])
.nodes(this.nodes)
.links(this.links)
.start();
force.on("tick", () => {
$scope.$digest();
});
Bind layout to data
D3 force layout + Angular
36. Template using force layout
<circle
ng-repeat=“item in nodes"
ng-attr-cx="{{ item.x }}"
ng-attr-cy="{{ item.y }}">
</circle>
Bind data to
markup
41. D3
use helpers to prepare data to DOM
define behaviours and attach to elements
define layouts and bind to data
Angular
bind data to DOM
create directive to encapsulate components / behaviours / layouts