Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to update options? #195

Closed
dinogit opened this issue Sep 13, 2017 · 11 comments
Closed

Is there a way to update options? #195

dinogit opened this issue Sep 13, 2017 · 11 comments

Comments

@dinogit
Copy link

dinogit commented Sep 13, 2017

Hi,

I have a form with inputs and when that form is filed and submited, I woulf like to change main title and title for axis.

This is component:
<bar-chart :data="barData" :options="barOptions" :height="400"></bar-chart>
import { Line, mixins } from "vue-chartjs";
export default Line.extend({
props: ["options", "chartData"],
mixins: [mixins.reactiveProp],
mounted() {
this.renderChart(this.chartData, this.options);
}
});

and these are the options:
barOptions: {
title: {
display: true,
text: 'Change this thing',
fontSize: 16,
padding: 24
}
}

I am wondering is there a way to change options on event or any other way after rendering of chart?

I am using importing component so I thought that I can call render function with
LineChart.options.methods.renderChart(this.barData, this.barOptions)
but this give me an error 'canvas is undefined'

Environment

  • vue.js version: 2.3.3
  • vue-chart.js version: 2.8.6
  • npm version: 5.3.0
@apertureless
Copy link
Owner

Simply add a watcher to your chart component. So everytime you change the options it will re-render the chart.

import { Line, mixins } from "vue-chartjs";
export default Line.extend({
  props: ["options", "chartData"],
  mixins: [mixins.reactiveProp],
  mounted() {
    this.renderChart(this.chartData, this.options);
   },
   watch: {
     options () {
        this.renderChart(this.chartData, this.options);
     }
   }
});

Another way, would be over an EventBus or vuex . There are many possibilities :)

@dinogit
Copy link
Author

dinogit commented Sep 14, 2017

Thank you @apertureless, work like a charm 👍

@dinogit dinogit closed this as completed Sep 14, 2017
@huangche007
Copy link

I also met this problem,Thank you @apertureless give the way to solve my problem

@outluch
Copy link

outluch commented Oct 6, 2017

When I do this, every time I update options, it rerenders, but increases hight of chart.. Will dig

@dinogit
Copy link
Author

dinogit commented Oct 6, 2017

I have set

.chartjs-render-monitor {
    height: 400px !important;
}

it is not a proper solution, but it's working

@outluch
Copy link

outluch commented Oct 6, 2017

just found this: #106.

@zanechua
Copy link

Probably the most correct way would be to do the following

    watch: {
        options: function() {
            this.$data._chart.options = this.options;
            this.$data._chart.update();
        },
    },

@apertureless
Copy link
Owner

Hm, I am not sure if this will work.
At least how I understand the update() method it it should be called after changing datasets. There is no mention of options.

https://www.chartjs.org/docs/latest/developers/api.html#updateconfig

Triggers an update of the chart. This can be safely called after updating the data object. This will update all scales, legends, and then re-render the chart.

But, as it re-renders the chart, it may work for options, too.

@zanechua
Copy link

zanechua commented Jul 31, 2019

@apertureless
Copy link
Owner

Ah nice!

@costea93
Copy link

The only way which works fine for me it is next:

chart.destroy();

and than:

// initialize a new chart again with new options and data

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants