Accepted answer

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

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

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

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