andi

added some documentation minor changes

... ... @@ -71,5 +71,14 @@
- (void)updateChartData:(NSArray *)data;
/**
* 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
... ...
... ... @@ -736,91 +736,108 @@
if ([self.chartData count] < 1) {
return nil;
}
/* This is a short line that refers to the chart data */
CGFloat legendLineWidth = 40;
/* 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 *legendLabels = [[NSMutableArray alloc] init];
NSMutableArray *legendLines = [[NSMutableArray alloc] init];
NSMutableArray *legendInflexion = [[NSMutableArray alloc] init];
for (PNLineChartData *pdata in self.chartData) {
NSMutableArray *legendViews = [[NSMutableArray alloc] init];
/* Determine the max width of each legend item */
CGFloat maxLabelWidth = self.legendStyle == PNLegendItemStyleStacked ? (mWidth - legendLineWidth) : (mWidth / [self.chartData count] - legendLineWidth);
/* 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 (PNLineChartData *pdata in self.chartData) {
/* Expected label size*/
CGSize labelsize = [PNLineChart sizeOfString:pdata.dataTitle
withWidth:maxLabelWidth
font:[UIFont systemFontOfSize:self.legendFontSize]];
/* draw line */
/* draw lines */
/* If there is inflection decorator, the line is composed of two lines
* and this is the space that separates two lines in order to put inflection
* decorator */
CGFloat inflexionWidthSpacer = pdata.inflexionPointStyle == PNLineChartPointStyleTriangle ? pdata.inflexionPointWidth / 2 : pdata.inflexionPointWidth;
CGFloat t1 = (legendLineWidth * 0.8 - inflexionWidthSpacer)/2;
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(x + legendLineWidth * 0.1, y + (labelsize.height - pdata.lineWidth) / 2, t1, pdata.lineWidth)];
CGFloat halfLineLength;
if (pdata.inflexionPointStyle != PNLineChartPointStyleNone) {
halfLineLength = (legendLineWidth * 0.8 - inflexionWidthSpacer)/2;
}else{
halfLineLength = legendLineWidth * 0.8;
}
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(x + legendLineWidth * 0.1, y + (singleRowHeight - pdata.lineWidth) / 2, halfLineLength, pdata.lineWidth)];
line.backgroundColor = pdata.color;
line.alpha = pdata.alpha;
[legendLines addObject:line];
[legendViews addObject:line];
line = [[UIView alloc] initWithFrame:CGRectMake(x + legendLineWidth * 0.1 + t1 + inflexionWidthSpacer, y + (labelsize.height - pdata.lineWidth) / 2, t1, pdata.lineWidth)];
if (pdata.inflexionPointStyle != PNLineChartPointStyleNone) {
line = [[UIView alloc] initWithFrame:CGRectMake(x + legendLineWidth * 0.1 + halfLineLength + inflexionWidthSpacer, y + (singleRowHeight - pdata.lineWidth) / 2, halfLineLength, pdata.lineWidth)];
line.backgroundColor = pdata.color;
line.alpha = pdata.alpha;
[legendLines addObject:line];
[legendViews addObject:line];
}
// Add inflexion type
[legendInflexion addObject:[self drawInflexion:pdata.inflexionPointWidth
center:CGPointMake(x + legendLineWidth / 2, y + labelsize.height / 2)
strokeWidth:2
[legendViews addObject:[self drawInflexion:pdata.inflexionPointWidth
center:CGPointMake(x + legendLineWidth / 2, y + singleRowHeight / 2)
strokeWidth:pdata.lineWidth
inflexionStyle:pdata.inflexionPointStyle
andColor:pdata.color
andAlpha:pdata.alpha]];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x + legendLineWidth, y, maxLabelWidth, labelsize.height)];
label.text = pdata.dataTitle;
label.font = [UIFont systemFontOfSize:self.legendFontSize];
label.lineBreakMode = NSLineBreakByWordWrapping;
label.numberOfLines = 0;
x += self.legendStyle == PNLegendItemStyleStacked ? 0 : labelsize.width + legendLineWidth;
y += self.legendStyle == PNLegendItemStyleStacked ? labelsize.height : 0;
totalWidth = self.legendStyle == PNLegendItemStyleStacked ? fmaxf(totalWidth, labelsize.width + legendLineWidth) : totalWidth + labelsize.width + legendLineWidth;
totalHeight = self.legendStyle == PNLegendItemStyleStacked ? fmaxf(totalHeight, labelsize.height) : totalHeight + labelsize.height;
[legendLabels addObject:label];
}
[legendViews addObject:label];
UIView *legend = [[UIView alloc] initWithFrame:CGRectMake(100, 400, totalWidth, totalHeight)];
for (UILabel *l in legendLabels) {
[legend addSubview:l];
}
for (UIView* v in legendLines) {
[legend addSubview:v];
}
for (UIImageView *iv in legendInflexion) {
UIView *legend = [[UIView alloc] initWithFrame:CGRectMake(0, 0, totalWidth, totalHeight)];
[legend addSubview:iv];
for (UIView* v in legendViews) {
[legend addSubview:v];
}
return legend;
}
//// PaintCode Trial Version
//// www.paintcodeapp.com
- (UIImageView*)drawInflexion:(CGFloat)size center:(CGPoint)center strokeWidth: (CGFloat)sw inflexionStyle:(PNLineChartPointStyle)type andColor:(UIColor*)color andAlpha:(CGFloat) alfa
{
//this is an arbitrary size for example
//Make the size a little bigger so it includes also border stroke
CGSize aSize = CGSizeMake(size + sw, size + sw);
//this can take any CGSize
//it works like the frame.size would in the drawRect: method
//in the way that it represents the context's size
UIGraphicsBeginImageContextWithOptions(aSize, NO, 0.0);
//this gets the graphic context
UIGraphicsBeginImageContextWithOptions(aSize, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
if (type == PNLineChartPointStyleCircle) {
//// Oval Drawing
CGContextAddArc(context, (size + sw)/2, (size + sw) / 2, size/2, 0, M_PI*2, YES);
}else if (type == PNLineChartPointStyleSquare){
CGContextAddRect(context, CGRectMake(sw/2, sw/2, size, size));
... ... @@ -832,12 +849,12 @@
CGContextClosePath(context);
}
//Set the width of the pen mark
//Set some stroke properties
CGContextSetLineWidth(context, sw);
CGContextSetAlpha(context, alfa);
CGContextSetStrokeColorWithColor(context, color.CGColor);
//Finally draw
CGContextDrawPath(context, kCGPathStroke);
//now get the image from the context
... ... @@ -845,14 +862,13 @@
UIGraphicsEndImageContext();
//// Variable Declarations
//// Translate origin
CGFloat originX = center.x - (size + sw) / 2.0;
CGFloat originY = center.y - (size + sw) / 2.0;
UIImageView *squareImageView = [[UIImageView alloc]initWithImage:squareImage];
[squareImageView setFrame:CGRectMake(originX, originY, size + sw, size + sw)];
return squareImageView;
}
@end
... ...
... ... @@ -47,11 +47,11 @@
// Line Chart #2
NSArray * data02Array = @[@20.1, @180.1, @26.4, @202.2, @126.2, @167.2, @276.2];
PNLineChartData *data02 = [PNLineChartData new];
data02.dataTitle = @"Beta";
data02.dataTitle = @"Beta Beta Beta Beta";
data02.color = PNTwitterColor;
data02.alpha = 0.5f;
data02.itemCount = data02Array.count;
data02.inflexionPointStyle = PNLineChartPointStyleCircle;
data02.inflexionPointStyle = PNLineChartPointStyleNone;
data02.getData = ^(NSUInteger index) {
CGFloat yValue = [data02Array[index] floatValue];
return [PNLineChartDataItem dataItemWithY:yValue];
... ... @@ -63,9 +63,12 @@
[self.view addSubview:self.lineChart];
self.lineChart.legendStyle = PNLegendItemStyleSerial;
self.lineChart.legendFontSize = 17.0;
[self.view addSubview:[self.lineChart getLegendWithMaxWidth:200]];
self.lineChart.legendStyle = PNLegendItemStyleStacked;
self.lineChart.legendFontSize = 12.0;
UIView *legend = [self.lineChart getLegendWithMaxWidth:200];
[legend setFrame:CGRectMake(100, 400, legend.frame.size.width, legend.frame.size.width)];
[self.view addSubview:legend];
}
else if ([self.title isEqualToString:@"Bar Chart"])
{
... ...