Re: Beginner Question About CustomView, -drawRect: and Plotting Data.
Re: Beginner Question About CustomView, -drawRect: and Plotting Data.
- Subject: Re: Beginner Question About CustomView, -drawRect: and Plotting Data.
- From: "Shawn Erickson" <email@hidden>
- Date: Tue, 27 Jun 2006 08:31:23 -0700
On 6/27/06, Daniel J Farrell <email@hidden> wrote:
Hi folks,
I have a CustomView that does some NSBezierPath drawing from an array
of data point that I pass it: I'm plotting some scientific data.
At the moment the only way I can get a custom view to draw is my
placing the data inside the -drawRect: method. So my question is: How
can I make a method that will plot my data on the custom view every
time I call the method e.g. something like -drawRect:WithXData:YData: ?
What your view displays needs to be provided to the view object
(external object tells your view object the data available for
drawing) or the view needs a reference to a object it can get the data
from when in needs to draw. The reason why is your view can be asked
to draw itself at anytime ... not just when you give it data to draw.
A few possibilities...
1) Your custom view is told about data points, you construct and store
an NSBezierPath in you view with those points and mark your view dirty
(-[NSView setNeedsDisplay:]). Then when your -[NSView drawRect:]
method is called you use that path to draw what is needed.
2) Same as above but store the data in some other data structure other
then a bezier path (depends on what you are doing exactly).
3) Your custom view is told about an object (aka "data source") it can
pull data from when it needs to draw. Some controller then marks your
view dirty when new data is available and when your view is asked to
draw it asks this other object for the data and then draws what is
needed. (I generally prefer this method if dealing with larger data
sets)
4) A more advanced and performance friendly version would be to render
things into an NSImage as needed (when data changes using one of the
above data access methodologies) and then mark the area of your view
affected by the changed data as dirty. Then in your drawRect method
simply blit your cached image. Doing this would allow you to only blit
the areas that are actually dirty (look at -[NSView
getRectsBeingDrawn:count:]). Of course this is advanced because of
having to correctly and efficiently manage the cached image (window
resize, on data change, etc.).
(didn't talk about binding your view to the data source but that is
also another possibility but one that should be saved until you get a
little more comfortable with things)
-Shawn
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden