vue2使用echarts
时间: 2023-09-05 21:13:52 浏览: 82
要在Vue 2中使用ECharts,您需要按照以下步骤进行操作:
1. 在Vue项目中安装ECharts
```
npm install echarts --save
```
2. 在Vue组件中引入ECharts
```
import echarts from 'echarts'
```
3. 在Vue组件的mounted钩子函数中初始化ECharts实例并设置其配置项
```
mounted() {
// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(document.getElementById('myChart'));
// 指定图表的配置项和数据
var option = {
title: {
text: 'ECharts 入门示例'
},
tooltip: {},
legend: {
data:['销量']
},
xAxis: {
data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
},
yAxis: {},
series: [{
name: '销量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
}
```
4. 在Vue组件的template中添加ECharts的DOM元素
```
<template>
<div id="myChart" style="width: 600px;height:400px;"></div>
</template>
```
这样就可以在Vue 2中使用ECharts了。当然,您可以根据自己的需要进行配置和优化。
阅读全文
相关推荐


















