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 Width, Fixed Height

See original GitHub issue

Expected Behavior

I would like to render a chart width a 100% width, but a fixed height (e.g. 500px). I would also like the chart to resize according to those constraints (without maintaining aspect ratio).

Actual Behavior

Currently, I’m having trouble working with the responsive/maintainAspectRatio options. Regardless of my styling & options, the height seems to grow beyond it’s containing elements (I believe it’s using a 1:1 aspect ratio).

I can easily get something working when I feed the chart numeric width/height as a landscape ratio w:h (e.g. <line-chart :chart-data="chartData" :width="5" :height="1" />). Since the prop validation expects a number, I’m wondering if my desired behavior is possible with the current api.

image image

Environment

  • vue.js version: 2.4.2
  • vue-chart.js version: 2.8.2
  • npm version: 5.3.0

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

19reactions
alexsasharegancommented, Aug 24, 2017

Fixed

Switching my component definition to the following solved my problem. This makes me think that somewhere in the chain, my maintainAspectRatio setting was getting merged with the default true in a way that merged the truthy value. Maybe, maybe not. But this works nicely now, so feel free to close this or keep it open for @wannymiarelli .

<script>
import { Line, mixins } from "vue-chartjs"
import { plainObj } from "@/core/object"

export default Line.extend({
  mixins: [mixins.reactiveProp],
  props: {
    options: {
      type: Object,
      default: plainObj
    }
  },
  mounted() {
    this.renderChart(this.chartData, this.options)
  },
  data() {
    return {
      defaultOptions: {
        responsive: true, // my new default options
        maintainAspectRatio: false, // my new default options
        scales: {
          yAxes: [{
            ticks: {
              beginAtZero: true
            },
            gridLines: {
              display: true // my new default options
            }
          }],
          xAxes: [{
            gridLines: {
              display: false
            }
          }]
        }
      },
      plugins: []
    }
  },
})
</script>
6reactions
aperturelesscommented, Aug 24, 2017

Do you have maintainAspectRatio set to true or false?

Can you maybe post a minimal codepen for reproduction?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Responsive images with fixed height CSS - Stack Overflow
You can set width and height using VW (view width), this means that your image will have width and height based in the...
Read more >
Responsive Height Design - Ahmad Shadeed
Set a fixed size (width and height) to the illustration instead of the width only. The absence of height will keep the problem....
Read more >
Setting Height And Width On Images Is Important Again
To prevent layout shifts and improve performance scores, we need to always set width and height attributes on our images.
Read more >
Fixed Width Design - GeeksforGeeks
Approach: Fixed-width is generally used when you don't want the width of your content to change according to the changing width of the...
Read more >
Turning a Fixed-Size Object into a Responsive Element
I set out to modify the library to make the devices responsive. Here is the final resizable library. Below, we'll walk through the...
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