Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions FSLineChart/FSLineChart/FSLineChart.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface FSLineChart : UIView

// Block definition for getting a label for a set index (use case: date, units,...)
typedef NSString *(^FSLabelForIndexGetter)(NSUInteger index);
typedef NSString * _Nullable (^FSLabelForIndexGetter)(NSUInteger index);

// Same as above, but for the value (for adding a currency, or a unit symbol for example)
typedef NSString *(^FSLabelForValueGetter)(CGFloat value);
typedef NSString * _Nullable (^FSLabelForValueGetter)(CGFloat value);

typedef NS_ENUM(NSInteger, ValueLabelPositionType) {
ValueLabelLeft,
Expand All @@ -36,13 +38,13 @@ typedef NS_ENUM(NSInteger, ValueLabelPositionType) {
};

// Index label properties
@property (copy) FSLabelForIndexGetter labelForIndex;
@property (copy, nullable) FSLabelForIndexGetter labelForIndex;
@property (nonatomic, strong) UIFont* indexLabelFont;
@property (nonatomic) UIColor* indexLabelTextColor;
@property (nonatomic) UIColor* indexLabelBackgroundColor;

// Value label properties
@property (copy) FSLabelForValueGetter labelForValue;
@property (copy, nullable) FSLabelForValueGetter labelForValue;
@property (nonatomic, strong) UIFont* valueLabelFont;
@property (nonatomic) UIColor* valueLabelTextColor;
@property (nonatomic) UIColor* valueLabelBackgroundColor;
Expand All @@ -65,7 +67,7 @@ typedef NS_ENUM(NSInteger, ValueLabelPositionType) {

// Chart parameters
@property (nonatomic, strong) UIColor* color;
@property (nonatomic, strong) UIColor* fillColor;
@property (nonatomic, strong, nullable) UIColor* fillColor;
@property (nonatomic) CGFloat lineWidth;

// Data points
Expand All @@ -87,7 +89,7 @@ typedef NS_ENUM(NSInteger, ValueLabelPositionType) {
@property (nonatomic) CGFloat animationDuration;

// Set the actual data for the chart, and then render it to the view.
- (void)setChartData:(NSArray *)chartData;
- (void)setChartData:(NSArray<NSNumber*> *)chartData;

// Clear all rendered data from the view.
- (void)clearChartData;
Expand All @@ -96,5 +98,6 @@ typedef NS_ENUM(NSInteger, ValueLabelPositionType) {
- (CGFloat)minVerticalBound;
- (CGFloat)maxVerticalBound;


@end

NS_ASSUME_NONNULL_END
29 changes: 12 additions & 17 deletions FSLineChart/FSLineChart/FSLineChart.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

@interface FSLineChart ()

@property (nonatomic, strong) NSMutableArray* data;
@property (nonatomic, strong) NSMutableArray* layers;
@property (nonatomic, strong) NSMutableArray<NSNumber*> * data;
@property (nonatomic, strong) NSMutableArray<CALayer*> * layers;

@property (nonatomic) CGFloat min;
@property (nonatomic) CGFloat max;
Expand Down Expand Up @@ -64,7 +64,7 @@ - (void)awakeFromNib

- (void)commonInit
{
_layers = [NSMutableArray array];
_layers = [NSMutableArray<CALayer*> array];
self.backgroundColor = [UIColor whiteColor];
[self setDefaultParameters];
}
Expand Down Expand Up @@ -113,9 +113,8 @@ - (void)layoutSubviews
[obj removeFromSuperview];
}];

[self.layers enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
CALayer* layer = (CALayer*)obj;
[layer removeFromSuperlayer];
[self.layers enumerateObjectsUsingBlock:^(__kindof CALayer * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[obj removeFromSuperlayer];
}];

[self layoutChart];
Expand Down Expand Up @@ -164,12 +163,8 @@ - (void)layoutChart
[self setNeedsDisplay];
}

- (void)setChartData:(NSArray *)chartData
- (void)setChartData:(nonnull NSArray<NSNumber*> *)chartData
{
if (chartData == nil || chartData.count == 0) {
return;
}

_data = [NSMutableArray arrayWithArray:chartData];
[self layoutChart];
}
Expand Down Expand Up @@ -271,7 +266,7 @@ - (void)drawGrid
CGContextAddLineToPoint(ctx, _margin, _axisHeight + _margin + 3);
CGContextStrokePath(ctx);

CGFloat scale = [self horizontalScale];
CGFloat scale = [self horizontalScale];
CGFloat minBound = [self minVerticalBound];
CGFloat maxBound = [self maxVerticalBound];

Expand Down Expand Up @@ -336,10 +331,10 @@ - (void)strokeChart
}

UIBezierPath *noPath = [self getLinePath:0 withSmoothing:_bezierSmoothing close:NO];
UIBezierPath *path = [self getLinePath:scale withSmoothing:_bezierSmoothing close:NO];
UIBezierPath *path = [self getLinePath:scale withSmoothing:_bezierSmoothing close:NO];

UIBezierPath *noFill = [self getLinePath:0 withSmoothing:_bezierSmoothing close:YES];
UIBezierPath *fill = [self getLinePath:scale withSmoothing:_bezierSmoothing close:YES];
UIBezierPath *fill = [self getLinePath:scale withSmoothing:_bezierSmoothing close:YES];

if(_fillColor) {
CAShapeLayer* fillLayer = [CAShapeLayer layer];
Expand Down Expand Up @@ -537,7 +532,7 @@ - (CGFloat)getUpperRoundNumber:(CGFloat)value forGridStep:(int)gridStep

- (void)setGridStep:(int)gridStep
{
_verticalGridStep = gridStep;
_verticalGridStep = gridStep;
_horizontalGridStep = gridStep;
}

Expand Down Expand Up @@ -589,7 +584,7 @@ - (UIBezierPath*)getLinePath:(float)scale withSmoothing:(BOOL)smoothed close:(BO
controlPoint[0].y = p.y + m.y * _bezierSmoothingTension;

// Second control point
nextPoint = [self getPointForIndex:i + 2 withScale:scale];
nextPoint = [self getPointForIndex:i + 2 withScale:scale];
previousPoint = [self getPointForIndex:i withScale:scale];
p = [self getPointForIndex:i + 1 withScale:scale];
m = CGPointZero;
Expand Down