Responsive charts based on container size
See original GitHub issueThis is a Vega 3 feature request: Provide a way to resize the graph based on the container’s height/width.
With a combination of the window resize event and windowSize
expression, there’s a way to resize the graph based on the window’s dimensions but not the container.
I’ve been told a workaround is to update the view instance with the containers height/width on window resize.
Example:
$(window).on('resize', function() {
// assume I have an element and viewInstance variables representing
// the container, and vega View instance, respectively.
const $element = $(element)
const width = $element.width();
const height = $element.height();
viewInstance.width(width).height(height).run();
});
Perhaps there is an easier and more elegant way to do this within Vega?
Issue Analytics
- State:
- Created 7 years ago
- Reactions:16
- Comments:17 (8 by maintainers)
Top Results From Across the Web
Responsive Charts - Chart.js
Chart.js provides a few options to enable responsiveness and control the resize behavior of charts by detecting when the canvas display size ......
Read more >A Fully Responsive Chart.js Chart | by Nora Brown - ITNEXT
Chart.js is a full-featured but easy-to-use Javascript library for creating many types of charts and graphs, which it draws on a canvas element....
Read more >A simple way to make D3.js charts responsive | by Louise Moxy
This post will be a quick way to make any SVG or D3.js chart responsive. ... looking at making the SVG grow/shrink in...
Read more >Chart Container - Concepts - Handbook - Apache ECharts
Specify the chart size If the height and width of the container do not exist, or you wish the chart size not equal...
Read more >Make any chart responsive with one function
While not the only way to create a responsive D3 chart, ... set viewBox attribute to the initial size ... gets the width...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@brylie here is our (definitely imperfect) solution, with width/height listener outside (in
main.js
). in this case, only the width was made responsive purposefully - but it is trivial to add height as well.example
with that being said, indeed It would be great to have this automated one way or another - for example, if vegaEmbed or vega itself would expose
globalWidth
andglobalHeight
signalsIf possible, the charts should be responsive automatically (see: Plot.ly). Wiring up screen resize events to chart dimensions should be abstracted by the library, unless specifically needing to be overridden. Just my two cents.