vue2 中使用Highcharts
stackoverflow 上的有答案 http://stackoverflow.com/questions/38777136/highcharts-in-vue-js-component
因为画布要刷新,涉及到数据销毁, 需要执行 this.target.destroy();
。
需要注意的是,当数据处于隐藏状态时不能触发 beforeDestroy
, 即 <div v-show="isShow"></div>
刷新数据需要销毁之前的画布数据, 利用 v-if
进行动态销毁、新增。
<script>
var Highcharts = require('highcharts');
export default {
name : "Chart",
props : {
series : {
type: Array,
required: true
}
},
data : function() {
return {
target: undefined
}
},
mounted : function() {
this.target = Highcharts.chart(this.$el, {
title: {
text: 'Monthly Average Temperature',
x: -20 //center
},
subtitle: {
text: 'Source: WorldClimate.com',
x: -20
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
yAxis: {
title: {
text: 'Temperature (°C)'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
valueSuffix: '°C'
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
borderWidth: 0
},
series: this.series
});
},
beforeDestroy: function() {
this.target.destroy();
},
}
</script>