How do I use angularjs directives in generated d3 html?

后端 未结 5 1882
北荒
北荒 2020-12-05 00:26

I\'m trying to use the angularjs tooltip directive on my d3 visualisation, so I have something like

var node = svg.selectAll(\".node\")
    .data(nodes)
            


        
5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-05 01:02

    I like this method much better since you don't have to call removeAttr (seems like a hack)

    myApp.directive('myNodes', ['$compile', function ($compile) {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            var nodes = [{"name": "foo"}, {"name": "bar"}] 
            var mySvg = d3.select(element[0])
                  .append("svg")
                  .attr("width", 100)
                  .attr("height", 100);
    
            var node = mySvg.selectAll(".node")
             .data(nodes)
             .enter()
             .append("circle")
             .attr("cx", function(d,i){
                return 20+i*50;
             })
             .attr("cy", 50)
             .attr("r", 10)
             .attr("tooltip-append-to-body", true)
             .attr("tooltip", function(d){
                 return d.name;
             });
    
            $compile(svg[0])(scope);
            }
        };
    }]);
    

提交回复
热议问题