简体   繁体   English

将图表缩放到固定的屏幕高度公式

[英]Scaling a chart to a fixed screen height formula

This is a simple math problem, but for some reason I can't figure out the proper formula, been trying to work it out for the past hour with no success. 这是一个简单的数学问题,但是由于某些原因,我无法找出正确的公式,在过去的一个小时里一直试图解决该问题,但没有成功。

Let's say that we have a chart with a max point on the vertical axis chartMax and a min point on the vertical axis chartMin , and a max screen size height of 667, and we would like to fit this chart within this screen height regardless of its min and max values, it will always fit within the screen. 比方说,我们有在垂直轴上的最高点的图表chartMax和垂直轴分点chartMin ,和667最大屏幕尺寸的高度,我们要适应这个屏幕高度内这个图表不管其最小值和最大值,它将始终适合屏幕。

The chart has many points (hMin, hMax) each represented by a bar with a height h = Math.abs(hMax - hMin) , in order to fit all these points within the screen, we need to multiply the height of each point by a factor, what is the formula of this factor? 图表中有许多点(hMin, hMax)每个点(hMin, hMax)高度为h = Math.abs(hMax - hMin) ,为了使所有这些点都适合屏幕内,我们需要将每个点的高度乘以一个因素,这个因素的公式是什么?

const chartMax = 4000;
const chartMin = 3500;

const screenMax = 667;

const factor = /* math magic */;

const points = [
  Math.abs(4000 - 3900) * factor,
  Math.abs(3800 - 3700) * factor,
  Math.abs(3600 - 3500) * factor,
];

If you only want to scale on the y-axis: 如果只想在y轴上缩放:

const height = chartMax - chartMin;
const scalar = windowHeight / height;   // scale factor

To transform the points from the chart to the screen: 要将点从图表转换到屏幕:

function transformPoint(onChart) {
    return (onChart - chartMin) * scalar; // make lowest point drop to the bottom, and the others drop by the same amount
    // then scale it up from chart space to screen space
}

Here's an example: https://jsfiddle.net/bftbqqvv/ 这是一个示例: https : //jsfiddle.net/bftbqqvv/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM