Last active
January 12, 2022 09:47
-
-
Save shivanshtalwar0/e8af230d459e3c349fada5a7cebdafc0 to your computer and use it in GitHub Desktop.
graph plotting logic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://stackoverflow.com/questions/326679/choosing-an-attractive-linear-scale-for-a-graphs-y-axis | |
var min=89; | |
var max=173; | |
var actualHeight=1200; // 500 pixels high graph | |
var tickCount =Math.round(actualHeight/100); | |
// we want lines about every 100 pixels. | |
if(tickCount <3) tickCount =3; | |
var range=Math.abs(max-min); | |
var unroundedTickSize = range/(tickCount-1); | |
var x = Math.ceil(Math.log10(unroundedTickSize)-1); | |
var pow10x = Math.pow(10, x); | |
var roundedTickRange = Math.ceil(unroundedTickSize / pow10x) * pow10x; | |
var min_rounded=roundedTickRange * Math.floor(min/roundedTickRange); | |
var max_rounded= roundedTickRange * Math.ceil(max/roundedTickRange); | |
var nr=tickCount; | |
var str=""; | |
for(var x=min_rounded;x<=max_rounded;x+=roundedTickRange) | |
{ | |
str+=x+", "; | |
} | |
console.log("nice Y axis "+str); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment