Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Danielle Hill
Danielle Hill
26,062 Points

Responsive d3.js

Can anyone offer insight on how to make my d3.js svg bar graph responsive? I'm working on a project and can't get the svg to resize.

3 Answers

Zoltán Holik
Zoltán Holik
3,401 Points
    d3.select(window).on('resize', resize);

   function resize(){
    // need to update all the svg chart properties
   }

And try to do not use fixed width and height values for the SVG.

http://eyeseast.github.io/visible-data/2013/08/28/responsive-charts-with-d3/

Danielle Hill
Danielle Hill
26,062 Points

Thanks for the insight Zoltan!

Fidel Torres
Fidel Torres
25,286 Points

I have added svg width: 100% height:100%

and a css class to the svg element in my case rect with this on them

transform: scale(0.125); // this one for mobiles to follow Mobile first @media only screen and (target tablet/desktop){ transform: scale(0.5);//bigger number that matches your screen