1

I'm trying to make a D3js chart responsive and can't manage to get it to work. Was wondering you guys could help.

The website is http://www.vizualytic.com/

The d3js chart is on the home page

Jimmyn
  • 531
  • 1
  • 5
  • 27

1 Answers1

3

Set width=100% and height=100% and viewBox and preserveAspectRatio attributes for your SVG elements.

Following solution will give you direction,

var svg = d3.select('#chart').append("svg")
    .attr("width", '100%')
    .attr("height", '100%')
    .attr('viewBox','0 0 '+Math.min(width,height)+' '+Math.min(width,height))
    .attr('preserveAspectRatio','xMinYMin')
    .append("g") ;
Somnath Kadam
  • 6,051
  • 6
  • 21
  • 37
  • I have tried this method and it would seem to be working very well with not much effort! Are there any side effects on the rest of your chart of does 'preserveAspectRatio' do all the work? And what about the width and the height variables? What should they refer to? Or can i pick just random numbers? – ocket-san Aug 14 '15 at 12:28