当前位置:   article > 正文

uniapp微信小程序使用echarts_uni-app微信小程序使用echarts

uni-app微信小程序使用echarts

 uni组件中无法引入uni-ec-canvas,或者引入uni-ec-canvas后也没有此依赖包,此时我们可以写组件来引用渲染。

1、引入组件

在pages.json中引入该组件

  1. "usingComponents": {
  2. "ec-canvas": "../../components/ec-canvas/ec-canvas"
  3. },

 在要使用echarts图的页面引入组件

详细代码如下

  1. <template>
  2. <view class="body">
  3. <u-ec-canvas class="uni-ec-canvas" id="uni-ec-canvas" ref="canvas" canvas-id="uni-ec-canvas" :ec="ec">
  4. </u-ec-canvas>
  5. </view>
  6. </template>
  7. <script>
  8. import uEcCanvas from '../components/uni-ec-canvas/test-ec.vue'
  9. import * as echarts from '../components/uni-ec-canvas/echarts.js'
  10. let chart = null
  11. export default {
  12. components: {
  13. uEcCanvas
  14. },
  15. mounted() {
  16. this.getData()
  17. },
  18. onUnload() {
  19. echarts.dispose(this.$refs.canvas);
  20. },
  21. methods: {
  22. initChart(canvas, width, height, canvasDpr) {
  23. chart = echarts.init(canvas, null, {
  24. width: width,
  25. height: height,
  26. devicePixelRatio: canvasDpr,
  27. resize: true
  28. })
  29. let option = {
  30. xAxis: {
  31. type: 'category',
  32. data: this.xdata,
  33. axisLabel: {
  34. rotate: -45, // 设置 x 轴标签旋转角度为 -45 度
  35. }
  36. },
  37. yAxis: [
  38. {
  39. type: 'value',
  40. name: "℃",
  41. // min: 10
  42. },
  43. {
  44. type: 'value',
  45. name: "湿度",
  46. }
  47. ],
  48. series: [
  49. {
  50. name: "温度",
  51. data: this.ydata,
  52. type: 'line',
  53. smooth: true,
  54. itemStyle: {
  55. normal: {
  56. color: "#00aaff"
  57. }
  58. },
  59. areaStyle: {
  60. color: 'rgba(0, 170, 255, 0.2)'
  61. }
  62. },
  63. {
  64. name: "湿度",
  65. data: this.y2data,
  66. type: 'line',
  67. smooth: true,
  68. itemStyle: {
  69. normal: {
  70. color: "#ff0000" // 设置第二条线的颜色
  71. }
  72. },
  73. areaStyle: {
  74. color: 'rgba(255, 0, 0, 0.2)' // 设置第二条线的区域颜色
  75. },
  76. yAxisIndex: 1 // 将第二条线的 y 轴设置为右边的 y 轴
  77. }
  78. ],
  79. legend: {
  80. data: ['温度', '湿度'] // 设置图例名称
  81. },
  82. tooltip: {
  83. trigger: 'axis', // 设置触发类型为坐标轴触发
  84. // axisPointer: {
  85. // type: 'cross' // 设置指示器类型为十字准星
  86. // },
  87. showContent: true, // 显示 Tooltip 内容
  88. showDelay: 0, // 鼠标移入后立即显示
  89. hideDelay: 0, // 鼠标移出后立即隐藏
  90. triggerOn: 'click', // 设置触发方式为点击
  91. hideOn: 'mouseout', // 鼠标移出时隐藏 Tooltip
  92. formatter: function(params) {
  93. return `时间: ${params[0].name}
  94. ${params[0].seriesName}: ${params[0].value}
  95. ${params[1].seriesName}: ${params[1].value}`
  96. }
  97. },
  98. dataZoom: [
  99. {
  100. type: 'slider',
  101. show: true,
  102. start: 0,
  103. end: 10 / this.xdata.length * 100, // 默认显示最后10条数据
  104. handleSize: 8,
  105. height: 2,
  106. top: '90%'
  107. },
  108. {
  109. type: 'inside',
  110. start: 0,
  111. end: 10 / this.xdata.length * 100, // 默认显示最后10条数据
  112. height: 2
  113. }
  114. ]
  115. }
  116. canvas.setChart(chart)
  117. chart.setOption(option)
  118. return chart
  119. },
  120. }

2、echarts.js包太大,小程序不能上传

echarts的包太大,我们可以在echarts的构建工具中选择自己需要的吗,模版工具进行打包

ECharts 在线构建

下载后替换文件中的echarts.js文件,并将自己下载的文件替换成echarts.js文件名,否则引入会找不到文件。

3、上传代码时,包还是太大,怎么解决

如果到最后,包文件超过2M,无法上传

我们可以尝试:

1.分包

2.静态文件,例如图片等,使用网络地址

3.在上传代码时,勾选“自动压缩脚本文件”

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/873271
推荐阅读
相关标签
  

闽ICP备14008679号