1.首先引入echarts.js
<script src="https://cdn.jsdelivr.net/npm/echarts@5.4.2/dist/echarts.min.js"></script>
2.为echarts 创建宽高DOM容器
<body>
<div id="main" style="width: 100%;height:400px;"></div>
</body>
3.获取DOM容器id类名
const myChart = echarts.init(document.getElementById('main'));
4.定义option在对象内导入echarts图表代码
const option = {
// 图表代码
title: {
text: 'ECharts 入门示例'
},
tooltip: {},
legend: {
data: ['销量']
},
xAxis: {
data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
},
yAxis: {},
series: [
{
name: '销量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}
]
};
5.通过获取到的DOM容器给 option对象内图标设置状态
myChart.setOption(option);