Skip to content
This repository has been archived by the owner on Jul 31, 2019. It is now read-only.

Commit

Permalink
Fixed some pixel alignment issues when drawing legends.
Browse files Browse the repository at this point in the history
  • Loading branch information
eskroch committed Dec 21, 2013
1 parent b9732b5 commit 17176d7
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 6 deletions.
2 changes: 1 addition & 1 deletion framework/Source/CPTBarPlot.m
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,7 @@ -(void)drawSwatchForLegend:(CPTLegend *)legend atIndex:(NSUInteger)idx inRect:(C
if ( [theLineStyle isKindOfClass:[CPTLineStyle class]] ) {
[theLineStyle setLineStyleInContext:context];
CGContextBeginPath(context);
AddRoundedRectPath(context, CPTAlignRectToUserSpace(context, rect), radius);
AddRoundedRectPath(context, CPTAlignBorderedRectToUserSpace(context, rect, theLineStyle), radius);
[theLineStyle strokePathInContext:context];
}
}
Expand Down
6 changes: 3 additions & 3 deletions framework/Source/CPTLegend.m
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ -(void)renderAsVectorInContext:(CGContextRef)context
if ( theLineStyle ) {
[theLineStyle setLineStyleInContext:context];
CGContextBeginPath(context);
AddRoundedRectPath(context, CPTAlignRectToUserSpace(context, entryRect), entryRadius);
AddRoundedRectPath(context, CPTAlignBorderedRectToUserSpace(context, entryRect, theLineStyle), entryRadius);
[theLineStyle strokePathInContext:context];
}

Expand Down Expand Up @@ -783,8 +783,8 @@ -(void)recalculateLayout
CGSize legendSize = CPTSizeMake(self.paddingLeft + self.paddingRight, self.paddingTop + self.paddingBottom);

CGFloat lineWidth = self.borderLineStyle.lineWidth;
legendSize.width += lineWidth * CPTFloat(2.0);
legendSize.height += lineWidth * CPTFloat(2.0);
legendSize.width += lineWidth;
legendSize.height += lineWidth;

if ( self.equalColumns ) {
NSNumber *maxWidth = [maxColumnWidths valueForKeyPath:@"@max.doubleValue"];
Expand Down
2 changes: 1 addition & 1 deletion framework/Source/CPTPieChart.m
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ -(void)drawSwatchForLegend:(CPTLegend *)legend atIndex:(NSUInteger)idx inRect:(C
if ( theLineStyle ) {
[theLineStyle setLineStyleInContext:context];
CGContextBeginPath(context);
AddRoundedRectPath(context, CPTAlignRectToUserSpace(context, rect), radius);
AddRoundedRectPath(context, CPTAlignBorderedRectToUserSpace(context, rect, theLineStyle), radius);
[theLineStyle strokePathInContext:context];
}
}
Expand Down
2 changes: 1 addition & 1 deletion framework/Source/CPTPlot.m
Original file line number Diff line number Diff line change
Expand Up @@ -1636,7 +1636,7 @@ -(void)drawSwatchForLegend:(CPTLegend *)legend atIndex:(NSUInteger)idx inRect:(C
if ( theLineStyle ) {
[theLineStyle setLineStyleInContext:context];
CGContextBeginPath(context);
AddRoundedRectPath(context, CPTAlignRectToUserSpace(context, rect), radius);
AddRoundedRectPath(context, CPTAlignBorderedRectToUserSpace(context, rect, theLineStyle), radius);
[theLineStyle strokePathInContext:context];
}
}
Expand Down
4 changes: 4 additions & 0 deletions framework/Source/CPTUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

/// @file

@class CPTLineStyle;

#if __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -120,6 +122,8 @@ CGRect CPTAlignRectToUserSpace(CGContextRef context, CGRect rect);
CGPoint CPTAlignIntegralPointToUserSpace(CGContextRef context, CGPoint point);
CGRect CPTAlignIntegralRectToUserSpace(CGContextRef context, CGRect rect);

CGRect CPTAlignBorderedRectToUserSpace(CGContextRef context, CGRect rect, CPTLineStyle *borderLineStyle);

/// @}

/// @name String Formatting for Core Graphics Structs
Expand Down
28 changes: 28 additions & 0 deletions framework/Source/CPTUtilities.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#import "CPTUtilities.h"

#import "CPTLineStyle.h"
#import <tgmath.h>

// cache common values to improve performance
Expand Down Expand Up @@ -836,6 +838,32 @@ CGRect CPTAlignIntegralRectToUserSpace(CGContextRef context, CGRect rect)
return CGContextConvertRectToUserSpace(context, rect);
}

CGRect CPTAlignBorderedRectToUserSpace(CGContextRef context, CGRect rect, CPTLineStyle *borderLineStyle)
{
CGRect borderRect;
CGFloat contextScale = CPTFloat(1.0);

if ( rect.size.height != CPTFloat(0.0) ) {
CGRect deviceRect = CGContextConvertRectToDeviceSpace(context, rect);
contextScale = deviceRect.size.height / rect.size.height;
}

if ( contextScale != CPTFloat(1.0) ) {
CGFloat borderWidth = borderLineStyle.lineWidth;
if ( ( borderWidth > CPTFloat(0.0) ) && ( borderWidth == round(borderWidth) ) ) {
borderRect = CPTAlignIntegralRectToUserSpace(context, rect);
}
else {
borderRect = CPTAlignRectToUserSpace(context, rect);
}
}
else {
borderRect = CPTAlignRectToUserSpace(context, rect);
}

return borderRect;
}

#pragma mark -
#pragma mark String formatting for Core Graphics structs

Expand Down

0 comments on commit 17176d7

Please sign in to comment.