score:4

Accepted answer

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);

Related Query