Sparklines: a how-to guide.
Monday 8 February 2010, 2:42PM
Edit: yes I know the code isn’t perfect. I’ll further optimize and clean everything up once I’m on that stage (implementing custom cells and whatnot).
Okay, so I’ve already gotten six… now seven requests on how I did those sparklines. The secret: I didn’t. Google is my best friend (Github a close second). The iPhone developer community is so darn awesome that most little utilities are probably available in one form or another.
Here’s the code I used to add Sparklines to my table (datalog is just an NSDictionary containing an array of numbers, which I extract to sparklinedatapoints):
NSMutableDictionary *datalog = [self.data objectAtIndex:indexPath.row];
NSMutableArray *sparklinedatapoints = [NSMutableArray arrayWithCapacity:0];
for (int i = 0; i < [[datalog objectForKey:@"datapoints"] count]; i++)
{
[sparklinedatapoints addObject:[[[datalog objectForKey:@"datapoints"] objectAtIndex:i] objectForKey:@”value”]];
}
CKSparkline *sparkline = [[CKSparkline alloc]
initWithFrame:CGRectMake(320-50-30, 10+44*(indexPath.row), 50, 24)];
sparkline.data = sparklinedatapoints;
[self.tableView addSubview:sparkline];
[sparkline release];
Huh? Where’s the rest of the code? Oh, it’s all available for download here. All you need is the above code and a #import statement and you should be good to go!