#import "BarChartVC.h"
#import "PNChart.h"
@interface BarChartVC () {
PNBarChart *_barChart;
IBOutlet UIView *_bgView;
}
@end
@implementation BarChartVC
- (void)viewDidLoad {
[super viewDidLoad];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// _bgView為xib上拖得View
_barChart = [[PNBarChart alloc] initWithFrame:_bgView.bounds];
_barChart.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.2];
// _barChart.yLabels = @[@"0", @"300", @"600", @"900", @"1200", @"1500"];
//_barChart.yMaxValue = 1456;
//_barChart.strokeColor = [UIColor blackColor];// 所有柱形顏色
_barChart.strokeColors = @[[UIColor whiteColor], [UIColor blackColor], [UIColor blueColor], [UIColor orangeColor], [UIColor lightGrayColor]];// 每個(gè)柱形的顏色 必須每個(gè)設(shè)置才有效
_barChart.barBackgroundColor = [UIColor yellowColor];// 柱形背景顏色
_barChart.isShowNumbers = YES;// 是否顯示柱子中的數(shù)字
_barChart.labelTextColor = [UIColor blueColor];// 所有l(wèi)abel文本顏色
// _barChart.yChartLabelWidth = 20;// Y軸label寬度
// _barChart.yLabelPrefix = @"p";// Y軸前綴
// _barChart.yLabelSuffix = @"s";// Y軸后綴
_barChart.showChartBorder = YES;// 是否顯示圖表邊框
_barChart.chartBorderColor = [UIColor redColor];// 圖表邊框顏色
_barChart.labelFont = [UIFont systemFontOfSize:10];// X、Y軸label文本大小
//_barChart.chartMarginLeft = 25;// 圖表距離左邊距離 默認(rèn)25
//_barChart.chartMarginBottom = 50;// 圖表距離底部距離 默認(rèn)25
//_barChart.chartMarginTop = 25;// 默認(rèn)25
//_barChart.barRadius = 2;// 柱形圓角半徑
_barChart.barWidth = 30;// 柱形寬度
_barChart.labelMarginTop = 25;// X軸label距離圖表的上外邊距 設(shè)置了這個(gè) 就不需要設(shè)置chartMarginBottom=50 來正確的顯示X軸label的位置
_barChart.yLabelSum = 10;// Y軸有多少個(gè)等級(jí)label
//_barChart.rotateForXAxisText = YES;// X軸label是否順時(shí)針旋轉(zhuǎn)45度 設(shè)置了這個(gè)屬性 就需要重新改變 labelMarginTop福侈、chartMarginBottom的值
// X軸和值的設(shè)置放最后 前面的一些設(shè)置才生效
_barChart.xLabels = @[@"普通", @"翡翠", @"白金", @"鉆石", @"金鉆"];
_barChart.yValues = @[@1456, @799, @233, @23, @2];
[_barChart strokeChart];
[_bgView addSubview:_barChart];
});
}
@end
效果如下圖
_barChart.xLabelSkip = 2;// 以xLabelSkip為基數(shù) X軸第xLabelSkip的倍數(shù)個(gè)顯示X軸label 效果圖如下
_barChart.isGradientShow = NO;// 柱形顏色是否漸變 效果圖如下