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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - SKILL·NULL

如何为GIT设置全局勾子,为每次提交追加信息 - SKILL·NULL 一文了解大模型、小模型与各类神经网络的关系 如何在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刻度对齐,但是仍旧存在不对齐的情况,后续再做探究。