Home >Web Front-end >JS Tutorial >Implement Echarts in Vue to change with the form
<p id="myChart" :style="{width: '100%', height: '345px'}"></p> <script> export default { mounted(){ this.drawLine(); }, methods: { drawLine(){ var myChartContainer = document.getElementById('myChart'); //用于使chart自适应宽度,通过窗体宽计算容器高宽 var resizeMyChartContainer = function(){ myChartContainer.style.width=(document.body.clientWidth-75)+'px' } //设置容器高宽 resizeMyChartContainer() // 基于准备好的dom,初始化echarts实例 var myChart = this.$echarts.init(myChartContainer) // 绘制图表 myChart.setOption({ title: { text: '启动次数' }, tooltip: {}, xAxis: { type: 'category', data: ['2019-02-15', '2019-02-16', '2019-02-17', '2019-02-18', '2019-02-19', '2019-02-20', '2019-02-21'] }, yAxis: { type:'value' }, series: [{ type: 'line', data: [0,0, 0, 7, 0, 0,12], smooth:true, symbol: 'circle', symbolSize: 6, itemStyle:{ normal:{ color:'#fc8a0f', lineStyle:{ color:'#ff9c35' } } } }], }); window.onresize=function(){ resizeMyChartContainer(); myChart.resize(); } } }}</script>
Supplementary knowledge: echarts chart size changes adaptively as the window changes (No need to refresh the browser to adjust)
Question:
After using echars to make charts, you need to make adaptive effects according to the zoom of the browser window.
Cause analysis and solution:
The icon instance of echars does not actually actively bind the resize() event , which means that the size change of the display area is not known internally. When you need to do some adaptive effects, you need to actively bind this time to achieve the effect of self-use. Common ones are window.onresize = myChart.resize( )
Example:
var map5 = echarts.init(document.getElementById('map5')); var option5 = { backgroundColor: '#def1f9', color: ['#c23531', '#1875ff'], title: { left: 10, top: 10, text: 'Bill charts for the past year' }, // color: ['#1875ff', '#1fe6ab', '#eee119', '#ff3074', '#6f99d9'], legend: { top: 30, right: 30 }, tooltip: {}, xAxis: {type: 'category'}, yAxis: {}, series: [ {type: 'bar'}, {type: 'bar'} ] } map5.setOption(option5); window.onresize = function () { setTimeout(function () { map1.resize() map2.resize() map3.resize() map4.resize() map5.resize() },10) }
Key points:
window.onresize = function () { map1.resize() ; // 如果有多个图标变动,可写多个 }
Recommended tutorial:《 JS tutorial》
The above is the detailed content of Implement Echarts in Vue to change with the form. For more information, please follow other related articles on the PHP Chinese website!