andi

improving serial legend

@@ -25,6 +25,7 @@ typedef NS_ENUM(NSUInteger, PNLegendItemStyle) { @@ -25,6 +25,7 @@ typedef NS_ENUM(NSUInteger, PNLegendItemStyle) {
25 @property (assign, nonatomic) BOOL hasLegend; 25 @property (assign, nonatomic) BOOL hasLegend;
26 @property (assign, nonatomic) PNLegendPosition legendPosition; 26 @property (assign, nonatomic) PNLegendPosition legendPosition;
27 @property (assign, nonatomic) PNLegendItemStyle legendStyle; 27 @property (assign, nonatomic) PNLegendItemStyle legendStyle;
  28 +@property (assign, nonatomic) NSUInteger labelRowsInSerialMode;
28 @property (assign, nonatomic) CGFloat legendFontSize; 29 @property (assign, nonatomic) CGFloat legendFontSize;
29 30
30 /** 31 /**
@@ -37,4 +38,6 @@ typedef NS_ENUM(NSUInteger, PNLegendItemStyle) { @@ -37,4 +38,6 @@ typedef NS_ENUM(NSUInteger, PNLegendItemStyle) {
37 */ 38 */
38 - (UIView*) getLegendWithMaxWidth:(CGFloat)mWidth; 39 - (UIView*) getLegendWithMaxWidth:(CGFloat)mWidth;
39 40
  41 +
  42 +- (void) setupDefaultValues;
40 @end 43 @end
@@ -24,37 +24,11 @@ @@ -24,37 +24,11 @@
24 } 24 }
25 */ 25 */
26 26
27 -- (UIView*) drawLegend{  
28 - return nil;  
29 -}  
30 -  
31 -  
32 -- (id)initWithCoder:(NSCoder *)coder  
33 -{  
34 - self = [super initWithCoder:coder];  
35 -  
36 - if (self) {  
37 - [self setupDefaultValues];  
38 - }  
39 -  
40 - return self;  
41 -}  
42 -  
43 -- (id)initWithFrame:(CGRect)frame  
44 -{  
45 - self = [super initWithFrame:frame];  
46 -  
47 - if (self) {  
48 - [self setupDefaultValues];  
49 - }  
50 -  
51 - return self;  
52 -}  
53 -  
54 - (void) setupDefaultValues{ 27 - (void) setupDefaultValues{
55 self.hasLegend = YES; 28 self.hasLegend = YES;
56 self.legendPosition = PNLegendPositionBottom; 29 self.legendPosition = PNLegendPositionBottom;
57 self.legendStyle = PNLegendItemStyleStacked; 30 self.legendStyle = PNLegendItemStyleStacked;
  31 + self.labelRowsInSerialMode = 1;
58 } 32 }
59 33
60 34
@@ -67,6 +41,13 @@ @@ -67,6 +41,13 @@
67 return nil; 41 return nil;
68 } 42 }
69 43
  44 +- (void) setLabelRowsInSerialMode:(NSUInteger)num{
  45 + if (self.legendStyle == PNLegendItemStyleSerial) {
  46 + _labelRowsInSerialMode = num;
  47 + }else{
  48 + _labelRowsInSerialMode = 1;
  49 + }
  50 +}
70 51
71 52
72 @end 53 @end
@@ -66,7 +66,6 @@ @@ -66,7 +66,6 @@
66 _yChartLabels = [NSMutableArray new]; 66 _yChartLabels = [NSMutableArray new];
67 } 67 }
68 68
69 -#warning modify origin  
70 if (yStep == 0.0) { 69 if (yStep == 0.0) {
71 PNChartLabel *minLabel = [[PNChartLabel alloc] initWithFrame:CGRectMake(0.0, (NSInteger)_chartCavanHeight, (NSInteger)_chartMargin, (NSInteger)_yLabelHeight)]; 70 PNChartLabel *minLabel = [[PNChartLabel alloc] initWithFrame:CGRectMake(0.0, (NSInteger)_chartCavanHeight, (NSInteger)_chartMargin, (NSInteger)_yLabelHeight)];
72 minLabel.text = [NSString stringWithFormat:yLabelFormat, 0.0]; 71 minLabel.text = [NSString stringWithFormat:yLabelFormat, 0.0];
@@ -149,7 +148,6 @@ @@ -149,7 +148,6 @@
149 for (int index = 0; index < xLabels.count; index++) { 148 for (int index = 0; index < xLabels.count; index++) {
150 labelText = xLabels[index]; 149 labelText = xLabels[index];
151 150
152 -#warning modify origin  
153 NSInteger x = 2 * _chartMargin + (index * _xLabelWidth) - (_xLabelWidth / 2); 151 NSInteger x = 2 * _chartMargin + (index * _xLabelWidth) - (_xLabelWidth / 2);
154 NSInteger y = _chartMargin + _chartCavanHeight; 152 NSInteger y = _chartMargin + _chartCavanHeight;
155 153
@@ -355,7 +353,6 @@ @@ -355,7 +353,6 @@
355 353
356 CGFloat offSetX = (_chartCavanWidth) / (chartData.itemCount); 354 CGFloat offSetX = (_chartCavanWidth) / (chartData.itemCount);
357 355
358 -#warning modify chart path  
359 int x = 2 * _chartMargin + (i * offSetX); 356 int x = 2 * _chartMargin + (i * offSetX);
360 int y = _chartCavanHeight - (innerGrade * _chartCavanHeight) + (_yLabelHeight / 2); 357 int y = _chartCavanHeight - (innerGrade * _chartCavanHeight) + (_yLabelHeight / 2);
361 358
@@ -604,7 +601,6 @@ @@ -604,7 +601,6 @@
604 - (void)drawRect:(CGRect)rect 601 - (void)drawRect:(CGRect)rect
605 { 602 {
606 if (self.isShowCoordinateAxis) { 603 if (self.isShowCoordinateAxis) {
607 -#warning modify  
608 CGFloat yAxisOffset = 10.f; 604 CGFloat yAxisOffset = 10.f;
609 605
610 CGContextRef ctx = UIGraphicsGetCurrentContext(); 606 CGContextRef ctx = UIGraphicsGetCurrentContext();
@@ -678,6 +674,7 @@ @@ -678,6 +674,7 @@
678 674
679 - (void)setupDefaultValues 675 - (void)setupDefaultValues
680 { 676 {
  677 + [super setupDefaultValues];
681 // Initialization code 678 // Initialization code
682 self.backgroundColor = [UIColor whiteColor]; 679 self.backgroundColor = [UIColor whiteColor];
683 self.clipsToBounds = YES; 680 self.clipsToBounds = YES;
@@ -758,15 +755,15 @@ @@ -758,15 +755,15 @@
758 CGFloat x = 0; 755 CGFloat x = 0;
759 CGFloat y = 0; 756 CGFloat y = 0;
760 757
761 - /* accumulated width and height */ 758 + /* accumulated height */
762 - CGFloat totalWidth = 0;  
763 CGFloat totalHeight = 0; 759 CGFloat totalHeight = 0;
764 760
765 NSMutableArray *legendViews = [[NSMutableArray alloc] init]; 761 NSMutableArray *legendViews = [[NSMutableArray alloc] init];
766 762
  763 + NSUInteger numLabelsPerRow = ceil((float)[self.chartData count] / self.labelRowsInSerialMode);
767 764
768 /* Determine the max width of each legend item */ 765 /* Determine the max width of each legend item */
769 - CGFloat maxLabelWidth = self.legendStyle == PNLegendItemStyleStacked ? (mWidth - legendLineWidth) : (mWidth / [self.chartData count] - legendLineWidth); 766 + CGFloat maxLabelWidth = self.legendStyle == PNLegendItemStyleStacked ? (mWidth - legendLineWidth) : (mWidth / numLabelsPerRow - legendLineWidth);
770 767
771 /* this is used when labels wrap text and the line 768 /* this is used when labels wrap text and the line
772 * should be in the middle of the first row */ 769 * should be in the middle of the first row */
@@ -774,6 +771,9 @@ @@ -774,6 +771,9 @@
774 withWidth:MAXFLOAT 771 withWidth:MAXFLOAT
775 font:[UIFont systemFontOfSize:self.legendFontSize]].height; 772 font:[UIFont systemFontOfSize:self.legendFontSize]].height;
776 773
  774 + NSUInteger counter = 0;
  775 + NSUInteger rowMaxHeight = 0;
  776 +
777 for (PNLineChartData *pdata in self.chartData) { 777 for (PNLineChartData *pdata in self.chartData) {
778 /* Expected label size*/ 778 /* Expected label size*/
779 CGSize labelsize = [PNLineChart sizeOfString:pdata.dataTitle 779 CGSize labelsize = [PNLineChart sizeOfString:pdata.dataTitle
@@ -782,6 +782,11 @@ @@ -782,6 +782,11 @@
782 782
783 /* draw lines */ 783 /* draw lines */
784 784
  785 + if (counter != 0 && counter % numLabelsPerRow == 0) {
  786 + x = 0;
  787 + y += rowMaxHeight;
  788 + rowMaxHeight = 0;
  789 + }
785 790
786 /* If there is inflection decorator, the line is composed of two lines 791 /* If there is inflection decorator, the line is composed of two lines
787 * and this is the space that separates two lines in order to put inflection 792 * and this is the space that separates two lines in order to put inflection
@@ -823,16 +828,18 @@ @@ -823,16 +828,18 @@
823 label.font = [UIFont systemFontOfSize:self.legendFontSize]; 828 label.font = [UIFont systemFontOfSize:self.legendFontSize];
824 label.lineBreakMode = NSLineBreakByWordWrapping; 829 label.lineBreakMode = NSLineBreakByWordWrapping;
825 label.numberOfLines = 0; 830 label.numberOfLines = 0;
  831 +
  832 + rowMaxHeight = fmaxf(rowMaxHeight, labelsize.height);
826 x += self.legendStyle == PNLegendItemStyleStacked ? 0 : labelsize.width + legendLineWidth; 833 x += self.legendStyle == PNLegendItemStyleStacked ? 0 : labelsize.width + legendLineWidth;
827 y += self.legendStyle == PNLegendItemStyleStacked ? labelsize.height : 0; 834 y += self.legendStyle == PNLegendItemStyleStacked ? labelsize.height : 0;
828 835
829 - totalWidth = self.legendStyle == PNLegendItemStyleStacked ? fmaxf(totalWidth, labelsize.width + legendLineWidth) : totalWidth + labelsize.width + legendLineWidth;  
830 - totalHeight = self.legendStyle == PNLegendItemStyleStacked ? fmaxf(totalHeight, labelsize.height) : totalHeight + labelsize.height;  
831 - [legendViews addObject:label];  
832 836
  837 + totalHeight = self.legendStyle == PNLegendItemStyleStacked ? fmaxf(totalHeight, rowMaxHeight + y) : totalHeight + labelsize.height;
  838 + [legendViews addObject:label];
  839 + counter++;
833 } 840 }
834 841
835 - UIView *legend = [[UIView alloc] initWithFrame:CGRectMake(0, 0, totalWidth, totalHeight)]; 842 + UIView *legend = [[UIView alloc] initWithFrame:CGRectMake(0, 0, mWidth, totalHeight)];
836 843
837 for (UIView* v in legendViews) { 844 for (UIView* v in legendViews) {
838 [legend addSubview:v]; 845 [legend addSubview:v];
@@ -57,6 +57,7 @@ @@ -57,6 +57,7 @@
57 _descriptionTextShadowOffset = CGSizeMake(0, 1); 57 _descriptionTextShadowOffset = CGSizeMake(0, 1);
58 _duration = 1.0; 58 _duration = 1.0;
59 59
  60 + [super setupDefaultValues];
60 [self loadDefault]; 61 [self loadDefault];
61 } 62 }
62 63
@@ -268,14 +269,14 @@ @@ -268,14 +269,14 @@
268 CGFloat y = 0; 269 CGFloat y = 0;
269 270
270 /* accumulated width and height */ 271 /* accumulated width and height */
271 - CGFloat totalWidth = 0;  
272 CGFloat totalHeight = 0; 272 CGFloat totalHeight = 0;
273 273
274 NSMutableArray *legendViews = [[NSMutableArray alloc] init]; 274 NSMutableArray *legendViews = [[NSMutableArray alloc] init];
275 275
  276 + NSUInteger numLabelsPerRow = ceil((float)[self.items count] / self.labelRowsInSerialMode);
276 277
277 /* Determine the max width of each legend item */ 278 /* Determine the max width of each legend item */
278 - CGFloat maxLabelWidth = self.legendStyle == PNLegendItemStyleStacked ? (mWidth - beforeLabel) : (mWidth / [self.items count] - beforeLabel); 279 + CGFloat maxLabelWidth = self.legendStyle == PNLegendItemStyleStacked ? (mWidth - beforeLabel) : (mWidth / numLabelsPerRow - beforeLabel);
279 280
280 /* this is used when labels wrap text and the line 281 /* this is used when labels wrap text and the line
281 * should be in the middle of the first row */ 282 * should be in the middle of the first row */
@@ -283,6 +284,9 @@ @@ -283,6 +284,9 @@
283 withWidth:MAXFLOAT 284 withWidth:MAXFLOAT
284 font:[UIFont systemFontOfSize:self.legendFontSize]].height; 285 font:[UIFont systemFontOfSize:self.legendFontSize]].height;
285 286
  287 + NSUInteger counter = 0;
  288 + NSUInteger rowMaxHeight = 0;
  289 +
286 for (PNPieChartDataItem *pdata in self.items) { 290 for (PNPieChartDataItem *pdata in self.items) {
287 /* Expected label size*/ 291 /* Expected label size*/
288 CGSize labelsize = [PNLineChart sizeOfString:pdata.textDescription 292 CGSize labelsize = [PNLineChart sizeOfString:pdata.textDescription
@@ -290,25 +294,36 @@ @@ -290,25 +294,36 @@
290 font:[UIFont systemFontOfSize:self.legendFontSize]]; 294 font:[UIFont systemFontOfSize:self.legendFontSize]];
291 295
292 296
  297 + if (counter != 0 && counter % numLabelsPerRow == 0) {
  298 + x = 0;
  299 + y += rowMaxHeight;
  300 + rowMaxHeight = 0;
  301 + }
  302 +
293 // Add inflexion type 303 // Add inflexion type
294 [legendViews addObject:[self drawInflexion:legendCircle * .6 304 [legendViews addObject:[self drawInflexion:legendCircle * .6
295 center:CGPointMake(x + legendCircle / 2, y + singleRowHeight / 2) 305 center:CGPointMake(x + legendCircle / 2, y + singleRowHeight / 2)
296 andColor:pdata.color]]; 306 andColor:pdata.color]];
297 307
  308 +
298 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x + beforeLabel, y, maxLabelWidth, labelsize.height)]; 309 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x + beforeLabel, y, maxLabelWidth, labelsize.height)];
299 label.text = pdata.textDescription; 310 label.text = pdata.textDescription;
300 label.font = [UIFont systemFontOfSize:self.legendFontSize]; 311 label.font = [UIFont systemFontOfSize:self.legendFontSize];
301 label.lineBreakMode = NSLineBreakByWordWrapping; 312 label.lineBreakMode = NSLineBreakByWordWrapping;
302 label.numberOfLines = 0; 313 label.numberOfLines = 0;
303 - x += self.legendStyle == PNLegendItemStyleStacked ? 0 : labelsize.width + beforeLabel; 314 +
  315 +
  316 + rowMaxHeight = fmaxf(rowMaxHeight, labelsize.height);
  317 + x += self.legendStyle == PNLegendItemStyleStacked ? 0 : maxLabelWidth + beforeLabel;
304 y += self.legendStyle == PNLegendItemStyleStacked ? labelsize.height : 0; 318 y += self.legendStyle == PNLegendItemStyleStacked ? labelsize.height : 0;
305 319
306 - totalWidth = self.legendStyle == PNLegendItemStyleStacked ? fmaxf(totalWidth, labelsize.width + beforeLabel) : totalWidth + labelsize.width + beforeLabel; 320 +
307 - totalHeight = self.legendStyle == PNLegendItemStyleSerial ? fmaxf(totalHeight, labelsize.height) : totalHeight + labelsize.height; 321 + totalHeight = self.legendStyle == PNLegendItemStyleSerial ? fmaxf(totalHeight, rowMaxHeight + y) : totalHeight + labelsize.height;
308 [legendViews addObject:label]; 322 [legendViews addObject:label];
  323 + counter ++;
309 } 324 }
310 325
311 - UIView *legend = [[UIView alloc] initWithFrame:CGRectMake(0, 0, totalWidth, totalHeight)]; 326 + UIView *legend = [[UIView alloc] initWithFrame:CGRectMake(0, 0, mWidth, totalHeight)];
312 327
313 for (UIView* v in legendViews) { 328 for (UIView* v in legendViews) {
314 [legend addSubview:v]; 329 [legend addSubview:v];
@@ -20,6 +20,7 @@ @@ -20,6 +20,7 @@
20 self.rightSwitch.hidden = YES; 20 self.rightSwitch.hidden = YES;
21 self.leftLabel.hidden = YES; 21 self.leftLabel.hidden = YES;
22 self.rightLabel.hidden = YES; 22 self.rightLabel.hidden = YES;
  23 + self.changeValueButton.hidden = YES;
23 24
24 if ([self.title isEqualToString:@"Line Chart"]) { 25 if ([self.title isEqualToString:@"Line Chart"]) {
25 26
@@ -56,7 +57,7 @@ @@ -56,7 +57,7 @@
56 data02.color = PNTwitterColor; 57 data02.color = PNTwitterColor;
57 data02.alpha = 0.5f; 58 data02.alpha = 0.5f;
58 data02.itemCount = data02Array.count; 59 data02.itemCount = data02Array.count;
59 - data02.inflexionPointStyle = PNLineChartPointStyleNone; 60 + data02.inflexionPointStyle = PNLineChartPointStyleCircle;
60 data02.getData = ^(NSUInteger index) { 61 data02.getData = ^(NSUInteger index) {
61 CGFloat yValue = [data02Array[index] floatValue]; 62 CGFloat yValue = [data02Array[index] floatValue];
62 return [PNLineChartDataItem dataItemWithY:yValue]; 63 return [PNLineChartDataItem dataItemWithY:yValue];
@@ -68,11 +69,11 @@ @@ -68,11 +69,11 @@
68 69
69 70
70 [self.view addSubview:self.lineChart]; 71 [self.view addSubview:self.lineChart];
71 - self.lineChart.legendStyle = PNLegendItemStyleStacked; 72 + self.lineChart.legendStyle = PNLegendItemStyleSerial;
72 self.lineChart.legendFontSize = 12.0; 73 self.lineChart.legendFontSize = 12.0;
73 74
74 - UIView *legend = [self.lineChart getLegendWithMaxWidth:200]; 75 + UIView *legend = [self.lineChart getLegendWithMaxWidth:320];
75 - [legend setFrame:CGRectMake(100, 400, legend.frame.size.width, legend.frame.size.width)]; 76 + [legend setFrame:CGRectMake(30, 340, legend.frame.size.width, legend.frame.size.width)];
76 [self.view addSubview:legend]; 77 [self.view addSubview:legend];
77 } 78 }
78 else if ([self.title isEqualToString:@"Bar Chart"]) 79 else if ([self.title isEqualToString:@"Bar Chart"])
@@ -145,7 +146,7 @@ @@ -145,7 +146,7 @@
145 self.pieChart.legendFontSize = 12.0; 146 self.pieChart.legendFontSize = 12.0;
146 147
147 UIView *legend = [self.pieChart getLegendWithMaxWidth:200]; 148 UIView *legend = [self.pieChart getLegendWithMaxWidth:200];
148 - [legend setFrame:CGRectMake(100, 400, legend.frame.size.width, legend.frame.size.width)]; 149 + [legend setFrame:CGRectMake(130, 350, legend.frame.size.width, legend.frame.size.height)];
149 [self.view addSubview:legend]; 150 [self.view addSubview:legend];
150 151
151 [self.view addSubview:self.pieChart]; 152 [self.view addSubview:self.pieChart];