Kevin

Merge pull request #174 from lunzii/master

Add PNLineChart with setYLabels.
@@ -47,6 +47,8 @@ @@ -47,6 +47,8 @@
47 @property (nonatomic) CGFloat chartCavanWidth; 47 @property (nonatomic) CGFloat chartCavanWidth;
48 @property (nonatomic) CGFloat chartMargin; 48 @property (nonatomic) CGFloat chartMargin;
49 @property (nonatomic) BOOL showLabel; 49 @property (nonatomic) BOOL showLabel;
  50 +@property (nonatomic) BOOL showGenYLabels;
  51 +
50 52
51 /** 53 /**
52 * Controls whether to show the coordinate axis. Default is NO. 54 * Controls whether to show the coordinate axis. Default is NO.
@@ -52,7 +52,7 @@ @@ -52,7 +52,7 @@
52 52
53 #pragma mark instance methods 53 #pragma mark instance methods
54 54
55 -- (void)setYLabels:(NSArray *)yLabels 55 +- (void)setYLabels
56 { 56 {
57 CGFloat yStep = (_yValueMax - _yValueMin) / _yLabelNum; 57 CGFloat yStep = (_yValueMax - _yValueMin) / _yLabelNum;
58 CGFloat yStepHeight = _chartCavanHeight / _yLabelNum; 58 CGFloat yStepHeight = _chartCavanHeight / _yLabelNum;
@@ -104,6 +104,54 @@ @@ -104,6 +104,54 @@
104 } 104 }
105 } 105 }
106 106
  107 +- (void)setYLabels:(NSArray *)yLabels
  108 +{
  109 + _showGenYLabels = NO;
  110 + _yLabelNum = yLabels.count - 1;
  111 +
  112 + CGFloat yLabelHeight;
  113 + if (_showLabel) {
  114 + yLabelHeight = _chartCavanHeight / [yLabels count];
  115 + } else {
  116 + yLabelHeight = (self.frame.size.height) / [yLabels count];
  117 + }
  118 +
  119 + return [self setYLabels:yLabels withHeight:yLabelHeight];
  120 +}
  121 +
  122 +- (void)setYLabels:(NSArray *)yLabels withHeight:(CGFloat)height
  123 +{
  124 + _yLabels = yLabels;
  125 + _yLabelHeight = height;
  126 + if (_yChartLabels) {
  127 + for (PNChartLabel * label in _yChartLabels) {
  128 + [label removeFromSuperview];
  129 + }
  130 + }else{
  131 + _yChartLabels = [NSMutableArray new];
  132 + }
  133 +
  134 + NSString *labelText;
  135 +
  136 + if (_showLabel) {
  137 + CGFloat yStepHeight = _chartCavanHeight / _yLabelNum;
  138 +
  139 + for (int index = 0; index < yLabels.count; index++) {
  140 + labelText = yLabels[index];
  141 +
  142 +#warning modify origin
  143 + NSInteger y = (NSInteger)(_chartCavanHeight - index * yStepHeight);
  144 +
  145 + PNChartLabel *label = [[PNChartLabel alloc] initWithFrame:CGRectMake(0.0, y, (NSInteger)_chartMargin, (NSInteger)_yLabelHeight)];
  146 + [label setTextAlignment:NSTextAlignmentRight];
  147 + label.text = labelText;
  148 + [self setCustomStyleForYLabel:label];
  149 + [self addSubview:label];
  150 + [_yChartLabels addObject:label];
  151 + }
  152 + }
  153 +}
  154 +
107 - (CGFloat)computeEqualWidthForXLabels:(NSArray *)xLabels 155 - (CGFloat)computeEqualWidthForXLabels:(NSArray *)xLabels
108 { 156 {
109 CGFloat xLabelWidth; 157 CGFloat xLabelWidth;
@@ -548,8 +596,8 @@ @@ -548,8 +596,8 @@
548 _yValueMin = _yFixedValueMin ? _yFixedValueMin : yMin ; 596 _yValueMin = _yFixedValueMin ? _yFixedValueMin : yMin ;
549 _yValueMax = _yFixedValueMax ? _yFixedValueMax : yMax + yMax / 10.0; 597 _yValueMax = _yFixedValueMax ? _yFixedValueMax : yMax + yMax / 10.0;
550 598
551 - if (_showLabel) { 599 + if (_showGenYLabels) {
552 - [self setYLabels:yLabelsArray]; 600 + [self setYLabels];
553 } 601 }
554 602
555 } 603 }
@@ -682,7 +730,8 @@ @@ -682,7 +730,8 @@
682 self.backgroundColor = [UIColor whiteColor]; 730 self.backgroundColor = [UIColor whiteColor];
683 self.clipsToBounds = YES; 731 self.clipsToBounds = YES;
684 self.chartLineArray = [NSMutableArray new]; 732 self.chartLineArray = [NSMutableArray new];
685 - _showLabel = YES; 733 + _showLabel = YES;
  734 + _showGenYLabels = YES;
686 _pathPoints = [[NSMutableArray alloc] init]; 735 _pathPoints = [[NSMutableArray alloc] init];
687 _endPointsOfPath = [[NSMutableArray alloc] init]; 736 _endPointsOfPath = [[NSMutableArray alloc] init];
688 self.userInteractionEnabled = YES; 737 self.userInteractionEnabled = YES;
@@ -33,11 +33,22 @@ @@ -33,11 +33,22 @@
33 33
34 //Use yFixedValueMax and yFixedValueMin to Fix the Max and Min Y Value 34 //Use yFixedValueMax and yFixedValueMin to Fix the Max and Min Y Value
35 //Only if you needed 35 //Only if you needed
36 - self.lineChart.yFixedValueMax = 500.0; 36 + self.lineChart.yFixedValueMax = 300.0;
37 - self.lineChart.yFixedValueMin = 1.0; 37 + self.lineChart.yFixedValueMin = 0.0;
  38 +
  39 + [self.lineChart setYLabels:@[
  40 + @"0 min",
  41 + @"50 min",
  42 + @"100 min",
  43 + @"150 min",
  44 + @"200 min",
  45 + @"250 min",
  46 + @"300 min",
  47 + ]
  48 + ];
38 49
39 // Line Chart #1 50 // Line Chart #1
40 - NSArray * data01Array = @[@60.1, @160.1, @126.4, @262.2, @186.2, @127.2, @176.2]; 51 + NSArray * data01Array = @[@60.1, @160.1, @126.4, @0.0, @186.2, @127.2, @176.2];
41 PNLineChartData *data01 = [PNLineChartData new]; 52 PNLineChartData *data01 = [PNLineChartData new];
42 data01.dataTitle = @"Alpha"; 53 data01.dataTitle = @"Alpha";
43 data01.color = PNFreshGreen; 54 data01.color = PNFreshGreen;
@@ -50,7 +61,7 @@ @@ -50,7 +61,7 @@
50 }; 61 };
51 62
52 // Line Chart #2 63 // Line Chart #2
53 - NSArray * data02Array = @[@20.1, @180.1, @26.4, @202.2, @126.2, @167.2, @276.2]; 64 + NSArray * data02Array = @[@0.0, @180.1, @26.4, @202.2, @126.2, @167.2, @276.2];
54 PNLineChartData *data02 = [PNLineChartData new]; 65 PNLineChartData *data02 = [PNLineChartData new];
55 data02.dataTitle = @"Beta Beta Beta Beta"; 66 data02.dataTitle = @"Beta Beta Beta Beta";
56 data02.color = PNTwitterColor; 67 data02.color = PNTwitterColor;