Kevin

Merge pull request #90 from klauslanza/master

Fix Y labels and graph position when all Y values are the same
@@ -66,16 +66,28 @@ @@ -66,16 +66,28 @@
66 { 66 {
67 CGFloat yStep = (_yValueMax - _yValueMin) / _yLabelNum; 67 CGFloat yStep = (_yValueMax - _yValueMin) / _yLabelNum;
68 CGFloat yStepHeight = _chartCavanHeight / _yLabelNum; 68 CGFloat yStepHeight = _chartCavanHeight / _yLabelNum;
  69 + NSString *yLabelFormat = self.yLabelFormat ?: @"%1.f";
69 70
70 - if (_showLabel) { 71 + if (yStep == 0.0) {
  72 + PNChartLabel *minLabel = [[PNChartLabel alloc] initWithFrame:CGRectMake(0.0, _chartCavanHeight, _chartMargin, _yLabelHeight)];
  73 + minLabel.text = [NSString stringWithFormat:yLabelFormat, 0.0];
  74 + [self addSubview:minLabel];
  75 +
  76 + PNChartLabel *midLabel = [[PNChartLabel alloc] initWithFrame:CGRectMake(0.0, _chartCavanHeight/2, _chartMargin, _yLabelHeight)];
  77 + midLabel.text = [NSString stringWithFormat:yLabelFormat, _yValueMax];
  78 + [self addSubview:midLabel];
71 79
  80 + PNChartLabel *maxLabel = [[PNChartLabel alloc] initWithFrame:CGRectMake(0.0, 0.0, _chartMargin, _yLabelHeight)];
  81 + maxLabel.text = [NSString stringWithFormat:yLabelFormat, _yValueMax * 2];
  82 + [self addSubview:maxLabel];
  83 +
  84 + } else {
72 NSInteger index = 0; 85 NSInteger index = 0;
73 NSInteger num = _yLabelNum + 1; 86 NSInteger num = _yLabelNum + 1;
74 87
75 while (num > 0) { 88 while (num > 0) {
76 PNChartLabel *label = [[PNChartLabel alloc] initWithFrame:CGRectMake(0.0, (_chartCavanHeight - index * yStepHeight), _chartMargin, _yLabelHeight)]; 89 PNChartLabel *label = [[PNChartLabel alloc] initWithFrame:CGRectMake(0.0, (_chartCavanHeight - index * yStepHeight), _chartMargin, _yLabelHeight)];
77 [label setTextAlignment:NSTextAlignmentRight]; 90 [label setTextAlignment:NSTextAlignmentRight];
78 - NSString *yLabelFormat = self.yLabelFormat ? self.yLabelFormat : @"%1.f";  
79 label.text = [NSString stringWithFormat:yLabelFormat, _yValueMin + (yStep * index)]; 91 label.text = [NSString stringWithFormat:yLabelFormat, _yValueMin + (yStep * index)];
80 [self addSubview:label]; 92 [self addSubview:label];
81 index += 1; 93 index += 1;
@@ -227,7 +239,11 @@ @@ -227,7 +239,11 @@
227 239
228 yValue = chartData.getData(i).y; 240 yValue = chartData.getData(i).y;
229 241
  242 + if (!(_yValueMax - _yValueMin)) {
  243 + innerGrade = 0.5;
  244 + } else {
230 innerGrade = (yValue - _yValueMin) / (_yValueMax - _yValueMin); 245 innerGrade = (yValue - _yValueMin) / (_yValueMax - _yValueMin);
  246 + }
231 247
232 int x = 2 * _chartMargin + (i * _xLabelWidth); 248 int x = 2 * _chartMargin + (i * _xLabelWidth);
233 int y = _chartCavanHeight - (innerGrade * _chartCavanHeight) + (_yLabelHeight / 2); 249 int y = _chartCavanHeight - (innerGrade * _chartCavanHeight) + (_yLabelHeight / 2);
@@ -310,7 +326,6 @@ @@ -310,7 +326,6 @@
310 // setup the color of the chart line 326 // setup the color of the chart line
311 if (chartData.color) { 327 if (chartData.color) {
312 chartLine.strokeColor = [chartData.color CGColor]; 328 chartLine.strokeColor = [chartData.color CGColor];
313 - pointLayer.strokeColor = [chartData.color CGColor];  
314 } 329 }
315 else { 330 else {
316 chartLine.strokeColor = [PNGreen CGColor]; 331 chartLine.strokeColor = [PNGreen CGColor];