简体   繁体   English

无法在D3条形图中将this.set()用于'hoveredLabel'

[英]Unable to use this.set() for 'hoveredLabel' in D3 Bar Chart

In my Ember appliation I have a D3 Stacked Bar Chart and I am trying to set a 'hoveredLabel' value for use in my tooltip code. 在我的Ember应用程序中,我有一个D3堆积条形图,并且我试图设置一个“ hoveredLabel”值以在我的工具提示代码中使用。 The hover code is here: 悬停代码在这里:

  .on('mouseover', d => {
        this.set('hoveredLabel', d.data.label);
        this.set('hoveredCount', d.data.count1);
        console.log(d.data.label)
        console.log(hoveredLabel)
      })
      .on('mouseout', () => {
        this.set('hoveredLabel', null);
        this.set('hoveredCount', null);
      })

The console log for 'd.data.label' prints the correct label to the console when I hover over the bar. 当我将鼠标悬停在栏上时,“ d.data.label”的控制台日志会在控制台上打印正确的标签。 The 'hoveredLabel' console log returns an undefined error. “ hoveredLabel”控制台日志返回未定义的错误。 I am using this code in another component and I get the desired result so I'm a bit stuck. 我在另一个组件中使用此代码,但得到了预期的结果,所以有点卡住了。

Full method chain: 完整的方法链:

  svg.append("g")
    .selectAll("g")
    .data(series)
    .enter().append("g")
      .attr("fill", function(d) { return z(d.key); })
    .selectAll("rect")
    .data(function(d) { return d; })
    .enter().append("rect")
      .attr("width", x.bandwidth)
      .attr("x", function(d) { return x(d.data.label); })
      .attr("y", function(d) { return y(d[1]); })
      .attr("height", function(d) { return y(d[0]) - y(d[1]); })
      .on('start', (data, index) => {
        if (index === 0) {
          (function updateTether() {
            Tether.position()
            rafId = requestAnimationFrame(updateTether);
          })();
        }
      })
      .on('end interrupt', (data, index) => {
        if (index === 0) {
          cancelAnimationFrame(rafId);
        }
      })
      .attr('opacity', d => {
        let selected = this.get('selectedLabel');

        return (selected && d.data.label !== selected) ? '0.5' : '1.0';
      })
      .on('mouseover', d => {
        this.set('hoveredLabel', d.data.label);
        this.set('hoveredCount', d.data.count1);
        console.log(d.data.label)
        console.log(hoveredLabel)
      })
      .on('mouseout', () => {
        this.set('hoveredLabel', null);
        this.set('hoveredCount', null);
      })
      .on('click', d => {
        let clickedLabel = d.data.label;
        let clickedCount = d.data.count1;
        // console.log(clickedLabel, clickedCount)

        if (this.get('on-click')) {
          this.get('on-click')(clickedLabel);
        } else {
          if (clickedLabel === this.get('selectedLabel')) {
            this.set('selectedLabel', '');
            this.set('selectedCount', '');
          } else {
            this.set('selectedLabel', clickedLabel);
            this.set('selectedCount', clickedCount);
          }

          this.buildChart()
        }
      });

If you are setting a property 'hoveredLabel' for a ember component , You should get them with 'get'. 如果要为余烬组件设置属性“ hoveredLabel”,则应使用“ get”获取它们。 I assume that 'this' is your ember component ? 我假设“这”是您的余烬组件?

Try 尝试

this.get('hoveredLabel')

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

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