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>
相关推荐
遂心_16 分钟前
深入理解 React Hook:useEffect 完全指南
前端·javascript·react.js
Moonbit18 分钟前
MoonBit 正式加入 WebAssembly Component Model 官方文档 !
前端·后端·编程语言
龙在天23 分钟前
ts中的函数重载
前端
卓伊凡38 分钟前
非常经典的Android开发问题-mipmap图标目录和drawable图标目录的区别和适用场景实战举例-优雅草卓伊凡
前端
前端Hardy39 分钟前
HTML&CSS: 谁懂啊!用代码 “擦去”图片雾气
前端·javascript·css
前端Hardy41 分钟前
HTML&CSS:好精致的导航栏
前端·javascript·css
天下无贼1 小时前
【手写组件】 Vue3 + Uniapp 手写一个高颜值日历组件(含跨月补全+今日高亮+选中状态)
前端·vue.js
我是天龙_绍1 小时前
🔹🔹🔹 vue 通信方式 eventBus
前端
一个不爱写代码的瘦子1 小时前
迭代器和生成器
前端·javascript
拳打南山敬老院2 小时前
漫谈 MCP 构建之概念篇
前端·后端·aigc