Skip navigation.
 
mlRe: Text not drawing where I think it should
FROM : R. Scott Thompson
DATE : Wed Nov 03 15:11:39 2004

On Nov 3, 2004, at 6:25 AM, Graham J Lee wrote:

> I'm creating an app to graph some data (and eventually do some simple
> stats on them), displaying the graph in a custom view.  The graphing
> is all fine, but labeling the axes is causing me some trouble.  I want
> to draw the Y label going 'up' the axis, so I'm drawing the labels
> like this:
>
>     p1=NSMakePoint(width*0.50,height*0.02);
>     [xLabel drawAtPoint:p1];
>     /* Rotate the context, draw the Y label, rotate back */
>     [NSGraphicsContext saveGraphicsState];
>     aTrans=[NSAffineTransform transform];
>     [aTrans translateXBy:width yBy:0];
>     [aTrans rotateByDegrees:90];
>     [aTrans concat];
>     /*p1=NSMakePoint(width*0.50,height*0.90);*/
>     p1=NSMakePoint(height*0.50,width*0.90);
>     [yLabel drawAtPoint:p1];
>     [NSGraphicsContext restoreGraphicsState];
>
> With p1 defined the second time by
> "p1=NSMakePoint(width*0.50,height*0.90);" then the label draws in
> "kindof" the right place, but it's clear that because width and height
> differ it's not *quite* the right place.  I think that this is because
> the width of the rotated context is equal to height and vice versa. 
> BTW, the variables width and height were found by e.g. [self
> bounds].size.width earlier in the method, i.e. back when the context
> was the "right" way around.
>
> So, I try to put the label in the correct place by
> "p1=NSMakePoint(height*0.50,width*0.90);" only it doesn't appear at
> all.  I found that for some value of the [rotated] y ordinate that
> happens to be around (width*0.86), the label starts to get chopped off
> [see http://www-teaching.physics.ox.ac.uk/~leeg/ppcgview.pdf]
> suggesting that it's popping off the top of the drawable area.  I
> guess my questions are twofold: firstly how can I engineer things so
> that I *can* draw right up to the edge of the visible view, and
> secondly what is the reason that I currently can't?  I.e. what is it
> about drawing on views that I don't understand, leading me to try
> something that doesn't work as I think it might?


I think part of the problem is that you are trying to use the
translations to move the objects. The transformations don't move the
objects, they move the coordinate axes.  The distinction is subtle, but
the order in which you have to apply transformations in each case is
the opposite of the other. Try thinking about moving the coordinate
system around.

I think what you want to do is translate the origin to the point
(height / 2, width * 9) then rotate the axes by 90 degress and draw the
text at the newly repositioned/rotated origin:

[NSGraphicsContext saveGraphicsState]
aTrans=[NSAffineTransform transform];
[aTrans translateXBy: height*0.50 yBy: width*0.90];
[aTrans rotateByDegrees:90];
[aTrans concat];
[yLabel drawAtPoint: NSZeroPoint];

For the "drawAtPoint:" call you might also have to adjust for the fact
that the text would draw with it's baseline on the newly oriented
xAxis.

Scott

Related mailsAuthorDate
mlText not drawing where I think it should Graham J Lee Nov 3, 13:25
mlRe: Text not drawing where I think it should R. Scott Thompson Nov 3, 15:11