JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://d3js.org/d3.v3.min.js"></script>
<div ng-app="myApp" ng-controller="AppCtrl">
<div graphe-forces></div>
</div>
CSS
.node {
stroke: #fff;
stroke-width: 1.5px;
}
.link {
stroke: #999;
stroke-opacity: .6;
}
JavaScript
angular.module('myApp', []).
// Directive contenant le code D3.js
directive('grapheForces', function() {
return {
restrict: 'A',
link: function (scope, element) {
var width = 450;
var height = 400;
var color = d3.scale.category20();
// On récupère les données présentent dans scope.grapheDatas
// Le $watch a pour but de mettre à jour le graphe dès que les
// données présentent dans $scope.grapheDatas changent.
// Ex : suppression ou ajout de noeuds
scope.$watch('grapheDatas', function (grapheDatas) {
var force = d3.layout.force()
.charge(-120)
.linkDistance(30)
.size([width, height])
.nodes(grapheDatas.nodes)
.links(grapheDatas.links)
.start();
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var link = svg.selectAll(".link")
.data(grapheDatas.links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", function(d) { return Math.sqrt(d.value); });
var node = svg.selectAll(".node")
.data(grapheDatas.nodes)
.enter().append("circle")
.attr("class", "node")
.attr("r", 5)
.style("fill", function(d) { return color(d.group); })
.call(force.drag);
node.append("title")
.text(function(d) { return d.name; });
force.on("tick", function() {
link.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2",...