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

uni-app 封装图表功能

文章目录

    • 需求
    • 分析
      • 1. 秋云 uchars
      • 2. Echarts

需求

在 uni-app 中使用图表功能,两种推荐的图表工具

分析

在 Dcloud市场 搜索Echarts关键词,会出现几款图表工具,通过大家的下载量,可以看到秋云这个库是比较受欢迎的,其次是Echarts

在这里插入图片描述

1. 秋云 uchars

我们先来说说 秋云 这个工具库,我们点击下载进行导入项目中,接下来我们看一下平台的兼容性

在这里插入图片描述

  1. 效果
    在这里插入图片描述

  2. 封装

<template><view class="charts-box"><qiun-data-charts :type="option.type" :opts="option.opts" :chartData="option.chartData" /></view>
</template><script setup>import {ref} from 'vue'import {onLoad,onUnload,onReachBottom,onShareAppMessage,onShareTimeline} from "@dcloudio/uni-app"defineProps({option: {type: Object,default () {return {type:'column',//column、lineopts:{color: ["#1890FF","#91CB74","#FAC858","#EE6666","#73C0DE","#3CA272","#FC8452","#9A60B4","#ea7ccc"],padding: [15,30,0,5],enableScroll: false,legend: {},xAxis: {boundaryGap: "justify",disableGrid: false,min: 0,axisLine: false,max: 70},yAxis: {},extra: {bar: {type: "stack",width: 30,meterBorde: 1,meterFillColor: "#FFFFFF",activeBgColor: "#000000",activeBgOpacity: 0.08,categoryGap: 2}}},chartData:{categories: ["2016","2017","2018","2019","2020","2021"],series: [{name: "目标值",data: [35,36,31,33,13,34]},{name: "完成量",data: [18,27,21,24,6,28]}]},}}}})const chartData = ref([])const opts = ref()onLoad((e) => {let res = {};chartData.value = JSON.parse(JSON.stringify(res));})
</script><style lang="scss" scoped>.charts-box {width: 100%;height: 100%;}
</style>
  1. 使用
<template><view class="homeLayout pageBg"><!-- #ifndef MP-TOUTIAO --><custom-nav-bar title="图表"></custom-nav-bar><!-- #endif --><view class="select"><common-title><template #name>折线图</template><template #custom><view class="date"><uni-icons type="calendar" size="18"></uni-icons><view class="text"><uni-dateformat :date="Date.now()" format="dd日"></uni-dateformat></view></view></template></common-title><view class="content"><echarts></echarts></view></view></view>
</template><script setup>import echarts from '@/components/echarts/qiuyun-echarts.vue'import {ref} from 'vue'function echartsClick(params) {console.log('点击数据', params)}
</script><style lang="scss" scoped>.homeLayout {background:linear-gradient(to bottom, transparent, #fff 400rpx),linear-gradient(to right, #beecd8 20%, #F4E2D8);min-height: 80vh;.banner {width: 750rpx;padding: 30rpx 0;swiper {width: 690rpx;height: 340rpx;margin: 0 auto;&-item {// width: 100%;height: 100%;padding: 0;.like {width: 100%;height: 100%;image {width: 100%;height: 100%;border-radius: 10rpx;}}}}}.notice {width: 690rpx;height: 80rpx;line-height: 80rpx;background: #f9f9f9;margin: 0 auto;border-radius: 80rpx;display: flex;.left {width: 140rpx;display: flex;align-items: center;justify-content: center;:deep() {.uni-icons {color: $brand-theme-color !important;}}.text {color: $brand-theme-color;font-weight: 600;font-size: 28rpx;}}.center {flex: 1;swiper {height: 100%;&-item {height: 100%;font-size: 30rpx;color: #666;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;}}}.right {width: 70rpx;display: flex;align-items: center;justify-content: center;}}.select {padding-top: 50rpx;.date {color: $brand-theme-color;display: flex;align-items: center;:deep() {.uni-icons {color: $brand-theme-color !important;}}.text {margin-left: 5rpx;}}.content {width: 720rpx;margin-left: 30rpx;margin-top: 30rpx;scroll-view {white-space: nowrap;.box {width: 200rpx;height: 430rpx;display: inline-block;margin-right: 15rpx;image {width: 100%;height: 100%;border-radius: 10rpx;}}.box:last-child {margin-right: 30rpx;}}}}.theme {padding: 50rpx 0;.more {font-size: 32rpx;color: #888;}.content {margin-top: 30rpx;padding: 0 30rpx;display: grid;gap: 15rpx;grid-template-columns: repeat(3, 1fr);}}}
</style>

2. Echarts

接下来看看 Echarts,随着图表的功能使用日渐普遍。接下来我们看一下 Echarts 的平台兼容性

在这里插入图片描述

  1. 效果
    在这里插入图片描述

  2. 导入:main.js文件中全局导入或大家觉得局部导入好就用局部导入

import * as echarts from '@/uni_modules/lime-echart/static/echarts.min'
  1. 封装
<template><view class="charts-box"><view style="width:700rpx; height:750rpx"><l-echart ref="chartRef"></l-echart></view></view>
</template><script setup>import {ref,watch,watchEffect} from 'vue'import {onLoad,onUnload,onReachBottom,onShareAppMessage,onShareTimeline} from "@dcloudio/uni-app"const props = defineProps({option: {type: Object,default () {return {}}}})const chartRef = ref(null)watchEffect(()=>{// 组件能被调用必须是组件的节点已经被渲染到页面上const option = props.optionsetTimeout(async()=>{if(!chartRef.value) returnconst myChart = await chartRef.value.init(echarts)myChart.setOption(option)},300)})onLoad( ()=>{})</script><style lang="scss" scoped>.charts-box {width: 100%;height: 100%;}
</style>
  1. 使用
<template><view class="homeLayout pageBg"><!-- #ifndef MP-TOUTIAO --><custom-nav-bar title="图表"></custom-nav-bar><!-- #endif --><view class="select"><common-title><template #name>折线图</template><template #custom><view class="date"><uni-icons type="calendar" size="18"></uni-icons><view class="text"><uni-dateformat :date="Date.now()" format="dd日"></uni-dateformat></view></view></template></common-title><view class="content"><echarts :option="option"></echarts></view></view></view>
</template><script setup>import echarts from '@/components/echarts/chart-echarts.vue'import {ref} from 'vue'const option = {tooltip: {trigger: 'axis',axisPointer: {type: 'shadow'},confine: true},legend: {data: ['热度', '正面', '负面']},grid: {left: 8,right: 20,bottom: 15,top: 40,containLabel: true},xAxis: [{type: 'value',axisLine: {lineStyle: {color: '#999999'}},axisLabel: {color: '#666666'}}],yAxis: [{type: 'category',axisTick: {show: false},data: ['汽车之家', '今日头条', '百度贴吧', '一点资讯', '微信', '微博', '知乎'],axisLine: {lineStyle: {color: '#999999'}},axisLabel: {color: '#666666'}}],series: [{name: '热度',type: 'bar',label: {normal: {show: true,position: 'inside'}},data: [300, 270, 340, 344, 300, 320, 310],},{name: '正面',type: 'bar',stack: '总量',label: {normal: {show: true}},data: [120, 102, 141, 174, 190, 250, 220]},{name: '负面',type: 'bar',stack: '总量',label: {normal: {show: true,position: 'left'}},data: [-20, -32, -21, -34, -90, -130, -110]}]};function echartsClick(params) {console.log('点击数据', params)}
</script><style lang="scss" scoped>.homeLayout {background:linear-gradient(to bottom, transparent, #fff 400rpx),linear-gradient(to right, #beecd8 20%, #F4E2D8);min-height: 80vh;.banner {width: 750rpx;padding: 30rpx 0;swiper {width: 690rpx;height: 340rpx;margin: 0 auto;&-item {// width: 100%;height: 100%;padding: 0;.like {width: 100%;height: 100%;image {width: 100%;height: 100%;border-radius: 10rpx;}}}}}.notice {width: 690rpx;height: 80rpx;line-height: 80rpx;background: #f9f9f9;margin: 0 auto;border-radius: 80rpx;display: flex;.left {width: 140rpx;display: flex;align-items: center;justify-content: center;:deep() {.uni-icons {color: $brand-theme-color !important;}}.text {color: $brand-theme-color;font-weight: 600;font-size: 28rpx;}}.center {flex: 1;swiper {height: 100%;&-item {height: 100%;font-size: 30rpx;color: #666;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;}}}.right {width: 70rpx;display: flex;align-items: center;justify-content: center;}}.select {padding-top: 50rpx;.date {color: $brand-theme-color;display: flex;align-items: center;:deep() {.uni-icons {color: $brand-theme-color !important;}}.text {margin-left: 5rpx;}}.content {width: 720rpx;margin-left: 30rpx;margin-top: 30rpx;scroll-view {white-space: nowrap;.box {width: 200rpx;height: 430rpx;display: inline-block;margin-right: 15rpx;image {width: 100%;height: 100%;border-radius: 10rpx;}}.box:last-child {margin-right: 30rpx;}}}}.theme {padding: 50rpx 0;.more {font-size: 32rpx;color: #888;}.content {margin-top: 30rpx;padding: 0 30rpx;display: grid;gap: 15rpx;grid-template-columns: repeat(3, 1fr);}}}
</style>

相关文章:

uni-app 封装图表功能

文章目录 需求分析1. 秋云 uchars2. Echarts 需求 在 uni-app 中使用图表功能&#xff0c;两种推荐的图表工具 分析 在 Dcloud市场 搜索Echarts关键词&#xff0c;会出现几款图表工具&#xff0c;通过大家的下载量&#xff0c;可以看到秋云这个库是比较受欢迎的&#xff0c;其…...

Kubernetes的基本构建块和最小可调度单元pod-0

文章目录 一&#xff0c;什么是pod1.1pod在k8s中使用方法&#xff08;1&#xff09;使用方法一&#xff08;2&#xff09;使用方法二 1.2pod中容器的进程1.3pod的网络隔离管理&#xff08;1&#xff09;pause容器的作用 1.4 Pod分类&#xff1a;&#xff08;1&#xff09;自主式…...

QT创建按钮篇

QT创建按钮篇 1.概述 这篇文章从创建一个按钮对QT开发流程熟悉。 2.代码 #include "mywidget.h" #include <QPushButton>myWidget::myWidget(QWidget *parent): QWidget(parent) { // 第一种创建按钮方式 // QPushButton *btn new QPushButton(); /…...

初级软件测试工程师就别出口喊15K了,连自动化测试都不会,还不如应届生

一. 为什么学软件测试 零基础转行&#xff0c;为什么首选软件测试&#xff1f; 软件测试是软件开发的重要过程之一&#xff0c;是软件质量的保证。国外信息技术领域软件开发人员与测试人员的比例是1:1&#xff0c;而国内目前专业软件测试人员很少&#xff0c;属于紧缺型人才&a…...

Mybatis查询数据库,返回List集合,集合元素也是List。

#有时间需求会要求&#xff1a;查询全校的学生数据&#xff0c;且学生数据按班级划分。那么就需要List<List<user>>类型的数据。 SQL语句 SELECT JSON_ARRAYAGG(JSON_OBJECT(name , name ,BJMC, BJMC ,BJBH,BJBH)) as dev_user FROM dev_user WHERE project_id …...

SQL 视图:概念、应用与最佳实践

SQL 视图&#xff1a;概念、应用与最佳实践 SQL&#xff08;Structured Query Language&#xff09;视图是数据库管理中的一个重要概念&#xff0c;它允许用户以虚拟表的形式查看数据。视图在数据库中并不实际存储数据&#xff0c;而是提供了一个查询结果的快照&#xff0c;这…...

ubuntu交叉编译expat库给arm平台使用

1.下载expat库源码: https://github.com/libexpat/libexpat/release?page=2 wget https://github.com/libexpat/libexpat/release/download/R_2_3_0/expat-2.3.0.tar.bz2 下载成功: 2.解压expat库,并进入解压后的目录: tar xjf expat-2.3.0.tar.bz2 cd expat-2.3.0 <…...

成都郝蓉宜恺文化传媒有限公司以诚信经营赢得客户长期信赖

成都郝蓉宜恺文化传媒有限公司秉承深厚的企业文化和价值观&#xff0c;其中“以诚信经营为本”是其核心理念之一。以下是对该公司如何以诚信经营为基础&#xff0c;赢得客户长期信赖的几点客观分析&#xff1a; 1.建立信任基石&#xff1a;在商业领域&#xff0c;信任是客户与企…...

LabVIEW for Linux 介绍

LabVIEW for Linux 介绍 1. 兼容性 LabVIEW for Linux 设计用于多种 Linux 发行版&#xff0c;包括 CentOS、Ubuntu 等。在安装之前&#xff0c;务必检查与您特定发行版版本的兼容性。 2. 程序移植 可移植性&#xff1a;在许多情况下&#xff0c;LabVIEW 程序&#xff08;VI…...

一次32bit有符号数据类型转换为64bit无符号数据类型引发的溢出错误

现象&#xff1a; 在调试一款sensor&#xff0c;通过10帧->8帧->6帧,这样不断的降低帧率调试低照度下的图像效果。ISP配置文件上设置的最大曝光曝光参数为&#xff1a; EXP&#xff1a;15266 Again:15494 Dgain:714 ISPDGain:1360。 当达到最低帧率最低亮度时&#x…...

aosp安卓15新特性dump的wms窗口层级树优化的更加美观

背景&#xff1a; 近来在体验调试aosp15时候&#xff0c;使用了dumpsys activity containers时候&#xff0c;发现wms层级结构树有一个巨大的变化。 很多学员朋友对这个优化改进都给出巨大的点赞&#xff0c;有的学员朋友还想老版本自己实现一下这种树绘制&#xff1a; 对比…...

git的使用、router和route的区别以及v-show和v-if的差别

这里写目录标题 多人协作使用git的步骤&#xff08;使用gitub&#xff09;建立自己的空仓库连接远程仓库使伙伴可以使用仓库将代码拉入空仓库进行git指令的学习 router和route的区别router定义&#xff1a;用途&#xff1a; route定义&#xff1a;用途&#xff1a; v-show和v-i…...

Win系统通过命令行查看笔记本电池损耗/寿命/健康

在 Windows 10/11 系统中&#xff0c;可以通过指令查看笔记本电池的寿命情况&#xff0c;方法如下&#xff1a; 0&#xff0c;打开cmd/终端 键盘快捷键&#xff1a;Win R&#xff0c;然后输入cmd&#xff0c;点击【确定】 1&#xff0c;执行命令 在命令行中输入下面指令并按…...

【安当产品应用案例100集】029-使用安全芯片保护设备核心业务逻辑

我国工业企业普遍缺乏数据安全意识&#xff0c;对数据安全保护缺乏基本认识。这导致企业在数据安全方面的投入不足&#xff0c;保护能力基本不具备&#xff0c;难以有效应对数据安全风险。不过随着安全事件越来越多&#xff0c;很多工业企业的安全意识也越来越高&#xff0c;在…...

Redis高级篇之缓存一致性详细教程

文章目录 0 前言1.缓存双写一致性的理解1.1 缓存按照操作来分 2. 数据库和缓存一致性的几种更新策略2.1 可以停机的情况2.2 我们讨论4种更新策略2.3 解决方案 总结 0 前言 缓存一致性问题在工作中绝对没办法回避的问题&#xff0c;比如&#xff1a;在实际开发过程中&#xff0c…...

C++ 文件操作详解

C 文件操作详解 在C中&#xff0c;文件操作分为文本文件和二进制文件的操作&#xff0c;通过文件流类&#xff08;ifstream、ofstream、fstream&#xff09;进行文件的读写。这些类封装了文件的输入和输出操作&#xff0c;并继承了istream和ostream的功能&#xff0c;使得流对…...

计算机网络:网络层 —— 边界网关协议 BGP

文章目录 路由选择协议动态路由协议边界网关协议 BGPBGP 的基本概念BGP-4 的四种报文 路由选择协议 因特网是全球最大的互联网&#xff0c;它所采取的路由选择协议具有以下三个主要特点&#xff1a; 自适应&#xff1a;因特网采用动态路由选择&#xff0c;能较好地适应网络状态…...

Python数据可视化seaborn

产品经理在做数据分析时可能需要通过可视化来分析。seaborn官网 1. relplot 散点图 https://seaborn.pydata.org/examples/scatterplot_sizes.html import pandas as pd import seaborn as sns df pd.DataFrame({x: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],y: [8, 6, 7, 8, 4, 6,…...

Java继承练习

构建Person类&#xff08;属性&#xff1a;名字、年龄、工作岗位&#xff09;&#xff0c;创建三个对象&#xff0c;并且根据对象的年龄或名字长度来进行冒泡排序 package chapter08.homework.test01;public class homework01 {public static void main(String[] args) {Perso…...

Excel怎么转换成word?分享两种方法!

Excel文件想要转换成word文档中&#xff0c;直接粘贴复制的话&#xff0c;可能会导致表格格式错乱&#xff0c;那么如何转换才能够保证表格不错乱&#xff1f;今天分享两个方法&#xff0c;excel表格转换为word文件。 方法一&#xff1a; 首先打开excel表格&#xff0c;将表格…...

XCTF-web-easyupload

试了试php&#xff0c;php7&#xff0c;pht&#xff0c;phtml等&#xff0c;都没有用 尝试.user.ini 抓包修改将.user.ini修改为jpg图片 在上传一个123.jpg 用蚁剑连接&#xff0c;得到flag...

【力扣数据库知识手册笔记】索引

索引 索引的优缺点 优点1. 通过创建唯一性索引&#xff0c;可以保证数据库表中每一行数据的唯一性。2. 可以加快数据的检索速度&#xff08;创建索引的主要原因&#xff09;。3. 可以加速表和表之间的连接&#xff0c;实现数据的参考完整性。4. 可以在查询过程中&#xff0c;…...

Matlab | matlab常用命令总结

常用命令 一、 基础操作与环境二、 矩阵与数组操作(核心)三、 绘图与可视化四、 编程与控制流五、 符号计算 (Symbolic Math Toolbox)六、 文件与数据 I/O七、 常用函数类别重要提示这是一份 MATLAB 常用命令和功能的总结,涵盖了基础操作、矩阵运算、绘图、编程和文件处理等…...

NFT模式:数字资产确权与链游经济系统构建

NFT模式&#xff1a;数字资产确权与链游经济系统构建 ——从技术架构到可持续生态的范式革命 一、确权技术革新&#xff1a;构建可信数字资产基石 1. 区块链底层架构的进化 跨链互操作协议&#xff1a;基于LayerZero协议实现以太坊、Solana等公链资产互通&#xff0c;通过零知…...

AI书签管理工具开发全记录(十九):嵌入资源处理

1.前言 &#x1f4dd; 在上一篇文章中&#xff0c;我们完成了书签的导入导出功能。本篇文章我们研究如何处理嵌入资源&#xff0c;方便后续将资源打包到一个可执行文件中。 2.embed介绍 &#x1f3af; Go 1.16 引入了革命性的 embed 包&#xff0c;彻底改变了静态资源管理的…...

【SSH疑难排查】轻松解决新版OpenSSH连接旧服务器的“no matching...“系列算法协商失败问题

【SSH疑难排查】轻松解决新版OpenSSH连接旧服务器的"no matching..."系列算法协商失败问题 摘要&#xff1a; 近期&#xff0c;在使用较新版本的OpenSSH客户端连接老旧SSH服务器时&#xff0c;会遇到 "no matching key exchange method found"​, "n…...

【JVM面试篇】高频八股汇总——类加载和类加载器

目录 1. 讲一下类加载过程&#xff1f; 2. Java创建对象的过程&#xff1f; 3. 对象的生命周期&#xff1f; 4. 类加载器有哪些&#xff1f; 5. 双亲委派模型的作用&#xff08;好处&#xff09;&#xff1f; 6. 讲一下类的加载和双亲委派原则&#xff1f; 7. 双亲委派模…...

比较数据迁移后MySQL数据库和OceanBase数据仓库中的表

设计一个MySQL数据库和OceanBase数据仓库的表数据比较的详细程序流程,两张表是相同的结构,都有整型主键id字段,需要每次从数据库分批取得2000条数据,用于比较,比较操作的同时可以再取2000条数据,等上一次比较完成之后,开始比较,直到比较完所有的数据。比较操作需要比较…...

从面试角度回答Android中ContentProvider启动原理

Android中ContentProvider原理的面试角度解析&#xff0c;分为​​已启动​​和​​未启动​​两种场景&#xff1a; 一、ContentProvider已启动的情况 1. ​​核心流程​​ ​​触发条件​​&#xff1a;当其他组件&#xff08;如Activity、Service&#xff09;通过ContentR…...

android RelativeLayout布局

<?xml version"1.0" encoding"utf-8"?> <RelativeLayout xmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_parent"android:layout_height"match_parent"android:gravity&…...