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>
相关推荐
粥里有勺糖2 分钟前
视野修炼-技术周刊第128期 | Bun 被收购
前端·javascript·github
用户120391129472616 分钟前
彻底搞定大模型流式输出:从二进制碎块到“嘚嘚嘚”打字机效果,让底层逻辑飞起来
前端·javascript·面试
CPU NULL28 分钟前
Vue 3 前端调试与开发指南
前端·javascript·vue.js
幼儿园技术家1 小时前
多方案统一认证体系对比
前端
十一.3661 小时前
83-84 包装类,字符串的方法
前端·javascript·vue.js
over6972 小时前
深入解析:基于 Vue 3 与 DeepSeek API 构建流式大模型聊天应用的完整实现
前端·javascript·面试
用户4099322502122 小时前
Vue3计算属性如何通过缓存特性优化表单验证与数据过滤?
前端·ai编程·trae
接着奏乐接着舞2 小时前
react useMeno useCallback
前端·javascript·react.js
码农阿豪2 小时前
Vue项目构建中ESLint的“换行符战争”:从报错到优雅解决
前端·javascript·vue.js
xhxxx3 小时前
AI打字机的秘密:一个 buffer 如何让机器学会“慢慢说话”
前端·vue.js·openai