uniapp导入echarts类库 开发图表类小程序vue3+ts+vite

本文详细介绍了如何在微信小程序和抖音小程序中使用Echarts插件,包括下载、安装、导入组件以及配置图表的过程,适用于HBuilder和VSCode开发环境。
摘要由CSDN通过智能技术生成

在这里插入图片描述
微信小程序和抖音小程序等都支持:
在这里插入图片描述
使用步骤如下
第一步:下载插件包

下载echarts插件包,并导入到项目中,然后使用插件中的组件创建容器,并导入数据就可以了。

echarts插件包地址:echarts - DCloud 插件市场

如果你是使用hbuilder写的,可以直接导入,如果你是vscode写的,就下载压缩包:
在这里插入图片描述
下载好解压然后将解压后的组件导入到项目的components下面:
在这里插入图片描述
并且将静态资源放到静态文件夹中:
在这里插入图片描述
第二步:安装echarts包

pnpm add echarts
-or-
npm install echarts

第三步:在页面中导入依赖并运行

然后在页面中导入这个LEchart这个组件:
在这里插入图片描述
将依赖按照不同的平台区分导入到页面组件中:下面是我的页面源代码

<template>
  <view>
    <view class="title">我的主页</view>
    <view>
      <LEchart class="echart" ref="chart" @finished="init"></LEchart>
    </view>
  </view>
</template>
 
<script setup>
import LEchart from '@/components/l-echart/l-echart.vue'
// lime-echart是一个demo的组件,用于测试组件
// import LEchart from '@/components/lime-echart/lime-echart.vue'
import { onMounted, reactive, ref } from "vue"
// nvue 不需要引入
// #ifdef VUE3
// #ifdef MP
// 由于vue3 使用vite 不支持umd格式的包,小程序依然可以使用,但需要使用require
const echarts = require('../../static/echarts.min');
// #endif
// #ifndef MP
// 由于 vue3 使用vite 不支持umd格式的包,故引入npm的包
import * as echarts from 'echarts';
// #endif
// #endif
 
 
let chart = ref(); // 获取dom
const state = reactive({
  option: {},
})
state.option = {
  legend: {
    show: true,
    data: []
  },
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      type: 'cross'
    }
  },
  grid: {
    left: '3%',
    right: '8%',
    top: '15%',
    bottom: '5%',
    containLabel: true
  },
  xAxis: {
    type: 'category',
    data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1, 4, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24],
    axisLabel: {
      // inside: true,
      // color: '#fff'
    },
    axisTick: {
      show: false
    },
    axisLine: {
      show: true,
      lineStyle: {
        color: '#83bff6'
      }
    },
    z: 10
  },
  yAxis: {
    type: 'value',
    axisLine: {
      show: true,
      lineStyle: {
        color: '#83bff6'
      }
    },
    axisTick: {
      show: false
    },
    // axisLabel: {
    //   color: '#999'
    // },
    splitLine: {
      show: true,
      lineStyle: {
        type: 'dashed',
        color: '#83bff6'
      }
    }
  },
  series: [
    {
      data: [100, 110, 113, 126, 143, 158, 165, 167, 152, 102, ,],
      type: "bar",
      itemStyle: {
        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
          { offset: 0, color: '#83bff6' },
          { offset: 0.5, color: '#188df0' },
          { offset: 1, color: '#188df0' }
        ])
      },
      emphasis: {
        itemStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: '#2378f7' },
            { offset: 0.7, color: '#2378f7' },
            { offset: 1, color: '#83bff6' }
          ])
        }
      },
      areaStyle: {
        show: true,
        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
          {
            offset: 0,
            color: '#188df0'
          },
          {
            offset: 1,
            color: '#fff'
          }
        ])
      },
    }
  ],
  color: ['#83bff6']
}
// 组件能被调用必须是组件的节点已经被渲染到页面上
onMounted(() => {
  chart.value.init(echarts, chart => {
    chart.setOption(state.option);
  });
})
 
// 渲染完成
const init = () => {
  console.log("渲染完成");
}
</script>
 
<style scopedlang='scss' scoped>
.echart {
  width: 100%;
  height: 300px;
}
 
.title {
  text-align: center;
}
</style>

最后运行小程序或者h5就可以看到效果了:

在这里插入图片描述

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值