2017-09-14 2 views
0

これは簡単な数学的な問題ですが、何らかの理由で私は適切な数式を見つけ出すことができず、過去1時間は成功しないようにしています。固定された画面の高さの数式にスケーリングする

グラフの縦軸にchartMaxの最大点と、縦軸にchartMinの最小点と、最大画面サイズの高さが667のグラフがあり、このグラフにこのグラフを当てはめたいとしますその最小値と最大値にかかわらず、常に画面内に収まります。

グラフには、それぞれが高さがh = Math.abs(hMax - hMin)の棒で表される多くの点があります。これらの点を画面内に収めるには、各点の高さに係数を掛ける必要があります因子?画面にグラフからポイントを変換するために

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

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, 
]; 

答えて

0

あなただけのy軸上にスケールする場合

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 
} 

ここでは例です:https://jsfiddle.net/bftbqqvv/

関連する問題