Skip to content

Instantly share code, notes, and snippets.

@forsythetony
Created February 7, 2014 03:33
Show Gist options
  • Save forsythetony/8857034 to your computer and use it in GitHub Desktop.
Save forsythetony/8857034 to your computer and use it in GitHub Desktop.
Modified Files for PNChart
//
// PNBarChart.h
// PNChartDemo
//
// Created by kevin on 11/7/13.
// Copyright (c) 2013年 kevinzhow. All rights reserved.
//
#import <UIKit/UIKit.h>
#define chartMargin 10
#define xLabelMargin 15
#define yLabelMargin 15
#define yLabelHeight 11
@interface PNBarChart : UIView
/**
* This method will call and stroke the line in animation
*/
-(void)strokeChart;
@property (strong, nonatomic) NSArray * xLabels;
@property (strong, nonatomic) NSArray * yLabels;
@property (strong, nonatomic) NSArray * yValues;
@property (nonatomic) CGFloat xLabelWidth;
@property (nonatomic) int yValueMax;
@property (nonatomic, strong) UIColor * strokeColor;
@property (nonatomic, strong) UIColor * barBackgroundColor;
@property (nonatomic) BOOL showLabel;
//Functions
-(void)setAllXlabelsForBottom:(NSArray*) bottom andTop:(NSArray*) top;
@end
//
// PNBarChart.m
// PNChartDemo
//
// Created by kevin on 11/7/13.
// Copyright (c) 2013年 kevinzhow. All rights reserved.
//
#import "PNBarChart.h"
#import "PNColor.h"
#import "PNChartLabel.h"
#import "PNBar.h"
@implementation PNBarChart
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = [UIColor whiteColor];
self.clipsToBounds = YES;
_showLabel = YES;
_barBackgroundColor = PNLightGrey;
}
return self;
}
-(void)setYValues:(NSArray *)yValues
{
_yValues = yValues;
[self setYLabels:yValues];
_xLabelWidth = (self.frame.size.width - chartMargin*2)/[_yValues count];
}
-(void)setYLabels:(NSArray *)yLabels
{
NSInteger max = 0;
for (NSString * valueString in yLabels) {
NSInteger value = [valueString integerValue];
if (value > max) {
max = value;
}
}
//Min value for Y label
if (max < 5) {
max = 5;
}
_yValueMax = (int)max;
}
-(void)setXLabels:(NSArray *)xLabels
{
_xLabels = xLabels;
if (_showLabel) {
_xLabelWidth = (self.frame.size.width - chartMargin*2)/[xLabels count];
for(int index = 0; index < xLabels.count; index++)
{
NSString* labelText = xLabels[index];
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];
}
}
}
-(void)setStrokeColor:(UIColor *)strokeColor
{
_strokeColor = strokeColor;
}
-(void)strokeChart
{
CGFloat chartCavanHeight = self.frame.size.height - chartMargin * 2 - 40.0;
NSInteger index = 0;
for (NSString * valueString in _yValues) {
float value = [valueString floatValue];
float grade = (float)value / (float)_yValueMax;
PNBar * bar;
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];
index += 1;
}
}
-(void)setAllXlabelsForBottom:(NSArray*) bottom andTop:(NSArray*) top
{
_xLabels = bottom;
if (_showLabel) {
_xLabelWidth = (self.frame.size.width - chartMargin*2)/[bottom count];
for(int index = 0; index < bottom.count; index++)
{
NSString* labelText = bottom[index];
PNChartLabel * label = [[PNChartLabel alloc] initWithFrame:CGRectMake((index * _xLabelWidth + chartMargin), self.frame.size.height - 30.0, _xLabelWidth, 20.0)];
[label setTextAlignment:NSTextAlignmentCenter];
label.text = labelText;
//Set text for the top labels
NSString* topLabelText = top[index];
PNChartLabel * topLabel = [[PNChartLabel alloc] initWithFrame:CGRectMake(index * _xLabelWidth + chartMargin, 0.0, _xLabelWidth, 20.0)];
[topLabel setTextAlignment:NSTextAlignmentCenter];
[topLabel setText:topLabelText];
[self addSubview:topLabel];
[self addSubview:label];
}
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment