echarts坐标轴刻度值使用科学计数法表示

news/2025/2/19 16:54:08/

最近使用echarts做图表,数字位数过多时,在小屏手机会出现数字显示不全,被遮挡的问题。

echarts: "5.4.0",

vue: "3.2.45",

使用科学计数法

//y轴使用科学计数法表示
const echartsYaxisLabelFormatter = (value: any) => {if (Math.abs(value) > 100000) {if (value == 0) {return "0";} else if ((value + '').indexOf('e') > 0) {return (value + '').replace(/e/, "E");} else {let res = value.toString(),numN1 = 0,numN2 = 0,num1 = 0,num2 = 0,t1 = 1for (let k = 0; k < res.length; k++) {if (res[k] == ".")t1 = 0;if (t1)num1++;elsenum2++;}// 均转换为科学计数法表示if (Math.abs(value) < 1) {// 小数点后一位开始计算for (let i = 2; i < res.length; i++) {if (res[i] == "0") {numN2++; //记录10的负指数值(默认值从1开始)} else if (res[i] == ".")continue;elsebreak;}let v: number | string = parseFloat(value);v = v * Math.pow(10, numN2);v = v.toFixed(1); //四舍五入 仅保留一位小数位数return `${v.toString()}e-${numN2}`;} else if (num1 > 1) {numN1 = num1 - 1;let v: number | string = parseFloat(value);v = v / Math.pow(10, numN1);if (num2 > 1) {v = v.toFixed(1);}return `${v.toString()}e${numN1}`;}}} else {return value;}
}
<template><div class="sku-details-echarts"><div ref="historicalPrice_chart" style="height:350px;width:100%;"></div></div>
</template><script setup name="SkuDetailsEcharts" lang="ts">
import { ComponentInternalInstance } from 'vue';
import * as echarts from 'echarts';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const historicalPrice_chart = ref();/********************** 方法调用 **********************/
let resizeObserver: ResizeObserver | null = null,canResize = true,historicalPrice_echarts: echarts.ECharts | null = nullconst initChart = () => {nextTick(() => {// 历史价格historicalPrice_echarts = echarts.init(historicalPrice_chart.value);historicalPrice_echarts.setOption({tooltip: {trigger: 'axis'},grid: {left: '1%',right: '4%',bottom: '3%',containLabel: true},xAxis: {type: 'category',data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']},yAxis: {type: 'value',axisLabel: {formatter: (value: number) => echartsYaxisLabelFormatter(value) //调用科学计数法方法},},series: [{data: ['0','0','41040142.00','363014742873.60','175749696.00','18144044046547.20','0','48448.20','10632.50','5024.00','8414.40'],type: 'line',smooth: true}]})const targetElement = document.getElementsByClassName('hasTagsView')[0];resizeObserver = new ResizeObserver(entries => {if (!canResize) {return}canResize = falsesetTimeout(() => {canResize = trueconsole.log('监听到宽高的变化++++++++++++');historicalPrice_echarts?.resize()}, 500)/*     console.log(entries[0].contentRect.width)console.log(entries[0].contentRect.height) */})resizeObserver.observe(targetElement)})
}onUnmounted(() => {// 停止监听给定的元素// resizeObserver?.unobserve(targetElement);// 停止监听所有元素resizeObserver?.disconnect();console.log('卸载了+++++++++++++++++++++');
})onMounted(() => {nextTick(() => {// 销毁echarts实例historicalPrice_echarts?.dispose();// initChart()initChart()})
});
</script><style scoped lang="scss"></style>

http://www.ppmy.cn/news/1044792.html

相关文章

代码随想录算法训练营第53天|动态规划part14

8.19周六 1143.最长公共子序列 1035.不相交的线 53. 最大子序和 动态规划 详细布置 1143.最长公共子序列 题目&#xff1a;两个字符串&#xff0c;问最长的公共子序列多长&#xff08;不连续&#xff09; 题解&#xff1a; 1、dp[i][j]&#xff1a;长度为[0, i - 1]的字…

Vue 2 处理边界情况

访问元素和组件 通过Vue 2 组件基础一文的学习&#xff0c;我们知道组件之间可以通过传递props或事件来进行通信。 但在一些情况下&#xff0c;我们使用下面的方法将更有用。 1.访问根实例 根实例可通过this.$root获取。 我们在所有子组件中都可以像上面那样访问根实例&…

终于找到了这款最好的文献下载网站

在我们文献资源匮乏时&#xff0c;查找下载文献是件非常困难的事。在网上搜索了许多文献下载网站&#xff0c;不是文献资源太少&#xff0c;就是性价比太低&#xff0c;经过筛检比对终于找到了这款文献资源既丰富&#xff0c;又经济适用的文献下载网站。 这款文献下载网站就是…

浏览器输入 URL 后执行过程

浏览器输入 URL 后执行过程 主要步骤如下 浏览器通过 dns 查找域名的 ip 地址浏览器与目标服务器建立 tcp 连接&#xff08;3 次握手&#xff09;浏览器通过 http 协议发送请求服务器端响应 http 请求释放 tcp 连接&#xff08;4 次挥手&#xff09;浏览器解析 html 代码浏览…

docker exec

docker exec 1. 由来 docker exec是Docker容器管理工具中的一个命令&#xff0c;用于在正在运行的容器中执行命令。 2. 常见五种示例命令和说明 以下是docker exec的常见示例命令及其说明&#xff1a; 示例一&#xff1a;在容器中执行命令 docker exec <container_nam…

C++ string类详解

⭐️ string string 是表示字符串的字符串类&#xff0c;该类的接口与常规容器的接口基本一致&#xff0c;还有一些额外的操作 string 的常规操作&#xff0c;在使用 string 类时&#xff0c;需要使用 #include <string> 以及 using namespace std;。 ✨ 帮助文档&…

UML笔记

UML笔记 枫叶云笔记

全面了解功能强大的 Shell,Bash 特殊变量,精华整理那些高级用法,掌握脚本命令将帮助您在 Linux 工作中变得更加高效和有效

全面了解功能强大的 Shell,Bash 特殊变量,精华整理那些高级用法,掌握脚本命令将帮助您在 Linux 工作中变得更加高效和有效。 Bash 是一个功能强大的 Shell,提供了各种特殊变量,可以用于操作和控制脚本的行为。这些变量提供了有关脚本运行环境的基本信息,包括命令行参数、…