• uniapp微信小程序开发踩坑日记:Vue3 + uniapp项目引入Echarts图表库


    一、下载插件包

    下载地址如下:

    lime-echart: 百度图表 echarts,uniapp、taro 使用 echarts 图表,全面兼容各平台小程序、H5、APP、Nvue

    将以下两个文件夹放到项目的components里

     

    同样地,将静态资源文件夹下内容放到自己项目的static文件夹下 

    二、安装Echarts包

    1. pnpm add echarts
    2. 或者
    3. npm install echarts

     

    三、在页面中导入依赖并运行

    下面是一个图表的代码示例,在微信开发者工具中运行代码就可以看到效果啦

    1. <template>
    2. <view>
    3. <view class="title">我的主页view>
    4. <view>
    5. <LEchart class="echart" ref="chart" @finished="init">LEchart>
    6. view>
    7. view>
    8. template>
    9. <script setup>
    10. import LEchart from '@/components/l-echart/l-echart.vue'
    11. // lime-echart是一个demo的组件,用于测试组件
    12. // import LEchart from '@/components/lime-echart/lime-echart.vue'
    13. import { onMounted, reactive, ref } from "vue"
    14. // nvue 不需要引入
    15. // #ifdef VUE3
    16. // #ifdef MP
    17. // 由于vue3 使用vite 不支持umd格式的包,小程序依然可以使用,但需要使用require
    18. const echarts = require('../../static/echarts.min');
    19. // #endif
    20. // #ifndef MP
    21. // 由于 vue3 使用vite 不支持umd格式的包,故引入npm的包
    22. import * as echarts from 'echarts';
    23. // #endif
    24. // #endif
    25. let chart = ref(); // 获取dom
    26. const state = reactive({
    27. option: {},
    28. })
    29. state.option = {
    30. legend: {
    31. show: true,
    32. data: []
    33. },
    34. tooltip: {
    35. trigger: 'axis',
    36. axisPointer: {
    37. type: 'cross'
    38. }
    39. },
    40. grid: {
    41. left: '3%',
    42. right: '8%',
    43. top: '15%',
    44. bottom: '5%',
    45. containLabel: true
    46. },
    47. xAxis: {
    48. type: 'category',
    49. 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],
    50. axisLabel: {
    51. // inside: true,
    52. // color: '#fff'
    53. },
    54. axisTick: {
    55. show: false
    56. },
    57. axisLine: {
    58. show: true,
    59. lineStyle: {
    60. color: '#83bff6'
    61. }
    62. },
    63. z: 10
    64. },
    65. yAxis: {
    66. type: 'value',
    67. axisLine: {
    68. show: true,
    69. lineStyle: {
    70. color: '#83bff6'
    71. }
    72. },
    73. axisTick: {
    74. show: false
    75. },
    76. // axisLabel: {
    77. // color: '#999'
    78. // },
    79. splitLine: {
    80. show: true,
    81. lineStyle: {
    82. type: 'dashed',
    83. color: '#83bff6'
    84. }
    85. }
    86. },
    87. series: [
    88. {
    89. data: [100, 110, 113, 126, 143, 158, 165, 167, 152, 102, ,],
    90. type: "bar",
    91. itemStyle: {
    92. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
    93. { offset: 0, color: '#83bff6' },
    94. { offset: 0.5, color: '#188df0' },
    95. { offset: 1, color: '#188df0' }
    96. ])
    97. },
    98. emphasis: {
    99. itemStyle: {
    100. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
    101. { offset: 0, color: '#2378f7' },
    102. { offset: 0.7, color: '#2378f7' },
    103. { offset: 1, color: '#83bff6' }
    104. ])
    105. }
    106. },
    107. areaStyle: {
    108. show: true,
    109. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
    110. {
    111. offset: 0,
    112. color: '#188df0'
    113. },
    114. {
    115. offset: 1,
    116. color: '#fff'
    117. }
    118. ])
    119. },
    120. }
    121. ],
    122. color: ['#83bff6']
    123. }
    124. // 组件能被调用必须是组件的节点已经被渲染到页面上
    125. onMounted(() => {
    126. chart.value.init(echarts, chart => {
    127. chart.setOption(state.option);
    128. });
    129. })
    130. // 渲染完成
    131. const init = () => {
    132. console.log("渲染完成");
    133. }
    134. script>
    135. <style scopedlang='scss' scoped>
    136. .echart {
    137. width: 100%;
    138. height: 300px;
    139. }
    140. .title {
    141. text-align: center;
    142. }
    143. style>

    四、使用Echarts的其他图表

    打开Echarts官网:Examples - Apache ECharts

    选择自己想要使用的图表,这里以折线图为例

    把左侧的option中的代码复制下来,替换掉上面给出的代码示例中option中的代码即可

  • 相关阅读:
    yocto meta-st-stm32mp conf文件夹分析
    K8S:kubeadm搭建K8S+Harbor 私有仓库
    9.2 Windows钩子
    Cisco Firepower 9300 Series FTD Software 7.2.0 & ASA Software 9.18.1
    IO和进程day07(IPC、管道、信号)
    hive进行base64 加密解密函数
    淘宝sku API接口实例代码
    浅析搭建校园在线教学视频汇聚平台的必要性及解决方案
    DDD—分层架构、洋葱架构、六边形架构
    sql10(Leetcode1661每台机器的进程平均运行时间)
  • 原文地址:https://blog.csdn.net/lbcbjtlhmjq/article/details/138223997