UUChart是一个用于绘制图表的第三方,尤其适合去绘制折线图。
二、下载地址:
https://github.com/ZhipingYang/UUChartView
三、使用
第一步、首先我们将下载下来的文件解压,里面有一个UUChar的文件夹,然后将整个文件夹都拖到项目里面。我们会发现里面有UUBar.h、UUChart.h等文件,这些就是UUChart的具体实现。
第二步、我们需要在引用的.h文件中引入
#import “UUChart.h"
然后实现代理
@interface currentViewController : BaseViewController<UUChartDataSource>
接着实例化一个视图:
@property(nonatomic,strong)UUChart *chartView;
@property(nonatomic,strong)NSMutableArray *chartArray;//Y的坐标
@property(nonatomic,strong)NSMutableArray *dateArray;//X的坐标
@property (nonatomic, strong) UIView *headerChartView;//图表view
-(UIView *)tableViewHeaderView{
CGFloat headerChartViewH = 400.0f;
self.headerChartView = [DMWidget viewWithFrame:CGRectMake(0, headerDataView.bottom+40, DMViewWidth(_currentProjectDetailsTableView), headerChartViewH) toView:headerView];
_headerChartView.backgroundColor = DMRGB(255, 255, 255);
CGFloat headerViewH = headerDataViewH+ headerChartViewH+40;
headerView.frame = CGRectMake(0, 0, DMViewWidth(_currentProjectDetailsTableView), headerViewH);
return headerView;
}
-(UUChart *)chartView
{
if (!_chartView) {
_chartView = [[UUChart alloc]initwithUUChartDataFrame:CGRectMake(20, 0, [UIScreen mainScreen].bounds.size.width-40, 150)
withSource:self
withStyle:UUChartLineStyle];
[_chartView showInView:_headerChartView];
}
return _chartView;
}
#pragma mark - @optional
//颜色数组
- (NSArray *)UUChart_ColorArray:(UUChart *)chart
{
return @[UUTwitterColor,UURed,UUBrown];
}
//显示数值范围
- (CGRange)UUChartChooseRangeInLineChart:(UUChart *)chart
{
// [self.chartArray addObjectsFromArray: @[@"11.2",@"11.4",@"11.5",@"11.6",@"11.8",@"11.9",@"12.0000"]];
return CGRangeMake([[[self getMaxAndMin:self.chartArray] firstObject] floatValue]*1.1, [[[self getMaxAndMin:self.chartArray] lastObject] floatValue]*0.9);
}
#pragma mark 折线图专享功能
//标记数值区域
- (CGRange)UUChartMarkRangeInLineChart:(UUChart *)chart
{
return CGRangeZero;
}
//判断显示横线条
- (BOOL)UUChart:(UUChart *)chart ShowHorizonLineAtIndex:(NSInteger)index
{
return YES;
}
-(NSArray*)getMaxAndMin:(NSArray*)sortArray
{
//取得两个值比较大的一个
NSComparator cmptr = ^(id obj1, id obj2){
if ([obj1 floatValue] > [obj2 floatValue]) {
return (NSComparisonResult)NSOrderedDescending;
}
if ([obj1 floatValue] < [obj2 floatValue]) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
};
NSArray *array = [sortArray sortedArrayUsingComparator:cmptr];
NSString *max = [array lastObject];
NSString *min = [array firstObject];
NSArray *maxAndMinArray = @[max,min];
return maxAndMinArray;
}