# Trouble with line graphs

Can someone point me in the right direction regarding Line Graphs, in particular how you flip the Y-Axis so that the 0 point is at the bottom of the Canvas.

Depends whether you are drawing them yourself, or using a library.

If drawing them yourself, and your drawing area is a canvas of 1000 x 1000 pixels
Y is at the top, you want it to be at the bottom.

So any drawing command you use will take the y co-ordinate and subtract it from 1000 (or Canvas.height)

eg 0,0 would normally be at the top

0, (1000-0) gets you 0,1000 which is bottom left

Dead centre is 500 , 500
Change that to 500,(1000- 500) and you get 500,500 … still dead centre

And a y value of 900 which in normal terms would be close to the top, but Xojo would normally draw at the bottom?

200,900 becomes 200, (1000-900) = 200,100 so the point is at the top.

y’ = g.Height - y

Do you use ChartDirector?

[quote=287490:@Will Shank]y’ = g.Height - y

;P[/quote]

assuming Y is a pixel location
if it is a graph value then

y' = (YMax-y)+YMin

where YMin and YMax are the limits of the Axis

[quote=287485:@Jeff Tullin]Depends whether you are drawing them yourself, or using a library.

If drawing them yourself, and your drawing area is a canvas of 1000 x 1000 pixels
Y is at the top, you want it to be at the bottom.

So any drawing command you use will take the y co-ordinate and subtract it from 1000 (or Canvas.height)

eg 0,0 would normally be at the top

0, (1000-0) gets you 0,1000 which is bottom left

Dead centre is 500 , 500
Change that to 500,(1000- 500) and you get 500,500 … still dead centre

And a y value of 900 which in normal terms would be close to the top, but Xojo would normally draw at the bottom?

200,900 becomes 200, (1000-900) = 200,100 so the point is at the top.[/quote]

Thanks Jeff
Nice clear, detailed explanation - I am sure I can work it out from there.

Thanks for the other replies. This Forum is a great resource for Xojoians: especially self-taught persons like myself, who can wander into most mysterious paths in trying to get the results I want.

To zoom to a range of Y you need a scaler from the range (YMax - YMin) to g.Height

scale = g.Height / (YMax - YMin)

Before scaling though you need to remove YMin so the 0’s are aligned

y' = (y - YMin) * scale

And invert for the Y-up orientation

y'' = g.Height - y'

All together

[code]scale = g.Height / (YMax - YMin) //precompute

projectedY = g.Height - (y - YMin) * scale[/code]