kevinzhow

Lots of new API for Bar Chart

//
// PNBarChart.h
// PNChartDemo
//
// Created by kevin on 11/7/13.
// Copyright (c) 2013年 kevinzhow. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "PNChartDelegate.h"
#define chartMargin 10
#define xLabelMargin 15
#define yLabelMargin 15
#define yLabelHeight 11
#define xLabelHeight 20
@interface PNBarChart : UIView
/**
* This method will call and stroke the line in animation
*/
- (void)strokeChart;
@property (nonatomic) NSArray *xLabels;
@property (nonatomic) NSArray *yLabels;
@property (nonatomic) NSArray *yValues;
@property (nonatomic) CGFloat xLabelWidth;
@property (nonatomic) int yValueMax;
@property (nonatomic) UIColor *strokeColor;
@property (nonatomic) NSArray *strokeColors;
@property (nonatomic) UIColor *barBackgroundColor;
@property (nonatomic) BOOL showLabel;
@property (nonatomic, retain) id<PNChartDelegate> delegate;
@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"
@interface PNBarChart () {
NSMutableArray *_bars;
NSMutableArray *_labels;
}
- (UIColor *)barColorAtIndex:(NSUInteger)index;
@end
@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;
_labels = [NSMutableArray array];
_bars = [NSMutableArray array];
}
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];
}
}
- (void)setStrokeColor:(UIColor *)strokeColor
{
_strokeColor = strokeColor;
}
- (void)strokeChart
{
[self viewCleanupForCollection:_labels];
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 - xLabelHeight - chartMargin, _xLabelWidth, xLabelHeight)];
[label setTextAlignment:NSTextAlignmentCenter];
label.text = labelText;
[_labels addObject:label];
[self addSubview:label];
}
[self viewCleanupForCollection:_bars];
CGFloat chartCavanHeight = self.frame.size.height - chartMargin * 2 - xLabelHeight * 2;
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 - xLabelHeight - chartMargin, _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 = [self barColorAtIndex:index];
bar.grade = grade;
bar.tag = index;
[_bars addObject:bar];
[self addSubview:bar];
index += 1;
}
}
- (void)viewCleanupForCollection:(NSMutableArray *)array
{
if (array.count) {
[array makeObjectsPerformSelector:@selector(removeFromSuperview)];
[array removeAllObjects];
}
}
#pragma mark - Class extension methods
- (UIColor *)barColorAtIndex:(NSUInteger)index
{
if ([self.strokeColors count] == [self.yValues count]) {
return self.strokeColors[index];
}
else {
return self.strokeColor;
}
}
#pragma mark - Touch detection
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchPoint:touches withEvent:event];
[super touchesBegan:touches withEvent:event];
}
- (void)touchPoint:(NSSet *)touches withEvent:(UIEvent *)event
{
//Get the point user touched
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self];
UIView *subview = [self hitTest:touchPoint withEvent:nil];
if ([subview isKindOfClass:[PNBar class]] && [self.delegate respondsToSelector:@selector(userClickedOnBarCharIndex:)]) {
[self.delegate userClickedOnBarCharIndex:subview.tag];
}
}
@end
... ... @@ -16,4 +16,6 @@
@property (nonatomic) float grade;
@property (nonatomic) CAShapeLayer *chartLine;
@property (nonatomic) UIColor *barColor;
@property (nonatomic) CGFloat barRadius;
@end
... ...
... ... @@ -24,12 +24,18 @@
_chartLine.strokeEnd = 0.0;
self.clipsToBounds = YES;
[self.layer addSublayer:_chartLine];
self.layer.cornerRadius = 2.0;
self.barRadius = 2.0;
}
return self;
}
-(void)setBarRadius:(CGFloat)barRadius
{
_barRadius = barRadius;
self.layer.cornerRadius = _barRadius;
}
- (void)setGrade:(float)grade
{
... ...
//
// PNBarChart.h
// PNChartDemo
//
// Created by kevin on 11/7/13.
// Copyright (c) 2013年 kevinzhow. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "PNChartDelegate.h"
#define xLabelMargin 15
#define yLabelMargin 15
#define yLabelHeight 11
#define xLabelHeight 20
typedef NSString *(^PNyLabelFromatter)(CGFloat yLabelValue);
@interface PNBarChart : UIView
/**
* This method will call and stroke the line in animation
*/
- (void)strokeChart;
@property (nonatomic) NSArray *xLabels;
@property (nonatomic) NSArray *yLabels;
@property (nonatomic) NSArray *yValues;
@property (nonatomic) CGFloat xLabelWidth;
@property (nonatomic) int yValueMax;
@property (nonatomic) UIColor *strokeColor;
@property (nonatomic) NSArray *strokeColors;
/*
chartMargin changes chart margin
*/
@property (nonatomic) CGFloat yChartLabelWidth;
/*
yLabelFormatter will format the ylabel text
*/
@property (copy) PNyLabelFromatter yLabelFormatter;
/*
chartMargin changes chart margin
*/
@property (nonatomic) CGFloat chartMargin;
/*
showLabelDefines if the Labels should be deplay
*/
@property (nonatomic) BOOL showLabel;
/*
showChartBorder if the chart border Line should be deplay
*/
@property (nonatomic) BOOL showChartBorder;
/*
chartBottomLine the Line at the chart bottom
*/
@property (nonatomic) CAShapeLayer * chartBottomLine;
/*
chartLeftLine the Line at the chart left
*/
@property (nonatomic) CAShapeLayer * chartLeftLine;
/*
barRadius changes the bar corner radius
*/
@property (nonatomic) CGFloat barRadius;
/*
barWidth changes the width of the bar
*/
@property (nonatomic) CGFloat barWidth;
/*
labelMarginTop changes the width of the bar
*/
@property (nonatomic) CGFloat labelMarginTop;
/*
barBackgroundColor changes the bar background color
*/
@property (nonatomic) UIColor * barBackgroundColor;
/*
labelTextColor changes the bar label text color
*/
@property (nonatomic) UIColor * labelTextColor;
/*
labelFont changes the bar label font
*/
@property (nonatomic) UIFont * labelFont;
/*
xLabelSkip define the label skip number
*/
@property (nonatomic) NSInteger xLabelSkip;
/*
yLabelSum define the label skip number
*/
@property (nonatomic) NSInteger yLabelSum;
/*
yMaxValue define the max value of the chart
*/
@property (nonatomic) CGFloat yMaxValue;
@property (nonatomic, retain) id<PNChartDelegate> delegate;
@end
... ...
This diff is collapsed. Click to expand it.
... ... @@ -17,13 +17,10 @@
if (self) {
// Initialization code
[self setLineBreakMode:NSLineBreakByWordWrapping];
[self setMinimumScaleFactor:11.0f];
[self setNumberOfLines:0];
[self setFont:[UIFont boldSystemFontOfSize:11.0f]];
[self setTextColor:PNDeepGrey];
self.backgroundColor = [UIColor clearColor];
[self setTextAlignment:NSTextAlignmentLeft];
[self setTextAlignment:NSTextAlignmentCenter];
self.userInteractionEnabled = YES;
}
... ...
... ... @@ -11,4 +11,5 @@
+ (PNLineChartDataItem *)dataItemWithY:(CGFloat)y;
@property (readonly) CGFloat y; // should be within the y range
@end
... ...
This diff is collapsed. Click to expand it.
... ... @@ -106,6 +106,12 @@
PNBarChart * barChart = [[PNBarChart alloc] initWithFrame:CGRectMake(0, 135.0, SCREEN_WIDTH, 200.0)];
barChart.backgroundColor = [UIColor clearColor];
barChart.yLabelFormatter = ^(CGFloat yValue){
CGFloat yValueParsed = yValue;
NSString * labelText = [NSString stringWithFormat:@"%1.f",yValueParsed];
return labelText;
};
barChart.labelMarginTop = 5.0;
[barChart setXLabels:@[@"SEP 1",@"SEP 2",@"SEP 3",@"SEP 4",@"SEP 5",@"SEP 6",@"SEP 7"]];
[barChart setYValues:@[@1,@24,@12,@18,@30,@10,@21]];
[barChart setStrokeColors:@[PNGreen,PNGreen,PNRed,PNGreen,PNGreen,PNYellow,PNGreen]];
... ... @@ -129,18 +135,13 @@
circleChartLabel.font = [UIFont fontWithName:@"Avenir-Medium" size:23.0];
circleChartLabel.textAlignment = NSTextAlignmentCenter;
PNCircleChart * circleChart = [[PNCircleChart alloc] initWithFrame:CGRectMake(0, 80.0, SCREEN_WIDTH, 100.0) andTotal:[NSNumber numberWithInt:100] andCurrent:[NSNumber numberWithInt:60] andClockwise:YES andShadow:NO];
PNCircleChart * circleChart = [[PNCircleChart alloc] initWithFrame:CGRectMake(0, 80.0, SCREEN_WIDTH, 100.0) andTotal:[NSNumber numberWithInt:100] andCurrent:[NSNumber numberWithInt:60] andClockwise:YES andShadow:YES];
circleChart.backgroundColor = [UIColor clearColor];
[circleChart setStrokeColor:PNGreen];
[circleChart strokeChart];
PNCircleChart * circleChart2 = [[PNCircleChart alloc] initWithFrame:CGRectMake(0, 80.0, SCREEN_WIDTH, 100.0) andTotal:[NSNumber numberWithInt:100] andCurrent:[NSNumber numberWithInt:90] andClockwise:YES andShadow:YES];
circleChart2.backgroundColor = [UIColor clearColor];
[circleChart2 setStrokeColor:PNBlue];
[circleChart2 strokeChart];
[viewController.view addSubview:circleChartLabel];
[viewController.view addSubview:circleChart2];
[viewController.view addSubview:circleChart];
viewController.title = @"Circle Chart";
... ...