5

给定以下用例:

我使用 D3js 来渲染由 AngularJS 管理的对象。我想为 D3 图表添加交互性。单击 svg 元素时,我希望有一种允许修改对象属性的弹出菜单。AngularJS 需要这些属性,但 D3 不呈现这些属性。

D3-Angular 集成源自使用闭包的http://bl.ocks.org/biovisualize/5372077 。

当前实施:

截至今天,我正在使用 angular-ui bootstrap 中的 $modal 服务来创建弹出菜单。从功能的角度来看,它工作得很好:当单击 svg 元素时,D3 调度一个事件该事件被调用 $modal 服务的 Angular 捕获 在模态窗口中我修改对象属性

但是我对渲染不满意。我希望弹出菜单看起来像一个弹出框。它应该靠近被点击的 svg 元素。

据我了解,我有两种选择

  1. 继续使用 $modal 服务并修改它的外观。应该采取什么方法?使用 windowClass 选项?
  2. 停止使用 $modal 服务并开始破解 popover 指令。问题是我认为不可能将这样的指令添加到 svg 元素。解决方案是在 $modal 服务附近创建一个弹出服务。

应该选择哪个选项?以及如何实施?

编辑:

使用自定义 my-popover 指令工作 plunker: http ://plnkr.co/edit/5KYvxi?p=preview

4

1 回答 1

9

可以向 生成的代码添加指令d3,唯一需要确保的是$compile在内容呈现后调用服务。

对于给定的示例,它看起来像这样:

    .directive('barChart', function($compile){  // inject $compile
        var chart = d3.custom.barChart();
        return {
            restrict: 'E',
            replace: true,
            template: '<div class="chart"></div>',
            scope:{
                height: '=height',
                data: '=data',
                hovered: '&hovered'
            },
            link: function(scope, element, attrs) {
                var chartEl = d3.select(element[0]);
                chart.on('customHover', function(d, i){
                    scope.hovered({args:d});
                });

                scope.$watch('data', function (newVal, oldVal) {
                    chartEl.datum(newVal).call(chart);
                    $compile(element.contents())(scope);   // <-- call to $compile
                });

                scope.$watch('height', function(d, i){
                    chartEl.call(chart.height(scope.height));
                    $compile(element.contents())(scope); // <-- call to $compile
                })
            }
        }

而在d3的绘图功能中:

       bars.enter().append('rect')
            .classed('bar', true)
            .attr('myPopover', 'Text to show') // <-- Adding an attribute here.
            .attr({x: chartW,
                width: barW,
                y: function(d, i) { return y1(d); },
                height: function(d, i) { return chartH - y1(d); }
            })
            .on('mouseover', dispatch.customHover);

Demo

于 2013-10-29T15:53:27.547 回答