Vue3统计数值(Statistic)

可自定义设置以下属性:

  • 数值的标题(title),类型:string | slot,默认:‘’
  • 数值的内容(value),类型:string | number,默认:‘’
  • 设置数值的样式(valueStyle),类型:CSSProperties,默认:{}
  • 数值精度(precision),类型:number,默认:0
  • 设置数值的前缀(prefix),类型:string | slot,默认:‘’
  • 设置数值的后缀(suffix),类型:string | slot,默认:‘’
  • 设置千分位标识符(separator),类型:string,默认:‘,’
  • 自定义数值展示(formatter),类型:Function,默认:(value: string) => value

效果如下图:在线预览

在这里插入图片描述
在这里插入图片描述

创建统计数值组件Statistic.vue

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import type { CSSProperties } from 'vue'
import { moneyFormat } from '../index'
interface Props {
  title?: string // 数值的标题 string | slot
  value?: string|number // 数值的内容
  valueStyle?: CSSProperties // 设置数值的样式
  precision?: number //	数值精度
  prefix?: string // 设置数值的前缀 string | slot
  suffix?: string // 设置数值的后缀 string | slot
  separator?: string // 设置千分位标识符
  formatter?: Function // 自定义数值展示
}
const props = withDefaults(defineProps<Props>(), {
  title: '',
  value: '',
  valueStyle: () => { return {} },
  precision: 0,
  prefix: '',
  suffix: '',
  separator: ',',
  formatter: (value: string) => value
})
const showValue = computed(() => {
  return props.formatter(moneyFormat(props.value, props.precision, props.separator))
})
const prefixRef = ref() // 声明一个同名的模板引用
const showPrefix = ref(1)
const suffixRef = ref() // 声明一个同名的模板引用
const showSuffix = ref(1)
onMounted(() => {
  showPrefix.value = prefixRef.value.offsetHeight
  showSuffix.value = suffixRef.value.offsetHeight
})
</script>
<template>
  <div class="m-statistic">
    <div class="u-title">
      <slot name="title">{{ title }}</slot>
    </div>
    <div class="m-content" :style="valueStyle">
      <span ref="prefixRef" class="u-prefix" v-if="showPrefix">
        <slot name="prefix">{{ prefix }}</slot>
      </span>
      <span class="u-content-value">{{ showValue }}</span>
      <span ref="suffixRef" class="u-suffix" v-if="showSuffix">
        <slot name="suffix">{{ suffix }}</slot>
      </span>
    </div>
  </div>
</template>
<style lang="less" scoped>
.m-statistic {
  font-size: 14px;
  color: rgba(0, 0, 0, 0.88);
  line-height: 1.5714285714285714;
  .u-title {
    margin-bottom: 4px;
    color: rgba(0, 0, 0, 0.45);
    font-size: 14px;
  }
  .m-content {
    color: rgba(0, 0, 0, 0.88);
    font-size: 24px;
    .u-prefix {
      display: inline-block;
      margin-inline-end: 4px;
    }
    .u-content-value {
      display: inline-block;
      direction: ltr;
    }
    .u-suffix {
      display: inline-block;
      margin-inline-start: 4px;
    }
  }
}
</style>

在要使用的页面引入

其中引入使用了 Vue3栅格(Grid) Vue3卡片(Card)

<script setup lang="ts">
import Statistic from './Statistic.vue'
function formatter (value: string): string {
  console.log('value', value)
  return '1年有 ' + value + ' 天'
}
</script>
<template>
  <div>
    <h1>Statistic 统计数值</h1>
    <h2 class="mt30 mb10">基本使用</h2>
    <Row>
      <Col :span="12">
        <Statistic title="Active Users" :value="112893" />
      </Col>
      <Col :span="12">
        <Statistic title="Account Balance (CNY)" :precision="2" :value="112893" />
      </Col>
    </Row>
    <h2 class="mt30 mb10">添加前缀和后缀</h2>
    <Row :gutter="16">
      <Col :span="12">
        <Statistic title="Feedback" :value="1128">
          <template #suffix>
            <svg focusable="false" class="u-svg" data-icon="like" width="1em" height="1em" fill="currentColor" aria-hidden="true" viewBox="64 64 896 896"><path d="M885.9 533.7c16.8-22.2 26.1-49.4 26.1-77.7 0-44.9-25.1-87.4-65.5-111.1a67.67 67.67 0 00-34.3-9.3H572.4l6-122.9c1.4-29.7-9.1-57.9-29.5-79.4A106.62 106.62 0 00471 99.9c-52 0-98 35-111.8 85.1l-85.9 311H144c-17.7 0-32 14.3-32 32v364c0 17.7 14.3 32 32 32h601.3c9.2 0 18.2-1.8 26.5-5.4 47.6-20.3 78.3-66.8 78.3-118.4 0-12.6-1.8-25-5.4-37 16.8-22.2 26.1-49.4 26.1-77.7 0-12.6-1.8-25-5.4-37 16.8-22.2 26.1-49.4 26.1-77.7-.2-12.6-2-25.1-5.6-37.1zM184 852V568h81v284h-81zm636.4-353l-21.9 19 13.9 25.4a56.2 56.2 0 016.9 27.3c0 16.5-7.2 32.2-19.6 43l-21.9 19 13.9 25.4a56.2 56.2 0 016.9 27.3c0 16.5-7.2 32.2-19.6 43l-21.9 19 13.9 25.4a56.2 56.2 0 016.9 27.3c0 22.4-13.2 42.6-33.6 51.8H329V564.8l99.5-360.5a44.1 44.1 0 0142.2-32.3c7.6 0 15.1 2.2 21.1 6.7 9.9 7.4 15.2 18.6 14.6 30.5l-9.6 198.4h314.4C829 418.5 840 436.9 840 456c0 16.5-7.2 32.1-19.6 43z"></path></svg>
          </template>
        </Statistic>
      </Col>
      <Col :span="12">
        <Statistic title="Unmerged" :value="90">
          <template #suffix>
            <span>%</span>
          </template>
        </Statistic>
      </Col>
    </Row>
    <h2 class="mt30 mb10">在卡片中使用</h2>
    <div style="width: 425px; background: #ececec; padding: 30px">
      <Row :gutter="16">
        <Col :span="12">
          <Card>
            <Statistic
              title="Feedback"
              :value="11.28"
              :precision="2"
              suffix="%"
              :value-style="{ color: '#3f8600' }"
              style="margin-right: 50px"
            >
              <template #prefix>
                <svg focusable="false" class="u-svg" data-icon="arrow-up" width="1em" height="1em" fill="currentColor" aria-hidden="true" viewBox="64 64 896 896"><path d="M868 545.5L536.1 163a31.96 31.96 0 00-48.3 0L156 545.5a7.97 7.97 0 006 13.2h81c4.6 0 9-2 12.1-5.5L474 300.9V864c0 4.4 3.6 8 8 8h60c4.4 0 8-3.6 8-8V300.9l218.9 252.3c3 3.5 7.4 5.5 12.1 5.5h81c6.8 0 10.5-8 6-13.2z"></path></svg>
              </template>
            </Statistic>
          </Card>
        </Col>
        <Col :span="12">
          <Card>
            <Statistic
              title="Idle"
              :value="9.3"
              :precision="2"
              suffix="%"
              class="demo-class"
              :value-style="{ color: '#cf1322' }"
            >
              <template #prefix>
                <svg focusable="false" class="u-svg" data-icon="arrow-down" width="1em" height="1em" fill="currentColor" aria-hidden="true" viewBox="64 64 896 896"><path d="M862 465.3h-81c-4.6 0-9 2-12.1 5.5L550 723.1V160c0-4.4-3.6-8-8-8h-60c-4.4 0-8 3.6-8 8v563.1L255.1 470.8c-3-3.5-7.4-5.5-12.1-5.5h-81c-6.8 0-10.5 8.1-6 13.2L487.9 861a31.96 31.96 0 0048.3 0L868 478.5c4.5-5.2.8-13.2-6-13.2z"></path></svg>
              </template>
            </Statistic>
          </Card>
        </Col>
      </Row>
    </div>
    <h2 class="mt30 mb10">自定义数值样式</h2>
    <Statistic
      title="Worth"
      :value="1600000"
      :value-style="{ color: '#3f8600' }"
      prefix="$"
      suffix="/ 天" />
    <h2 class="mt30 mb10">设置数值精度</h2>
    <Statistic
      title="Precision"
      :value="100000000.1"
      :precision="2" />
    <h2 class="mt30 mb10">自定义千分位标识符</h2>
    <Statistic
      title="Precision"
      :value="100000000.1"
      separator=";"
      :precision="3" />
    <h2 class="mt30 mb10">自定义数值展示</h2>
    <Statistic
      title="Formatter"
      :value="365"
      :value-style="{ color: '#1677ff' }"
      :formatter="formatter" />
  </div>
</template>
<style lang="less" scoped>
.u-svg {
  display: inline-flex;
  align-items: center;
  color: inherit;
  text-align: center;
  vertical-align: -0.125em;
}
</style>

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【Python】数据可视化利器PyCharts在测试工作中的应用

点击跳转原文&#xff1a;【Python】数据可视化利器PyCharts在测试工作中的应用 实际应用&#xff1a;常态化性能压测数据统计 import random from pyecharts.charts import Line, Bar, Grid, Pie, Page from pyecharts import options as opts # 查询过去 8 次数据 time_rang…

Low-Light Image Enhancement via Self-Reinforced Retinex Projection Model 论文阅读笔记

这是马龙博士2022年在TMM期刊发表的基于改进的retinex方法去做暗图增强&#xff08;非深度学习&#xff09;的一篇论文 文章用一张图展示了其动机&#xff0c;第一行是估计的亮度层&#xff0c;第二列是通常的retinex方法会对估计的亮度层进行RTV约束优化&#xff0c;从而产生…

NFTScan 与 Decert 达成合作伙伴,双方在 NFT 数据方面展开合作

近日&#xff0c;NFT 数据基础设施 NFTScan 与 Decert 达成合作伙伴关系&#xff0c;双方在多链 NFT 数据层面展开合作。在 Decert 产品中&#xff0c;由 NFTScan 为其提供专业的多链 NFT 数据支持&#xff0c;为用户带来优质的 NFT 搜索查询等相关交互功能&#xff0c;提升用户…

事务@transactional执行产生重复数据

背景 系统设计之初&#xff0c;每次来新请求&#xff0c;业务层会先查询数据库&#xff0c;判断是否存在相同的id数据&#xff08;id是唯一标识产品的&#xff09;&#xff0c;有则返回当前数据库查到的数据&#xff0c;根据数据决定下一步动作&#xff0c;没有则认为是初次请…

一则 MySQL 参数设置不当导致复制中断的故障案例

本文分享了一个数据库参数错误配置导致复制中断的问题&#xff0c;以及对参数配置的建议。 作者&#xff1a;秦福朗 爱可生 DBA 团队成员&#xff0c;负责项目日常问题处理及公司平台问题排查。热爱互联网&#xff0c;会摄影、懂厨艺&#xff0c;不会厨艺的 DBA 不是好司机&…

wsl2中安装docker

1、安装docker 执行以下脚本&#xff1a; 这个脚本在执行之前需要先执行chmod x install-docker.sh这个命令 # install docker curl -fsSL get.docker.com -o get-docker.sh sh get-docker.shif [ ! $(getent group docker) ]; thensudo groupadd docker; elseecho "doc…

UE4/5AI制作基础AI跳跃(适合新手)

目录 制作 添加逻辑 添加导航链接代理 结果 在上一章中&#xff0c;我们讲解了简单的AI跟随玩家&#xff0c;制作了一个基础的ai。 UE4/5AI制作基础AI&#xff08;适合新手入门&#xff0c;运用黑板&#xff0c;行为树&#xff0c;ai控制器&#xff0c;角色类&#xff0c;任…

数据库应用:CentOS 7离线安装PostgreSQL

目录 一、理论 1.PostgreSQL 2.PostgreSQL离线安装 3.PostgreSQL初始化 4.PostgreSQL登录操作 二、实验 1.CentOS 7离线安装PostgreSQL 2.登录PostgreSQL 3.Navicat连接PostgreSQL 三、总结 一、理论 1.PostgreSQL &#xff08;1&#xff09;简介 PostgreSQL 是一个…

性能测试工具 jmeter 录制脚本,传递 cookie,循环执行接口

目录 前言&#xff1a; 代理录制脚本 循环重复添加接口 登录并传递 cookie 给新建产品接口 循环执行脚本 前言&#xff1a; 在使用JMeter进行性能测试时&#xff0c;录制脚本是一种常用的方法。录制脚本可以帮助你捕获和重放用户与应用程序之间的交互&#xff0c;以模拟真…

matlab中画有重影的机器人运动过程【给另一个机器人设置透明度】

1、前言如题 2、参考连接如下 How to plot two moving robot in the same figure and change one of them transparency&#xff1f; - MATLAB Answers - MATLAB Central (mathworks.cn)3、代码&#xff1a;【找到figure中对应对象并设置属性】 % Create two instances of a…

什么是70v转12v芯片?

问&#xff1a;什么是70v转12v芯片&#xff1f; 答&#xff1a;70v转12v芯片是一种电子器件&#xff0c;其功能是将输入电压范围在9v至100v之间的电源转换为稳定的12v输出电压。这种芯片通常被用于充电器、车载电池充电器和电源适配器等设备中。 问&#xff1a;这种芯片的最大…

微信小程序使用字体图标——链接引入

目录 1.下载字体图标 1.选择需要的图标加入购物车添加到项目 2.查看项目 3.生成在线链接 4.复制生成的链接 等下放到iconfont.json中​编辑 2.引入链接 1.下载 2.生成iconfont.json文件 3. 在iconfont.json中 放入生成的链接 4.需要重新编译小程序之后在终端执行 5…

03 QT对象树

Tips: QT通过对象树机制&#xff0c;能够自动、有效的组织和管理继承自QObject的Qt对象&#xff0c;不需要用户手动回收资源&#xff0c;系统自动调用析构函数。 验证对象树功能&#xff1a; 新建C文件 继承自QPushButton&#xff0c;但没有QPushButton&#xff0c;但有其父类…

【雕爷学编程】Arduino动手做(164)---Futaba S3003舵机模块3

37款传感器与模块的提法&#xff0c;在网络上广泛流传&#xff0c;其实Arduino能够兼容的传感器模块肯定是不止37种的。鉴于本人手头积累了一些传感器和执行器模块&#xff0c;依照实践出真知&#xff08;一定要动手做&#xff09;的理念&#xff0c;以学习和交流为目的&#x…

GPT与人类:人工智能是否能够真正复制人类语言?

人类语言是一种复杂的系统&#xff0c;它不仅包含着无数单词和语法规则&#xff0c;更重要的是具有丰富的含义和上下文。这些语言特征涉及到常识、文化、情感和经验等方面&#xff0c;是人类在长期进化和文明发展中所积累起来的丰富知识和经验的体现。然而&#xff0c;人工智能…

微服务Day3——Nacos配置管理\Feign远程调用\Gateway网关

一、Nacos配置管理 1、统一配置管理 当微服务部署的实例越来越多&#xff0c;达到数十、数百时&#xff0c;逐个修改微服务配置就会让人抓狂&#xff0c;而且很容易出错。我们需要一种统一配置管理方案&#xff0c;可以集中管理所有实例的配置。 Nacos一方面可以将配置集中管理…

基于阿里云微信小程序语音识别

页面效果 其中采用阿里云语音识别&#xff1a;阿里云一句话语音识别 语音识别页面 <template><view><view class"chat_list"><view v-for"v in chatList" :class"v.type right ? type_right : type_left"><chat …

redis数据类型和数据结构你了解吗 学习总结篇!

大家好&#xff0c;我是三叔&#xff0c;很高兴这期又和大家见面了&#xff0c;一个奋斗在互联网的打工人。 这期给大家讲一下关于 Redis 数据类型和数据结构的区别&#xff0c;很多读者包括笔者自己&#xff0c;早期也是傻傻分不清。备注&#xff1a;部分图片借鉴小林哥&…

Appium 安卓环境的配置

目录 前言&#xff1a; 环境准备 写个脚本玩玩 前言&#xff1a; 在使用Appium进行安卓自动化测试之前&#xff0c;需要配置相应的安卓环境。 环境准备 为了避免走弯路&#xff0c;我们先要确保三点&#xff1a; Android SDK API > 17 (Additional features require …

JMeter正则表达式提取器和JSON提取器基础用法,小白必会!

最近在利用JMeter做接口自动化测试&#xff0c;正则表达式提取器和JSON提取器用的还挺多&#xff0c;想着分享下&#xff0c;希望对大家的接口自动化测试项目有所启发。 在 JMeter 中&#xff0c;正则表达式和 JSON 提取器都是用于从响应数据中提取所需内容&#xff0c;但它们的…
最新文章