d3 fisheye(d)返回未定义的对象

时间:2013-04-22 14:09:23

标签: d3.js

所以我正在使用d3鱼眼插件,我有一些非常基本的问题。 我实现了这个非常基本的代码,几乎是来自这里的直接副本https://github.com/d3/d3-plugins/tree/master/fisheye

    fisheye = d3.fisheye.circular()
        .radius(200)
        .distortion(2);

    //initialize fisheye
    chart.on("mousemove", function() {
        fisheye.focus(d3.mouse(this));

        dataPoint.each(function(d) { d.fisheye = fisheye(d); })
            .attr('y', function(d){ return d.fisheye.y; })
            .attr('x', function(d){ return d.fisheye.x; });
    });

d.fisheye.xd.fisheye.y未定义。事实上,看fisheye(d),它会返回:

  {x: undefined, y: undefined, z: 1}

另一方面,d3.mouse(this)正确返回一个数组。

是否有人建议可能发生这种情况的原因?

更多代码:顺便说一下,代码是这样的,因为它在ext-js面板中,所以每个函数(drawWords都是这个对象的属性)。这有点复杂,这就是为什么我犹豫要发布这一切,这仍然不是所有的代码,而是我认为的相关部分。我没有包含任何其他全局变量或辅助函数的初始化。

//imagine some sort of onload function
    onLoad: function () {
         this.drawWords();
         this.animateVis();
    }

,drawWords: function () {
    toolObject = this;
    var h = this.body.getHeight(),
        w = this.body.getWidth();

    //initialize word text
    this.dataPoint = this.chart.selectAll('text')
        .data(toolObject.termometerData, function (d) {return d.word;})
      .enter().append('text')
        .attr('class', 'points')
        .attr('id', function(d) {return d.word + '-point';})
        .attr('x', function() {
            return toolObject.xScale(toolObject.shiftCount);
        })
        .attr('y', function (d) {
            return toolObject.fanVertical(d, toolObject.shiftCount);
        })
        .attr('transform', function (d) {
            var thisXPosition = toolObject.xScale(toolObject.shiftCount),
                thisYPosition = toolObject.fanVertical(d, toolObject.shiftCount);
            return 'translate(0, 0) rotate(-20 ' + thisXPosition + ' ' + thisYPosition + ')';
        })
        .attr('text-anchor', 'middle')
        .attr('fill-opacity', function (d) {return toolObject.opacityScale(0);})
        .text(function(d) {return d.word;});

    this.applyFisheye();
 }

 ,fisheye: d3.fisheye.circular()
        .radius(200)
        .distortion(2)

 ,applyFisheye: function () {
    var toolObject = this;

    //initialize fisheye
    this.chart.on("mousemove", function() {

        fisheye.focus(d3.mouse(this));

        toolObject.dataPoint.each(function(d) { d.fisheye = toolObject.fisheye(d); })
            .attr('y', function(d){ return d.fisheye.y; })
            .attr('x', function(d){ return d.fisheye.x; })
            .attr('transform', function(d){
                return 'translate(0, 0) rotate(-20 ' + d.fisheye.x + ' '+ d.fisheye.y + ')';
            });
    });
 }
 ,animateVis: function () {
    var toolObject = this;
    var h = this.body.getHeight(),
        w = this.body.getWidth();

    var tick;

    if(this.animationIdArray.length < 1){
        tick = setInterval(function(){
            animate();
        }, this.duration);
        this.animationIdArray.push(tick);
    }

    function animate() {
        if(toolObject.shiftCount < toolObject.numDataPoints){
            toolObject.shiftCount++;

            //animate words
            toolObject.dataPoint.transition()
                .duration(toolObject.duration)
                .ease('linear')
                .attr('x', function(d){ return toolObject.xScale(toolObject.shiftCount - 1); })
                .attr('y', function(d){ return toolObject.fanVertical(d, toolObject.shiftCount - 1); })
                .attr('transform', function(d){
                    var thisXPosition = toolObject.xScale(toolObject.shiftCount - 1),
                        thisYPosition = toolObject.fanVertical(d, toolObject.shiftCount - 1);
                    return 'translate(0, 0) rotate(-20 ' + thisXPosition + ' '+ thisYPosition + ')';
                })
                .attr('fill-opacity', function(d){
                    return toolObject.opacityScale(d.series[toolObject.shiftCount - 1].freq);
                });

            toolObject.applyFisheye();
        }else{
            clearInterval(tick);
            toolObject.animationIdArray.shift();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

fisheye假设对象的x和y坐标由名为“x”和“y”的键定义。使用

可能就足够了(但可能有些过分,取决于调用此代码的频率)
this.dataPoint
    .each(function(d) { 
        d.x = toolObject.xScale(toolObject.shiftCount);
        d.y = toolObject.fanVertical(d, toolObject.shiftCount)
    .attr('x', function(d) { return d.x; })
    .attr('y', function(d) { return d.y; });

当你//initialize word text