Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Dushyant Pathak
Dushyant Pathak

Posted on

     

Making Google Charts responsive

I used the npm package for google charts, calledangular-google-charts, to display charts in my Angular app.

Setting the width and height as percentages does not make the graph responsive. Moreover, wrapping the chart element in a div and making that div responsive also doesn't work.

Here's a hack I developed to make it work.

<!--mycomponent.component.html file --><!-- This is the container element of your chart--><div (window:resize) = "resetWindowSize($event)">    <google-chart>    ....    </google-chart></div>
Enter fullscreen modeExit fullscreen mode

This is an event listener that will listen for changes to window size, and on an event capture, will call the resetWindowSize() function.

//mycomponent.component.ts file//setting state variables for chart parameterswidth = document.documentElement.ClientWidth;height = document.documentElement.ClientHeight;....resetWindowSize(event){    //Reset the width and height based on current window size    this.width = event.target.innerWidth;    this.height = event.target.innerHeight;    makeGraph();}
Enter fullscreen modeExit fullscreen mode

Thus, on changing window size, the resetWindowSize function is triggered, and sets the width and height based on the current window size.

Note that you might have to re-call the function that makes the graph. That's the way it works for me.

Cheers! Happy Coding

Top comments(3)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
lvanerstrom profile image
lvanerstrom
  • Joined

Where are you using the height and width? Can you post the entire code?

CollapseExpand
 
lfrz74 profile image
Fernando Rivera
  • Joined

Thx@dkp1903 it works beautifully

CollapseExpand
 
agajpal123 profile image
AGajpal123
  • Joined

Awesome :)

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

JavaScript | Java
  • Location
    India
  • Joined

More fromDushyant Pathak

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp