组件间的值传递:改进若依框架中的BarChart.vue组件

改进前的BarChart

如下是若依(Ruoyi)框架中的BarChart.vue文件,该BarChart.vue无法实现组件间的值传递。到这里您不妨可以试试该如何去传值。如果您不想思考,请看改进后的BarChart。直接拿走使用!

<template><div :class="className" :style="{height:height,width:width}" />
</template><script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'const animationDuration = 6000export default {mixins: [resize],props: {className: {type: String,default: 'chart'},width: {type: String,default: '100%'},height: {type: String,default: '300px'}},data() {return {chart: null}},mounted() {this.$nextTick(() => {this.initChart()})},beforeDestroy() {if (!this.chart) {return}this.chart.dispose()this.chart = null},methods: {initChart() {this.chart = echarts.init(this.$el, 'macarons')this.chart.setOption({tooltip: {trigger: 'axis',axisPointer: { // 坐标轴指示器,坐标轴触发有效type: 'shadow' // 默认为直线,可选为:'line' | 'shadow'}},grid: {top: 10,left: '2%',right: '2%',bottom: '3%',containLabel: true},xAxis: [{type: 'category',data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],axisTick: {alignWithLabel: true}}],yAxis: [{type: 'value',axisTick: {show: false}}],series: [{name: 'pageA',type: 'bar',stack: 'vistors',barWidth: '60%',data: [79, 52, 200, 334, 390, 330, 220],animationDuration}, {name: 'pageB',type: 'bar',stack: 'vistors',barWidth: '60%',data: [80, 52, 200, 334, 390, 330, 220],animationDuration}, {name: 'pageC',type: 'bar',stack: 'vistors',barWidth: '60%',data: [30, 52, 200, 334, 390, 330, 220],animationDuration}]})}}
}
</script>

改进后的BarChart

如下是根据若依框架改写后的BarChart.vue文件,该BarChart.vue可实现组件间的值传递。

<template><div :class="className" :style="{height:height,width:width}" />
</template><script>
import * as echarts from 'echarts';
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'const animationDuration = 6000export default {mixins: [resize],props: {className: {type: String,default: 'chart'},width: {type: String,default: '100%'},height: {type: String,default: '300px'},//prop定义要求使用该组件时需要绑定bar-chart进行传值barDataChart:{type: Object,required: true}},data() {return {chart: null}},//监听barChart中值的变化watch:{barDataChart:{deep:true,handler(val){this.setOptions(val)}}},mounted() {this.$nextTick(() => {this.initChart()})},beforeDestroy() {if (!this.chart) {return}this.chart.dispose()this.chart = null},methods: {initChart() {this.chart = echarts.init(this.$el, 'macarons')this.setOptions(this.barDataChart)},setOptions({work_days, hj_main_count, hj_right_count, hj_left_count, hj_main, hj_right, hj_left}) {this.chart.setOption({tooltip: {trigger: 'axis',axisPointer: { // 坐标轴指示器,坐标轴触发有效type: 'shadow' // 默认为直线,可选为:'line' | 'shadow'}},grid: {top: 10,left: '2%',right: '2%',bottom: '3%',containLabel: true},xAxis: [{type: 'category',data: work_days,// name:'日期',nameLocation: 'middle', // 显示位置nameTextStyle: {fontWeight: 'bold' // 字体加粗},axisTick: {alignWithLabel: true}}],yAxis: [{type: 'value',axisTick: {show: false},// name: '预警次数', // 添加单位// nameLocation: 'end', // 显示位置// nameTextStyle: {//    fontWeight: 'bold' // 字体加粗//   }axisLabel: {formatter: '{value} 次'}}],series: [{name: hj_main,type: 'bar',stack: 'vistors',barWidth: '60%',data: hj_main_count,animationDuration}, {name: hj_right,type: 'bar',stack: 'vistors',barWidth: '60%',data: hj_right_count,animationDuration}, {name: hj_left,type: 'bar',stack: 'vistors',barWidth: '60%',data: hj_left_count,animationDuration}]})}}
}
</script>

其他页面使用BarChart

如下以在ruoyi-ui/index.vue下使用BarChart文件为例进行介绍。

1.导入BarChart组件

<script>import BarChart from './dashboard/BarChart'
<script>

2.注册组件

export default {name: 'Index',components: {BarChart}
}

3.在所需位置引入

 <div class="chart-wrapper"><bar-chart :bar-data-chart="barFalseChart"/></div>

4.使用:bar-data-chart绑定需要展示的值,barFalseChart就是处理好等待渲染的对象

//柱状图数据
const barFalseChart = {work_days: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],hj_main_count: [9, 2, 0, 8, 0, 5, 1],hj_right_count: [8, 5, 3, 3, 9, 6, 4],hj_left_count: [3, 7, 1, 0, 3, 6, 4],hj_main: '圆棒浇铸区主摄像头预警次数',hj_left: '圆棒浇铸区左摄像头预警次数',hj_right: '圆棒浇铸区右摄像头预警次数'
}

其中barFalseChart为啥是对象,原因在这:

5.在data中也要绑定一下:

  data() {return {//柱状图数据barFalseChart:barFalseChart,}
}

6.最后一步,根据需求修改只要需要的值,文中需要修改和自定义的地方已经用注释标注出来了,各位看官按需进行修改。

(1)父组件中的定义:

//柱状图数据
const barFalseChart = {work_days: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],hj_main_count: [9, 2, 0, 8, 0, 5, 1],hj_right_count: [8, 5, 3, 3, 9, 6, 4],hj_left_count: [3, 7, 1, 0, 3, 6, 4],hj_main: '主摄像头预警次数',hj_left: '左摄像头预警次数',hj_right: '右摄像头预警次数'
}

(2)上手修改:

methods: {initChart() {this.chart = echarts.init(this.$el, 'macarons')this.setOptions(this.barDataChart)},
//  按需修改传入值,按需在setOptions({work_days, hj_main_count, hj_right_count, hj_left_count, hj_main, hj_right, hj_left})中进行自定义(但这些值是来自于父组件定义的值)setOptions({work_days, hj_main_count, hj_right_count, hj_left_count, hj_main, hj_right, hj_left}) {this.chart.setOption({tooltip: {trigger: 'axis',axisPointer: { type: 'shadow' }},grid: {top: 10,left: '2%',right: '2%',bottom: '3%',containLabel: true},xAxis: [{type: 'category',data: work_days,   //这里修改x轴的值axisTick: {alignWithLabel: true}}],yAxis: [{type: 'value',axisTick: {show: false},axisLabel: {formatter: '{value} 次'   //给y轴加上单位}}],series: [{name: hj_main,    //修改序列1的名称type: 'bar',stack: 'vistors',barWidth: '60%',data: hj_main_count, //修改序列1绑定的值animationDuration}, {name: hj_right,     //修改序列2的名称type: 'bar',stack: 'vistors',barWidth: '60%',data: hj_right_count,  //修改序列2绑定的值animationDuration}, {name: hj_left,   //修改序列3的名称type: 'bar',stack: 'vistors',barWidth: '60%',data: hj_left_count,  //修改序列3绑定的值animationDuration}]})}}
}
</script>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/305349.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Python新手上路:“用Python和Pygame创造你的流星雨”

文章目录 一、前言二、下载安装过程1.官网下载安装包2.安装python过程第一步第二步第三步第四步第五步安装完成 3.简单测试Python3.1 检查 Python 版本号3.2 打开 Python 解释器3.3 输入你的第一个代码3.4 运行 Python 脚本 4.安装Pygame4.1 cmd命令安装Pygame4.2 pip升级4.3 安…

GPT分区格式

GPT分区格式 [rootlocalhost ~]# gdisk /dev/sdb -bash: gdisk: 未找到命令 [rootlocalhost ~]# yum -y install gdisk- gdisk命令用于查看磁盘使用情况和磁盘分区&#xff08;GPT分区格式&#xff09; - 命令格式&#xff1a;gdisk [选项...] [设备路径] - 常用选项&…

python学习14

前言&#xff1a;相信看到这篇文章的小伙伴都或多或少有一些编程基础&#xff0c;懂得一些linux的基本命令了吧&#xff0c;本篇文章将带领大家服务器如何部署一个使用django框架开发的一个网站进行云服务器端的部署。 文章使用到的的工具 Python&#xff1a;一种编程语言&…

redis的搭建及应用(二)-redis的持久化策略

Redis的持久化策略 RDB RDB持久化是指在指定的时间间隔内将redis内存中的数据集快照写入磁盘&#xff0c;实现原理是redis服务在指定的时间间隔内先fork一个子进程&#xff0c;由子进程将数据集写入临时文件&#xff0c;写入成功后&#xff0c;再替换之前的文件&#xff0c;用二…

pyDAL一个python的ORM(2)pyDAL的安装与初始化

一、pyDAL库的安装 通过PIP方式就能迅速安装 pyDAL pip install pydal 二、PIP安装的常见问题 1、PIP命令无效的排查处理办法 &#xff08;1&#xff09;检查是否 启用pip python -m ensurepip --default-pip &#xff08;2&#xff09;升级 pip版本 python -m pip inst…

web3方向产品调研

每次互联网形态的改变&#xff0c;都会对世界产生很大的影响&#xff0c;上一次对社会产生重大影响的互联网形态&#xff08;Web2.0&#xff09;催生了一批改变人类生活和信息交互方式的企业。 目录 概述DAO是什么&#xff1f;为什么我们需要DAO? 金融服务金融桥接及周边服务D…

Linux下MQTT环境的简单应用及搭建——之Mosquitto

文章目录 前言一、ubuntu搭建mqtt服务器 | 概要二、整体架构流程 | 技术实现细节1、下载源码2、安装Mosquitto3、解压并修改配置文件4、关于Mosquitto常见的一些操作指令5、启动mosquitto6、测试mosquitto测试1&#xff1a;Linux多终端交互测试测试2&#xff1a;Linux与Windows…

一款降压型开关模式转换器解决方案

一、基本概述 TX4145 是一款降压型开关模式转换器。TX4145 在 6-60V 宽输入电源范围内实现不同峰值输出电流&#xff0c;并且具有出色的线电压和负载调整率。 TX4145 采用 PWM 电流模工作模式&#xff0c;环路易于稳定并提供快速的瞬态响应。 TX4145 外部提供 FS 脚&#xf…

Flink Kafka[输入/输出] Connector

本章重点介绍生产环境中最常用到的Flink kafka connector。使用Flink的同学&#xff0c;一定会很熟悉kafka&#xff0c;它是一个分布式的、分区的、多副本的、 支持高吞吐的、发布订阅消息系统。生产环境环境中也经常会跟kafka进行一些数据的交换&#xff0c;比如利用kafka con…

如何在Mac中设置三指拖移,这里有详细步骤

三指拖移手势允许你选择文本&#xff0c;或通过在触控板上用三指拖动窗口或任何其他元素来移动它。它可以用于快速移动或调整窗口、文件或图像在屏幕上的位置。 然而&#xff0c;这个手势在默认情况下是禁用的&#xff0c;因此在本教程中&#xff0c;我们将向你展示如何在你的…

算法导论复习(七) 动态规划

动态规划一般用来求解最优化问题 设计一个动态规划算法一般有以下四步&#xff1a; 描述一个最优解的结构特征。递归地定义最优解的值。计算最优解的值&#xff0c;通常采用自底向上的方法。利用计算出的信息构造出一个最优解。 钢条切割问题 体现了动态规划的一个重要性质&a…

K8S结合Prometheus构建监控系统

一、Prometheus简介 Prometheus 是一个开源的系统监控和警报工具&#xff0c;用于收集、存储和查询时间序列数据。它专注于监控应用程序和基础设施的性能和状态&#xff0c;并提供丰富的查询语言和灵活的告警机制1、Prometheus基本介绍 数据模型&#xff1a;Prometheus 使用时…