andi

Legend data title only

... ... @@ -7,6 +7,7 @@
//
#import <UIKit/UIKit.h>
#import "PNGenericChart.h"
#import "PNChartDelegate.h"
#import "PNBar.h"
... ... @@ -17,7 +18,7 @@
typedef NSString *(^PNYLabelFormatter)(CGFloat yLabelValue);
@interface PNBarChart : UIView
@interface PNBarChart : PNGenericChart
/**
* Draws the chart in an animated fashion.
... ...
//
// PNGenericChart.h
// PNChartDemo
//
// Created by Andi Palo on 26/02/15.
// Copyright (c) 2015 kevinzhow. All rights reserved.
//
#import <UIKit/UIKit.h>
typedef NS_ENUM(NSUInteger, PNLegendPosition) {
PNLegendPositionTop = 0,
PNLegendPositionBottom = 1,
PNLegendPositionLeft = 2,
PNLegendPositionRight = 3
};
typedef NS_ENUM(NSUInteger, PNLegendItemStyle) {
PNLegendItemStyleStacked = 0,
PNLegendItemStyleSerial = 1
};
@interface PNGenericChart : UIView
@property (assign, nonatomic) BOOL hasLegend;
@property (assign, nonatomic) PNLegendPosition legendPosition;
@property (assign, nonatomic) PNLegendItemStyle legendStyle;
@property (assign, nonatomic) CGFloat legendFontSize;
- (UIView*) drawLegend;
@end
... ...
//
// PNGenericChart.m
// PNChartDemo
//
// Created by Andi Palo on 26/02/15.
// Copyright (c) 2015 kevinzhow. All rights reserved.
//
#import "PNGenericChart.h"
@interface PNGenericChart ()
@end
@implementation PNGenericChart
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
- (UIView*) drawLegend{
return nil;
}
- (id)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
[self setupDefaultValues];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setupDefaultValues];
}
return self;
}
- (void) setupDefaultValues{
self.hasLegend = YES;
self.legendPosition = PNLegendPositionBottom;
self.legendStyle = PNLegendItemStyleStacked;
}
@end
... ...
... ... @@ -9,8 +9,9 @@
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import "PNChartDelegate.h"
#import "PNGenericChart.h"
@interface PNLineChart : UIView
@interface PNLineChart : PNGenericChart
/**
* Draws the chart in an animated fashion.
... ... @@ -70,4 +71,5 @@
- (void)updateChartData:(NSArray *)data;
- (UIView*) getLegendWithMaxWidth:(CGFloat)mWidth;
@end
... ...
... ... @@ -20,6 +20,10 @@
@property (nonatomic) NSMutableArray *chartPath; // Array of line path, one for each line.
@property (nonatomic) NSMutableArray *pointPath; // Array of point path, one for each line
@property (nonatomic) NSMutableArray *endPointsOfPath; // Array of start and end points of each line path, one for each line
@property (strong, nonatomic) UIView *graphView;
@property (assign, nonatomic) CGRect *chartFrame;
@end
@implementation PNLineChart
... ... @@ -48,6 +52,7 @@
return self;
}
#pragma mark instance methods
- (void)setYLabels:(NSArray *)yLabels
... ... @@ -64,6 +69,7 @@
_yChartLabels = [NSMutableArray new];
}
#warning modify origin
if (yStep == 0.0) {
PNChartLabel *minLabel = [[PNChartLabel alloc] initWithFrame:CGRectMake(0.0, (NSInteger)_chartCavanHeight, (NSInteger)_chartMargin, (NSInteger)_yLabelHeight)];
minLabel.text = [NSString stringWithFormat:yLabelFormat, 0.0];
... ... @@ -132,6 +138,7 @@
for (int index = 0; index < xLabels.count; index++) {
labelText = xLabels[index];
#warning modify origin
NSInteger x = 2 * _chartMargin + (index * _xLabelWidth) - (_xLabelWidth / 2);
NSInteger y = _chartMargin + _chartCavanHeight;
... ... @@ -337,6 +344,7 @@
CGFloat offSetX = (_chartCavanWidth) / (chartData.itemCount);
#warning modify chart path
int x = 2 * _chartMargin + (i * offSetX);
int y = _chartCavanHeight - (innerGrade * _chartCavanHeight) + (_yLabelHeight / 2);
... ... @@ -585,7 +593,7 @@
- (void)drawRect:(CGRect)rect
{
if (self.isShowCoordinateAxis) {
#warning modify
CGFloat yAxisOffset = 10.f;
CGContextRef ctx = UIGraphicsGetCurrentContext();
... ... @@ -639,14 +647,14 @@
// draw y unit
if ([self.yUnit length]) {
CGFloat height = [PNLineChart heightOfString:self.yUnit withWidth:30.f font:font];
CGFloat height = [PNLineChart sizeOfString:self.yUnit withWidth:30.f font:font].height;
CGRect drawRect = CGRectMake(_chartMargin + 10 + 5, 0, 30.f, height);
[self drawTextInContext:ctx text:self.yUnit inRect:drawRect font:font];
}
// draw x unit
if ([self.xUnit length]) {
CGFloat height = [PNLineChart heightOfString:self.xUnit withWidth:30.f font:font];
CGFloat height = [PNLineChart sizeOfString:self.xUnit withWidth:30.f font:font].height;
CGRect drawRect = CGRectMake(CGRectGetWidth(rect) - _chartMargin + 5, _chartMargin + _chartCavanHeight - height / 2, 25.f, height);
[self drawTextInContext:ctx text:self.xUnit inRect:drawRect font:font];
}
... ... @@ -684,7 +692,7 @@
#pragma mark - tools
+ (float)heightOfString:(NSString *)text withWidth:(float)width font:(UIFont *)font
+ (CGSize)sizeOfString:(NSString *)text withWidth:(float)width font:(UIFont *)font
{
NSInteger ch;
CGSize size = CGSizeMake(width, MAXFLOAT);
... ... @@ -703,7 +711,7 @@
}
ch = size.height;
return ch;
return size;
}
- (void)drawTextInContext:(CGContextRef )ctx text:(NSString *)text inRect:(CGRect)rect font:(UIFont *)font
... ... @@ -726,4 +734,80 @@
}
}
- (UIView*) constructLegend{
if (self.hasLegend) {
UIView *legend = [self constructLegend];
CGSize graphSize;
/* Determine legend size */
switch (self.legendPosition) {
case PNLegendPositionTop | PNLegendPositionBottom:
graphSize = CGSizeMake(fmaxf(self.frame.size.width, legend.frame.size.width), self.frame.size.height + legend.frame.size.height);
break;
case PNLegendPositionLeft | PNLegendPositionRight:
graphSize = CGSizeMake(self.frame.size.width + legend.frame.size.width, fmaxf(self.frame.size.height, legend.frame.size.height));
break;
default:
break;
}
CGPoint graphOrigin;
switch (self.legendPosition) {
case PNLegendPositionTop:
graphOrigin.x = 2;
break;
case PNLegendPositionBottom:
graphOrigin.x = 2;
break;
case PNLegendPositionLeft:
case PNLegendPositionRight:
graphOrigin.x = 2;
break;
default:
break;
}
self.graphView = [[UIView alloc] initWithFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, graphSize.width, graphSize.height)];
[self.graphView addSubview:self];
[self.graphView addSubview:legend];
return self.graphView;
}else{
[self strokeChart];
return self;
}
}
- (UIView*) getLegendWithMaxWidth:(CGFloat)mWidth{
if ([self.chartData count] < 1) {
return nil;
}
CGFloat legendLineWidth = 40;
CGFloat x = legendLineWidth;
CGFloat y = 0;
CGFloat totalWidth = 0;
CGFloat totalHeight = 0;
NSMutableArray *legendLabels = [[NSMutableArray alloc] init];
for (PNLineChartData *pdata in self.chartData) {
CGFloat maxLabelWidth = self.legendStyle == PNLegendItemStyleStacked ? (mWidth - legendLineWidth) : (mWidth / [self.chartData count] - legendLineWidth);
CGSize labelsize = [PNLineChart sizeOfString:pdata.dataTitle
withWidth:maxLabelWidth
font:[UIFont systemFontOfSize:self.legendFontSize]];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, maxLabelWidth, labelsize.height)];
label.text = pdata.dataTitle;
x += self.legendStyle == PNLegendItemStyleStacked ? 0 : labelsize.width + legendLineWidth;
y += self.legendStyle == PNLegendItemStyleStacked ? labelsize.height : 0;
totalWidth = self.legendStyle == PNLegendItemStyleStacked ? fmaxf(totalWidth, labelsize.width + legendLineWidth) : totalWidth + labelsize.width + legendLineWidth;
totalHeight = self.legendStyle == PNLegendItemStyleStacked ? fmaxf(totalHeight, labelsize.height) : totalHeight + labelsize.height;
[legendLabels addObject:label];
}
UIView *legend = [[UIView alloc] initWithFrame:CGRectMake(100, 100, totalWidth, totalHeight)];
for (UILabel *l in legendLabels) {
[legend addSubview:l];
}
return legend;
}
@end
... ...
... ... @@ -22,6 +22,7 @@ typedef PNLineChartDataItem *(^LCLineChartDataGetter)(NSUInteger item);
@property (nonatomic) CGFloat alpha;
@property NSUInteger itemCount;
@property (copy) LCLineChartDataGetter getData;
@property (strong, nonatomic) NSString *dataTitle;
@property (nonatomic, assign) PNLineChartPointStyle inflexionPointStyle;
... ...
... ... @@ -8,8 +8,9 @@
#import <UIKit/UIKit.h>
#import "PNPieChartDataItem.h"
#import "PNGenericChart.h"
@interface PNPieChart : UIView
@interface PNPieChart : PNGenericChart
- (id)initWithFrame:(CGRect)frame items:(NSArray *)items;
... ...
... ... @@ -9,10 +9,11 @@
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import "PNChartDelegate.h"
#import "PNGenericChart.h"
#import "PNScatterChartData.h"
#import "PNScatterChartDataItem.h"
@interface PNScatterChart : UIView
@interface PNScatterChart : PNGenericChart
@property (nonatomic, retain) id<PNChartDelegate> delegate;
... ...
... ... @@ -44,6 +44,7 @@
9FA23B10184A5944002DBBA4 /* PCChartsTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 9FA23B0F184A5944002DBBA4 /* PCChartsTableViewController.m */; };
9FE15DF8190BB014004129F5 /* PNChartLabel.m in Sources */ = {isa = PBXBuildFile; fileRef = 9FE15DE9190BB014004129F5 /* PNChartLabel.m */; };
9FE15DFA190BB014004129F5 /* PNColor.m in Sources */ = {isa = PBXBuildFile; fileRef = 9FE15DEE190BB014004129F5 /* PNColor.m */; };
A9C75FA61A9F1DA900A54638 /* PNGenericChart.m in Sources */ = {isa = PBXBuildFile; fileRef = A9C75FA51A9F1DA900A54638 /* PNGenericChart.m */; };
E2C3ED5773A1409C8367CC70 /* libPods.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 3BA6321352024B1FBA0158B0 /* libPods.a */; };
/* End PBXBuildFile section */
... ... @@ -110,6 +111,8 @@
9FE15DE9190BB014004129F5 /* PNChartLabel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PNChartLabel.m; sourceTree = "<group>"; };
9FE15DED190BB014004129F5 /* PNColor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PNColor.h; sourceTree = "<group>"; };
9FE15DEE190BB014004129F5 /* PNColor.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PNColor.m; sourceTree = "<group>"; };
A9C75FA41A9F1DA900A54638 /* PNGenericChart.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PNGenericChart.h; sourceTree = "<group>"; };
A9C75FA51A9F1DA900A54638 /* PNGenericChart.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PNGenericChart.m; sourceTree = "<group>"; };
EFE4F6360943ED4001072124 /* Pods.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.debug.xcconfig; path = "Pods/Target Support Files/Pods/Pods.debug.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */
... ... @@ -266,6 +269,8 @@
91177ED5198DFAC400017E27 /* PNPieChart.m */,
91177ED6198DFAC400017E27 /* PNPieChartDataItem.h */,
91177ED7198DFAC400017E27 /* PNPieChartDataItem.m */,
A9C75FA41A9F1DA900A54638 /* PNGenericChart.h */,
A9C75FA51A9F1DA900A54638 /* PNGenericChart.m */,
);
path = PNChart;
sourceTree = "<group>";
... ... @@ -411,6 +416,7 @@
91177EE2198DFAC400017E27 /* PNLineChartDataItem.m in Sources */,
0AF7A8AF182AAEEF003645C4 /* PCChartViewController.m in Sources */,
0A29228A1A423FB300A42BC4 /* PNScatterChartDataItem.m in Sources */,
A9C75FA61A9F1DA900A54638 /* PNGenericChart.m in Sources */,
91177EDA198DFAC400017E27 /* PNBarChart.m in Sources */,
91177EE6198DFAC400017E27 /* PNPieChartDataItem.m in Sources */,
9FA23B10184A5944002DBBA4 /* PCChartsTableViewController.m in Sources */,
... ...
... ... @@ -34,6 +34,7 @@
// Line Chart #1
NSArray * data01Array = @[@60.1, @160.1, @126.4, @262.2, @186.2, @127.2, @176.2];
PNLineChartData *data01 = [PNLineChartData new];
data01.dataTitle = @"Alpha";
data01.color = PNFreshGreen;
data01.alpha = 0.3f;
data01.itemCount = data01Array.count;
... ... @@ -46,6 +47,7 @@
// Line Chart #2
NSArray * data02Array = @[@20.1, @180.1, @26.4, @202.2, @126.2, @167.2, @276.2];
PNLineChartData *data02 = [PNLineChartData new];
data02.dataTitle = @"Beta";
data02.color = PNTwitterColor;
data02.alpha = 0.5f;
data02.itemCount = data02Array.count;
... ... @@ -61,6 +63,9 @@
[self.view addSubview:self.lineChart];
self.lineChart.legendStyle = PNLegendItemStyleSerial;
self.lineChart.legendFontSize = 17.0;
[self.view addSubview:[self.lineChart getLegendWithMaxWidth:200]];
}
else if ([self.title isEqualToString:@"Bar Chart"])
{
... ...
... ... @@ -7,4 +7,4 @@ DEPENDENCIES:
SPEC CHECKSUMS:
UICountingLabel: a55223a9357af71f833af76665164d2e3f3654b5
COCOAPODS: 0.34.4
COCOAPODS: 0.35.0
... ...