Skip to content

Commit

Permalink
Added starting and ending anchor point properties for radial gradients.
Browse files Browse the repository at this point in the history
  • Loading branch information
eskroch committed Mar 31, 2013
1 parent 67bb6fb commit f011567
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
1 change: 1 addition & 0 deletions documentation/changelog.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

## Details
- **New**: Added animation support for plot ranges, decimal values, and other properties.
- **New**: Added starting and ending anchor point properties for radial gradients.
- **Changed**: Changed the type of all axis and plot label formatters from `NSNumberFormatter` to `NSFormatter`.
- **Changed**: Miscellaneous bug fixes and cleanup.

Expand Down
11 changes: 7 additions & 4 deletions examples/CorePlotGallery/src/plots/GradientScatterPlot.m
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,14 @@ -(void)renderInLayer:(CPTGraphHostingView *)layerHostingView withTheme:(CPTTheme
plotSpace.globalYRange = globalYRange;

// Add plot symbols
CPTMutableLineStyle *symbolLineStyle = [CPTMutableLineStyle lineStyle];
symbolLineStyle.lineColor = [CPTColor blackColor];
CPTGradient *symbolGradient = [CPTGradient gradientWithBeginningColor:[CPTColor colorWithComponentRed:0.75 green:0.75 blue:1.0 alpha:1.0]
endingColor:[CPTColor blueColor]];
symbolGradient.gradientType = CPTGradientTypeRadial;
symbolGradient.startAnchor = CPTPointMake(0.25, 0.75);

CPTPlotSymbol *plotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
plotSymbol.fill = [CPTFill fillWithColor:[CPTColor blueColor]];
plotSymbol.lineStyle = symbolLineStyle;
plotSymbol.fill = [CPTFill fillWithGradient:symbolGradient];
plotSymbol.lineStyle = nil;
plotSymbol.size = CGSizeMake(10.0, 10.0);
dataSourceLinePlot.plotSymbol = plotSymbol;

Expand Down
17 changes: 16 additions & 1 deletion framework/Source/CPTGradient.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,26 @@ CPTGradientType;
CGFunctionRef gradientFunction;
CGFloat angle; // angle in degrees
CPTGradientType gradientType;
CGPoint startAnchor;
CGPoint endAnchor;
}

/// @name Gradient Type
/// @{
@property (nonatomic, readonly, assign) CPTGradientBlendingMode blendingMode;
@property (nonatomic, readwrite, assign) CGFloat angle;
@property (nonatomic, readwrite, assign) CPTGradientType gradientType;
/// @}

/// @name Axial Gradients
/// @{
@property (nonatomic, readwrite, assign) CGFloat angle;
/// @}

/// @name Radial Gradients
/// @{
@property (nonatomic, readwrite, assign) CGPoint startAnchor;
@property (nonatomic, readwrite, assign) CGPoint endAnchor;
/// @}

/// @name Factory Methods
/// @{
Expand Down
30 changes: 28 additions & 2 deletions framework/Source/CPTGradient.m
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ @implementation CPTGradient
**/
@synthesize gradientType;

/** @property CGPoint startAnchor
* @brief The anchor point for starting point of a radial gradient. Defaults to (@num{0.5}, @num{0.5}) which centers the gradient on the drawing rectangle.
**/
@synthesize startAnchor;

/** @property CGPoint endAnchor
* @brief The anchor point for ending point of a radial gradient. Defaults to (@num{0.5}, @num{0.5}) which centers the gradient on the drawing rectangle.
**/
@synthesize endAnchor;

#pragma mark -
#pragma mark Init/Dealloc

Expand All @@ -89,6 +99,8 @@ @implementation CPTGradient
* - @ref blendingMode = #CPTLinearBlendingMode
* - @ref angle = @num{0.0}
* - @ref gradientType = #CPTGradientTypeAxial
* - @ref startAnchor = (@num{0.5}, @num{0.5})
* - @ref endAnchor = (@num{0.5}, @num{0.5})
*
* @return The initialized object.
**/
Expand All @@ -101,6 +113,8 @@ -(id)init

angle = CPTFloat(0.0);
gradientType = CPTGradientTypeAxial;
startAnchor = CPTPointMake(0.5, 0.5);
endAnchor = CPTPointMake(0.5, 0.5);
}
return self;
}
Expand Down Expand Up @@ -151,6 +165,8 @@ -(id)copyWithZone:(NSZone *)zone
copy.blendingMode = self.blendingMode;
copy->angle = self->angle;
copy->gradientType = self->gradientType;
copy->startAnchor = self->startAnchor;
copy->endAnchor = self->endAnchor;

return copy;
}
Expand Down Expand Up @@ -181,6 +197,8 @@ -(void)encodeWithCoder:(NSCoder *)coder
[coder encodeInt:blendingMode forKey:@"CPTGradient.blendingMode"];
[coder encodeCGFloat:angle forKey:@"CPTGradient.angle"];
[coder encodeInt:gradientType forKey:@"CPTGradient.type"];
[coder encodeCPTPoint:self.startAnchor forKey:@"CPTPlotSymbol.startAnchor"];
[coder encodeCPTPoint:self.endAnchor forKey:@"CPTPlotSymbol.endAnchor"];
}
else {
[NSException raise:NSInvalidArchiveOperationException format:@"Only supports NSKeyedArchiver coders"];
Expand All @@ -195,6 +213,8 @@ -(id)initWithCoder:(NSCoder *)coder
gradientType = (CPTGradientType)[coder decodeIntForKey : @"CPTGradient.type"];
angle = [coder decodeCGFloatForKey:@"CPTGradient.angle"];
self.blendingMode = (CPTGradientBlendingMode)[coder decodeIntForKey : @"CPTGradient.blendingMode"];
startAnchor = [coder decodeCPTPointForKey:@"CPTPlotSymbol.startAnchor"];
endAnchor = [coder decodeCPTPointForKey:@"CPTPlotSymbol.endAnchor"];

NSUInteger count = (NSUInteger)[coder decodeIntegerForKey : @"CPTGradient.elementCount"];

Expand Down Expand Up @@ -1015,7 +1035,6 @@ -(CGShadingRef)newAxialGradientInRect:(CGRect)rect
endPoint = CPTPointMake(CGRectGetMidX(rect) + deltaX, CGRectGetMidY(rect) + deltaY);
}

// Calls to CoreGraphics
CGShadingRef myCGShading = CGShadingCreateAxial(self.colorspace.cgColorSpace, startPoint, endPoint, gradientFunction, false, false);

return myCGShading;
Expand All @@ -1027,7 +1046,14 @@ -(CGShadingRef)newRadialGradientInRect:(CGRect)rect context:(CGContextRef)contex
CGFloat startRadius, endRadius;
CGFloat scaleX, scaleY;

startPoint = endPoint = CPTPointMake( CGRectGetMidX(rect), CGRectGetMidY(rect) );
CGPoint theStartAnchor = self.startAnchor;

startPoint = CPTPointMake( fma( CGRectGetWidth(rect), theStartAnchor.x, CGRectGetMinX(rect) ),
fma( CGRectGetHeight(rect), theStartAnchor.y, CGRectGetMinY(rect) ) );

CGPoint theEndAnchor = self.endAnchor;
endPoint = CPTPointMake( fma( CGRectGetWidth(rect), theEndAnchor.x, CGRectGetMinX(rect) ),
fma( CGRectGetHeight(rect), theEndAnchor.y, CGRectGetMinY(rect) ) );

startRadius = -CPTFloat(1.0);
if ( CGRectGetHeight(rect) > CGRectGetWidth(rect) ) {
Expand Down

0 comments on commit f011567

Please sign in to comment.