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>
相关推荐
GIS之路3 分钟前
使用命令行工具 ogr2ogr 将 CSV 转换为 Shp 数据(二)
前端
嘉琪00116 分钟前
Vue3+JS 高级前端面试题
开发语言·前端·javascript
vipbic1 小时前
用 Turborepo 打造 Strapi 插件开发的极速全栈体验
前端·javascript
天涯学馆1 小时前
为什么 JavaScript 可以单线程却能处理异步?
前端·javascript
Henry_Lau6171 小时前
主流IDE常用快捷键对照
前端·css·ide
陶甜也1 小时前
使用Blender进行现代建筑3D建模:前端开发者的跨界探索
前端·3d·blender
iOS阿玮2 小时前
想偷懒购买现成的应用,结果一更新就遇到了4.3a!
uni-app·app·apple
我命由我123452 小时前
VSCode - Prettier 配置格式化的单行长度
开发语言·前端·ide·vscode·前端框架·编辑器·学习方法
HashTang2 小时前
【AI 编程实战】第 4 篇:一次完美 vs 五轮对话 - UnoCSS 配置的正确姿势
前端·uni-app·ai编程
JIngJaneIL2 小时前
基于java + vue校园快递物流管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js