kevinzhow

Add hide label mode, Bar chart add rollback

... ... @@ -17,4 +17,6 @@
@property (nonatomic, strong) UIColor * barColor;
-(void)rollBack;
@end
... ...
... ... @@ -57,6 +57,13 @@
_chartLine.strokeEnd = 1.0;
}
-(void)rollBack{
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
_chartLine.strokeColor = [UIColor clearColor].CGColor;
} completion:nil];
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
... ... @@ -64,7 +71,7 @@
//Draw BG
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0].CGColor);
CGContextSetFillColorWithColor(context, self.backgroundColor.CGColor);
CGContextFillRect(context, rect);
}
... ...
... ... @@ -31,9 +31,10 @@
@property (nonatomic) int yValueMax;
@property (nonatomic, strong) UIColor * strokeColor;
@property (nonatomic, strong) UIColor * barBackgroundColor;
@property (nonatomic) BOOL showLabel;
@end
... ...
... ... @@ -20,7 +20,8 @@
// Initialization code
self.backgroundColor = [UIColor whiteColor];
self.clipsToBounds = YES;
_showLabel = YES;
_barBackgroundColor = PNLightGrey;
}
return self;
... ... @@ -30,6 +31,8 @@
{
_yValues = yValues;
[self setYLabels:yValues];
_xLabelWidth = (self.frame.size.width - chartMargin*2)/[_yValues count];
}
-(void)setYLabels:(NSArray *)yLabels
... ... @@ -50,22 +53,22 @@
_yValueMax = (int)max;
NSLog(@"Y Max is %d", _yValueMax );
}
-(void)setXLabels:(NSArray *)xLabels
{
_xLabels = xLabels;
_xLabelWidth = (self.frame.size.width - chartMargin*2)/[xLabels count];
for (NSString * labelText in xLabels) {
NSInteger index = [xLabels indexOfObject:labelText];
PNChartLabel * label = [[PNChartLabel alloc] initWithFrame:CGRectMake((index * _xLabelWidth + chartMargin), self.frame.size.height - 30.0, _xLabelWidth, 20.0)];
[label setTextAlignment:NSTextAlignmentCenter];
label.text = labelText;
[self addSubview:label];
if (_showLabel) {
_xLabelWidth = (self.frame.size.width - chartMargin*2)/[xLabels count];
for (NSString * labelText in xLabels) {
NSInteger index = [xLabels indexOfObject:labelText];
PNChartLabel * label = [[PNChartLabel alloc] initWithFrame:CGRectMake((index * _xLabelWidth + chartMargin), self.frame.size.height - 30.0, _xLabelWidth, 20.0)];
[label setTextAlignment:NSTextAlignmentCenter];
label.text = labelText;
[self addSubview:label];
}
}
}
... ... @@ -77,7 +80,7 @@
-(void)strokeChart
{
CGFloat chartCavanHeight = self.frame.size.height - chartMargin * 2 - 40.0;
NSInteger index = 0;
... ... @@ -85,8 +88,13 @@
float value = [valueString floatValue];
float grade = (float)value / (float)_yValueMax;
PNBar * bar = [[PNBar alloc] initWithFrame:CGRectMake((index * _xLabelWidth + chartMargin + _xLabelWidth * 0.25), self.frame.size.height - chartCavanHeight - 30.0, _xLabelWidth * 0.5, chartCavanHeight)];
PNBar * bar = [[PNBar alloc] init];
if (_showLabel) {
bar = [[PNBar alloc] initWithFrame:CGRectMake((index * _xLabelWidth + chartMargin + _xLabelWidth * 0.25), self.frame.size.height - chartCavanHeight - 30.0, _xLabelWidth * 0.5, chartCavanHeight)];
}else{
bar = [[PNBar alloc] initWithFrame:CGRectMake((index * _xLabelWidth + chartMargin + _xLabelWidth * 0.25), self.frame.size.height - chartCavanHeight , _xLabelWidth * 0.6, chartCavanHeight)];
}
bar.backgroundColor = _barBackgroundColor;
bar.barColor = _strokeColor;
bar.grade = grade;
[self addSubview:bar];
... ...
... ... @@ -69,6 +69,13 @@ typedef enum {
@property (nonatomic, strong) UIColor * strokeColor;
/**
* PNChart bar chart background color. The default is PNLightGrey.
*
*/
@property (nonatomic, strong) UIColor * barBackgroundColor;
/**
* PNChart circle chart total number.
*
*/
... ... @@ -81,5 +88,12 @@ typedef enum {
*/
@property (nonatomic, strong) NSNumber * current;
/**
* PNChart if chart show labels.
*
*/
@property (nonatomic) BOOL showLabel;
@end
... ...
... ... @@ -18,6 +18,7 @@
self.backgroundColor = [UIColor whiteColor];
self.clipsToBounds = NO;
self.type = PNLineType;
_showLabel = YES;
self.strokeColor = PNFreshGreen;
}
... ... @@ -28,6 +29,7 @@
if (self.type == PNLineType) {
_lineChart = [[PNLineChart alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
_lineChart.backgroundColor = [UIColor clearColor];
_lineChart.showLabel = _showLabel;
[self addSubview:_lineChart];
[_lineChart setYValues:_yValues];
[_lineChart setXLabels:_xLabels];
... ... @@ -38,6 +40,10 @@
{
_barChart = [[PNBarChart alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
_barChart.backgroundColor = [UIColor clearColor];
if (_barBackgroundColor) {
_barChart.barBackgroundColor = _barBackgroundColor;
}
_barChart.showLabel = _showLabel;
[self addSubview:_barChart];
[_barChart setYValues:_yValues];
[_barChart setXLabels:_xLabels];
... ... @@ -59,7 +65,25 @@
-(void)strokeChart
{
[self setUpChart];
if (_lineChart) {
[_lineChart strokeChart];
[_lineChart setStrokeColor:_strokeColor];
}else if (_barChart)
{
[_barChart strokeChart];
[_barChart setStrokeColor:_strokeColor];
}else if (_circleChart)
{
[_circleChart strokeChart];
[_circleChart setStrokeColor:_strokeColor];
}else{
[self setUpChart];
}
}
... ...
... ... @@ -36,5 +36,6 @@
@property (nonatomic, strong) UIColor * strokeColor;
@property (nonatomic) BOOL showLabel;
@end
... ...
... ... @@ -25,6 +25,7 @@
_chartLine.fillColor = [[UIColor whiteColor] CGColor];
_chartLine.lineWidth = 3.0;
_chartLine.strokeEnd = 0.0;
_showLabel = YES;
[self.layer addSublayer:_chartLine];
}
... ... @@ -34,18 +35,14 @@
-(void)setYValues:(NSArray *)yValues
{
_yValues = yValues;
[self setYLabels:yValues];
}
-(void)setYLabels:(NSArray *)yLabels
{
NSInteger max = 0;
for (NSString * valueString in yLabels) {
NSInteger value = [valueString integerValue];
_xLabelWidth = (self.frame.size.width)/[_yValues count];
float max = 0;
for (NSString * valueString in yValues) {
float value = [valueString floatValue];
if (value > max) {
max = value;
}
}
//Min value for Y label
... ... @@ -53,9 +50,19 @@
max = 5;
}
_yValueMax = (int)max;
_yValueMax = (float)max;
if (_showLabel) {
[self setYLabels:yValues];
}
}
-(void)setYLabels:(NSArray *)yLabels
{
float level = max /[yLabels count];
float level = _yValueMax /[yLabels count];
NSInteger index = 0;
NSInteger num = [yLabels count] + 1;
... ... @@ -75,16 +82,19 @@
-(void)setXLabels:(NSArray *)xLabels
{
_xLabels = xLabels;
_xLabelWidth = (self.frame.size.width - chartMargin - 30.0)/[xLabels count];
for (NSString * labelText in xLabels) {
NSInteger index = [xLabels indexOfObject:labelText];
PNChartLabel * label = [[PNChartLabel alloc] initWithFrame:CGRectMake(index * _xLabelWidth + 30.0, self.frame.size.height - 30.0, _xLabelWidth, 20.0)];
[label setTextAlignment:NSTextAlignmentCenter];
label.text = labelText;
[self addSubview:label];
if(_showLabel){
_xLabelWidth = (self.frame.size.width - chartMargin - 30.0)/[xLabels count];
for (NSString * labelText in xLabels) {
NSInteger index = [xLabels indexOfObject:labelText];
PNChartLabel * label = [[PNChartLabel alloc] initWithFrame:CGRectMake(index * _xLabelWidth + 30.0, self.frame.size.height - 30.0, _xLabelWidth, 20.0)];
[label setTextAlignment:NSTextAlignmentCenter];
label.text = labelText;
[self addSubview:label];
}
}
}
-(void)setStrokeColor:(UIColor *)strokeColor
... ... @@ -101,25 +111,29 @@
CGFloat firstValue = [[_yValues objectAtIndex:0] floatValue];
CGFloat xPosition = _xLabelWidth ;
CGFloat chartCavanHeight = self.frame.size.height - chartMargin * 2 - 40.0;
CGFloat xPosition = 0;
CGFloat chartCavanHeight = self.frame.size.height - 40.0;
if(_showLabel){
chartCavanHeight = self.frame.size.height - chartMargin * 2 - 40.0;
xPosition = _xLabelWidth;
}
float grade = (float)firstValue / (float)_yValueMax;
[progressline moveToPoint:CGPointMake( xPosition, chartCavanHeight - grade * chartCavanHeight + 20.0)];
[progressline setLineWidth:3.0];
[progressline setLineCapStyle:kCGLineCapRound];
[progressline setLineJoinStyle:kCGLineJoinRound];
NSInteger index = 0;
for (NSString * valueString in _yValues) {
NSInteger value = [valueString integerValue];
float value = [valueString floatValue];
float grade = (float)value / (float)_yValueMax;
if (index != 0) {
[progressline addLineToPoint:CGPointMake(index * xPosition + 30.0+ _xLabelWidth /2.0, chartCavanHeight - grade * chartCavanHeight + 20.0)];
[progressline moveToPoint:CGPointMake(index * xPosition + 30.0 + _xLabelWidth /2.0, chartCavanHeight - grade * chartCavanHeight + 20.0 )];
[progressline addLineToPoint:CGPointMake(index * _xLabelWidth + 30.0+ _xLabelWidth /2.0, chartCavanHeight - grade * chartCavanHeight + 20.0)];
[progressline moveToPoint:CGPointMake(index * _xLabelWidth + 30.0 + _xLabelWidth /2.0, chartCavanHeight - grade * chartCavanHeight + 20.0 )];
[progressline stroke];
}
... ...