🪀 Uni ECharts:也许是 uni-app 中使用 ECharts 最优雅的解决方案

1,367 阅读2分钟

Uni ECharts 是适用于 uni-app 的 Apache ECharts 组件,无需繁琐的步骤即可轻松在 uni-app 平台上使用 echarts。

官网 & 文档:uni-echarts.xiaohe.ink

Github:github.com/xiaohe0601/…

🎉 特性

  • 🚀 快速上手:与 Vue ECharts 近乎一致的使用体验
  • 📱 多端兼容:支持 Web、小程序、APP
  • 📦 Easycom:组件自动导入
  • TypeScript:完整的组件和方法类型定义
  • 🍳 免费商用:基于 MIT 协议发布

🚀 快速开始

👉 前往 Uni ECharts 官网 快速开始 查看完整内容

前置条件:

  • echarts >= 5.3.0
  • vue >= 3.3.0(目前 uni-app 尚未适配 Vue 3.5,推荐使用 3.4.x 与 uni-app 保持一致)

安装

  1. 使用 npm 安装 echarts

    # pnpm
    pnpm add echarts
    
    # yarn
    yarn add echarts
    
    # npm
    npm install echarts
    
  2. 前往 uni-app 插件市场下载 Uni Echarts

配置

Uni ECharts 支持 easycom 规范, 当使用 uni-modules 方式时无需配置即可免导入直接使用组件。

使用

<template>
  <uni-echarts custom-class="chart" :option="option"></uni-echarts>
</template>

<script setup>
import { PieChart } from "echarts/charts";
import { DatasetComponent, LegendComponent, TooltipComponent } from "echarts/components";
import * as echarts from "echarts/core";
import { CanvasRenderer } from "echarts/renderers";
import { ref } from "vue";
import { provideEcharts } from "@/uni_modules/xiaohe-echarts";

provideEcharts(echarts);

echarts.use([
  LegendComponent,
  TooltipComponent,
  DatasetComponent,
  PieChart,
  CanvasRenderer
]);

const option = ref({
  legend: {
    top: 10,
    left: "center"
  },
  tooltip: {
    trigger: "item",
    textStyle: {
      // #ifdef MP-WEIXIN
      // 临时解决微信小程序 tooltip 文字阴影问题
      textShadowBlur: 1
      // #endif
    }
  },
  series: [
    {
      type: "pie",
      radius: ["30%", "52%"],
      label: {
        show: false,
        position: "center"
      },
      itemStyle: {
        borderWidth: 2,
        borderColor: "#ffffff",
        borderRadius: 10
      },
      emphasis: {
        label: {
          show: true,
          fontSize: 20
        }
      }
    }
  ],
  dataset: {
    dimensions: ["来源", "数量"],
    source: [
      ["Search Engine", 1048],
      ["Direct", 735],
      ["Email", 580],
      ["Union Ads", 484],
      ["Video Ads", 300]
    ]
  }
});
</script>

<style>
.chart {
  height: 300px;
}
</style>

小程序端图表不显示?

请参考常见问题中 小程序端 class / style 无效 部分的说明。

❤️ 支持 & 鼓励

如果 Uni ECharts 对你有帮助,可以通过以下渠道对我们表示鼓励:

无论 ⭐️ 还是 💰 支持,我们铭记于心,这将是我们继续前进的动力,感谢您的支持!

🍬 鸣谢

得益于以下项目对开源的付出,让 Uni ECharts 能够站在巨人的肩膀上。

🏆 开源协议

Uni ECharts 基于 MIT 许可发布,请自由地享受和参与开源。