Feeds

Hidden recipes for OS X charts and graphs

Get behind Activity Monitor

Top 5 reasons to deploy VMware with Tegile

Mac Secrets One of the many secrets hidden away inside OS X is a private undocumented library called GraphKit.framework. Despite being private, the name is familiar to many Mac developers - and for all the wrong reasons.

Some while back, Apple accidentally introduced a bug into the Xcode 2.1 uninstaller software: If you chose to uninstall this version of the developer tools, the GraphKit.framework library got blown away too.

This wouldn't have been a problem, except for the fact that Activity Monitor won't run without GraphKit.

So what's the GraphKit framework, and why should you care? In essence, GraphKit is a fairly simple graphing library designed to display pie charts, column graphs, X-Y plots, and more. There's nothing to stop you using it in your own programs, aside from the usual caveats regarding undocumented features.

As mentioned, GraphKit is used by Activity Monitor: It's responsible for familiar favorites such as the disk-usage pie chart and scrolling network traffic display. Admittedly, these graphs don't look particularly wonderful. That's largely due to the very small size of the graphs as they appear in the program.

To make use of GraphKit, simply drag the framework in the usual way from the PrivateFrameworks system directory into your Xcode project. Once you've done that, you can fire up Interface Builder and add a custom view to your window. Set the class name of the custom view to GRChartView. This is a descendant of NSView that implements all the graphing rendering.

A GRChartView object has two outlets, delegate and dataSource, which need to be hooked up to the controlling object. In this month's sample project at the end, I've done this in the NIB. However, you could obviously do it programmatically, even creating the GRChartView "on the fly" if wished:

GRDataSet * dataSet = [[[self dataSetClass] alloc] initWithOwnerChart: _chartView];
[dataSet setProperty: [NSNumber numberWithInt: 1] forKey: GRDataSetDrawPlotLine];
[dataSet setProperty: [NSNumber numberWithInt: 90] forKey: GRDataSetPieStartAngle];
        
[_chartView setProperty: [NSNumber numberWithInt: 0] forKey: GRChartDrawBackground];
[_chartView addDataSet: dataSet loadData: YES];
[dataSet release];

Once you've got this far, you need to create a dataset object (GRDataSet) and associate it with the chart view. This is done by the first line of code in the above code snippet. It's worth pointing out that - if wished - you can add multiple datasets to the same chart view. For example, in the Activity Monitor, the network-traffic chart simultaneously displays either packets in/out or data in/out. The "in" and "out" parts are distinct datasets.

One rather irritating aspect of GraphKit is that what you'd expect to be properties are all implemented via a dictionary based mechanism. Thus, in the above code, we've specifically told the dataset to use plot lines via the GRDataSetDrawPlotLine key. Actually, this property is "on" by default, but I've included this code for illustrative purposes.

The next statement sets the starting angle for a pie chart, and so on. If I were going to really be using GraphKit in anger, I'd probably spend a couple of hours adding some Objective-C 2.0 properties to these classes. It would sure reduce wear and tear on my keyboard.

Finally, the code tells chart view not to display its background and adds the dataset to the chart. After this, the dataset can be released because the GRDataSet object is retained by the chart.

Delegate responses

The delegate is expected to implement a few selectors, the prototypes for which are show below:

- (NSInteger) chart: (GRChartView *) chartView numberOfElementsForDataSet: (GRDataSet *) dataset;

- (double) chart: (GRChartView *) chartView yValueForDataSet: (GRDataSet *) dataSet element: (NSInteger) element;

- (NSColor *) chart: (GRChartView *) chartView colorForDataSet: (GRDataSet *) dataSet element: (NSInteger) element

These delegate calls are all pretty straightforward. If you only have one chart view and one dataset, you can just ignore the first two parameters. The first call tells GraphKit how many elements are in the data set, the second returns the value of the designated element while the third returns the wanted color.

The GRColumnDataSet class

The GraphKit GRColumnDataSet class with line drawing turned on

You'll notice that my code decides what type of chart to display by returning the appropriate dataset class from the dataSetClass call. You can experiment with this by returning the other possible classes - I've listed them in the source code. You might also want to try turning the GRDataSetDrawPlotLine property on or off to see the effect in each case.

I've also included the header file for GRAxes, the class which encapsulates X and Y axis functionality. By default, the axes "float" according to the range of the supplied dataset, but (being a traditionalist) I wanted the Y-axis to start from zero.

My source code shows how to do this. If you find this article interesting and you do have more of those all-important key identifiers - used extensively throughout GraphKit - then let me know.

The full source code and demo project can be found here. The code was tested under Leopard, but GraphKit is also present on Tiger. ®

Remote control for virtualized desktops

More from The Register

next story
PEAK APPLE: iOS 8 is least popular Cupertino mobile OS in all of HUMAN HISTORY
'Nerd release' finally staggers past 50 per cent adoption
Microsoft to bake Skype into IE, without plugins
Redmond thinks the Object Real-Time Communications API for WebRTC is ready to roll
Microsoft promises Windows 10 will mean two-factor auth for all
Sneak peek at security features Redmond's baking into new OS
Mozilla: Spidermonkey ATE Apple's JavaScriptCore, THRASHED Google V8
Moz man claims the win on rivals' own benchmarks
Yes, Virginia, there IS a W3C HTML5 standard – as of now, that is
You asked for it! You begged for it! Then you gave up! And now it's HERE!
FTDI yanks chip-bricking driver from Windows Update, vows to fight on
Next driver to battle fake chips with 'non-invasive' methods
DEATH by PowerPoint: Microsoft warns of 0-day attack hidden in slides
Might put out patch in update, might chuck it out sooner
Ubuntu 14.10 tries pulling a Steve Ballmer on cloudy offerings
Oi, Windows, centOS and openSUSE – behave, we're all friends here
prev story

Whitepapers

Cloud and hybrid-cloud data protection for VMware
Learn how quick and easy it is to configure backups and perform restores for VMware environments.
Forging a new future with identity relationship management
Learn about ForgeRock's next generation IRM platform and how it is designed to empower CEOS's and enterprises to engage with consumers.
High Performance for All
While HPC is not new, it has traditionally been seen as a specialist area – is it now geared up to meet more mainstream requirements?
Intelligent flash storage arrays
Tegile Intelligent Storage Arrays with IntelliFlash helps IT boost storage utilization and effciency while delivering unmatched storage savings and performance.
Security and trust: The backbone of doing business over the internet
Explores the current state of website security and the contributions Symantec is making to help organizations protect critical data and build trust with customers.