【问题标题】:creating a enlarged duplicate of the d3.js element onclick event创建 d3.js 元素 onclick 事件的放大副本
【发布时间】:2013-09-19 16:46:01
【问题描述】:

这是代码:http://jsfiddle.net/nikunj2512/74qrC/18/

我正在使用d3.js

我想要做的是当用户点击任何矩形时,矩形的精确副本但尺寸更大(比原始矩形大两倍)将显示在它们下方。

我添加了 onclick 方法,然后通过获取旧矩形的属性创建了更大的矩形。但它不起作用。请帮帮我

我不知道该怎么做...请帮帮我...

var numVisible = 4;
var rectWidth = 100,
rectHeight = 100,
rectPadding = 2,
numRects = 10;

var clipWidth = numVisible * rectWidth + (numVisible - 1) * rectPadding,
clipHeight = rectHeight;

var width = (rectWidth * numRects) + ((numRects - 1) * rectPadding),
height = rectHeight;

var data = d3.range(numRects);
var xScale = d3.scale.linear()
    .domain(d3.extent(data))
    .range([0, width - rectWidth]);

var svg = d3.select('#chart').append('svg')
    .attr('width', clipWidth)
    .attr('height', clipHeight);
var bigRectContainer = d3.select('#bigRectContainer').append('svg')
    .attr('width', clipWidth*2)
    .attr('height', clipHeight*2);

svg.append("defs").append("clipPath")
.attr("id", "clip")
.append("rect")
    .attr("width", clipWidth)
    .attr("height", clipHeight);

var g = svg.append("g");
g.selectAll("rect")
.data(data)
.enter()
.append('rect')
    .attr("class", "area").attr("clip-path", "url(#clip)")
    .attr('x', xScale)
    .attr('width', rectWidth)
    .attr('height', rectHeight)
    .style('fill', d3.scale.category20());

var update = function(){
g.selectAll("rect")
    .transition().duration(500)
    .attr('x', xScale);
};

d3.select("#left").on("click", function(){ 
if(xScale.domain()[0] - 1 > -numVisible){
    xScale.domain([xScale.domain()[0] - 1, xScale.domain()[1] - 1]);
    update();
}else if(xScale.domain()[0] - 1 == -numVisible)
{
    xScale.domain([xScale.domain()[0] + numRects, xScale.domain()[1] + numRects]);
    update();
}
});

d3.select("#right").on("click", function(){ 
if(xScale.domain()[0] + 1 < numRects){
    xScale.domain([xScale.domain()[0] + 1, xScale.domain()[1] + 1]);
    update();
}else if(xScale.domain()[0] + 1 == numRects)
{
    xScale.domain([xScale.domain()[0] - numRects, xScale.domain()[1] - numRects]);
    update();
}
});


svg.selectAll("rect").on("click", function() {
    var littleRect = d3.select(this);
console.log(littleRect)

var bigRect = bigRectContainer.append("rect").attr("width", littleRect.attr("width") * 2)
    .attr("height", littleRect.attr("height") * 2)
});

请帮帮我...

【问题讨论】:

  • 你想让所有矩形一次变大?
  • 不,当我点击某个矩形时,我想创建一个矩形的副本,但尺寸更大。

标签: javascript html css d3.js


【解决方案1】:

你可以使用 selection.on 在你的矩形上注册一个点击事件监听器。单击矩形时将调用它,以便this 将引用单击的节点。您可以使用它来创建一个大小为两倍的新矩形。

g.selectAll("rects").on("click", function() {
    var littleRect = d3.select(this);

    bigRectContainer.select("rect").remove();
    bigRectContainer.append("rect")
        .attr("class", "bigrect")
        .attr("width", littleRect.attr("width") * 2)
        .attr("height", littleRect.attr("height") * 2)
        .style("fill", litteRect.attr("fill"));
});

【讨论】:

  • 谢谢,但您的代码不起作用。当我在我的代码中复制和粘贴您的代码时,所有矩形都消失了......请在我的 jsfiddle 代码中编辑......谢谢
  • 看起来我错过了一个右括号。我现在已经添加了。话虽如此,这段代码只是为了让您了解它应该如何工作。我没有以任何方式测试它。因此,您可能需要自己进行修饰。您还会注意到我引用了一个名为“bigRectContainer”的选择,我假设您在代码中的其他位置设置了它以引用您想要添加较大矩形的容器。
  • 仍然无法正常工作..当我点击它时不会触发点击事件....请您在 Jsfiddle 中进行更改...
【解决方案2】:

我明白了..您的代码中有一些错误,但我已修复...

var bigRectContainer = d3.select('#bigRectContainer').append('svg')
    .attr('width', clipWidth*2)
    .attr('height', clipHeight*2);
svg.selectAll("rect").on("click", function() {
var littleRect = d3.select(this);
console.log(littleRect)

var bigRect = bigRectContainer.append("rect").attr("width", littleRect.attr("width") * 2)
    .attr("height", littleRect.attr("height") * 2).style('fill', d3.scale.category20())
});

但是谢谢...

【讨论】:

  • 当我看到这个时,我正打算再看一下小提琴,所以我很高兴你弄明白了。小心你的“填充”行,因为你在每次调用时都构建了一个新的色阶,所以我认为你每次都会得到相同的颜色。此外,您不会在任何地方删除任何以前添加的矩形,因此每次单击时都会不断添加新的矩形。旧的可能隐藏在新的后面。
猜你喜欢
  • 1970-01-01
  • 2016-10-04
  • 2020-11-09
  • 1970-01-01
  • 2014-01-28
  • 2023-03-06
  • 2014-02-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多