最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - D3: Select SVG attribute and change it - Stack Overflow

programmeradmin4浏览0评论

if I have an SVG...

var canvasBars = d3.select("#chart1").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)    
.append("g")
.attr("transform", "translate("+margin.left+","+margin.top+")");

If I modify the width variable (used to calculate the width attribute) How do I then select the width attribute and modify it with this new width variable?

this does not seem to work:

canvasBars.attr("width", width + margin.left + margin.right);

if I have an SVG...

var canvasBars = d3.select("#chart1").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)    
.append("g")
.attr("transform", "translate("+margin.left+","+margin.top+")");

If I modify the width variable (used to calculate the width attribute) How do I then select the width attribute and modify it with this new width variable?

this does not seem to work:

canvasBars.attr("width", width + margin.left + margin.right);
Share Improve this question asked May 12, 2014 at 12:12 NewToJSNewToJS 2,1114 gold badges38 silver badges67 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

Your other option is to ensure the former 'canvasBars' variable is actually the canvas.

var svgSelection = d3.select("#chart1").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)

var baseGroup = svgSelection.append("g")
.attr("transform", "translate("+margin.left+","+margin.top+")");

Your former 'canvasBars' I have renamed to 'svgSelection'. If you want to modify the "canvas" directly, your former idea will now work.

svgSelection.attr("width", width + margin.left + margin.right);
发布评论

评论列表(0)

  1. 暂无评论