Using Echarts with Uniapp: Tutorial
In uni-app, you can use echarts by following these steps:
- To install ECharts using npm, open your terminal and run the following command:
npm install echarts
- Import echarts in the vue file of the page where you need to use echarts.
import * as echarts from 'echarts';
- When the page is loaded
onLoad() {
this.chart = echarts.init(this.$refs.chartRef); // chartRef为echarts容器的ref属性
// 初始化配置项
let option = {
// echarts配置项
};
this.chart.setOption(option);
},
- prepared
onReady() {
// 调整echarts的大小
this.chart.resize();
},
- when the page unloads
onUnload() {
this.chart.dispose();
},
- Create a container in the template for displaying Echarts charts.
<view ref="chartRef" class="chart-container"></view>
- Set the size of the container in the style.
.chart-container {
width: 100%;
height: 300rpx; // 设置合适的高度
}
The above are the basic steps for using Echarts in uni-app. For specific Echarts configurations and usage, you can refer to the official Echarts documentation.