当前位置: 首页 > news >正文

uniApp实战六:Echart图表集成

文章目录

    • 1.最终效果预览
    • 2.顶部时间选择,中间echart图,底部监测项
    • 3.Echart集成
    • 4.集成网络请求
    • 5.底部按钮切换调用方法
    • 6.调用接口获取echart数据
    • 7.设置echart图的option
    • 8.顶部时间点击方法

1.最终效果预览

QQ_1753843529696

2.顶部时间选择,中间echart图,底部监测项

<view class="button-group-top"><button v-for="(item, index) in buttonList" :key="index":class="['time-btn', { 'active': currentBtnIndex === index }]" @click="handleClick(index,item)">{{ item.name }}</button></view><view @click="echarts.onClick" :prop="option" :change:prop="echarts.updateEcharts" id="echarts" class="echarts"></view><view class="btn-group"><view v-for="(item, index) in project" :key="index":class="['btn-item', { active: currentIndex === index }]" @click="switchIndicator(index,item)">{{ item.structName }}</view></view>

3.Echart集成

在uni插件市场找到插件renderjs-echarts-demo,我们运行demo就可以出现一个柱状图,我们基于这个demo改造下我们的项目即可。

<script module="echarts" lang="renderjs">let myChartexport default {mounted() {if (typeof window.echarts === 'function') {this.initEcharts()} else {const script = document.createElement('script')script.src = 'static/echarts.js'script.onload = this.initEcharts.bind(this)document.head.appendChild(script)}},methods: {initEcharts() {myChart = echarts.init(document.getElementById('echarts'))myChart.setOption(this.option)},updateEcharts(newValue, oldValue, ownerInstance, instance) {myChart.setOption(newValue)},onClick(event, ownerInstance) {ownerInstance.callMethod('onViewClick', {test: 'test'})}}}
</script>

4.集成网络请求

uni.request({url: 'xxxx', data: obj,success: (res) => {this.initData(res.data.data)}});

在页面中直接用这个网络请求就能拿到数据,不知道为啥将网络请求封装后发反而拿不到数据了,返回的参数多了好几层

5.底部按钮切换调用方法

switchIndicator(index, item) {this.currentIndex = indexthis.getSluiceMoniDataPicitemId(item.siteItemId)}getSluiceMoniDataPicitemId(siteItemId) {let arr = this.project.filter(item => {return siteItemId == item.siteItemId})if (arr && arr.length > 0) {this.structName = arr[0].structName}this.siteItemId = siteItemId;this.getDataByTime()}

6.调用接口获取echart数据

getEchartData(obj) {this.yArr = []this.xArr = []uni.request({url: 'xxxxx', data: obj,success: (res) => {let arr = res.data.data;for (let i in arr) {if (arr[i].DATA_TIME) {this.yArr.push(arr[i].DATA_VALUE)this.xArr.push(arr[i].DATA_TIME)}}if (this.yArr) {this.addOption();}}});}

7.设置echart图的option

addOption() {this.option = {grid: {left: '7%',right: '5%',bottom: '11%',top: '7%',containLabel: true},xAxis: [{splitLine: {show: false},type: 'category',boundaryGap: true,data: this.xArr}],yAxis: [{type: 'value',splitLine: {show: true},}],series: [{name: '',type: 'line',stack: '总量',smooth: true,symbol: 'none',areaStyle: {normal: {color: 'rgba(255,255,255,0.4)'}},itemStyle: {normal: {color: 'rgba(3,169,244,1)'}},lineStyle: {normal: {color: 'blue'}},data: this.yArr}]}}

8.顶部时间点击方法

handleClick(index, item) {this.currentBtnIndex = indexthis.btnHourValue = item.valuethis.getDataByTime()}getDataByTime(){let tTime = new Date()let endTimeValue = timeFormat(tTime, 'yyyy-MM-dd HH:mm:ss')let sValue = tTime.setHours(tTime.getHours() - parseInt(this.btnHourValue)) // value小时前let startTimeValue = timeFormat(sValue, 'yyyy-MM-dd HH:mm:ss')let obj = {'itemMoniId': this.siteItemId,'startTime': startTimeValue,'endTime': endTimeValue,}this.getEchartData(obj);}
http://www.dtcms.com/a/305970.html

相关文章:

  • uniapp-vue3来实现一个金额千分位展示效果
  • 《CLIP改进工作串讲》论文精读笔记
  • uniapp使用谷歌地图获取位置
  • uniapp实现微信小程序导航功能
  • 从单机到分布式:Redis如何成为架构升级的胜负手
  • 问题1:uniapp在pages样式穿刺没有问题,在components组件中样式穿刺小程序不起效果
  • Oracle迁移PostgreSQL隐式类型转换配置指南
  • FPGA实现CameraLink视频解码转SRIO与DSP交互,FPGA+DSP多核异构图像处理架构,提供2套工程源码和技术支持
  • Windows Server 2019 查询最近7天远程登录源 IP 地址(含 RDP 和网络登录)
  • 【OD机试题解法笔记】符号运算
  • AWS Blockchain Templates:快速部署企业级区块链网络的终极解决方案
  • Keil-C51 与 Keil -ARM 项目工程兼容的方法
  • leetcode热题——搜索二维矩阵Ⅱ
  • Syzkaller实战教程2:运行环境配置+实例运行
  • 多模通信·数据采集:AORO P9000U三防平板带来定制化解决方案
  • Rust × Elasticsearch官方 `elasticsearch` crate 上手指南
  • Hyperchain 的分级权限体系如何应对潜在的安全威胁和攻击?
  • 龙虎榜——20250730
  • 2018 年 NOI 最后一题题解
  • 学会使用golang zap日志库
  • 【MATLAB】(一)简介
  • 字节跳动“扣子”(Coze)开源:AI智能体生态的技术革命
  • ansible 版本升级
  • colima 修改镜像源为国内源
  • mybatis-入门
  • 笔记本电脑开机慢系统启动慢怎么办?【图文详解】win7/10/11开机慢
  • [leetcode] 反转字符串中的单词
  • 【JVM篇10】:三种垃圾回收算法对比详解
  • CMS框架漏洞
  • QT笔记--》QMenu