Skip to content

Instantly share code, notes, and snippets.

@JavaCS3
Created February 25, 2017 07:01
Show Gist options
  • Save JavaCS3/bb18c69a76dc8d59da8a144699c038ab to your computer and use it in GitHub Desktop.
Save JavaCS3/bb18c69a76dc8d59da8a144699c038ab to your computer and use it in GitHub Desktop.
Vue.component('vue-chart', {
template: '<canvas></canvas>',
props: {
type: {
type: String,
default: 'line'
},
data: {
type: Object,
default: function _default() {
return {
labels: [],
datasets: []
};
}
},
options: {
type: Object
}
},
data: function data() {
return {
chart: null
};
},
mounted: function mounted() {
var _this = this;
this.chart = new Chart(this.$el, {
type: this.type,
data: this.data,
options: this.options
});
this.$watch('data', function () {
if (_this.data.datasets && _this.data.datasets.length > 0) {
_this.data.datasets.map(function (dataset, i) {
if (i === _this.chart.config.data.datasets.length) return;
_this.data.datasets[i] = _.assign(_this.chart.config.data.datasets[i], dataset);
});
}
_this.chart.config.data = _.assign(_this.chart.config.data, _this.data);
_this.$nextTick(function () {
_this.chart.update();
});
});
this.$watch('options', function () {
_this.chart.config.options = _.assign(_this.chart.config.options, _this.options);
_this.$nextTick(function () {
_this.chart.update();
});
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment