<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Blog @ Tim Isted &#187; NSView</title>
	<atom:link href="http://www.timisted.net/blog/archive/tag/nsview/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.timisted.net/blog</link>
	<description>Cocoa Programming and Other Things</description>
	<lastBuildDate>Sat, 12 Dec 2009 17:43:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>A Bindable Custom NSView Subclass</title>
		<link>http://www.timisted.net/blog/archive/a-bindable-custom-nsview-subclass/</link>
		<comments>http://www.timisted.net/blog/archive/a-bindable-custom-nsview-subclass/#comments</comments>
		<pubDate>Thu, 27 Nov 2008 15:54:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Bindings]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Core Data]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[NSView]]></category>
		<category><![CDATA[Xcode]]></category>

		<guid isPermaLink="false">http://www.timisted.net/blog/?p=259</guid>
		<description><![CDATA[In this post I&#8217;ll walk through writing a custom subclass of NSView that can bind to an NSArrayController to display information either from some generic array connected to the array controller, or from a Core Data object model.
The view we&#8217;ll be creating is a custom view to display a pie-chart; in the sample app the [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I&#8217;ll walk through writing a custom subclass of NSView that can bind to an NSArrayController to display information either from some generic array connected to the array controller, or from a Core Data object model.</p>
<p>The view we&#8217;ll be creating is a custom view to display a pie-chart; in the sample app the pie-chart will be drawn from data entered in an NSTableView. The finished application looks like the following:<br />
<img class="size-full wp-image-235" title="customnsviewapp" src="http://www.timisted.net/blog/wp-content/uploads/2008/09/customnsviewapp.gif" alt="The finished sample application showing the custom NSView subclass bound to a Core Data entity" /><br />
<span id="more-259"></span><br />
If you&#8217;ve read previous posts on this site, you just might possibly have realized that I&#8217;m quite a big fan of Core Data. Unsurprisingly, therefore, the sample application is a Core Data document-based app. Its project and code can be downloaded here: <a href="http://www.timisted.net/blog/wp-content/uploads/2008/11/customnsview.zip">customnsview.zip</a>.</p>
<p>We&#8217;ll be looking at binding to an array controller&#8217;s &#8216;arrangedObjects&#8217; key, and also its &#8217;selectionIndexes&#8217; key to enable the user to change which object is selected by clicking in our view. The segments on our pie-chart have both a &#8216;name&#8217; and a numeric &#8216;value&#8217; so, in the same way that you can bind a model&#8217;s keys to different column tables in an NSTableView, we&#8217;ll make it possible to bind a model&#8217;s keys to the names and values on the pie-chart. This would allow us to bind, for example, a department&#8217;s employees&#8217; &#8216;fullNames&#8217; and their &#8217;salaries&#8217; to view relative salaries, or in a file-viewing application, &#8216;fileNames&#8217; and &#8216;fileSizes&#8217; etc.</p>
<h3>Basic Principles</h3>
<p>To be able to display our information in the view, the view needs to maintain its own store of the data. We&#8217;ll maintain one array of segment names and one array of segment values. When the view is told to display itself, it will sum the values and use the total to determine how big each segment should be relative to the others; the view will then draw each segment using various geometric functions and bezier paths.</p>
<h3>Creating our View</h3>
<p>We&#8217;ll begin by creating a very basic subclass of NSView to which we can add various functionality throughout this article.</p>
<h4>The View Subclass</h4>
<ul>
<li>Begin by creating a new Xcode &#8216;Cocoa Core Data Document-Based Application&#8217;, called &#8216;CustomNSView&#8217;.</li>
<li>In the new project&#8217;s browser, right-click (or control-click) on the Classes group and choose Add -&gt; New File&#8230;; in the window that appears, chose the &#8216;Objective-C NSView Subclass&#8217; and click Next.</li>
<li>Name the file &#8216;MyPieChartView.m&#8217; and make sure the &#8216;Also create &#8220;MyPieChartView.h&#8221; checkbox is ticked.</li>
<li>After clicking &#8216;Finish&#8217; to create the files, open up &#8216;MyPieChartView.m&#8217; and change the template <code>drawRect:</code> method with the one below; we&#8217;ll also override the &#8216;isFlipped&#8217; method to return YES so that our coordinate system has {0,0} set to the top left of the view:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>drawRect<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSRect</span><span style="color: #002200;">&#41;</span>rect
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> blackColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
	NSRectFill<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>isFlipped
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>Next we need to add an instance of our view to the Window of our document object.</p>
<ul>
<li>Open up MyDocument.xib in Interface Builder and delete the placeholder label from the Window.</li>
<li>Drag a custom view onto the Window, set it to resize with the window and use the identity inspector to change the class of the view to &#8216;MyPieChartView&#8217;. My interface looks like this:<br />
<img class="size-full wp-image-262" title="basic-interface-builder-win" src="http://www.timisted.net/blog/wp-content/uploads/2008/11/basic-interface-builder-win.gif" alt="The basic interface for the document" /></li>
</ul>
<p>If you build and run the application, you should find that each new document opens a window with our black box view showing.</p>
<h4>Adding the Arrays and Bindings</h4>
<p>At this point, we need to add a couple of arrays to the view that will hold the names and values of each pie segment.</p>
<ul>
<li>Open the MyPieChartView.h header file and add in two NSArrays for the names and values, together with KVC accessors:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> MyPieChartView <span style="color: #002200;">:</span> <span style="color: #400080;">NSView</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>_segmentNamesArray;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>_segmentValuesArray;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentNamesArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentNamesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentValuesArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentValuesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
<li>We&#8217;ll be allowing other objects to bind to these arrays so we&#8217;ll need to add an <code>initialize:</code> method to our view that exposes them for binding. Whenever the arrays change, we need to get the view to redisplay itself so in the setter methods we need to call <code>[self setNeedsDisplayInRect:]</code>. We also need to release the stored arrays when the view is deallocated. In MyPieChartView.m, implement these methods:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>initialize
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self exposeBinding<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentNamesArray&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self exposeBinding<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentValuesArray&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentNamesArray
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>_segmentNamesArray retain<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentNamesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self willChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentNamesArray&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>_segmentNamesArray release<span style="color: #002200;">&#93;</span>;
	_segmentNamesArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>newArray copy<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self didChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentNamesArray&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>self setNeedsDisplayInRect<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self visibleRect<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentValuesArray
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>_segmentValuesArray retain<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentValuesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self willChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentValuesArray&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>_segmentValuesArray release<span style="color: #002200;">&#93;</span>;
	_segmentValuesArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>newArray copy<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self didChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentValuesArray&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>self setNeedsDisplayInRect<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self visibleRect<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>_segmentNamesArray release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>_segmentValuesArray release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>With our arrays setup, we need to create a data model and some objects to which we can bind.</p>
<h3>The Data Model</h3>
<p>For the purposes of this demonstration, we need only a very simple data model together with an NSTableView in the interface to add and remove objects.</p>
<ul>
<li>Open MyDocument.xcdatamodel and add a new entity called &#8216;PieSegment&#8217;. Add two attributes, a String and a Decimal, called &#8216;name&#8217; and &#8216;amount&#8217; respectively. Give the &#8216;name&#8217; attribute a default string like &#8216;new segment&#8217; and &#8216;amount&#8217; a default value of 0.</li>
<li>Return to MyDocument.xib in Interface Builder and add a new NSArrayController called &#8217;segmentsArrayController&#8217; for the &#8216;PieSegment&#8217; entity, that prepares content. Bind its Managed Object Context to the File&#8217;s Owner managedObjectContext.</li>
<li>Add an NSTableView and two buttons to the document window; bind the columns of the table view to the &#8216;name&#8217; and &#8216;amount&#8217; path of the segmentsArrayController arrangedObjects key.</li>
<li>Add a decimal NSNumberFormatter to the &#8216;amount&#8217; column.</li>
<li>Connect the buttons to the array controller&#8217;s add: and remove: actions and make any other adjustments as you wish. My interface looks like this:<br />
<img class="aligncenter size-full wp-image-266" title="The revised interface for the CustomNSView application" src="http://www.timisted.net/blog/wp-content/uploads/2008/11/interface-redesigned.gif" alt="" /></li>
</ul>
<p>Build and Run the application to make sure that the table view and buttons work as expected.</p>
<h3>Setting up the Bindings and Displaying the Data</h3>
<p>Obviously, nothing is happening yet with our custom NSView since we haven&#8217;t bound anything to our names and values arrays, nor have we modified the view&#8217;s <code>drawRect:</code> method. We&#8217;ll rectify that now:</p>
<ul>
<li>Open the MyDocument.h header file and add in IBOutlets for the array controller and view:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> MyDocument <span style="color: #002200;">:</span> <span style="color: #400080;">NSPersistentDocument</span>
<span style="color: #002200;">&#123;</span>
	IBOutlet <span style="color: #400080;">NSArrayController</span> <span style="color: #002200;">*</span>segmentsArrayController;
	IBOutlet <span style="color: #400080;">NSView</span> <span style="color: #002200;">*</span>pieChartView;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
<li>Connect these outlets to the relevant view and controller in Interface Builder.</li>
<li>In MyDocument.m, modify the <code>windowControllerDidLoadNib:</code> method so that we bind the view to the array controller:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>windowControllerDidLoadNib<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSWindowController</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>windowController
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>super windowControllerDidLoadNib<span style="color: #002200;">:</span>windowController<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>pieChartView bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentNamesArray&quot;</span> toObject<span style="color: #002200;">:</span>segmentsArrayController withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;arrangedObjects.name&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>pieChartView bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentValuesArray&quot;</span> toObject<span style="color: #002200;">:</span>segmentsArrayController withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;arrangedObjects.amount&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>In MyPieChartView.m, modify the <code>drawRect:</code> method with some very simple text display code that will just output the two arrays side-by-side:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>drawRect<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSRect</span><span style="color: #002200;">&#41;</span>rect
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> blackColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
	NSRectFill<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">NSPoint</span> midPoint <span style="color: #002200;">=</span> NSMakePoint<span style="color: #002200;">&#40;</span>NSMidX<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>, NSMidY<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>namesText <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self segmentNamesArray<span style="color: #002200;">&#93;</span> description<span style="color: #002200;">&#93;</span>;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>valuesText <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self segmentValuesArray<span style="color: #002200;">&#93;</span> description<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>textAttributes <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> whiteColor<span style="color: #002200;">&#93;</span>, NSForegroundColorAttributeName, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFont</span> systemFontOfSize<span style="color: #002200;">:</span><span style="color: #2400d9;">10</span><span style="color: #002200;">&#93;</span>, NSFontAttributeName, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	NSSize namesSize <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>namesText sizeWithAttributes<span style="color: #002200;">:</span>textAttributes<span style="color: #002200;">&#93;</span>;
	NSSize valuesSize <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>valuesText sizeWithAttributes<span style="color: #002200;">:</span>textAttributes<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">NSPoint</span> namesTextPoint <span style="color: #002200;">=</span> NSMakePoint<span style="color: #002200;">&#40;</span>midPoint.x <span style="color: #002200;">-</span> namesSize.width <span style="color: #002200;">-</span> <span style="color: #2400d9;">2</span>, midPoint.y <span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>namesSize.height <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">NSPoint</span> valuesTextPoint <span style="color: #002200;">=</span> NSMakePoint<span style="color: #002200;">&#40;</span>midPoint.x <span style="color: #002200;">+</span> valuesSize.width <span style="color: #002200;">+</span> <span style="color: #2400d9;">2</span>, midPoint.y <span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>valuesSize.height <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>namesText drawAtPoint<span style="color: #002200;">:</span>namesTextPoint withAttributes<span style="color: #002200;">:</span>textAttributes<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>valuesText drawAtPoint<span style="color: #002200;">:</span>valuesTextPoint withAttributes<span style="color: #002200;">:</span>textAttributes<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p style="text-align: center;">If you build and run the application, you&#8217;ll find that values added to the table view will show up in columns in the pie-chart view:<br />
<img class="aligncenter size-full wp-image-269" title="The view showing data with a basic textual display" src="http://www.timisted.net/blog/wp-content/uploads/2008/11/application-with-basic-disp.gif" alt="" /></p>
<h3>Displaying the Data as a Pie-Chart</h3>
<p>Whilst at this point we have a nicely-bound custom NSView subclass, we still need to use the provided data to display a pie-chart. There are many different ways to go about displaying such a chart; what follows is the path I decided to take. You may have different suggestions, in which case please feel free to make comments. The methods I use to calculate the sizes and shapes of the segments may not be the fastest, or least memory-intensive, but should be fairly easily understood by anyone with basic mathematical and geometric knowledge.</p>
<p>We&#8217;ll draw the segments of the &#8216;pie&#8217; using NSBezierPaths. Since it isn&#8217;t only the changing of data that can cause a view to be told to draw itself, we&#8217;ll &#8216;cache&#8217; these paths in an array held by the view, updating them only if the data changes. To make it easy to color each segment we&#8217;ll maintain a static class array of colors, generated randomly when needed, such that additional instances of our class can also use the same colors.</p>
<h4>Adding Drawing Information to our View</h4>
<p>We need to add an array pointer to MyPieChartView that will hold the segment paths. We also need a method to access this paths array, and a method that will regenerate it when necessary. We need a method that will produce a random color, and another method that will supply a color from a static colors array if given a zero-based index, generating it if necessary.</p>
<ul>
<li>Change the MyPieChartView.h header file by adding the new array pointer and methods:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> MyPieChartView <span style="color: #002200;">:</span> <span style="color: #400080;">NSView</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>_segmentNamesArray;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>_segmentValuesArray;
&nbsp;
	<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>_segmentPathsArray;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentNamesArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentNamesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentValuesArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentValuesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentPathsArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>generateDrawingInformation;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSColor</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>randomColor;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSColor</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>colorForIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">unsigned</span><span style="color: #002200;">&#41;</span>index;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
<li>In MyPieChartView.m implement the <code>segmentPathsArray:</code> method and modify the <code>dealloc:</code> method to release the array:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentPathsArray
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> _segmentPathsArray;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>_segmentNamesArray release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>_segmentValuesArray release<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> _segmentPathsArray <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#91;</span>_segmentPathsArray release<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>Implement the <code>randomColor:</code> and <code>colorForIndex:</code> methods like this:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSColor</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>randomColor
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">float</span> red <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>random<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">%</span>1000<span style="color: #002200;">&#41;</span><span style="color: #002200;">/</span><span style="color: #2400d9;">1000.0</span>;
	<span style="color: #a61390;">float</span> green <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>random<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">%</span>1000<span style="color: #002200;">&#41;</span><span style="color: #002200;">/</span><span style="color: #2400d9;">1000.0</span>;
	<span style="color: #a61390;">float</span> blue <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>random<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">%</span>1000<span style="color: #002200;">&#41;</span><span style="color: #002200;">/</span><span style="color: #2400d9;">1000.0</span>;
	<span style="color: #a61390;">float</span> alpha <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>random<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">%</span>1000<span style="color: #002200;">&#41;</span><span style="color: #002200;">/</span><span style="color: #2400d9;">1000.0</span>;
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> colorWithCalibratedRed<span style="color: #002200;">:</span>red green<span style="color: #002200;">:</span>green blue<span style="color: #002200;">:</span>blue alpha<span style="color: #002200;">:</span>alpha<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSColor</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>colorForIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">unsigned</span><span style="color: #002200;">&#41;</span>index
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">static</span> <span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>colorsArray <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> colorsArray <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		colorsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> index <span style="color: #002200;">&amp;</span>gt;<span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>colorsArray count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">unsigned</span> currentNum <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
		<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> currentNum <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>colorsArray count<span style="color: #002200;">&#93;</span>; currentNum <span style="color: #002200;">&amp;</span>lt;<span style="color: #002200;">=</span> index; currentNum<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			<span style="color: #002200;">&#91;</span>colorsArray addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self randomColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>colorsArray objectAtIndex<span style="color: #002200;">:</span>index<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>The <code>generateDrawingInformation:</code> method is quite complicated. It keeps hold of its own pointer to the segmentValuesArray, and uses this to generate the paths array. Here I&#8217;m also using <code>#define</code> to set a value for the padding around the pie-chart.

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>generateDrawingInformation
<span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// Keep a pointer to the segmentValuesArray</span>
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>cachedSegmentValuesArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentValuesArray<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Get rid of any existing Paths Array</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> _segmentPathsArray <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>_segmentPathsArray release<span style="color: #002200;">&#93;</span>;
		_segmentPathsArray <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// If there aren't any values to display, we can exit now</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>cachedSegmentValuesArray count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;</span>lt; <span style="color: #2400d9;">1</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #a61390;">return</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Get the sum of the amounts and exit if it is zero</span>
	<span style="color: #a61390;">float</span> sumOfAmounts <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> <span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span>eachAmountToSum <span style="color: #a61390;">in</span> cachedSegmentValuesArray <span style="color: #002200;">&#41;</span>
		sumOfAmounts <span style="color: #002200;">+=</span> <span style="color: #002200;">&#91;</span>eachAmountToSum floatValue<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> sumOfAmounts <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #a61390;">return</span>;
&nbsp;
	_segmentPathsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithCapacity<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>cachedSegmentValuesArray count<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #6e371a;">#define PADDINGAROUNDGRAPH 20.0</span>
&nbsp;
	<span style="color: #a61390;">NSRect</span> viewBounds <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self bounds<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">NSRect</span> graphRect <span style="color: #002200;">=</span> NSInsetRect<span style="color: #002200;">&#40;</span>viewBounds, PADDINGAROUNDGRAPH, PADDINGAROUNDGRAPH<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Make the graphRect square and centred</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> graphRect.size.width <span style="color: #002200;">&amp;</span>gt; graphRect.size.height <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">double</span> sizeDifference <span style="color: #002200;">=</span> graphRect.size.width <span style="color: #002200;">-</span> graphRect.size.height;
		graphRect.size.width <span style="color: #002200;">=</span> graphRect.size.height;
		graphRect.origin.x <span style="color: #002200;">+=</span> <span style="color: #002200;">&#40;</span>sizeDifference <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> graphRect.size.height <span style="color: #002200;">&amp;</span>gt; graphRect.size.width <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">double</span> sizeDifference <span style="color: #002200;">=</span> graphRect.size.height <span style="color: #002200;">-</span> graphRect.size.width;
		graphRect.size.height <span style="color: #002200;">=</span> graphRect.size.width;
		graphRect.origin.y <span style="color: #002200;">+=</span> <span style="color: #002200;">&#40;</span>sizeDifference <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Calculate how big a 'unit' is</span>
	<span style="color: #a61390;">float</span> unitSize <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #2400d9;">360.0</span> <span style="color: #002200;">/</span> sumOfAmounts<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> unitSize <span style="color: #002200;">&amp;</span>gt; <span style="color: #2400d9;">360</span> <span style="color: #002200;">&#41;</span>
		unitSize <span style="color: #002200;">=</span> <span style="color: #2400d9;">360</span>;
&nbsp;
	<span style="color: #a61390;">float</span> radius <span style="color: #002200;">=</span> graphRect.size.width <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span>;
&nbsp;
	<span style="color: #a61390;">NSPoint</span> midPoint <span style="color: #002200;">=</span> NSMakePoint<span style="color: #002200;">&#40;</span> NSMidX<span style="color: #002200;">&#40;</span>graphRect<span style="color: #002200;">&#41;</span>, NSMidY<span style="color: #002200;">&#40;</span>graphRect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// cycle through the segmentValues and create the bezier paths</span>
	<span style="color: #a61390;">float</span> currentDegree <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
	<span style="color: #a61390;">unsigned</span> currentIndex;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> currentIndex <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; currentIndex <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>cachedSegmentValuesArray count<span style="color: #002200;">&#93;</span>; currentIndex<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span>eachValue <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>cachedSegmentValuesArray objectAtIndex<span style="color: #002200;">:</span>currentIndex<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #a61390;">float</span> startDegree <span style="color: #002200;">=</span> currentDegree;
		currentDegree <span style="color: #002200;">+=</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>eachValue floatValue<span style="color: #002200;">&#93;</span> <span style="color: #002200;">*</span> unitSize<span style="color: #002200;">&#41;</span>;
		<span style="color: #a61390;">float</span> endDegree <span style="color: #002200;">=</span> currentDegree;
&nbsp;
		<span style="color: #400080;">NSBezierPath</span> <span style="color: #002200;">*</span>eachSegmentPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBezierPath</span> bezierPath<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>eachSegmentPath moveToPoint<span style="color: #002200;">:</span>midPoint<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>eachSegmentPath appendBezierPathWithArcWithCenter<span style="color: #002200;">:</span>midPoint radius<span style="color: #002200;">:</span>radius startAngle<span style="color: #002200;">:</span>startDegree endAngle<span style="color: #002200;">:</span>endDegree<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>eachSegmentPath closePath<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// close path also handles the lines from the midPoint to the start and end of the arc</span>
		<span style="color: #002200;">&#91;</span>eachSegmentPath setLineWidth<span style="color: #002200;">:</span><span style="color: #2400d9;">2.0</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>_segmentPathsArray addObject<span style="color: #002200;">:</span>eachSegmentPath<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>The paths array should be recalculated whenever the bound arrays change, so modify the two setter methods:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentNamesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self willChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentNamesArray&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>_segmentNamesArray release<span style="color: #002200;">&#93;</span>;
	_segmentNamesArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>newArray copy<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self didChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentNamesArray&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>self generateDrawingInformation<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self setNeedsDisplayInRect<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self visibleRect<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentValuesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self willChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentValuesArray&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>_segmentValuesArray release<span style="color: #002200;">&#93;</span>;
	_segmentValuesArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>newArray copy<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self didChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentValuesArray&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>self generateDrawingInformation<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self setNeedsDisplayInRect<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self visibleRect<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>Finally, we can now replace the <code>drawRect:</code> method so that it displays the paths:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>drawRect<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSRect</span><span style="color: #002200;">&#41;</span>rect
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> whiteColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// white background</span>
	NSRectFill<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>pathsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentPathsArray<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">unsigned</span> count;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> count <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; count <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>pathsArray count<span style="color: #002200;">&#93;</span>; count<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSBezierPath</span> <span style="color: #002200;">*</span>eachPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>pathsArray objectAtIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// fill the path with the drawing color for this index</span>
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self colorForIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>eachPath fill<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// draw a black border around it</span>
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> blackColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>eachPath stroke<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>When you build and run the project, you&#8217;ll find that segments added to the table view with non-zero amounts will show up as a basic, colored pie-chart in the view:<br />
<img class="aligncenter size-full wp-image-276" title="Basic pie chart display in the custom NSView" src="http://www.timisted.net/blog/wp-content/uploads/2008/11/basic-pie-chart-display.gif" alt="" width="500" height="281" /></p>
<h3>Displaying the Text</h3>
<p>So far we haven&#8217;t done anything at all with the segment names; we still need to display these on our pie-chart. We&#8217;ll maintain an array of dictionaries containing information to display the text. We&#8217;ll modify the <code>generateDrawingInformation:</code> method so that it creates this array of dictionaries, and change the <code>drawRect:</code> method to display the text on the pie-chart.</p>
<ul>
<li>Open the MyPieChartView.h header file and add in an array for the text information, along with an accessor:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> MyPieChartView <span style="color: #002200;">:</span> <span style="color: #400080;">NSView</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>_segmentNamesArray;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>_segmentValuesArray;
&nbsp;
	<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>_segmentPathsArray;
	<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>_segmentTextsArray;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentNamesArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentNamesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentValuesArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentValuesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentPathsArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentTextsArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>generateDrawingInformation;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSColor</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>randomColor;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSColor</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>colorForIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">unsigned</span><span style="color: #002200;">&#41;</span>index;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
<li>Add the accessor method to MyPieChartView.m and change <code>dealloc:</code> to release it:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentTextsArray
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> _segmentTextsArray;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>_segmentNamesArray release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>_segmentValuesArray release<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> _segmentPathsArray <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#91;</span>_segmentPathsArray release<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> _segmentTextsArray <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#91;</span>_segmentTextsArray release<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>Now modify the <code>generateDrawingInformation:</code> method so that it stores necessary information in this texts array. We draw each segment arc using two &#8216;half&#8217; arcs rather than one in order to find out the midpoint from which we decide where to put the text. We also check to see in which &#8216;quarter&#8217; of the pie-chart view that midpoint falls and offset it by a <code>#defined</code> TEXTPADDING value accordingly, also making sure that the resultant point doesn&#8217;t fall outside of the view&#8217;s bounds:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>generateDrawingInformation
<span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// Keep pointers to the segmentValuesArray and segmentNamesArray</span>
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>cachedSegmentValuesArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentValuesArray<span style="color: #002200;">&#93;</span>;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>cachedSegmentNamesArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentNamesArray<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Get rid of any existing Paths Array</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> _segmentPathsArray <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>_segmentPathsArray release<span style="color: #002200;">&#93;</span>;
		_segmentPathsArray <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Get rid of any existing Texts Array</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> _segmentTextsArray <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>_segmentTextsArray release<span style="color: #002200;">&#93;</span>;
		_segmentTextsArray <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// If there aren't any values to display, we can exit now</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>cachedSegmentValuesArray count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;</span>lt; <span style="color: #2400d9;">1</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #a61390;">return</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Get the sum of the amounts and exit if it is zero</span>
	<span style="color: #a61390;">float</span> sumOfAmounts <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> <span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span>eachAmountToSum <span style="color: #a61390;">in</span> cachedSegmentValuesArray <span style="color: #002200;">&#41;</span>
		sumOfAmounts <span style="color: #002200;">+=</span> <span style="color: #002200;">&#91;</span>eachAmountToSum floatValue<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> sumOfAmounts <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #a61390;">return</span>;
&nbsp;
	_segmentPathsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithCapacity<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>cachedSegmentValuesArray count<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	_segmentTextsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithCapacity<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>cachedSegmentValuesArray count<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #6e371a;">#define PADDINGAROUNDGRAPH 20.0</span>
<span style="color: #6e371a;">#define TEXTPADDING 5.0</span>
&nbsp;
	<span style="color: #a61390;">NSRect</span> viewBounds <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self bounds<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">NSRect</span> graphRect <span style="color: #002200;">=</span> NSInsetRect<span style="color: #002200;">&#40;</span>viewBounds, PADDINGAROUNDGRAPH, PADDINGAROUNDGRAPH<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Make the graphRect square and centred</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> graphRect.size.width <span style="color: #002200;">&amp;</span>gt; graphRect.size.height <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">double</span> sizeDifference <span style="color: #002200;">=</span> graphRect.size.width <span style="color: #002200;">-</span> graphRect.size.height;
		graphRect.size.width <span style="color: #002200;">=</span> graphRect.size.height;
		graphRect.origin.x <span style="color: #002200;">+=</span> <span style="color: #002200;">&#40;</span>sizeDifference <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> graphRect.size.height <span style="color: #002200;">&amp;</span>gt; graphRect.size.width <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">double</span> sizeDifference <span style="color: #002200;">=</span> graphRect.size.height <span style="color: #002200;">-</span> graphRect.size.width;
		graphRect.size.height <span style="color: #002200;">=</span> graphRect.size.width;
		graphRect.origin.y <span style="color: #002200;">+=</span> <span style="color: #002200;">&#40;</span>sizeDifference <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// get NSRects for the different quarters of the pie-chart</span>
	<span style="color: #a61390;">NSRect</span> topLeftRect, topRightRect;
	NSDivideRect<span style="color: #002200;">&#40;</span>viewBounds, <span style="color: #002200;">&amp;</span>amp;topLeftRect, <span style="color: #002200;">&amp;</span>amp;topRightRect, <span style="color: #002200;">&#40;</span>viewBounds.size.width <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>, NSMinXEdge <span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">NSRect</span> bottomLeftRect, bottomRightRect;
	NSDivideRect<span style="color: #002200;">&#40;</span>topLeftRect, <span style="color: #002200;">&amp;</span>amp;topLeftRect, <span style="color: #002200;">&amp;</span>amp;bottomLeftRect, <span style="color: #002200;">&#40;</span>viewBounds.size.height <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>, NSMinYEdge <span style="color: #002200;">&#41;</span>;
	NSDivideRect<span style="color: #002200;">&#40;</span>topRightRect, <span style="color: #002200;">&amp;</span>amp;topRightRect, <span style="color: #002200;">&amp;</span>amp;bottomRightRect, <span style="color: #002200;">&#40;</span>viewBounds.size.height <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>, NSMinYEdge <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Calculate how big a 'unit' is</span>
	<span style="color: #a61390;">float</span> unitSize <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #2400d9;">360.0</span> <span style="color: #002200;">/</span> sumOfAmounts<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> unitSize <span style="color: #002200;">&amp;</span>gt; <span style="color: #2400d9;">360</span> <span style="color: #002200;">&#41;</span>
		unitSize <span style="color: #002200;">=</span> <span style="color: #2400d9;">360</span>;
&nbsp;
	<span style="color: #a61390;">float</span> radius <span style="color: #002200;">=</span> graphRect.size.width <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span>;
&nbsp;
	<span style="color: #a61390;">NSPoint</span> midPoint <span style="color: #002200;">=</span> NSMakePoint<span style="color: #002200;">&#40;</span> NSMidX<span style="color: #002200;">&#40;</span>graphRect<span style="color: #002200;">&#41;</span>, NSMidY<span style="color: #002200;">&#40;</span>graphRect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Set the text attributes to be used for each textual display</span>
	<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>textAttributes <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> whiteColor<span style="color: #002200;">&#93;</span>, NSBackgroundColorAttributeName, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> blackColor<span style="color: #002200;">&#93;</span>, NSForegroundColorAttributeName, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFont</span> systemFontOfSize<span style="color: #002200;">:</span><span style="color: #2400d9;">12</span><span style="color: #002200;">&#93;</span>, NSFontAttributeName, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// cycle through the segmentValues and create the bezier paths</span>
	<span style="color: #11740a; font-style: italic;">// Also add the text details (note we expect the texts' indexes to tie up with the values' indexes)</span>
	<span style="color: #a61390;">float</span> currentDegree <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
	<span style="color: #a61390;">unsigned</span> currentIndex;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> currentIndex <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; currentIndex <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>cachedSegmentValuesArray count<span style="color: #002200;">&#93;</span>; currentIndex<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span>eachValue <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>cachedSegmentValuesArray objectAtIndex<span style="color: #002200;">:</span>currentIndex<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #a61390;">float</span> startDegree <span style="color: #002200;">=</span> currentDegree;
		currentDegree <span style="color: #002200;">+=</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>eachValue floatValue<span style="color: #002200;">&#93;</span> <span style="color: #002200;">*</span> unitSize<span style="color: #002200;">&#41;</span>;
		<span style="color: #a61390;">float</span> endDegree <span style="color: #002200;">=</span> currentDegree;
		<span style="color: #a61390;">float</span> midDegree <span style="color: #002200;">=</span> startDegree <span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>endDegree <span style="color: #002200;">-</span> startDegree<span style="color: #002200;">&#41;</span> <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #400080;">NSBezierPath</span> <span style="color: #002200;">*</span>eachSegmentPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBezierPath</span> bezierPath<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>eachSegmentPath moveToPoint<span style="color: #002200;">:</span>midPoint<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>eachSegmentPath appendBezierPathWithArcWithCenter<span style="color: #002200;">:</span>midPoint radius<span style="color: #002200;">:</span>radius startAngle<span style="color: #002200;">:</span>startDegree endAngle<span style="color: #002200;">:</span>midDegree clockwise<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #a61390;">NSPoint</span> textPoint <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eachSegmentPath currentPoint<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>eachSegmentPath appendBezierPathWithArcWithCenter<span style="color: #002200;">:</span>midPoint radius<span style="color: #002200;">:</span>radius startAngle<span style="color: #002200;">:</span>midDegree endAngle<span style="color: #002200;">:</span>endDegree clockwise<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>eachSegmentPath closePath<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// close path also handles the lines from the midPoint to the start and end of the arc</span>
		<span style="color: #002200;">&#91;</span>eachSegmentPath setLineWidth<span style="color: #002200;">:</span><span style="color: #2400d9;">2.0</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>_segmentPathsArray addObject<span style="color: #002200;">:</span>eachSegmentPath<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Get the text to be displayed, if it exists, and see how big it is</span>
		<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>eachText <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span>;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>cachedSegmentNamesArray count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;</span>gt; currentIndex <span style="color: #002200;">&#41;</span>
			eachText <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>cachedSegmentNamesArray objectAtIndex<span style="color: #002200;">:</span>currentIndex<span style="color: #002200;">&#93;</span>;
&nbsp;
		NSSize textSize <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eachText sizeWithAttributes<span style="color: #002200;">:</span>textAttributes<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Offset it by TEXTPADDING in direction suitable for whichever quarter of the view it is in</span>
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> NSPointInRect<span style="color: #002200;">&#40;</span>textPoint, topLeftRect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			textPoint.y <span style="color: #002200;">-=</span> <span style="color: #002200;">&#40;</span>textSize.height <span style="color: #002200;">+</span> TEXTPADDING<span style="color: #002200;">&#41;</span>;
			textPoint.x <span style="color: #002200;">-=</span> <span style="color: #002200;">&#40;</span>textSize.width <span style="color: #002200;">+</span> TEXTPADDING<span style="color: #002200;">&#41;</span>;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> NSPointInRect<span style="color: #002200;">&#40;</span>textPoint, topRightRect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			textPoint.y <span style="color: #002200;">-=</span> <span style="color: #002200;">&#40;</span>textSize.height <span style="color: #002200;">+</span> TEXTPADDING<span style="color: #002200;">&#41;</span>;
			textPoint.x <span style="color: #002200;">+=</span> TEXTPADDING;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> NSPointInRect<span style="color: #002200;">&#40;</span>textPoint, bottomLeftRect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			textPoint.y <span style="color: #002200;">+=</span> TEXTPADDING;
			textPoint.x <span style="color: #002200;">-=</span> <span style="color: #002200;">&#40;</span>textSize.width <span style="color: #002200;">+</span> TEXTPADDING<span style="color: #002200;">&#41;</span>;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> NSPointInRect<span style="color: #002200;">&#40;</span>textPoint, bottomRightRect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			textPoint.y <span style="color: #002200;">+=</span> TEXTPADDING;
			textPoint.x <span style="color: #002200;">+=</span> TEXTPADDING;
		<span style="color: #002200;">&#125;</span>
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Make sure the point isn't outside the view's bounds</span>
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> textPoint.x <span style="color: #002200;">&amp;</span>lt; viewBounds.origin.x <span style="color: #002200;">&#41;</span>
			textPoint.x <span style="color: #002200;">=</span> viewBounds.origin.x;
&nbsp;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#40;</span>textPoint.x <span style="color: #002200;">+</span> textSize.width<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&amp;</span>gt; <span style="color: #002200;">&#40;</span>viewBounds.origin.x <span style="color: #002200;">+</span> viewBounds.size.width<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
			textPoint.x <span style="color: #002200;">=</span> viewBounds.origin.x <span style="color: #002200;">+</span> viewBounds.size.width <span style="color: #002200;">-</span> textSize.width;
&nbsp;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> textPoint.y <span style="color: #002200;">&amp;</span>lt; viewBounds.origin.y <span style="color: #002200;">&#41;</span>
			textPoint.y <span style="color: #002200;">=</span> viewBounds.origin.y;
&nbsp;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#40;</span>textPoint.y <span style="color: #002200;">+</span> textSize.height<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&amp;</span>gt; <span style="color: #002200;">&#40;</span>viewBounds.origin.y <span style="color: #002200;">+</span> viewBounds.size.height<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
			textPoint.y <span style="color: #002200;">=</span> viewBounds.origin.y <span style="color: #002200;">+</span> viewBounds.size.height <span style="color: #002200;">-</span> textSize.height;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Finally add the details as a dictionary to our segmentTextsArray.</span>
		<span style="color: #11740a; font-style: italic;">// We include here the textAttributes lest we decide later to e.g. color the texts the same color as the segment fill</span>
		<span style="color: #002200;">&#91;</span>_segmentTextsArray addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithFloat<span style="color: #002200;">:</span>textPoint.x<span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textPointX&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithFloat<span style="color: #002200;">:</span>textPoint.y<span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textPointY&quot;</span>, eachText, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;text&quot;</span>, textAttributes, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textAttributes&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>Modify the <code>drawRect:</code> method to display the text:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>drawRect<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSRect</span><span style="color: #002200;">&#41;</span>rect
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> whiteColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// white background</span>
	NSRectFill<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>pathsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentPathsArray<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">unsigned</span> count;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> count <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; count <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>pathsArray count<span style="color: #002200;">&#93;</span>; count<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSBezierPath</span> <span style="color: #002200;">*</span>eachPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>pathsArray objectAtIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// fill the path with the drawing color for this index</span>
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self colorForIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>eachPath fill<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// draw a black border around it</span>
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> blackColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>eachPath stroke<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>textsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentTextsArray<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> count <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; count <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>textsArray count<span style="color: #002200;">&#93;</span>; count<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>eachTextDictionary <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>textsArray objectAtIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span>;
		<span style="color: #a61390;">NSPoint</span> textPoint <span style="color: #002200;">=</span> NSMakePoint<span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textPointX&quot;</span><span style="color: #002200;">&#93;</span> floatValue<span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textPointY&quot;</span><span style="color: #002200;">&#93;</span> floatValue<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>textAttributes <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textAttributes&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;text&quot;</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>text drawAtPoint<span style="color: #002200;">:</span>textPoint withAttributes<span style="color: #002200;">:</span>textAttributes<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>The view should now display text on the chart:<br />
<img class="aligncenter size-full wp-image-278" title="The Basic Pie Chart now also displaying text" src="http://www.timisted.net/blog/wp-content/uploads/2008/11/basic-pie-chart-with-text.gif" alt="" width="500" height="291" /></p>
<h3>Indicating the Selected Segment(s)</h3>
<p>In order to display which segment is selected, we need to bind to the array controller&#8217;s &#8217;selectionIndexes&#8217;; the view needs to keep its own local copy of these indexes, and expose binding for them:</p>
<ul>
<li>Add an NSIndexSet for the selectionIndexes to the MyPieChartView.h header file, along with a getter and setter:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> MyPieChartView <span style="color: #002200;">:</span> <span style="color: #400080;">NSView</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>_segmentNamesArray;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>_segmentValuesArray;
&nbsp;
	<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>_segmentPathsArray;
	<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>_segmentTextsArray;
&nbsp;
	<span style="color: #400080;">NSIndexSet</span> <span style="color: #002200;">*</span>_selectionIndexes;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentNamesArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentNamesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentValuesArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentValuesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentPathsArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentTextsArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>generateDrawingInformation;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSColor</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>randomColor;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSColor</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>colorForIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">unsigned</span><span style="color: #002200;">&#41;</span>index;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>selectionIndexes;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSelectionIndexes<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newIndexes;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
<li>Implement these accessor methods and expose the binding:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>selectionIndexes
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>_selectionIndexes retain<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSelectionIndexes<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newIndexes
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>_selectionIndexes <span style="color: #002200;">!=</span> newIndexes<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&amp;</span>amp;<span style="color: #002200;">&amp;</span>amp; <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span>_selectionIndexes isEqualToIndexSet<span style="color: #002200;">:</span>newIndexes<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>self willChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>_selectionIndexes release<span style="color: #002200;">&#93;</span>;
		_selectionIndexes <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>newIndexes copy<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>self didChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>self generateDrawingInformation<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>self setNeedsDisplayInRect<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self visibleRect<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>initialize
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self exposeBinding<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentNamesArray&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self exposeBinding<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentValuesArray&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self exposeBinding<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>In MyDocument.m, bind the selectionIndexes to the array controller:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>windowControllerDidLoadNib<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSWindowController</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>windowController
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>super windowControllerDidLoadNib<span style="color: #002200;">:</span>windowController<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>pieChartView bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentNamesArray&quot;</span> toObject<span style="color: #002200;">:</span>segmentsArrayController withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;arrangedObjects.name&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>pieChartView bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentValuesArray&quot;</span> toObject<span style="color: #002200;">:</span>segmentsArrayController withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;arrangedObjects.amount&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>pieChartView bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span> toObject<span style="color: #002200;">:</span>segmentsArrayController withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>We also need to modify the <code>generateDrawingInformation:</code> method to offset the selected segment (and its text) by a <code>#defined</code> amount:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>generateDrawingInformation
<span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// Keep pointers to the segmentValuesArray and segmentNamesArray</span>
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>cachedSegmentValuesArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentValuesArray<span style="color: #002200;">&#93;</span>;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>cachedSegmentNamesArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentNamesArray<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Get rid of any existing Paths Array</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> _segmentPathsArray <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>_segmentPathsArray release<span style="color: #002200;">&#93;</span>;
		_segmentPathsArray <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Get rid of any existing Texts Array</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> _segmentTextsArray <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>_segmentTextsArray release<span style="color: #002200;">&#93;</span>;
		_segmentTextsArray <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// If there aren't any values to display, we can exit now</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>cachedSegmentValuesArray count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;</span>lt; <span style="color: #2400d9;">1</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #a61390;">return</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Get the sum of the amounts and exit if it is zero</span>
	<span style="color: #a61390;">float</span> sumOfAmounts <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> <span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span>eachAmountToSum <span style="color: #a61390;">in</span> cachedSegmentValuesArray <span style="color: #002200;">&#41;</span>
		sumOfAmounts <span style="color: #002200;">+=</span> <span style="color: #002200;">&#91;</span>eachAmountToSum floatValue<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> sumOfAmounts <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #a61390;">return</span>;
&nbsp;
	_segmentPathsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithCapacity<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>cachedSegmentValuesArray count<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	_segmentTextsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithCapacity<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>cachedSegmentValuesArray count<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #400080;">NSIndexSet</span> <span style="color: #002200;">*</span>selectionIndexes <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self selectionIndexes<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">BOOL</span> shouldOffsetSelectedSegment <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>selectionIndexes count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;</span>gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> ? <span style="color: #a61390;">YES</span> <span style="color: #002200;">:</span> <span style="color: #a61390;">NO</span>;
&nbsp;
<span style="color: #6e371a;">#define PADDINGAROUNDGRAPH 20.0</span>
<span style="color: #6e371a;">#define TEXTPADDING 5.0</span>
<span style="color: #6e371a;">#define SELECTEDSEGMENTOFFSET 5.0</span>
&nbsp;
	<span style="color: #a61390;">NSRect</span> viewBounds <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self bounds<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">NSRect</span> graphRect <span style="color: #002200;">=</span> NSInsetRect<span style="color: #002200;">&#40;</span>viewBounds, PADDINGAROUNDGRAPH, PADDINGAROUNDGRAPH<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Make the graphRect square and centred</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> graphRect.size.width <span style="color: #002200;">&amp;</span>gt; graphRect.size.height <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">double</span> sizeDifference <span style="color: #002200;">=</span> graphRect.size.width <span style="color: #002200;">-</span> graphRect.size.height;
		graphRect.size.width <span style="color: #002200;">=</span> graphRect.size.height;
		graphRect.origin.x <span style="color: #002200;">+=</span> <span style="color: #002200;">&#40;</span>sizeDifference <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> graphRect.size.height <span style="color: #002200;">&amp;</span>gt; graphRect.size.width <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">double</span> sizeDifference <span style="color: #002200;">=</span> graphRect.size.height <span style="color: #002200;">-</span> graphRect.size.width;
		graphRect.size.height <span style="color: #002200;">=</span> graphRect.size.width;
		graphRect.origin.y <span style="color: #002200;">+=</span> <span style="color: #002200;">&#40;</span>sizeDifference <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// get NSRects for the different quarters of the pie-chart</span>
	<span style="color: #a61390;">NSRect</span> topLeftRect, topRightRect;
	NSDivideRect<span style="color: #002200;">&#40;</span>viewBounds, <span style="color: #002200;">&amp;</span>amp;topLeftRect, <span style="color: #002200;">&amp;</span>amp;topRightRect, <span style="color: #002200;">&#40;</span>viewBounds.size.width <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>, NSMinXEdge <span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">NSRect</span> bottomLeftRect, bottomRightRect;
	NSDivideRect<span style="color: #002200;">&#40;</span>topLeftRect, <span style="color: #002200;">&amp;</span>amp;topLeftRect, <span style="color: #002200;">&amp;</span>amp;bottomLeftRect, <span style="color: #002200;">&#40;</span>viewBounds.size.height <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>, NSMinYEdge <span style="color: #002200;">&#41;</span>;
	NSDivideRect<span style="color: #002200;">&#40;</span>topRightRect, <span style="color: #002200;">&amp;</span>amp;topRightRect, <span style="color: #002200;">&amp;</span>amp;bottomRightRect, <span style="color: #002200;">&#40;</span>viewBounds.size.height <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>, NSMinYEdge <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Calculate how big a 'unit' is</span>
	<span style="color: #a61390;">float</span> unitSize <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #2400d9;">360.0</span> <span style="color: #002200;">/</span> sumOfAmounts<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> unitSize <span style="color: #002200;">&amp;</span>gt; <span style="color: #2400d9;">360</span> <span style="color: #002200;">&#41;</span>
		unitSize <span style="color: #002200;">=</span> <span style="color: #2400d9;">360</span>;
&nbsp;
	<span style="color: #a61390;">float</span> radius <span style="color: #002200;">=</span> graphRect.size.width <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span>;
&nbsp;
	<span style="color: #a61390;">NSPoint</span> midPoint <span style="color: #002200;">=</span> NSMakePoint<span style="color: #002200;">&#40;</span> NSMidX<span style="color: #002200;">&#40;</span>graphRect<span style="color: #002200;">&#41;</span>, NSMidY<span style="color: #002200;">&#40;</span>graphRect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Set the text attributes to be used for each textual display</span>
	<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>textAttributes <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> whiteColor<span style="color: #002200;">&#93;</span>, NSBackgroundColorAttributeName, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> blackColor<span style="color: #002200;">&#93;</span>, NSForegroundColorAttributeName, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFont</span> systemFontOfSize<span style="color: #002200;">:</span><span style="color: #2400d9;">12</span><span style="color: #002200;">&#93;</span>, NSFontAttributeName, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// cycle through the segmentValues and create the bezier paths</span>
	<span style="color: #11740a; font-style: italic;">// Also add the text details (note we expect the texts' indexes to tie up with the values' indexes)</span>
	<span style="color: #a61390;">float</span> currentDegree <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
	<span style="color: #a61390;">unsigned</span> currentIndex;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> currentIndex <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; currentIndex <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>cachedSegmentValuesArray count<span style="color: #002200;">&#93;</span>; currentIndex<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span>eachValue <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>cachedSegmentValuesArray objectAtIndex<span style="color: #002200;">:</span>currentIndex<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #a61390;">float</span> startDegree <span style="color: #002200;">=</span> currentDegree;
		currentDegree <span style="color: #002200;">+=</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>eachValue floatValue<span style="color: #002200;">&#93;</span> <span style="color: #002200;">*</span> unitSize<span style="color: #002200;">&#41;</span>;
		<span style="color: #a61390;">float</span> endDegree <span style="color: #002200;">=</span> currentDegree;
		<span style="color: #a61390;">float</span> midDegree <span style="color: #002200;">=</span> startDegree <span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>endDegree <span style="color: #002200;">-</span> startDegree<span style="color: #002200;">&#41;</span> <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #400080;">NSBezierPath</span> <span style="color: #002200;">*</span>eachSegmentPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBezierPath</span> bezierPath<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>eachSegmentPath moveToPoint<span style="color: #002200;">:</span>midPoint<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>eachSegmentPath appendBezierPathWithArcWithCenter<span style="color: #002200;">:</span>midPoint radius<span style="color: #002200;">:</span>radius startAngle<span style="color: #002200;">:</span>startDegree endAngle<span style="color: #002200;">:</span>midDegree clockwise<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #a61390;">NSPoint</span> textPoint <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eachSegmentPath currentPoint<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>eachSegmentPath appendBezierPathWithArcWithCenter<span style="color: #002200;">:</span>midPoint radius<span style="color: #002200;">:</span>radius startAngle<span style="color: #002200;">:</span>midDegree endAngle<span style="color: #002200;">:</span>endDegree clockwise<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>eachSegmentPath closePath<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// close path also handles the lines from the midPoint to the start and end of the arc</span>
		<span style="color: #002200;">&#91;</span>eachSegmentPath setLineWidth<span style="color: #002200;">:</span><span style="color: #2400d9;">2.0</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Check to see whether we should offset this segment if it's currently selected in the array controller</span>
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> shouldOffsetSelectedSegment <span style="color: #002200;">&amp;</span>amp;<span style="color: #002200;">&amp;</span>amp; <span style="color: #002200;">&#91;</span>selectionIndexes containsIndex<span style="color: #002200;">:</span>currentIndex<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			<span style="color: #a61390;">float</span> differenceRatio <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>SELECTEDSEGMENTOFFSET <span style="color: #002200;">/</span> radius<span style="color: #002200;">&#41;</span> <span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span>SELECTEDSEGMENTOFFSET <span style="color: #002200;">/</span> <span style="color: #002200;">&#40;</span>endDegree <span style="color: #002200;">-</span> startDegree<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
			<span style="color: #a61390;">float</span> diffY <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>textPoint.y <span style="color: #002200;">-</span> midPoint.y<span style="color: #002200;">&#41;</span> <span style="color: #002200;">*</span> differenceRatio;
			<span style="color: #a61390;">float</span> diffX <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>textPoint.x <span style="color: #002200;">-</span> midPoint.x<span style="color: #002200;">&#41;</span> <span style="color: #002200;">*</span> differenceRatio;
&nbsp;
			<span style="color: #400080;">NSAffineTransform</span> <span style="color: #002200;">*</span>transform <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAffineTransform</span> transform<span style="color: #002200;">&#93;</span>;
&nbsp;
			<span style="color: #002200;">&#91;</span>transform translateXBy<span style="color: #002200;">:</span>diffX yBy<span style="color: #002200;">:</span>diffY<span style="color: #002200;">&#93;</span>;
			<span style="color: #002200;">&#91;</span>eachSegmentPath transformUsingAffineTransform<span style="color: #002200;">:</span> transform<span style="color: #002200;">&#93;</span>;
&nbsp;
			textPoint <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>transform transformPoint<span style="color: #002200;">:</span>textPoint<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
&nbsp;
		<span style="color: #002200;">&#91;</span>_segmentPathsArray addObject<span style="color: #002200;">:</span>eachSegmentPath<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Get the text to be displayed, if it exists, and see how big it is</span>
		<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>eachText <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span>;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>cachedSegmentNamesArray count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;</span>gt; currentIndex <span style="color: #002200;">&#41;</span>
			eachText <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>cachedSegmentNamesArray objectAtIndex<span style="color: #002200;">:</span>currentIndex<span style="color: #002200;">&#93;</span>;
&nbsp;
		NSSize textSize <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eachText sizeWithAttributes<span style="color: #002200;">:</span>textAttributes<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Offset it by TEXTPADDING in direction suitable for whichever quarter of the view it is in</span>
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> NSPointInRect<span style="color: #002200;">&#40;</span>textPoint, topLeftRect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			textPoint.y <span style="color: #002200;">-=</span> <span style="color: #002200;">&#40;</span>textSize.height <span style="color: #002200;">+</span> TEXTPADDING<span style="color: #002200;">&#41;</span>;
			textPoint.x <span style="color: #002200;">-=</span> <span style="color: #002200;">&#40;</span>textSize.width <span style="color: #002200;">+</span> TEXTPADDING<span style="color: #002200;">&#41;</span>;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> NSPointInRect<span style="color: #002200;">&#40;</span>textPoint, topRightRect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			textPoint.y <span style="color: #002200;">-=</span> <span style="color: #002200;">&#40;</span>textSize.height <span style="color: #002200;">+</span> TEXTPADDING<span style="color: #002200;">&#41;</span>;
			textPoint.x <span style="color: #002200;">+=</span> TEXTPADDING;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> NSPointInRect<span style="color: #002200;">&#40;</span>textPoint, bottomLeftRect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			textPoint.y <span style="color: #002200;">+=</span> TEXTPADDING;
			textPoint.x <span style="color: #002200;">-=</span> <span style="color: #002200;">&#40;</span>textSize.width <span style="color: #002200;">+</span> TEXTPADDING<span style="color: #002200;">&#41;</span>;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> NSPointInRect<span style="color: #002200;">&#40;</span>textPoint, bottomRightRect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			textPoint.y <span style="color: #002200;">+=</span> TEXTPADDING;
			textPoint.x <span style="color: #002200;">+=</span> TEXTPADDING;
		<span style="color: #002200;">&#125;</span>
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Make sure the point isn't outside the view's bounds</span>
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> textPoint.x <span style="color: #002200;">&amp;</span>lt; viewBounds.origin.x <span style="color: #002200;">&#41;</span>
			textPoint.x <span style="color: #002200;">=</span> viewBounds.origin.x;
&nbsp;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#40;</span>textPoint.x <span style="color: #002200;">+</span> textSize.width<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&amp;</span>gt; <span style="color: #002200;">&#40;</span>viewBounds.origin.x <span style="color: #002200;">+</span> viewBounds.size.width<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
			textPoint.x <span style="color: #002200;">=</span> viewBounds.origin.x <span style="color: #002200;">+</span> viewBounds.size.width <span style="color: #002200;">-</span> textSize.width;
&nbsp;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> textPoint.y <span style="color: #002200;">&amp;</span>lt; viewBounds.origin.y <span style="color: #002200;">&#41;</span>
			textPoint.y <span style="color: #002200;">=</span> viewBounds.origin.y;
&nbsp;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#40;</span>textPoint.y <span style="color: #002200;">+</span> textSize.height<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&amp;</span>gt; <span style="color: #002200;">&#40;</span>viewBounds.origin.y <span style="color: #002200;">+</span> viewBounds.size.height<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
			textPoint.y <span style="color: #002200;">=</span> viewBounds.origin.y <span style="color: #002200;">+</span> viewBounds.size.height <span style="color: #002200;">-</span> textSize.height;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Finally add the details as a dictionary to our segmentTextsArray.</span>
		<span style="color: #11740a; font-style: italic;">// We include here the textAttributes lest we decide later to e.g. color the texts the same color as the segment fill</span>
		<span style="color: #002200;">&#91;</span>_segmentTextsArray addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithFloat<span style="color: #002200;">:</span>textPoint.x<span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textPointX&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithFloat<span style="color: #002200;">:</span>textPoint.y<span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textPointY&quot;</span>, eachText, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;text&quot;</span>, textAttributes, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textAttributes&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>We&#8217;ll also modify the <code>drawRect:</code> method so that it fills selected segments with blue rather than the usual colour:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>drawRect<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSRect</span><span style="color: #002200;">&#41;</span>rect
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> whiteColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// white background</span>
	NSRectFill<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>pathsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentPathsArray<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">unsigned</span> count;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> count <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; count <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>pathsArray count<span style="color: #002200;">&#93;</span>; count<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSBezierPath</span> <span style="color: #002200;">*</span>eachPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>pathsArray objectAtIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// fill the path with the drawing color for this index unless it's selected</span>
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self selectionIndexes<span style="color: #002200;">&#93;</span> containsIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
			<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> blueColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
		<span style="color: #a61390;">else</span>
			<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self colorForIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>eachPath fill<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// draw a black border around it</span>
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> blackColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>eachPath stroke<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>textsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentTextsArray<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> count <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; count <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>textsArray count<span style="color: #002200;">&#93;</span>; count<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>eachTextDictionary <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>textsArray objectAtIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span>;
		<span style="color: #a61390;">NSPoint</span> textPoint <span style="color: #002200;">=</span> NSMakePoint<span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textPointX&quot;</span><span style="color: #002200;">&#93;</span> floatValue<span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textPointY&quot;</span><span style="color: #002200;">&#93;</span> floatValue<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>textAttributes <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textAttributes&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;text&quot;</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>text drawAtPoint<span style="color: #002200;">:</span>textPoint withAttributes<span style="color: #002200;">:</span>textAttributes<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>Now the view should display the selected segment as desired, updating when the user clicks on a different line in the table view:<br />
<img class="aligncenter size-full wp-image-280" title="Pie Chart highlighting selected segment" src="http://www.timisted.net/blog/wp-content/uploads/2008/11/pie-chart-with-selection-in.gif" alt="" width="500" height="291" /></p>
<p>If you wish, go into Interface Builder and change the selection type of the table view to &#8216;Multiple&#8217;. You should find that you can shift-click multiple items in the table view and they will show as desired in the chart view.</p>
<h3>Resizing Issues</h3>
<p>Since we cache the array of segment paths, only updating it when the arrays of data are changed, you might have noticed that the view doesn&#8217;t behave very well when we resize it. Assuming you set the view to resize with its window, whilst the white background will enlarge, the actual paths won&#8217;t change. This is easy to rectify:</p>
<ul>
<li>Simply check in the <code>drawRect:</code> method whether we are in the middle of a &#8216;liveResize&#8217; and, if so, update the drawing information arrays:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>drawRect<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSRect</span><span style="color: #002200;">&#41;</span>rect
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> whiteColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// white background</span>
	NSRectFill<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>self inLiveResize<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>self generateDrawingInformation<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>pathsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentPathsArray<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">unsigned</span> count;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> count <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; count <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>pathsArray count<span style="color: #002200;">&#93;</span>; count<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSBezierPath</span> <span style="color: #002200;">*</span>eachPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>pathsArray objectAtIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// fill the path with the drawing color for this index unless it's selected</span>
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self selectionIndexes<span style="color: #002200;">&#93;</span> containsIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
			<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> blueColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
		<span style="color: #a61390;">else</span>
			<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self colorForIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>eachPath fill<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// draw a black border around it</span>
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> blackColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>eachPath stroke<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>textsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentTextsArray<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> count <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; count <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>textsArray count<span style="color: #002200;">&#93;</span>; count<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>eachTextDictionary <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>textsArray objectAtIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span>;
		<span style="color: #a61390;">NSPoint</span> textPoint <span style="color: #002200;">=</span> NSMakePoint<span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textPointX&quot;</span><span style="color: #002200;">&#93;</span> floatValue<span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textPointY&quot;</span><span style="color: #002200;">&#93;</span> floatValue<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>textAttributes <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textAttributes&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;text&quot;</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>text drawAtPoint<span style="color: #002200;">:</span>textPoint withAttributes<span style="color: #002200;">:</span>textAttributes<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>The view will now &#8216;live resize&#8217; itself as would be expected.</p>
<h3>Changing the Selection Indexes when the user clicks a Segment</h3>
<p>We have already set up the view to display a segment &#8217;selected&#8217; in the table view, but it would be nice if the user could also click on a segment to select it. Given that we store an array of segment paths inside the view, it should be pretty easy to &#8216;hit-test&#8217; each path to determine which segment was clicked.</p>
<ul>
<li>To receive clicks in our view, we need to override the <code>acceptsFirstResponder:</code> method for our view to return YES so add the following to MyPieChartView.m:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>acceptsFirstResponder
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>Next we&#8217;ll implement a method to determine which segment was clicked so add the method declaration to MyPieChartView.h:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>objectIndexForPoint<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSPoint</span><span style="color: #002200;">&#41;</span>thePoint;</pre></div></div>

</li>
<li>And implement it in MyPieChartView.m:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>objectIndexForPoint<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSPoint</span><span style="color: #002200;">&#41;</span>thePoint
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>cachedPathsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentPathsArray<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">int</span> count;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> count <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; count <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>cachedPathsArray count<span style="color: #002200;">&#93;</span>; count<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSBezierPath</span> <span style="color: #002200;">*</span>eachPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>cachedPathsArray objectAtIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>eachPath containsPoint<span style="color: #002200;">:</span>thePoint <span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			<span style="color: #a61390;">return</span> count;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// if control reaches here, no segment contained the point so return -1</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">-</span><span style="color: #2400d9;">1</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>We also need to implement a <code>mouseUp:</code> method that will be called when the user has clicked on a segment. Expected behavior varies depending on whether the user is holding down a modifier key at the time the segment was clicked; holding down the command key should either add or remove the clicked segment to the current selection; holding down the shift key and clicking should select a range of segments between the existing selection and the clicked segment:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>mouseUp<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSEvent</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>theEvent
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">int</span> index <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self objectIndexForPoint<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self convertPoint<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>theEvent locationInWindow<span style="color: #002200;">&#93;</span> fromView<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #400080;">NSMutableIndexSet</span> <span style="color: #002200;">*</span>newSelectionIndexes <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self selectionIndexes<span style="color: #002200;">&#93;</span> mutableCopy<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>theEvent modifierFlags<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;</span>amp; NSCommandKeyMask <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #11740a; font-style: italic;">// Add or remove the clicked segment</span>
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>newSelectionIndexes containsIndex<span style="color: #002200;">:</span>index<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			<span style="color: #002200;">&#91;</span>newSelectionIndexes removeIndex<span style="color: #002200;">:</span>index<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">else</span>
		<span style="color: #002200;">&#123;</span>
			<span style="color: #002200;">&#91;</span>newSelectionIndexes addIndex<span style="color: #002200;">:</span>index<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>theEvent modifierFlags<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;</span>amp; NSShiftKeyMask <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #11740a; font-style: italic;">// Add range to selection</span>
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>newSelectionIndexes count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			<span style="color: #002200;">&#91;</span>newSelectionIndexes addIndex<span style="color: #002200;">:</span>index<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">else</span>
		<span style="color: #002200;">&#123;</span>
			<span style="color: #a61390;">unsigned</span> <span style="color: #a61390;">int</span> origin <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>index <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>newSelectionIndexes lastIndex<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> ? index <span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>newSelectionIndexes lastIndex<span style="color: #002200;">&#93;</span>;
			<span style="color: #a61390;">unsigned</span> <span style="color: #a61390;">int</span> length <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>index <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>newSelectionIndexes lastIndex<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> ? <span style="color: #002200;">&#91;</span>newSelectionIndexes lastIndex<span style="color: #002200;">&#93;</span> <span style="color: #002200;">-</span> index <span style="color: #002200;">:</span> index <span style="color: #002200;">-</span> <span style="color: #002200;">&#91;</span>newSelectionIndexes lastIndex<span style="color: #002200;">&#93;</span>;
&nbsp;
			length<span style="color: #002200;">++</span>;
			<span style="color: #002200;">&#91;</span>newSelectionIndexes addIndexesInRange<span style="color: #002200;">:</span>NSMakeRange<span style="color: #002200;">&#40;</span>origin, length<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">else</span> <span style="color: #11740a; font-style: italic;">// the user just clicked without modifier keys so simply select the segment</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>newSelectionIndexes removeAllIndexes<span style="color: #002200;">&#93;</span>;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> index <span style="color: #002200;">&amp;</span>gt;<span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span>
			<span style="color: #002200;">&#91;</span>newSelectionIndexes addIndex<span style="color: #002200;">:</span>index<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #002200;">&#91;</span>self setSelectionIndexes<span style="color: #002200;">:</span>newSelectionIndexes<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>newSelectionIndexes release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>Finally we need to bind the selectionIndexes of our view to the selectionIndexes of the array controller so that it updates the table view as well. Add this last binding in MyDocument.m:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>windowControllerDidLoadNib<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSWindowController</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>windowController
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>super windowControllerDidLoadNib<span style="color: #002200;">:</span>windowController<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>pieChartView bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentNamesArray&quot;</span> toObject<span style="color: #002200;">:</span>segmentsArrayController withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;arrangedObjects.name&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>pieChartView bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentValuesArray&quot;</span> toObject<span style="color: #002200;">:</span>segmentsArrayController withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;arrangedObjects.amount&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>pieChartView bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span> toObject<span style="color: #002200;">:</span>segmentsArrayController withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>segmentsArrayController bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span> toObject<span style="color: #002200;">:</span>pieChartView withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>When the user clicks on a segment in our chart view, that segment should become selected and also change the selection in the table view; modifier keys should also affect the way that segments become selected.</p>
<h3>One Last Binding</h3>
<p>Just in case you haven&#8217;t had enough bindings yet, we&#8217;ll add one final feature to the view &#8211; the ability to rotate the chart, perhaps so the user can better fit the related text or place one segment in the middle etc.</p>
<ul>
<li>We&#8217;ll start by adding a &#8216;rotationAmount&#8217; attribute to our view, along with the necessary accessors in MyPieChartView.h:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> MyPieChartView <span style="color: #002200;">:</span> <span style="color: #400080;">NSView</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>_segmentNamesArray;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>_segmentValuesArray;
&nbsp;
	<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>_segmentPathsArray;
	<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>_segmentTextsArray;
&nbsp;
	<span style="color: #400080;">NSIndexSet</span> <span style="color: #002200;">*</span>_selectionIndexes;
&nbsp;
	<span style="color: #a61390;">float</span> _rotationAmount;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentNamesArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentNamesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentValuesArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentValuesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentPathsArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentTextsArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>generateDrawingInformation;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSColor</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>randomColor;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSColor</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>colorForIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">unsigned</span><span style="color: #002200;">&#41;</span>index;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>selectionIndexes;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSelectionIndexes<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newIndexes;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>objectIndexForPoint<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSPoint</span><span style="color: #002200;">&#41;</span>thePoint;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setRotationAmount<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>value;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>rotationAmount;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
<li>Expose the binding for the rotationAmount attribute and add the accessor methods to MyPieChartView.m:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>initialize
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self exposeBinding<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentNamesArray&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self exposeBinding<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentValuesArray&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self exposeBinding<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self exposeBinding<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;rotationAmount&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setRotationAmount<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>value
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>value floatValue<span style="color: #002200;">&#93;</span> <span style="color: #002200;">!=</span> _rotationAmount <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>self willChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;rotationAmount&quot;</span><span style="color: #002200;">&#93;</span>;
		_rotationAmount <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>value floatValue<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>self didChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;rotationAmount&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>self generateDrawingInformation<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>self setNeedsDisplayInRect<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self visibleRect<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>rotationAmount
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithFloat<span style="color: #002200;">:</span>_rotationAmount<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>To make use of the rotation amount, we simply need to modify our drawing code to set the &#8216;currentDegree&#8217; to the rotation amount so find and change this in <code>generateDrawingInformation:</code>:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">	<span style="color: #11740a; font-style: italic;">// cycle through the segmentValues and create the bezier paths</span>
	<span style="color: #11740a; font-style: italic;">// Also add the text details (note we expect the texts' indexes to tie up with the values' indexes)</span>
	<span style="color: #a61390;">float</span> currentDegree <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self rotationAmount<span style="color: #002200;">&#93;</span> floatValue<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">unsigned</span> currentIndex;</pre></div></div>

</li>
<li>Next we need to have a control to rotate the graph so open up MyDocument.xib in InterfaceBuilder and add a horizontal slider control; set its minimum and maximum values to 0.0 and 360.0, and its current value to 0.0; tick the &#8216;Continuous&#8217; box in the inspector. My interface looks like this:<br />
<img class="aligncenter size-full wp-image-284" title="The new slider control in Interface Builder" src="http://www.timisted.net/blog/wp-content/uploads/2008/11/the-new-slider-control.gif" alt="" width="500" height="316" /></li>
<li>Back in Xcode, add an IBOutlet for the new slider to MyDocument.h:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> MyDocument <span style="color: #002200;">:</span> <span style="color: #400080;">NSPersistentDocument</span>
<span style="color: #002200;">&#123;</span>
	IBOutlet <span style="color: #400080;">NSArrayController</span> <span style="color: #002200;">*</span>segmentsArrayController;
	IBOutlet <span style="color: #400080;">NSView</span> <span style="color: #002200;">*</span>pieChartView;
	IBOutlet <span style="color: #400080;">NSSlider</span> <span style="color: #002200;">*</span>sliderRotationControl;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
<li>Connect the outlet to the control in Interface Builder.</li>
<li>In MyDocument.m, bind the rotation control&#8217;s value to the rotationAmount on the chart view:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>windowControllerDidLoadNib<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSWindowController</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>windowController
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>super windowControllerDidLoadNib<span style="color: #002200;">:</span>windowController<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>pieChartView bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentNamesArray&quot;</span> toObject<span style="color: #002200;">:</span>segmentsArrayController withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;arrangedObjects.name&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>pieChartView bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentValuesArray&quot;</span> toObject<span style="color: #002200;">:</span>segmentsArrayController withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;arrangedObjects.amount&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>pieChartView bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span> toObject<span style="color: #002200;">:</span>segmentsArrayController withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>segmentsArrayController bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span> toObject<span style="color: #002200;">:</span>pieChartView withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>sliderRotationControl bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;value&quot;</span> toObject<span style="color: #002200;">:</span>pieChartView withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;rotationAmount&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<h3>That&#8217;s It!</h3>
<p>At this point you should have a pretty functional, bindable pie-chart view. For such a view it would make sense to create an Interface Builder palette so that the bindings can be setup visually in Interface Builder rather having to do it programmatically; this may well be a topic for a future post!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.timisted.net/blog/archive/a-bindable-custom-nsview-subclass/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
