How to bind angularjs directive to d3js node defined in link function

I would like to use the d3js graph in an AngularJs application and then bind the directives to the nodes.

First I put the js code in the directive's link function, and everything works fine.

angular.module('myApp', []).

directive('grapheForces', function() {
    return {
        restrict: 'A',
        link: function (scope, element) {
            var width = 450;
            var height = 400;
            var color = d3.scale.category20();

            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", function(d) { return d.target.x; })
                    .attr("y2", function(d) { return d.target.y; });

                    node.attr("cx", function(d) { return d.x; })
                    .attr("cy", function(d) { return d.y; });
                });
            });
        }
    }
}).

And then I would like to add a hint to the nodes, so I go:

                var node = svg.selectAll(".node")
                .attr("tooltip", function(){
                    return "tooltipTextHere";
                 });

Since I use angular-bootstrap, tooltip is a directive. The tooltip attribute is well present in the html results:

<circle tooltip="tooltipTextHere" class="nodeCircle" r="4.5" style="fill: #b0c4de;"></circle>

But the tooltip is ineffective, and therefore it is suitable for every directive that I link this way.

I assume this is because the directive was not taken into account during the compilation phase, but I cannot find how to do this when I reach my current limits of understanding in AngularJs.

, ? .

+4
1

. .

, - element.removeAttr("graphe-forces"), $compile(element)(scope), .

.

+3

Source: https://habr.com/ru/post/1526802/


All Articles