Vue-chartjs: Responsive chart not working when resizing

Created on 22 May 2018  ยท  2Comments  ยท  Source: apertureless/vue-chartjs

Expected Behavior

Chart resizes correctly everytime.

Actual Behavior

At first, chart adapts correctly, but when window is maximized (navigation drawer is displayed), chart does not resize correctly. If I resize again to a smaller window (navigation drawer hides), chart does not resize, leaving the empty space of the navigation drawer.

Before resize
After resize
Another resize

Here is my code:

LineChart.vue:

<script>
  import {Line, mixins} from 'vue-chartjs'
  const { reactiveProp } = mixins
  export default {
    extends: Line,
    mixins: [reactiveProp],
    name: 'line-chart',
    props: ['options'],
    mounted () {
      this.renderChart({
        labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
        datasets: [
          {
            label: 'Data One',
            backgroundColor: '#f87979',
            data: [40, 39, 10, 40, 39, 80, 40]
          }
        ]
      }, {responsive: true, maintainAspectRatio: false})
    }
  }
</script>

Visualizer.vue

<template>
  <v-flex xs12 md12>
    <line-chart :chart-data="lineChartData" :options="options"></line-chart>
  </v-flex>
</template>
<script>
  import LineChart from './LineChart'
  export default {
    name: 'visualizer',
    components: {
      LineChart,
      'line-chart': LineChart
    },
    data () {
      return {
        options: {
          scales: {
            yAxes: [{
              ticks: {
                beginAtZero: true
              },
              gridLines: {
                display: true
              }
            }],
            xAxes: [{
              gridLines: {
                display: false
              }
            }]
          },
          legend: {
            display: false
          },
          responsive: true,
          maintainAspectRatio: false
        }
      }
    }
  }
</script>

App.vue

<template>
  <v-app>
    <v-navigation-drawer
      persistent
      :mini-variant="miniVariant"
      :clipped="clipped"
      v-model="drawer"
      enable-resize-watcher
      app
    >
      <v-list>
        <v-list-tile
          value="true"
          v-for="(item, i) in items"
          :key="i"
          href="javascript:;"
        >
          <v-list-tile-action>
            <v-icon v-html="item.icon"></v-icon>
          </v-list-tile-action>
          <v-list-tile-content @click.stop="changeUrl(item.url)">
            <v-list-tile-title v-text="item.title"></v-list-tile-title>
          </v-list-tile-content>
        </v-list-tile>
      </v-list>
    </v-navigation-drawer>
    <v-toolbar
      app
      :clipped-left="clipped"
      class="primary"
      dark
    >
      <v-toolbar-side-icon @click.stop="drawer = !drawer"></v-toolbar-side-icon>

      <v-toolbar-title v-text="title"></v-toolbar-title>
      <v-spacer></v-spacer>
    </v-toolbar>
    <v-content>
      <router-view/>
    </v-content>

    <v-footer :fixed="fixed" app>
      <span>&copy; 2017</span>
    </v-footer>
  </v-app>
</template>
<script>
  import {mapMutations} from 'vuex'
  export default {
    data () {
      return {
        clipped: false,
        drawer: true,
        fixed: false,
        items: [{
          icon: 'bubble_chart',
          title: 'Serie 1',
          url: 'http://'
        }, {
          icon: 'bubble_chart',
          title: 'Serie2',
          url: 'http://'
        }],
        miniVariant: false,
        right: true,
        rightDrawer: false,
        title: 'data'
      }
    },
    name: 'App',
    methods: {...mapMutations(['changeUrl'])}
}
</script>

Environment

  • vue.js version: 2.5.16
  • vue-chart.js version: 3.3.1
  • npm version: 6.0.1
โ“ question

All 2 comments

+1

Hey @alc32 & @luis-kaufmann-silva

This is mostly related to your css. Check out the docs about responsive charts: http://www.chartjs.org/docs/latest/general/responsive.html

Here is also a codepen with vue-grid so you see that it properly resizes.
https://codepen.io/apertureless/pen/jZOBeV?editors=1010

Was this page helpful?
0 / 5 - 0 ratings

Related issues

timster picture timster  ยท  5Comments

kurbar picture kurbar  ยท  4Comments

Tirke picture Tirke  ยท  4Comments

Scalpel78 picture Scalpel78  ยท  4Comments

rzb picture rzb  ยท  4Comments