uni-app如何引入echarts

在uni-app官网的官网插件中找echarts

打开图片对应的echarts,点击下载并导入插件

如果是vue3使用const echarts = require('../../uni_modules/lime-echart/static/echarts.min');引入echarts

复制代码
<template>
  <view>
    <view style="width:750rpx; height:750rpx"><l-echart ref="chartRef" @finished="init"></l-echart></view>
  </view>
</template>

<script lang="ts" setup>
// 插件内的 三选一
const echarts = require('../../uni_modules/lime-echart/static/echarts.min');
import { onMounted, ref } from "vue";

// 定义数据数组
const xAxisData: string[] = [];
const data1: number[] = [];
const data2: number[] = [];
const data3: number[] = [];
const data4: number[] = [];

// 循环生成随机数据
for (let i = 0; i < 10; i++) {
  xAxisData.push('Class' + i);
  data1.push(+(Math.random() * 2).toFixed(2));
  data2.push(+(Math.random() * 5).toFixed(2));
  data3.push(+(Math.random() + -0.3).toFixed(2));
  data4.push(+Math.random().toFixed(2));
}

// 定义强调样式对象
const emphasisStyle = {
  itemStyle: {
    shadowBlur: 10,
    shadowColor: 'rgba(0, 0, 0, 0.3)'
  }
};

// 构建图表配置选项对象
const buildOption = () => {
  return {
    legend: {
      data: ['bar', 'bar2', 'bar3', 'bar4'],
      left: '10%'
    },
    tooltip: {},
    xAxis: {
      data: xAxisData
    },
    yAxis: {},
    grid: {
      bottom: 100
    },
    series: [
      {
        name: 'bar',
        type: 'bar',
        stack: 'one',
        emphasis: emphasisStyle,
        data: data1
      },
      {
        name: 'bar2',
        type: 'bar',
        stack: 'one',
        emphasis: emphasisStyle,
        data: data2
      },
      {
        name: 'bar3',
        type: 'bar',
        stack: 'one',
        emphasis: emphasisStyle,
        data: data3
      },
      {
        name: 'bar4',
        type: 'bar',
        stack: 'two',
        emphasis: emphasisStyle,
        data: data4
      }
    ]
  };
};

const chartRef = ref(null);

onMounted(() => {
  // 组件能被调用必须是组件的节点已经被渲染到页面上
  setTimeout(async () => {
    if (!chartRef.value) return;
    const myChart = await chartRef.value.init(echarts);
    const option = buildOption();
    // 先设置基础的图表配置选项
    option && myChart.setOption(option);
    // 监听brushSelected事件,用于处理图表交互后的操作
    myChart.on('brushSelected', function (params) {
      const brushed: string[] = [];
      const brushComponent = params.batch[0];
      for (let sIdx = 0; sIdx < brushComponent.selected.length; sIdx++) {
        const rawIndices = brushComponent.selected[sIdx].dataIndex;
        brushed.push(`[Series ${sIdx}] ${rawIndices.join(', ')}`);
      }
      myChart.setOption({
        title: {
          backgroundColor: '#333',
          text: `SELECTED DATA INDICES: \n${brushed.join('\n')}`,
          bottom: 0,
          right: '10%',
          width: 100,
          textStyle: {
            fontSize: 12,
            color: '#fff'
          }
        }
      });
    });
  }, 300);
});
</script>
相关推荐
xlq223222 分钟前
16.环境变量与地址空间
前端·chrome
wulijuan88866615 分钟前
Vue 组件的通信方式有哪些?
前端·javascript·vue.js
k093316 分钟前
vue中view-design的校验及各种坑
前端·vue.js·view design
乘风gg22 分钟前
企业级 Prompt 工程实战指南(下):构建可复用 Prompt 架构平台
前端·面试·架构
宇擎智脑科技36 分钟前
AntV G6、X6 与 React Flow 深度对比:核心差异与大模型时代的应用场景分析
前端·人工智能·react.js·前端框架
山核桃&17°36 分钟前
基于 Vue + Node.js 批处理bat脚本实现多环境一键部署
运维·前端·自动化
AC赳赳老秦37 分钟前
云原生AI趋势:DeepSeek与云3.0架构协同,提升AI部署性能与可移植性
大数据·前端·人工智能·算法·云原生·架构·deepseek
程序哥聊面试42 分钟前
React + TS 初始化新项目报错解决方法
前端·react.js·npm
codeGoogle44 分钟前
2026 年 IM 怎么选?聊聊 4 家主流即时通讯方案的差异
android·前端·后端
C澒1 小时前
从单体到分布式:SLDS 2.0 全球物流履约网络架构演进之路
前端·分布式·架构·系统架构·教育电商·交通物流