andi

pie chart label

... ... @@ -27,6 +27,14 @@ typedef NS_ENUM(NSUInteger, PNLegendItemStyle) {
@property (assign, nonatomic) PNLegendItemStyle legendStyle;
@property (assign, nonatomic) CGFloat legendFontSize;
- (UIView*) drawLegend;
/**
* returns the Legend View, or nil if no chart data is present.
* The origin of the legend frame is 0,0 but you can set it with setFrame:(CGRect)
*
* @param mWidth Maximum width of legend. Height will depend on this and font size
*
* @return UIView of Legend
*/
- (UIView*) getLegendWithMaxWidth:(CGFloat)mWidth;
@end
... ...
... ... @@ -57,4 +57,16 @@
self.legendStyle = PNLegendItemStyleStacked;
}
/**
* to be implemented in subclass
*/
- (UIView*) getLegendWithMaxWidth:(CGFloat)mWidth{
[self doesNotRecognizeSelector:_cmd];
return nil;
}
@end
... ...
... ... @@ -81,4 +81,7 @@
* @return UIView of Legend
*/
- (UIView*) getLegendWithMaxWidth:(CGFloat)mWidth;
+ (CGSize)sizeOfString:(NSString *)text withWidth:(float)width font:(UIFont *)font;
@end
... ...
... ... @@ -7,6 +7,8 @@
//
#import "PNPieChart.h"
//needed for the expected label size
#import "PNLineChart.h"
@interface PNPieChart()
... ... @@ -224,4 +226,98 @@
}];
}];
}
- (UIView*) getLegendWithMaxWidth:(CGFloat)mWidth{
if ([self.items count] < 1) {
return nil;
}
/* This is a small circle that refers to the chart data */
CGFloat legendCircle = 10;
/* x and y are the coordinates of the starting point of each legend item */
CGFloat x = 0;
CGFloat y = 0;
/* accumulated width and height */
CGFloat totalWidth = 0;
CGFloat totalHeight = 0;
NSMutableArray *legendViews = [[NSMutableArray alloc] init];
/* Determine the max width of each legend item */
CGFloat maxLabelWidth = self.legendStyle == PNLegendItemStyleStacked ? (mWidth - legendCircle) : (mWidth / [self.items count] - legendCircle);
/* this is used when labels wrap text and the line
* should be in the middle of the first row */
CGFloat singleRowHeight = [PNLineChart sizeOfString:@"Test"
withWidth:MAXFLOAT
font:[UIFont systemFontOfSize:self.legendFontSize]].height;
for (PNPieChartDataItem *pdata in self.items) {
/* Expected label size*/
CGSize labelsize = [PNLineChart sizeOfString:pdata.textDescription
withWidth:maxLabelWidth
font:[UIFont systemFontOfSize:self.legendFontSize]];
// Add inflexion type
[legendViews addObject:[self drawInflexion:legendCircle * .8
center:CGPointMake(x + legendCircle / 2, y + singleRowHeight / 2)
andColor:pdata.color]];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x + legendCircle, y, maxLabelWidth, labelsize.height)];
label.text = pdata.textDescription;
label.font = [UIFont systemFontOfSize:self.legendFontSize];
label.lineBreakMode = NSLineBreakByWordWrapping;
label.numberOfLines = 0;
x += self.legendStyle == PNLegendItemStyleStacked ? 0 : labelsize.width + legendCircle;
y += self.legendStyle == PNLegendItemStyleStacked ? labelsize.height : 0;
totalWidth = self.legendStyle == PNLegendItemStyleStacked ? fmaxf(totalWidth, labelsize.width + legendCircle) : totalWidth + labelsize.width + legendCircle;
totalHeight = self.legendStyle == PNLegendItemStyleStacked ? fmaxf(totalHeight, labelsize.height) : totalHeight + labelsize.height;
[legendViews addObject:label];
}
UIView *legend = [[UIView alloc] initWithFrame:CGRectMake(0, 0, totalWidth, totalHeight)];
for (UIView* v in legendViews) {
[legend addSubview:v];
}
return legend;
}
- (UIImageView*)drawInflexion:(CGFloat)size center:(CGPoint)center andColor:(UIColor*)color
{
//Make the size a little bigger so it includes also border stroke
CGSize aSize = CGSizeMake(size, size);
UIGraphicsBeginImageContextWithOptions(aSize, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddArc(context, size/2, size/ 2, size/2, 0, M_PI*2, YES);
//Set some fill color
CGContextSetFillColorWithColor(context, color.CGColor);
//Finally draw
CGContextDrawPath(context, kCGPathFill);
//now get the image from the context
UIImage *squareImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//// Translate origin
CGFloat originX = center.x - (size) / 2.0;
CGFloat originY = center.y - (size) / 2.0;
UIImageView *squareImageView = [[UIImageView alloc]initWithImage:squareImage];
[squareImageView setFrame:CGRectMake(originX, originY, size, size)];
return squareImageView;
}
@end
... ...
... ... @@ -128,6 +128,13 @@
self.pieChart.descriptionTextShadowColor = [UIColor clearColor];
[self.pieChart strokeChart];
self.pieChart.legendStyle = PNLegendItemStyleStacked;
self.pieChart.legendFontSize = 12.0;
UIView *legend = [self.pieChart getLegendWithMaxWidth:200];
[legend setFrame:CGRectMake(100, 400, legend.frame.size.width, legend.frame.size.width)];
[self.view addSubview:legend];
[self.view addSubview:self.pieChart];
self.changeValueButton.hidden = YES;
... ...