A Cell with Sparkline in a Simple Data Grid

Description: data grid custom cell inherited from the NuGridCellWithSparkline class.

The SparklineCell.h File

#import <NuGridView/NuGridView.h>
#import <NuGridView/NuGridCellWithSparkline.h>
#import <NuGridView/NuGridSparklineHelper.h>
@interface SparklineCell : NuGridCellWithSparkline
@property bool isUseSparkline; // Indicates whether cell with sparkline is used
@end

The SparklineCell.m File

#import "SparklineCell.h"
@implementation SparklineCell
@synthesize isUseSparkline = _isUseSparkline;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    }
    return self;
}
- (void)drawRect:(CGRect)rect
{
    if ([self isUseSparkline]) {
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGRect frame = self.bounds;
        // Determine sparkline data
        NuGridSparklineData *sparklineData = [[NuGridSparklineData new] autorelease];
        [sparklineData setValues:[NSArray arrayWithObjects:[NSNumber numberWithInt:0],
        [NSNumber numberWithInt:0],
        [NSNumber numberWithInt:1],
        [NSNumber numberWithInt:4],
        [NSNumber numberWithInt:4],
        [NSNumber numberWithInt:3],
        [NSNumber numberWithInt:8],
        [NSNumber numberWithInt:8],
        [NSNumber numberWithInt:5],
        nil]];
        // Set minimum and maximum data values
        [sparklineData setMinValue:0];
        [sparklineData setMaxValue:10];
        // Set indexes for points with peak data values
        [sparklineData setMaxPeakElementIndex:3];
        [sparklineData setMinPeakElementIndex:1];
        // Create an object used for building sparkline
        NuGridSparklineHelper *helper = [NuGridSparklineHelper sparklineHelper];
        CGPathRef pathRef = [helper createPathWith:sparklineData forSize:frame.size];
        CGPathRef maxPeakRef = [helper createPathForMaxPeakIn:sparklineData forSize:frame.size];
        CGPathRef minPeakRef = [helper createPathForMinPeakIn:sparklineData forSize:frame.size];
        // Set sparkline color
        [[UIColor orangeColor] set];
        // Draw sparklines and peak values
        CGContextAddPath(context, pathRef);
        CGContextAddPath(context, maxPeakRef);
        CGContextAddPath(context, minPeakRef);
        CGContextFillPath(context);
        CGPathRelease(pathRef);
    }
    [super drawRect:rect];
}
@end

See also:

Creating a Simple Data Grid