javascript - 如何将 angularjs 指令绑定(bind)到链接函数中定义的 d3js 节点

标签 javascript angularjs d3.js angularjs-directive

我想在 AngularJs 应用程序中使用 d3js 图形,然后将指令绑定(bind)到节点。

首先,我将 js 代码放在指令的链接函数中,一切正常。

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; });
                });
            });
        }
    }
}).

然后我想向节点添加工具提示,所以我开始了:

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

当我使用 angular-bootstrap 时,工具提示是一个指令。 tooltip 属性很好地出现在 html 结果中:

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

但是工具提示是无效的,所以它适用于我这样绑定(bind)的每个指令。

我想这是因为在编译阶段没有考虑该指令,但是当我达到我目前对 AngularJs 的理解极限时,我找不到如何做到这一点。

您知道我如何实现它吗?? 非常感谢您提供非常宝贵的答案。

最佳答案

我已经回答了一个非常相似的question here .你可以在那里看到完整的答案。

基本上,在添加工具提示属性后,您需要使用 element.removeAttr("graphe-forces") 之类的东西删除自定义指令属性,然后运行 ​​$compile(element )(scope) 因此应用程序将找到工具提示指令。

这是一个 fiddle of a working example .

关于javascript - 如何将 angularjs 指令绑定(bind)到链接函数中定义的 d3js 节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21763475/

相关文章:

javascript - Grunt list : Put to a subdirectory the source paths

javascript - Angular 和 FF 中的 $watch 方法

angularjs - angular 1 $scope 和 Controller as,什么时候 $scope 死了?

d3.js - 绘制网格线的正确方法

javascript - 如何使用 Chosen.JS 多重选择以编程方式选择多个选项

javascript - 我们可以使用 javascript 或 jquery 通过循环将属性 'id' 设置为不同的 'buttons' 吗?

javascript - 未捕获( promise )TypeError : response. json 不是函数

html - 当键盘出现 ionic 时隐藏页脚

javascript - D3,将工具提示添加到水平条形图,工具提示不显示

javascript - 向 D3 网络添加节点标签