第一步:安装:
npm install echarts --save
第二步: 引入Echarts
import * as echarts from 'echarts'
第三步:初始化Echarts 对象,并且设置配置进行绘制
- 通过echarts.init(dom,theme,options) 初始化;
- 通过setOption 方法设置绘制的数据
<template>
<div class="dashboard">
<h2>dashboard</h2>
<div ref="diyRef" :style="{width:'700px',height:'500px'}"></div>
</div>
</template>
<script lang="ts">
import { defineComponent,ref,onMounted} from 'vue'
import * as echarts from 'echarts'
export default defineComponent({
name: 'dashboard',
setup() {
const diyRef=ref<HTMLElement>()
//1.初始化
onMounted(()=>{
const myEchart=echarts.init(diyRef.value!)
// 2.编写配置化文件
var option = {
title: {
text: 'ECharts 入门示例'
},
tooltip: {},
legend: {
data: ['销量']
},
xAxis: {
data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
},
yAxis: {},
series: [
{
name: '销量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}
]
};
// 3.设置配置,并且开始绘制
myEchart.setOption(option);
})
return {
diyRef
}
}
})
</script>