简单动态条形图,设置和操作非常简单。
pod 'HACBarChart'
HACBarChart.h
、HACBarChart.m
、HACBarLayer.h
和HACBarLayer.m
复制到您的项目中要将它设置为xib或 storyboard 图形界面,添加一个UIView,并分配一个HACBarChart
的子类。根据需要适当地分配所需的约束。下一步是添加一个IBOutlet来分配属性和数据。完成后,我们将数据传递给我们想要在图表中绘制的数据,这些数据是一个NSDictionary
的NSArray
,其键应如以下示例所示。键kHACColor
和kHACCustomText
是可选的,但kHACPercentage
是必需的。
#import "HACBarChart.h"
@interface ViewController (){
NSArray *data;
}
@property (weak, nonatomic) IBOutlet HACBarChart *chart;
- (void)viewDidLoad
{
[super viewDidLoad];
data = @[
@{kHACPercentage:@1000, kHACColor : [UIColor colorWithRed:0.000f green:0.620f blue:0.890f alpha:1.0f], kHACCustomText : @"January"},
@{kHACPercentage:@875, kHACColor : [UIColor colorWithRed:0.431f green:0.000f blue:0.533f alpha:1.0f], kHACCustomText : @"February"},
@{kHACPercentage:@875, kHACColor : [UIColor colorWithRed:0.922f green:0.000f blue:0.000f alpha:1.0f], kHACCustomText : @"March"},
@{kHACPercentage:@875, kHACColor : [UIColor colorWithRed:0.000f green:0.671f blue:0.180f alpha:1.0f], kHACCustomText : @"April"},
@{kHACPercentage:@750, kHACColor : [UIColor colorWithRed:1.000f green:0.000f blue:0.851f alpha:1.0f], kHACCustomText : @"May"},
@{kHACPercentage:@750, kHACColor : [UIColor colorWithRed:1.000f green:0.808f blue:0.000f alpha:1.0f], kHACCustomText : @"June"},
@{kHACPercentage:@625, kHACColor : [UIColor colorWithRed:0.294f green:0.843f blue:0.251f alpha:1.0f], kHACCustomText : @"July"},
@{kHACPercentage:@500, kHACColor : [UIColor colorWithRed:1.000f green:0.404f blue:0.000f alpha:1.0f], kHACCustomText : @"August"},
@{kHACPercentage:@500, kHACColor : [UIColor colorWithRed:0.282f green:0.631f blue:0.620f alpha:1.0f], kHACCustomText : @"September"},
@{kHACPercentage:@375, kHACColor : [UIColor colorWithRed:0.776f green:0.000f blue:0.702f alpha:1.0f], kHACCustomText : @"October"},
@{kHACPercentage:@250, kHACColor : [UIColor colorWithRed:0.282f green:0.631f blue:0.620f alpha:1.0f], kHACCustomText : @"November"},
@{kHACPercentage:@125, kHACColor : [UIColor colorWithRed:0.776f green:0.000f blue:0.702f alpha:1.0f], kHACCustomText : @"December"}
];
// Assign data
_chart.data = data;
}
要绘制图表,在分配数据后,必须在viewDidAppear
方法中执行,如下面的示例所示:
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[_chart draw];
}
////// CHART CUSTOMIZE
_chart.showAxis = YES; // Show axis line
_chart.showProgressLabel = YES; // Show text for bar
_chart.vertical = YES; // Orientation chart
_chart.reverse = NO; // Orientation chart
_chart.showDataValue = YES; // Show value contains _data, or real percent value
_chart.showCustomText = YES; // Show custom text, in _data with key kHACCustomText
_chart.barsMargin = 0; // Margin between bars
_chart.sizeLabelProgress = 30; // Width of label progress text
_chart.numberDividersAxisY = 8;
_chart.animationDuration = 2;
// _chart.axisMaxValue = 1500; // If no define maxValue, get maxium of _data
_chart.progressTextColor = [UIColor darkGrayColor];
_chart.axisYTextColor = [UIColor colorWithRed:0.80 green:0.80 blue:0.80 alpha:1.0];
_chart.progressTextFont = [UIFont fontWithName:@"DINCondensed-Bold" size:6];
_chart.typeBar = HACBarType2;
_chart.dashedLineColor = [UIColor colorWithRed:0.44 green:0.66 blue:0.86 alpha:.3];
_chart.axisXColor = [UIColor colorWithRed:0.44 green:0.66 blue:0.86 alpha:1.0];
_chart.axisYColor = [UIColor colorWithRed:0.44 green:0.66 blue:0.86 alpha:1.0];
_chart.data = data;
////// CHART SET DATA
[_chart draw];
享受 :D
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)