惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Troy Hunt's Blog
人人都是产品经理
人人都是产品经理
MongoDB | Blog
MongoDB | Blog
Stack Overflow Blog
Stack Overflow Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Security Blog
Microsoft Security Blog
F
Fortinet All Blogs
博客园 - 三生石上(FineUI控件)
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Proofpoint News Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - Franky
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Martin Fowler
Martin Fowler
V
Visual Studio Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Microsoft Azure Blog
Microsoft Azure Blog
云风的 BLOG
云风的 BLOG
G
Google Developers Blog
WordPress大学
WordPress大学
腾讯CDC
J
Java Code Geeks
T
The Blog of Author Tim Ferriss
博客园 - 司徒正美
博客园 - 【当耐特】
小众软件
小众软件
Attack and Defense Labs
Attack and Defense Labs
宝玉的分享
宝玉的分享
Google DeepMind News
Google DeepMind News
罗磊的独立博客
Schneier on Security
Schneier on Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
量子位
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
O
OpenAI News
TaoSecurity Blog
TaoSecurity Blog
H
Heimdal Security Blog
Blog — PlanetScale
Blog — PlanetScale
Forbes - Security
Forbes - Security
B
Blog
I
InfoQ
D
DataBreaches.Net
月光博客
月光博客
T
Tenable Blog
S
Security Affairs
C
Check Point Blog
T
Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
www.infosecurity-magazine.com
www.infosecurity-magazine.com

博客园 - SKILL·NULL

如何为GIT设置全局勾子,为每次提交追加信息 一文了解大模型、小模型与各类神经网络的关系 如何在Mac上调整外星人鼠标AW720M的灯光颜色 Karabiner-Elements最常用配置 IndexedDB封装 echarts获取坐标上的点距离顶部底部高度 Let`s Encrypt 生成免费自动续签 HTTPS 证书 H5滚动截取长图 ReactNative常见问题及处理 根据.nvmrc自动切换项目所需node版本 Command PhaseScriptExecution failed with a nonzero exit code env(safe-area-inset-bottom) 兼容写法 缩放实现0.5px 禁止 IOS 橡皮筋效果 JS 拦截浏览器返回 海康威视DS-IPC-E42H-IWPT监控画面竖线处理 Echarts 5 动态按需引入图表 React 18 自定义 Hook 获取 useState 最新值 处理报错 ResizeObserver loop completed with undelivered notifications.
echarts双Y轴,实现均分为包含刻度0的指定段数,同时对齐刻度
SKILL·NULL · 2025-04-19 · via 博客园 - SKILL·NULL

要实现强制分为指定段数,并且对齐刻度,需要用的配置分别为yAxis的max、min、interval

echarts5.3有个配置项alignTicks,在多个 y 轴为数值轴的时候,可以开启该配置项自动对齐刻度。只对'value''log'类型的轴有效。实际使用中,发现在双轴数据格式不同,且数值相差巨大的时候,达不到理想状态下的均分效果。

以下方法实现了,包含0刻度,且均分为指定段数,并且对齐左右刻度:

const row = 5;

const recursion = ({ min, max }: any) => {
  if ((max !== 0 && !max) || (min !== 0 && !min)) return;

  // 减少一位预定行数,用来展示0刻度
  const interval = Math.ceil((max - min) / (row - 1));

  // 将最大最小根据间隔取整
  max = Math.ceil(max / interval) * interval;
  min = Math.floor(min / interval) * interval;

  // 实际0刻度线以上间隔数
  const top = Math.ceil(Math.abs(max) / interval);
  // 实际0刻度线以下间隔数
  const bottom = Math.ceil(Math.abs(min) / interval);
  // 实际总间隔数
  const total = top + bottom;

  // 根据预定行数,重新推算包含0刻度的最终间隔
  const _interval = Math.ceil((total * interval) / row);
  // 根据最终推算间隔,重置最大数
  const _max = Math.ceil(_interval * row + min);
  // 根据最终推算间隔,重置最小数
  const _min = Math.floor(min / _interval) * _interval;
  // 推算过后的0刻度以上间隔数
  const _top = Math.ceil(Math.abs(_max) / _interval);
  // 推算过后的0刻度以下间隔数
  const _bottom = Math.ceil(Math.abs(_min) / _interval);

  if (_top + _bottom !== row) {
   return recursion({ min: _min, max: _max });
  }

  return {
   max: _max,
   min: _min,
   interval: _interval,
   top: _top,
   bottom: _bottom,
  };
};

// 计算最大最小和间隔
export const calc = (data: any) => {
  // 原始最大
  let max = data?.reduce((a: any, b: any) => Math.max(a, b), -Infinity);
  max = max < 0 ? 0 : max;

  // 原始最小
  let min = Math.min(...data);
  min = min > 0 ? 0 : min;

  return {
   ...recursion({ min, max }),
  };
};

将max、min、interval配置到对应yAxis后,0刻度没有对齐,下面进一步优化,实现0刻度对齐:

	let left: any = calc(left_list);
	let right: any = calc(right_list);

	// 值的比例
	const ratio = (left.max - left.min) / (right.max - right.min);

	if (ratio) {
		if (left.max < right.max * ratio) {
			// 同比例下,右边的最大值大,左边向右对齐
			left.max = Math.ceil(right.max * ratio);
		} else {
			// 同比例下,左边的最大值大,右边向左对齐
			right.max = Math.ceil(left.max / ratio);
		}

		if (left.min < right.min * ratio) {
			// 同比例下,左边最小值更小,右边向左边对齐
			right.min = Math.floor(left.min / ratio);
		} else {
			// 同比例下,右边最小值更小,左边向右边对齐
			left.min = Math.floor(right.min * ratio);
		}

		// 重新根据指定段数,计算最大最小和间隔
		left = recursion({ min: left.min, max: left.max });
		right = recursion({ min: right.min, max: right.max });
	}

通过上述方法,实现了多数情况下的0刻度对齐,但是仍旧存在不对齐的情况,后续再做探究。