• uniapp+vue3+ts+vite+echarts开发图表类小程序,将echarts导入项目使用的详细步骤,耗时一天终于弄好了


    想在uniapp和vue3环境中使用echarts是一件相当前卫的事情,官方适配的还不是很好,echarts的使用插件写的是有些不太清晰的,这里我花费了一天的时间,终于将这个使用步骤搞清楚了,并且建了一个仓库,大家可以直接clone下来使用。先看一下pc端和小程序端的效果:

    微信小程序和抖音小程序等都支持:

    使用步骤如下

    第一步:下载插件包

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

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

    如果你是使用hbuilder写的,可以直接导入,如果你是vscode写的,就下载压缩包:

    我这里将我下载好的zip包分享出来:lime-echart_0.8.1.zip - 蓝奏云

     

    下载好解压然后将解压后的组件导入到项目的components下面:

    并且将静态资源放到静态文件夹中:

    第二步:安装echarts包

    1. pnpm add echarts
    2. -or-
    3. npm install echarts

     

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

    然后在页面中导入这个LEchart这个组件:

    将依赖按照不同的平台区分导入到页面组件中:下面是我的页面源代码

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

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

  • 相关阅读:
    <C++>详解list类
    程序员面对生活
    离线数仓同步数据3
    Rainbond的 Gateway API 插件制作实践
    什么是跨境电商独立站?有什么优势和劣势?
    〔020〕Stable Diffusion 之 骨骼姿势 篇
    闭包的定义,原理,应用场景,优点,缺点
    HTML5教程
    基于STM32 ZigBee无线远程火灾报警监控系统物联网温度烟雾
    用python做一个压缩图片的小程序
  • 原文地址:https://blog.csdn.net/weixin_44786530/article/details/134326991