1 下载 echarts4taro3
javascript
yarn add echarts4taro3 或
pnpm add echarts4taro3 或
npm i echarts4taro3 --save
2 图表初始化需要先加载echarts模块
javascript
import * as echarts from "echarts4taro3/lib/assets/echarts"; // 这里用了内置的,也可以用自定义的 echarts.js
import { EChart ,loadEcharts } from "echarts4taro3";
如果图表较多,建议放在app.ts里先引入
javascript
// app.ts
import { createApp } from 'vue'
import './app.less'
import * as echarts from "echarts4taro3/lib/assets/echarts"; // 这里用了内置的,也可以用自定义的 echarts.js
import { EChart ,loadEcharts } from "echarts4taro3";
const App = createApp({
onShow(options) {
},
created() {
loadEcharts(echarts);
},
// 入口组件不需要实现 render 方法,即使实现了也会被 taro 所覆盖
})
export default App
3 页面使用
css
index.less
.bar-chart {
width: 100%;
height: 100vh;
}
javascript
<template>
<view class="bar-chart">
<EChart ref="canvas" canvas-id="bar-canvas" />
</view>
</template>
<script setup>
import Taro from "@tarojs/taro";
import "./index.less";
import { EChart } from "echarts4taro3";
import { onMounted, ref } from "vue";
const canvas = ref(null);
onMounted(() => {
const echartComponentInstance = canvas.value;
const option = {
tooltip: {
trigger: "axis",
axisPointer: {
// 坐标轴指示器,坐标轴触发有效
type: "shadow" // 默认为直线,可选为:'line' | 'shadow'
}
},
xAxis: {
type: "category",
data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
},
yAxis: {
type: "value"
},
series: [
{
data: [120, 200, 150, 80, 70, 110, 130],
type: "bar"
}
]
};
Taro.nextTick(() => {
echartComponentInstance.refresh(option);
});
});
</script>
4 效果
需要注意的是taro4 生成的项目使用vite发布成小程序有问题,我这边选择的是webpack
微信小程序
h5