andi

improving serial legend

... ... @@ -25,6 +25,7 @@ typedef NS_ENUM(NSUInteger, PNLegendItemStyle) {
@property (assign, nonatomic) BOOL hasLegend;
@property (assign, nonatomic) PNLegendPosition legendPosition;
@property (assign, nonatomic) PNLegendItemStyle legendStyle;
@property (assign, nonatomic) NSUInteger labelRowsInSerialMode;
@property (assign, nonatomic) CGFloat legendFontSize;
/**
... ... @@ -37,4 +38,6 @@ typedef NS_ENUM(NSUInteger, PNLegendItemStyle) {
*/
- (UIView*) getLegendWithMaxWidth:(CGFloat)mWidth;
- (void) setupDefaultValues;
@end
... ...
... ... @@ -24,37 +24,11 @@
}
*/
- (UIView*) drawLegend{
return nil;
}
- (id)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
[self setupDefaultValues];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setupDefaultValues];
}
return self;
}
- (void) setupDefaultValues{
self.hasLegend = YES;
self.legendPosition = PNLegendPositionBottom;
self.legendStyle = PNLegendItemStyleStacked;
self.labelRowsInSerialMode = 1;
}
... ... @@ -67,6 +41,13 @@
return nil;
}
- (void) setLabelRowsInSerialMode:(NSUInteger)num{
if (self.legendStyle == PNLegendItemStyleSerial) {
_labelRowsInSerialMode = num;
}else{
_labelRowsInSerialMode = 1;
}
}
@end
... ...
... ... @@ -66,7 +66,6 @@
_yChartLabels = [NSMutableArray new];
}
#warning modify origin
if (yStep == 0.0) {
PNChartLabel *minLabel = [[PNChartLabel alloc] initWithFrame:CGRectMake(0.0, (NSInteger)_chartCavanHeight, (NSInteger)_chartMargin, (NSInteger)_yLabelHeight)];
minLabel.text = [NSString stringWithFormat:yLabelFormat, 0.0];
... ... @@ -149,7 +148,6 @@
for (int index = 0; index < xLabels.count; index++) {
labelText = xLabels[index];
#warning modify origin
NSInteger x = 2 * _chartMargin + (index * _xLabelWidth) - (_xLabelWidth / 2);
NSInteger y = _chartMargin + _chartCavanHeight;
... ... @@ -355,7 +353,6 @@
CGFloat offSetX = (_chartCavanWidth) / (chartData.itemCount);
#warning modify chart path
int x = 2 * _chartMargin + (i * offSetX);
int y = _chartCavanHeight - (innerGrade * _chartCavanHeight) + (_yLabelHeight / 2);
... ... @@ -604,7 +601,6 @@
- (void)drawRect:(CGRect)rect
{
if (self.isShowCoordinateAxis) {
#warning modify
CGFloat yAxisOffset = 10.f;
CGContextRef ctx = UIGraphicsGetCurrentContext();
... ... @@ -678,6 +674,7 @@
- (void)setupDefaultValues
{
[super setupDefaultValues];
// Initialization code
self.backgroundColor = [UIColor whiteColor];
self.clipsToBounds = YES;
... ... @@ -758,15 +755,15 @@
CGFloat x = 0;
CGFloat y = 0;
/* accumulated width and height */
CGFloat totalWidth = 0;
/* accumulated height */
CGFloat totalHeight = 0;
NSMutableArray *legendViews = [[NSMutableArray alloc] init];
NSUInteger numLabelsPerRow = ceil((float)[self.chartData count] / self.labelRowsInSerialMode);
/* Determine the max width of each legend item */
CGFloat maxLabelWidth = self.legendStyle == PNLegendItemStyleStacked ? (mWidth - legendLineWidth) : (mWidth / [self.chartData count] - legendLineWidth);
CGFloat maxLabelWidth = self.legendStyle == PNLegendItemStyleStacked ? (mWidth - legendLineWidth) : (mWidth / numLabelsPerRow - legendLineWidth);
/* this is used when labels wrap text and the line
* should be in the middle of the first row */
... ... @@ -774,6 +771,9 @@
withWidth:MAXFLOAT
font:[UIFont systemFontOfSize:self.legendFontSize]].height;
NSUInteger counter = 0;
NSUInteger rowMaxHeight = 0;
for (PNLineChartData *pdata in self.chartData) {
/* Expected label size*/
CGSize labelsize = [PNLineChart sizeOfString:pdata.dataTitle
... ... @@ -782,6 +782,11 @@
/* draw lines */
if (counter != 0 && counter % numLabelsPerRow == 0) {
x = 0;
y += rowMaxHeight;
rowMaxHeight = 0;
}
/* 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
... ... @@ -823,16 +828,18 @@
label.font = [UIFont systemFontOfSize:self.legendFontSize];
label.lineBreakMode = NSLineBreakByWordWrapping;
label.numberOfLines = 0;
rowMaxHeight = fmaxf(rowMaxHeight, labelsize.height);
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;
[legendViews addObject:label];
totalHeight = self.legendStyle == PNLegendItemStyleStacked ? fmaxf(totalHeight, rowMaxHeight + y) : totalHeight + labelsize.height;
[legendViews addObject:label];
counter++;
}
UIView *legend = [[UIView alloc] initWithFrame:CGRectMake(0, 0, totalWidth, totalHeight)];
UIView *legend = [[UIView alloc] initWithFrame:CGRectMake(0, 0, mWidth, totalHeight)];
for (UIView* v in legendViews) {
[legend addSubview:v];
... ...
... ... @@ -57,6 +57,7 @@
_descriptionTextShadowOffset = CGSizeMake(0, 1);
_duration = 1.0;
[super setupDefaultValues];
[self loadDefault];
}
... ... @@ -268,14 +269,14 @@
CGFloat y = 0;
/* accumulated width and height */
CGFloat totalWidth = 0;
CGFloat totalHeight = 0;
NSMutableArray *legendViews = [[NSMutableArray alloc] init];
NSUInteger numLabelsPerRow = ceil((float)[self.items count] / self.labelRowsInSerialMode);
/* Determine the max width of each legend item */
CGFloat maxLabelWidth = self.legendStyle == PNLegendItemStyleStacked ? (mWidth - beforeLabel) : (mWidth / [self.items count] - beforeLabel);
CGFloat maxLabelWidth = self.legendStyle == PNLegendItemStyleStacked ? (mWidth - beforeLabel) : (mWidth / numLabelsPerRow - beforeLabel);
/* this is used when labels wrap text and the line
* should be in the middle of the first row */
... ... @@ -283,6 +284,9 @@
withWidth:MAXFLOAT
font:[UIFont systemFontOfSize:self.legendFontSize]].height;
NSUInteger counter = 0;
NSUInteger rowMaxHeight = 0;
for (PNPieChartDataItem *pdata in self.items) {
/* Expected label size*/
CGSize labelsize = [PNLineChart sizeOfString:pdata.textDescription
... ... @@ -290,25 +294,36 @@
font:[UIFont systemFontOfSize:self.legendFontSize]];
if (counter != 0 && counter % numLabelsPerRow == 0) {
x = 0;
y += rowMaxHeight;
rowMaxHeight = 0;
}
// Add inflexion type
[legendViews addObject:[self drawInflexion:legendCircle * .6
center:CGPointMake(x + legendCircle / 2, y + singleRowHeight / 2)
andColor:pdata.color]];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x + beforeLabel, 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 + beforeLabel;
rowMaxHeight = fmaxf(rowMaxHeight, labelsize.height);
x += self.legendStyle == PNLegendItemStyleStacked ? 0 : maxLabelWidth + beforeLabel;
y += self.legendStyle == PNLegendItemStyleStacked ? labelsize.height : 0;
totalWidth = self.legendStyle == PNLegendItemStyleStacked ? fmaxf(totalWidth, labelsize.width + beforeLabel) : totalWidth + labelsize.width + beforeLabel;
totalHeight = self.legendStyle == PNLegendItemStyleSerial ? fmaxf(totalHeight, labelsize.height) : totalHeight + labelsize.height;
totalHeight = self.legendStyle == PNLegendItemStyleSerial ? fmaxf(totalHeight, rowMaxHeight + y) : totalHeight + labelsize.height;
[legendViews addObject:label];
counter ++;
}
UIView *legend = [[UIView alloc] initWithFrame:CGRectMake(0, 0, totalWidth, totalHeight)];
UIView *legend = [[UIView alloc] initWithFrame:CGRectMake(0, 0, mWidth, totalHeight)];
for (UIView* v in legendViews) {
[legend addSubview:v];
... ...
... ... @@ -20,6 +20,7 @@
self.rightSwitch.hidden = YES;
self.leftLabel.hidden = YES;
self.rightLabel.hidden = YES;
self.changeValueButton.hidden = YES;
if ([self.title isEqualToString:@"Line Chart"]) {
... ... @@ -56,7 +57,7 @@
data02.color = PNTwitterColor;
data02.alpha = 0.5f;
data02.itemCount = data02Array.count;
data02.inflexionPointStyle = PNLineChartPointStyleNone;
data02.inflexionPointStyle = PNLineChartPointStyleCircle;
data02.getData = ^(NSUInteger index) {
CGFloat yValue = [data02Array[index] floatValue];
return [PNLineChartDataItem dataItemWithY:yValue];
... ... @@ -68,11 +69,11 @@
[self.view addSubview:self.lineChart];
self.lineChart.legendStyle = PNLegendItemStyleStacked;
self.lineChart.legendStyle = PNLegendItemStyleSerial;
self.lineChart.legendFontSize = 12.0;
UIView *legend = [self.lineChart getLegendWithMaxWidth:200];
[legend setFrame:CGRectMake(100, 400, legend.frame.size.width, legend.frame.size.width)];
UIView *legend = [self.lineChart getLegendWithMaxWidth:320];
[legend setFrame:CGRectMake(30, 340, legend.frame.size.width, legend.frame.size.width)];
[self.view addSubview:legend];
}
else if ([self.title isEqualToString:@"Bar Chart"])
... ... @@ -145,7 +146,7 @@
self.pieChart.legendFontSize = 12.0;
UIView *legend = [self.pieChart getLegendWithMaxWidth:200];
[legend setFrame:CGRectMake(100, 400, legend.frame.size.width, legend.frame.size.width)];
[legend setFrame:CGRectMake(130, 350, legend.frame.size.width, legend.frame.size.height)];
[self.view addSubview:legend];
[self.view addSubview:self.pieChart];
... ...