当前位置: 首页 > 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.lryc.cn/news/604987.html

相关文章:

  • 实现implements InitializingBean, DisposableBean 有什么用
  • 【MATLAB/Simulink】查看MATLAB以往版本的帮助文档
  • 牛顿-拉夫森法求解非线性方程组
  • 无人机惯性导航模块运行与技术难点!
  • 25年新算法!基于猛禽的优化算法(BPBO):一种元启发式优化算法,附完整免费MATLAB代码
  • 《数学模型》——最大流与最小费用流问题
  • Mediapipe 的某些模型,网络下载不来可以去gitee找找看
  • 双塔模型 + 自监督学习:解决长尾物品表征难题
  • Helm在Kubernetes中的应用部署指南与案例解析
  • FragmentManager 返回栈与 Activity 运行栈的关系(当按下Back键时屏幕会如何变化?)
  • 基于SpringBoot+MyBatis+MySQL+VUE实现的便利店信息管理系统(附源码+数据库+毕业论文+远程部署)
  • 如何不让android studio自动换行
  • AI服务器中,EEPROM有哪些部件使用,需要存储哪些信息?
  • NLU 语义解析评测实践:基于函数调用的 ACC、ROUGE 与 BLEU 综合指标
  • 《SAM:Segment Anything》论文精读笔记
  • 《CLIP改进工作串讲》论文精读笔记
  • AtCoder Beginner Contest 416(ABCDE)
  • 机器视觉halcon7-缺陷检测
  • 「源力觉醒 创作者计划」_文心大模型 4.5 开源 28 天:从车间轴承到山村课堂的 AI 突围
  • 数据结构-Set集合(一)Set集合介绍、优缺点
  • labview控制软件开发
  • 多模通信·数据采集:AORO P9000U三防平板带来定制化解决方案
  • Kafka 单机多 Broker 实例集群搭建 | 详情
  • 【力扣热题100】哈希——最长连续序列
  • 中国高铁从追赶到领跑的破壁之路
  • Ubuntu 本地部署和使用 n8n 指南and ai almost anything
  • 《Java 程序设计》第 10 章 - 接口与 Lambda 表达式
  • 锁定中科院1区TOP!融合LSTM与Attention做时间序列预测 !
  • 新手向:DeepSeek 部署中的常见问题及解决方案
  • 【OD机试题解法笔记】符号运算