【问题标题】:How to bind an angularjs directive to a d3js node defined in a link function如何将 angularjs 指令绑定到链接函数中定义的 d3js 节点
【发布时间】:2014-02-13 19:15:13
【问题描述】:

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

首先我把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>

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

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

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

【问题讨论】:

    标签: javascript angularjs d3.js angularjs-directive


    【解决方案1】:

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

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

    这是fiddle of a working example

    【讨论】:

    • 非常棒,非常感谢。然而,工具提示仍然没有显示,但这是一个显示问题,因为我可以看到它出现在 html 代码中。
    • 没问题!如果您仍然无法显示工具提示,请发布 fiddle 或 plunker,我们也可以提供帮助。
    猜你喜欢
    • 1970-01-01
    • 2015-02-08
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多