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>
相关推荐
A尘埃18 小时前
大模型应用python+Java后端+Vue前端的整合
java·前端·python
遥遥晚风点点18 小时前
Spark导出数据文件到HDFS
前端·javascript·ajax
克里斯蒂亚L19 小时前
开发一个计时器组件
前端·浏览器
克里斯蒂亚诺更新19 小时前
微信小程序 点击某个marker改变其大小
开发语言·前端·javascript
一人一程温一壶酒19 小时前
微信小程序uniapp开发附源码——图片加水印
微信小程序·uni-app·notepad++
天才奇男子19 小时前
从零开始搭建Linux Web服务器
linux·服务器·前端
长空任鸟飞_阿康20 小时前
AI 多模态全栈应用项目描述
前端·vue.js·人工智能·node.js·语音识别
Mintopia20 小时前
🌐 实时协同 AIGC:多人在线 Web 创作的技术架构设计
前端·人工智能·trae
Mintopia20 小时前
🔥 “Solo Coding”的近期热度解析(截至 2025 年末)
前端·人工智能·trae
顾安r21 小时前
11.14 脚本网页游戏 猜黑红
前端·javascript·游戏·flask·html