LoginSignup
9
4

More than 5 years have passed since last update.

D3.jsで矩形にクリックイベント

Posted at

自分がハマったのでメモ。
以下は矩形を斜めに描写しクリックイベントを乗っけたコード。

  var rectData = [
  {"x":100,"y":300,"width":150,"height":10,"color":"red"},
  ];
  var rect = svg
     .selectAll("rect")
     .data(rectData)
     .enter()
     .append("rect")
     .on("click", function(d){alert();})
     .attr("x",function(d){return d.x;})
     .attr("y",function(d){return d.y;})
     .attr("width",function(d){return d.width;})
     .attr("height",function(d){return d.height;})
     .style("fill",function(d){return d.color;})
     .attr("transform", "rotate(-15)")
}

.on("click", function(d){alert();})をattrの後に書くとなぜか動かなかった。

9
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
4