|
| 1 | +/* |
| 2 | + File: GraphView.m |
| 3 | +Abstract: A custom view for plotting history of x, y, and z magnetic values. |
| 4 | + Version: 1.2 |
| 5 | +
|
| 6 | +Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple |
| 7 | +Inc. ("Apple") in consideration of your agreement to the following |
| 8 | +terms, and your use, installation, modification or redistribution of |
| 9 | +this Apple software constitutes acceptance of these terms. If you do |
| 10 | +not agree with these terms, please do not use, install, modify or |
| 11 | +redistribute this Apple software. |
| 12 | +
|
| 13 | +In consideration of your agreement to abide by the following terms, and |
| 14 | +subject to these terms, Apple grants you a personal, non-exclusive |
| 15 | +license, under Apple's copyrights in this original Apple software (the |
| 16 | +"Apple Software"), to use, reproduce, modify and redistribute the Apple |
| 17 | +Software, with or without modifications, in source and/or binary forms; |
| 18 | +provided that if you redistribute the Apple Software in its entirety and |
| 19 | +without modifications, you must retain this notice and the following |
| 20 | +text and disclaimers in all such redistributions of the Apple Software. |
| 21 | +Neither the name, trademarks, service marks or logos of Apple Inc. may |
| 22 | +be used to endorse or promote products derived from the Apple Software |
| 23 | +without specific prior written permission from Apple. Except as |
| 24 | +expressly stated in this notice, no other rights or licenses, express or |
| 25 | +implied, are granted by Apple herein, including but not limited to any |
| 26 | +patent rights that may be infringed by your derivative works or by other |
| 27 | +works in which the Apple Software may be incorporated. |
| 28 | +
|
| 29 | +The Apple Software is provided by Apple on an "AS IS" basis. APPLE |
| 30 | +MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION |
| 31 | +THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS |
| 32 | +FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND |
| 33 | +OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. |
| 34 | +
|
| 35 | +IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL |
| 36 | +OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 37 | +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 38 | +INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, |
| 39 | +MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED |
| 40 | +AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), |
| 41 | +STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE |
| 42 | +POSSIBILITY OF SUCH DAMAGE. |
| 43 | +
|
| 44 | +Copyright (C) 2010 Apple Inc. All Rights Reserved. |
| 45 | +
|
| 46 | +*/ |
| 47 | + |
| 48 | +#import "GraphView.h" |
| 49 | + |
| 50 | +@implementation GraphView |
| 51 | + |
| 52 | +- (void)updateHistoryWithX:(CLHeadingComponentValue)x y:(CLHeadingComponentValue)y z:(CLHeadingComponentValue)z { |
| 53 | + |
| 54 | + // Add to history. |
| 55 | + history[nextIndex][0] = x; |
| 56 | + history[nextIndex][1] = y; |
| 57 | + history[nextIndex][2] = z; |
| 58 | + |
| 59 | + // Advance the index counter. |
| 60 | + nextIndex = (nextIndex + 1) % 150; |
| 61 | + |
| 62 | + // Mark itself as needing to be redrawn. |
| 63 | + [self setNeedsDisplay]; |
| 64 | +} |
| 65 | + |
| 66 | +- (void)drawGraphInContext:(CGContextRef)context withBounds:(CGRect)bounds { |
| 67 | + CGFloat value, temp; |
| 68 | + |
| 69 | + // Save any previous graphics state settings before setting the color and line width for the current draw. |
| 70 | + CGContextSaveGState(context); |
| 71 | + CGContextSetLineWidth(context, 1.0); |
| 72 | + |
| 73 | + // Draw the intermediate lines |
| 74 | + CGContextSetGrayStrokeColor(context, 0.6, 1.0); |
| 75 | + CGContextBeginPath(context); |
| 76 | + for (value = -5 + 1.0; value <= 5 - 1.0; value += 1.0) { |
| 77 | + |
| 78 | + if (value == 0.0) { |
| 79 | + continue; |
| 80 | + } |
| 81 | + temp = 0.5 + roundf(bounds.origin.y + bounds.size.height / 2 + value / (2 * 5) * bounds.size.height); |
| 82 | + CGContextMoveToPoint(context, bounds.origin.x, temp); |
| 83 | + CGContextAddLineToPoint(context, bounds.origin.x + bounds.size.width, temp); |
| 84 | + } |
| 85 | + CGContextStrokePath(context); |
| 86 | + |
| 87 | + // Draw the center line |
| 88 | + CGContextSetGrayStrokeColor(context, 0.25, 1.0); |
| 89 | + CGContextBeginPath(context); |
| 90 | + temp = 0.5 + roundf(bounds.origin.y + bounds.size.height / 2); |
| 91 | + CGContextMoveToPoint(context, bounds.origin.x, temp); |
| 92 | + CGContextAddLineToPoint(context, bounds.origin.x + bounds.size.width, temp); |
| 93 | + CGContextStrokePath(context); |
| 94 | + |
| 95 | + // Restore previous graphics state. |
| 96 | + CGContextRestoreGState(context); |
| 97 | +} |
| 98 | + |
| 99 | +- (void)drawHistory:(NSUInteger)axis fromIndex:(NSUInteger)index inContext:(CGContextRef)context bounds:(CGRect)bounds { |
| 100 | + CGFloat value; |
| 101 | + |
| 102 | + CGContextBeginPath(context); |
| 103 | + for (NSUInteger counter = 0; counter < 150; ++counter) { |
| 104 | + // UIView referential has the Y axis going down, so we need to draw upside-down. |
| 105 | + value = history[(index + counter) % 150][axis] / -128; |
| 106 | + if (counter > 0) { |
| 107 | + CGContextAddLineToPoint(context, bounds.origin.x + (float)counter / (float)(150 - 1) * bounds.size.width, bounds.origin.y + bounds.size.height / 2 + value * bounds.size.height / 2); |
| 108 | + } else { |
| 109 | + CGContextMoveToPoint(context, bounds.origin.x + (float)counter / (float)(150 - 1) * bounds.size.width, bounds.origin.y + bounds.size.height / 2 + value * bounds.size.height / 2); |
| 110 | + } |
| 111 | + } |
| 112 | + // Save any previous graphics state settings before setting the color and line width for the current draw. |
| 113 | + CGContextSaveGState(context); |
| 114 | + CGContextSetRGBStrokeColor(context, (axis == 0 ? 1.0 : 0.0), (axis == 1 ? 1.0 : 0.0), (axis == 2 ? 1.0 : 0.0), 1.0); |
| 115 | + CGContextSetLineWidth(context, 2.0); |
| 116 | + CGContextStrokePath(context); |
| 117 | + // Restore previous graphics state. |
| 118 | + CGContextRestoreGState(context); |
| 119 | +} |
| 120 | + |
| 121 | +- (void)drawRect:(CGRect)clip { |
| 122 | + NSUInteger index = nextIndex; |
| 123 | + |
| 124 | + CGContextRef context = UIGraphicsGetCurrentContext(); |
| 125 | + CGRect bounds = CGRectMake(0, 0, [self bounds].size.width, [self bounds].size.height); |
| 126 | + |
| 127 | + // create the graph |
| 128 | + [self drawGraphInContext:context withBounds:bounds]; |
| 129 | + |
| 130 | + // plot x,y,z with anti-aliasing turned off |
| 131 | + CGContextSetAllowsAntialiasing(context, false); |
| 132 | + for (NSUInteger i = 0; i < 3; ++i) { |
| 133 | + [self drawHistory:i fromIndex:index inContext:context bounds:bounds]; |
| 134 | + } |
| 135 | + CGContextSetAllowsAntialiasing(context, true); |
| 136 | +} |
| 137 | + |
| 138 | + |
| 139 | +@end |
0 commit comments