Skip to content

Commit

Permalink
Implemented graph title properties, using the new annotation classes.…
Browse files Browse the repository at this point in the history
… Not fully working yet.
  • Loading branch information
drewmccormack committed Jun 22, 2010
1 parent 3f8de3b commit 5b162c1
Show file tree
Hide file tree
Showing 12 changed files with 200 additions and 39 deletions.
9 changes: 9 additions & 0 deletions examples/CPTestApp/Source/Controller.m
Expand Up @@ -32,6 +32,15 @@ -(void)awakeFromNib
CPTheme *theme = [CPTheme themeNamed:kCPDarkGradientTheme];
[graph applyTheme:theme];
hostView.hostedLayer = graph;

// Graph title
graph.title = @"Test";
CPTextStyle *textStyle = [CPTextStyle textStyle];
textStyle.color = [CPColor whiteColor];
textStyle.fontName = @"Helvetica";
textStyle.fontSize = 18.0f;
graph.titleTextStyle = textStyle;
graph.titleDisplacement = CGPointMake(0.0f, -50.0f);

// Graph padding
graph.paddingLeft = 60.0;
Expand Down
3 changes: 3 additions & 0 deletions framework/CorePlot.h
@@ -1,6 +1,8 @@
#import <CorePlot/CPAnimation.h>
#import <CorePlot/CPAnimationKeyFrame.h>
#import <CorePlot/CPAnimationTransition.h>
#import <CorePlot/CPAnnotation.h>
#import <CorePlot/CPAnnotationLayer.h>
#import <CorePlot/CPAxis.h>
#import <CorePlot/CPAxisLabel.h>
#import <CorePlot/CPAxisLabelGroup.h>
Expand All @@ -21,6 +23,7 @@
#import <CorePlot/CPGridLines.h>
#import <CorePlot/CPImage.h>
#import <CorePlot/CPLayer.h>
#import <CorePlot/CPLayerAnnotation.h>
#import <CorePlot/CPLayoutManager.h>
#import <CorePlot/CPLineStyle.h>
#import <CorePlot/CPPieChart.h>
Expand Down
8 changes: 4 additions & 4 deletions framework/CorePlot.xcodeproj/project.pbxproj
Expand Up @@ -111,16 +111,16 @@
07C467B90FE1A96E00299939 /* CPTextStylePlatformSpecific.m in Sources */ = {isa = PBXBuildFile; fileRef = 07C467B70FE1A96E00299939 /* CPTextStylePlatformSpecific.m */; };
07CA112F0FAC8F85000861CE /* CPGradient.h in Headers */ = {isa = PBXBuildFile; fileRef = 07CA112D0FAC8F85000861CE /* CPGradient.h */; settings = {ATTRIBUTES = (Public, ); }; };
07CA11300FAC8F85000861CE /* CPGradient.m in Sources */ = {isa = PBXBuildFile; fileRef = 07CA112E0FAC8F85000861CE /* CPGradient.m */; };
07E10BB111D1016B000B8DAB /* CPPlotSpaceAnnotation.h in Headers */ = {isa = PBXBuildFile; fileRef = 07E10BAF11D1016B000B8DAB /* CPPlotSpaceAnnotation.h */; };
07E10BB111D1016B000B8DAB /* CPPlotSpaceAnnotation.h in Headers */ = {isa = PBXBuildFile; fileRef = 07E10BAF11D1016B000B8DAB /* CPPlotSpaceAnnotation.h */; settings = {ATTRIBUTES = (Public, ); }; };
07E10BB211D1016B000B8DAB /* CPPlotSpaceAnnotation.m in Sources */ = {isa = PBXBuildFile; fileRef = 07E10BB011D1016B000B8DAB /* CPPlotSpaceAnnotation.m */; };
07E10BB311D1016B000B8DAB /* CPPlotSpaceAnnotation.m in Sources */ = {isa = PBXBuildFile; fileRef = 07E10BB011D1016B000B8DAB /* CPPlotSpaceAnnotation.m */; };
07E10BB611D10177000B8DAB /* CPAnnotation.h in Headers */ = {isa = PBXBuildFile; fileRef = 07E10BB411D10177000B8DAB /* CPAnnotation.h */; };
07E10BB611D10177000B8DAB /* CPAnnotation.h in Headers */ = {isa = PBXBuildFile; fileRef = 07E10BB411D10177000B8DAB /* CPAnnotation.h */; settings = {ATTRIBUTES = (Public, ); }; };
07E10BB711D10177000B8DAB /* CPAnnotation.m in Sources */ = {isa = PBXBuildFile; fileRef = 07E10BB511D10177000B8DAB /* CPAnnotation.m */; };
07E10BB811D10177000B8DAB /* CPAnnotation.m in Sources */ = {isa = PBXBuildFile; fileRef = 07E10BB511D10177000B8DAB /* CPAnnotation.m */; };
07E10BBB11D10183000B8DAB /* CPLayerAnnotation.h in Headers */ = {isa = PBXBuildFile; fileRef = 07E10BB911D10183000B8DAB /* CPLayerAnnotation.h */; };
07E10BBB11D10183000B8DAB /* CPLayerAnnotation.h in Headers */ = {isa = PBXBuildFile; fileRef = 07E10BB911D10183000B8DAB /* CPLayerAnnotation.h */; settings = {ATTRIBUTES = (Public, ); }; };
07E10BBC11D10183000B8DAB /* CPLayerAnnotation.m in Sources */ = {isa = PBXBuildFile; fileRef = 07E10BBA11D10183000B8DAB /* CPLayerAnnotation.m */; };
07E10BBD11D10183000B8DAB /* CPLayerAnnotation.m in Sources */ = {isa = PBXBuildFile; fileRef = 07E10BBA11D10183000B8DAB /* CPLayerAnnotation.m */; };
07E10BC011D1018C000B8DAB /* CPAnnotationLayer.h in Headers */ = {isa = PBXBuildFile; fileRef = 07E10BBE11D1018C000B8DAB /* CPAnnotationLayer.h */; };
07E10BC011D1018C000B8DAB /* CPAnnotationLayer.h in Headers */ = {isa = PBXBuildFile; fileRef = 07E10BBE11D1018C000B8DAB /* CPAnnotationLayer.h */; settings = {ATTRIBUTES = (Public, ); }; };
07E10BC111D1018C000B8DAB /* CPAnnotationLayer.m in Sources */ = {isa = PBXBuildFile; fileRef = 07E10BBF11D1018C000B8DAB /* CPAnnotationLayer.m */; };
07E10BC211D1018C000B8DAB /* CPAnnotationLayer.m in Sources */ = {isa = PBXBuildFile; fileRef = 07E10BBF11D1018C000B8DAB /* CPAnnotationLayer.m */; };
07FCF2C6115B54AE00E46606 /* CPSlateTheme.h in Headers */ = {isa = PBXBuildFile; fileRef = 07FCF2C4115B54AE00E46606 /* CPSlateTheme.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down
9 changes: 9 additions & 0 deletions framework/Source/CPAnnotationLayer.m
Expand Up @@ -35,4 +35,13 @@ -(void)removeAnnotation:(CPAnnotation *)annotation
[mutableAnnotations removeObject:annotation];
}

-(NSSet *)sublayersExcludedFromAutomaticLayout
{
NSMutableSet *layers = [NSMutableSet set];
for ( CPAnnotation *annotation in mutableAnnotations ) {
[layers addObject:annotation.contentLayer];
}
return layers;
}

@end
6 changes: 3 additions & 3 deletions framework/Source/CPConstrainedPosition.m
Expand Up @@ -58,19 +58,19 @@ -(id)initWithAlignment:(CPAlignment)newAlignment lowerBound:(CGFloat)newLowerBou
switch ( newAlignment ) {
case CPAlignmentLeft:
case CPAlignmentBottom:
newPosition = lowerBound;
newPosition = newLowerBound;
lowerConstraint = CPConstraintFixed;
upperConstraint = CPConstraintNone;
break;
case CPAlignmentRight:
case CPAlignmentTop:
newPosition = upperBound;
newPosition = newUpperBound;
lowerConstraint = CPConstraintNone;
upperConstraint = CPConstraintFixed;
break;
case CPAlignmentCenter:
case CPAlignmentMiddle:
newPosition = (upperBound - lowerBound) * 0.5 + lowerBound;
newPosition = (newUpperBound - newLowerBound) * 0.5 + newLowerBound;
lowerConstraint = CPConstraintNone;
upperConstraint = CPConstraintNone;
break;
Expand Down
14 changes: 14 additions & 0 deletions framework/Source/CPDefinitions.h
Expand Up @@ -78,6 +78,20 @@ typedef struct _CPConstraints {
CPConstraint upper; ///< The constraint on the upper range.
} CPConstraints;

/**
* @brief Locations around the edge of a rect
**/
typedef enum _CPRectAnchor {
CPRectAnchorBottomLeft, ///< The bottom left corner
CPRectAnchorBottom, ///< The bottom center
CPRectAnchorBottomRight, ///< The bottom right corner
CPRectAnchorLeft, ///< The left middle
CPRectAnchorRight, ///< The right middle
CPRectAnchorTopLeft, ///< The top left corner
CPRectAnchorTop, ///< The top center
CPRectAnchorTopRight, ///< The top right
CPRectAnchorCenter ///< The center of the rect
} CPRectAnchor;

/// @name Default Z Positions
/// @{
Expand Down
14 changes: 13 additions & 1 deletion framework/Source/CPGraph.h
@@ -1,5 +1,6 @@
// Abstract class
#import "CPAnnotationLayer.h"
#import "CPDefinitions.h"

/// @file

Expand All @@ -8,6 +9,8 @@
@class CPPlotAreaFrame;
@class CPPlotSpace;
@class CPTheme;
@class CPTextStyle;
@class CPLayerAnnotation;

/**
* @brief Enumeration of graph layers.
Expand All @@ -22,12 +25,21 @@ typedef enum _CPGraphLayerType {
} CPGraphLayerType;

@interface CPGraph : CPAnnotationLayer {
@private
@private
CPPlotAreaFrame *plotAreaFrame;
NSMutableArray *plots;
NSMutableArray *plotSpaces;
NSString *title;
CPTextStyle *titleTextStyle;
CPRectAnchor titleEdgeLocation;
CGPoint titleDisplacement;
CPLayerAnnotation *titleAnnotation;
}

@property (nonatomic, readwrite, copy) NSString *title;
@property (nonatomic, readwrite, copy) CPTextStyle *titleTextStyle;
@property (nonatomic, readwrite, assign) CGPoint titleDisplacement;
@property (nonatomic, readwrite, assign) CPRectAnchor titleEdgeLocation;
@property (nonatomic, readwrite, retain) CPAxisSet *axisSet;
@property (nonatomic, readwrite, retain) CPPlotAreaFrame *plotAreaFrame;
@property (nonatomic, readonly, retain) CPPlotSpace *defaultPlotSpace;
Expand Down
65 changes: 65 additions & 0 deletions framework/Source/CPGraph.m
Expand Up @@ -3,11 +3,14 @@
#import "CPPlot.h"
#import "CPPlotArea.h"
#import "CPPlotAreaFrame.h"
#import "CPTextStyle.h"
#import "CPPlotSpace.h"
#import "CPFill.h"
#import "CPAxisSet.h"
#import "CPAxis.h"
#import "CPTheme.h"
#import "CPLayerAnnotation.h"
#import "CPTextLayer.h"

/// @cond
@interface CPGraph()
Expand Down Expand Up @@ -58,6 +61,28 @@ @implementation CPGraph
**/
@dynamic topDownLayerOrder;

/** @property title
* @brief The title string.
* Default is nil.
**/
@synthesize title;

/** @property titleTextStyle
* @brief The text style of the title.
**/
@synthesize titleTextStyle;

/** @property titleEdgeLocation
* @brief The location of the title with respect to the plot area frame.
* Default is top center.
**/
@synthesize titleEdgeLocation;

/** @property titleDisplacement
* @brief A vector giving the displacement of the title from the edge location.
**/
@synthesize titleDisplacement;

#pragma mark -
#pragma mark Init/Dealloc

Expand Down Expand Up @@ -87,6 +112,15 @@ -(id)initWithFrame:(CGRect)newFrame
CPAxisSet *newAxisSet = [self newAxisSet];
self.axisSet = newAxisSet;
[newAxisSet release];

// Title
self.title = nil;
self.titleEdgeLocation = CPRectAnchorTop;
self.titleTextStyle = [CPTextStyle textStyle];
self.titleDisplacement = CGPointZero;
titleAnnotation = [[CPLayerAnnotation alloc] initWithReferenceLayer:plotAreaFrame];
titleAnnotation.contentLayer = [[[CPTextLayer alloc] initWithText:@"" style:self.titleTextStyle] autorelease];
[self addAnnotation:titleAnnotation];

self.needsDisplayOnBoundsChange = YES;
}
Expand All @@ -100,6 +134,9 @@ -(void)dealloc
[plotAreaFrame release];
[plots release];
[plotSpaces release];
[title release];
[titleTextStyle release];
[titleAnnotation release];

[super dealloc];
}
Expand Down Expand Up @@ -398,6 +435,34 @@ -(void)setTopDownLayerOrder:(NSArray *)newArray
self.plotAreaFrame.plotArea.topDownLayerOrder = newArray;
}

-(void)setTitle:(NSString *)newTitle
{
if ( newTitle != title ) {
[title release];
title = [newTitle copy];
CPTextLayer *textLayer = (id)titleAnnotation.contentLayer;
textLayer.text = title;
}
}

-(void)setTitleTextStyle:(CPTextStyle *)newStyle
{
if ( newStyle != titleTextStyle ) {
[titleTextStyle release];
titleTextStyle = [newStyle copy];
CPTextLayer *textLayer = (id)titleAnnotation.contentLayer;
textLayer.textStyle = titleTextStyle;
}
}

-(void)setTitleDisplacement:(CGPoint)newDisplace
{
if ( !CGPointEqualToPoint(newDisplace, titleDisplacement) ) {
titleDisplacement = newDisplace;
titleAnnotation.displacement = newDisplace;
}
}

#pragma mark -
#pragma mark Event Handling

Expand Down
1 change: 1 addition & 0 deletions framework/Source/CPLayer.h
Expand Up @@ -45,6 +45,7 @@
/// @name Layout
/// @{
@property (readwrite, retain) id <CPLayoutManager> layoutManager;
@property (readonly) NSSet *sublayersExcludedFromAutomaticLayout;
/// @}

/// @name Initialization
Expand Down
8 changes: 7 additions & 1 deletion framework/Source/CPLayer.m
Expand Up @@ -388,13 +388,19 @@ -(void)layoutSublayers
subLayerSize.height -= self.paddingTop + bottomPadding;
subLayerSize.height = MAX(subLayerSize.height, 0.0);

NSSet *excludedSublayers = [self sublayersExcludedFromAutomaticLayout];
for (CALayer *subLayer in self.sublayers) {
if ([subLayer isKindOfClass:[CPLayer class]]) {
if (![excludedSublayers containsObject:subLayer] && [subLayer isKindOfClass:[CPLayer class]]) {
subLayer.frame = CGRectMake(leftPadding, bottomPadding, subLayerSize.width, subLayerSize.height);
}
}
}

-(NSSet *)sublayersExcludedFromAutomaticLayout
{
return [NSSet set];
}

#pragma mark -
#pragma mark Masking

Expand Down
4 changes: 3 additions & 1 deletion framework/Source/CPLayerAnnotation.h
Expand Up @@ -9,10 +9,12 @@
CPLayer *referenceLayer;
CPConstrainedPosition *xConstrainedPosition;
CPConstrainedPosition *yConstrainedPosition;
CPRectAnchor rectAnchor;
}

@property (readonly, assign) CPLayer *referenceLayer;
@property (readwrite, assign) CPRectAnchor rectAnchor;

-(id)initWithReferenceLayer:(CPLayer *)referenceLayer layerEdge:(CGRectEdge)edge alignment:(CPAlignment)alignment;
-(id)initWithReferenceLayer:(CPLayer *)referenceLayer;

@end

0 comments on commit 5b162c1

Please sign in to comment.