前言:最近喜欢上封装一些东西,一来提升自己的封装能力,二来为以后的复用提供便利
功能如下:
- 可设置不超过20个元素的雷达图
- 文字可点击
- 分割值可显示
来看下效果图

1.如何使用
直接看代码,使用起来非常简单。
NSMutableArray *items = [NSMutableArray array];
NSArray *values = @[@8,@5,@7,@3,@6];
NSArray *descs = @[@"苹果",@"香蕉",@"花生",@"橙子",@"车厘子"];
for (int i = 0; i < values.count; i++) {
GBRadarChartDataItem *item = [GBRadarChartDataItem dataItemWithValue:[values[i] floatValue] description:descs[i]];
[items addObject:item];
}
GBRadarChart *radarChart = [[GBRadarChart alloc] initWithFrame:CGRectMake(0, 100, CGRectGetWidth(self.view.bounds), 400) items:items valueDivider:2];
radarChart.isShowGraduation = YES;
[radarChart strokeChart];
[self.view addSubview:radarChart];
2.看下源代码,了解一下实现过程
首先是.h中的属性和方法
@interface GBRadarChart : UIView
/**
初始化图表
@param frame frame
@param items 模型数组
@param unitValue 均分值
@return 对象
*/
- (id)initWithFrame:(CGRect)frame items:(NSArray <GBRadarChartDataItem *> *)items valueDivider:(CGFloat)unitValue;
/** 绘制图表 */
- (void)strokeChart;
/**
更新图表
@param chartData 模型数组
*/
- (void)updateChartWithChartData:(NSArray *)chartData;
/** Array of `RadarChartDataItem` objects, one for each corner. */
@property (nonatomic) NSArray <GBRadarChartDataItem *> *chartData;
/** The unit of this chart ,default is 1 */
@property (nonatomic) CGFloat valueDivider;
/** The maximum for the range of values to display on the chart */
@property (nonatomic) CGFloat maxValue;
/** Default is gray. */
@property (nonatomic) UIColor *webColor;
/** Default is green , with an alpha of 0.7 */
@property (nonatomic) UIColor *plotFillColor;
/** Default is green*/
@property (nonatomic) UIColor *plotStrokeColor;
/** Default is black */
@property (nonatomic) UIColor *fontColor;
/** Default is orange */
@property (nonatomic) UIColor *graduationColor;
/** Default is 12 */
@property (nonatomic) CGFloat fontSize;
/** Tap the label will display detail value ,default is YES. */
@property (nonatomic, assign) BOOL isLabelTouchable;
/** is show graduation on the chart ,default is NO. */
@property (nonatomic, assign) BOOL isShowGraduation;
/** is display animated, default is YES */
@property (nonatomic, assign) BOOL displayAnimated;
@end
.m中代码就不粘贴,最后直接上demo吧
传送门:RadarChartDemo
大家有什么不理解的或者好的意见,评论回复吧~~~
网友评论