question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Responsive charts based on container size

See original GitHub issue

This 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:closed
  • Created 7 years ago
  • Reactions:16
  • Comments:17 (8 by maintainers)

github_iconTop GitHub Comments

10reactions
Casyfillcommented, Sep 27, 2018

@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 and globalHeight signals

4reactions
bryliecommented, Sep 27, 2018

If 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found