简体   繁体   English

如何使用D3.select选择指令的父项?

[英]How to select parent of directive with D3.select?

I'm trying to get the height of the parent of a directive, the idea is to automatically adapt the size of the chart to it's parent html container. 我正在尝试获取指令父级的高度,其想法是使图表的大小自动适应其父级html容器。 Feels like this problem has been solved a million times and yet I have no clue why my code doesn't work - I assume my mistake is related to Angular.js though, not D3. 感觉这个问题已经解决了上百万次,但是我不知道为什么我的代码无法正常工作-我认为我的错误与Angular.js有关,但与D3无关。

Adapted the very good tutorial at http://www.ng-newsletter.com/posts/d3-on-angular.html , here is my code: http://codepen.io/anon/pen/QjNodL 改编了很好的教程, 网址http://www.ng-newsletter.com/posts/d3-on-angular.html ,这是我的代码: http : //codepen.io/anon/pen/QjNodL

I watch for changes of the window-size and execute this function: 我观察窗口大小的变化并执行以下功能:

scope.render = function(data) {
    var svg = parent.append('svg');

    renderTimeout = $timeout(function() {
          var width = d3.select(ele[0])[0][0].offsetWidth - margin,
              height = d3.select(ele[0]).node().getBoundingClientRect().height;
              svg.attr('height', height);

This is the HTML: 这是HTML:

<div class="col-md-12" style="height: 100px;">
    <d3-bars bar-height="20" bar-padding="5" data="ragChartData" on-click="onClick(item)" style="height: 100px;">
    </d3-bars>
</div>

Unfortunately 不幸

d3.select(ele[0])

selects the directive 选择指令

<d3-bars></d3-bars>

but not it's parent, and I have no clue how to select that. 但它不是父母,我不知道如何选择它。

您可以尝试使用以下选择函数: d3.select(ele[0].parentNode)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM