Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multiple options #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
12 changes: 10 additions & 2 deletions FSLineChart/FSLineChart/FSLineChart.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ typedef NSString *(^FSLabelForIndexGetter)(NSUInteger index);
typedef NSString *(^FSLabelForValueGetter)(CGFloat value);

typedef NS_ENUM(NSInteger, ValueLabelPositionType) {
ValueLabelLeft,
ValueLabelRight
// ValueLabelLeft, // @mhergon Pull request
ValueLabelRight,
ValueLabelLeftInside, // @mhergon Pull request
ValueLabelLeftOutside // @mhergon Pull request
};

// Index label properties
Expand Down Expand Up @@ -85,6 +87,12 @@ typedef NS_ENUM(NSInteger, ValueLabelPositionType) {
// Animations
@property (nonatomic) CGFloat animationDuration;

// @mhergon Pull request
@property (nonatomic) BOOL drawVerticalGrid;
@property (nonatomic) BOOL drawHorizontalGrid;



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

Expand Down
72 changes: 43 additions & 29 deletions FSLineChart/FSLineChart/FSLineChart.m
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ - (void)setChartData:(NSArray *)chartData

if(_labelForValue) {
for(int i=0;i<_verticalGridStep;i++) {
CGPoint p = CGPointMake(_margin + (_valueLabelPosition == ValueLabelRight ? _axisWidth : 0), _axisHeight + _margin - (i + 1) * _axisHeight / _verticalGridStep);
CGPoint p = CGPointMake(_margin + (_valueLabelPosition == ValueLabelRight ? _axisWidth : (_valueLabelPosition == ValueLabelLeftInside ? (_axisWidth / _horizontalGridStep) * 0.6 : 0)), _axisHeight + _margin - (i + 1) * _axisHeight / _verticalGridStep); // @mhergon Pull request

NSString* text = _labelForValue(minBound + (maxBound - minBound) / _verticalGridStep * (i + 1));

Expand Down Expand Up @@ -165,41 +165,51 @@ - (void)drawGrid

// draw grid
if(_drawInnerGrid) {
for(int i=0;i<_horizontalGridStep;i++) {
CGContextSetStrokeColorWithColor(ctx, [_innerGridColor CGColor]);
CGContextSetLineWidth(ctx, _innerGridLineWidth);

CGPoint point = CGPointMake((1 + i) * _axisWidth / _horizontalGridStep * scale + _margin, _margin);

CGContextMoveToPoint(ctx, point.x, point.y);
CGContextAddLineToPoint(ctx, point.x, _axisHeight + _margin);
CGContextStrokePath(ctx);

CGContextSetStrokeColorWithColor(ctx, [_axisColor CGColor]);
CGContextSetLineWidth(ctx, _axisLineWidth);
CGContextMoveToPoint(ctx, point.x - 0.5f, _axisHeight + _margin);
CGContextAddLineToPoint(ctx, point.x - 0.5f, _axisHeight + _margin + 3);
CGContextStrokePath(ctx);
}

for(int i=0;i<_verticalGridStep + 1;i++) {
// If the value is zero then we display the horizontal axis
CGFloat v = maxBound - (maxBound - minBound) / _verticalGridStep * i;

if(v == 0) {
CGContextSetLineWidth(ctx, _axisLineWidth);
CGContextSetStrokeColorWithColor(ctx, [_axisColor CGColor]);
} else {
if (_drawVerticalGrid) { // @mhergon Pull request

for(int i=0;i<_horizontalGridStep;i++) {
CGContextSetStrokeColorWithColor(ctx, [_innerGridColor CGColor]);
CGContextSetLineWidth(ctx, _innerGridLineWidth);

CGPoint point = CGPointMake((1 + i) * _axisWidth / _horizontalGridStep * scale + _margin, _margin);

CGContextMoveToPoint(ctx, point.x, point.y);
CGContextAddLineToPoint(ctx, point.x, _axisHeight + _margin);
CGContextStrokePath(ctx);

CGContextSetStrokeColorWithColor(ctx, [_axisColor CGColor]);
CGContextSetLineWidth(ctx, _axisLineWidth);
CGContextMoveToPoint(ctx, point.x - 0.5f, _axisHeight + _margin);
CGContextAddLineToPoint(ctx, point.x - 0.5f, _axisHeight + _margin + 3);
CGContextStrokePath(ctx);
}

CGPoint point = CGPointMake(_margin, (i) * _axisHeight / _verticalGridStep + _margin);
}

if (_drawHorizontalGrid) { // @mhergon Pull request

for(int i=0;i<_verticalGridStep + 1;i++) {
// If the value is zero then we display the horizontal axis
CGFloat v = maxBound - (maxBound - minBound) / _verticalGridStep * i;

if(v == 0) {
CGContextSetLineWidth(ctx, _axisLineWidth);
CGContextSetStrokeColorWithColor(ctx, [_axisColor CGColor]);
} else {
CGContextSetStrokeColorWithColor(ctx, [_innerGridColor CGColor]);
CGContextSetLineWidth(ctx, _innerGridLineWidth);
}

CGPoint point = CGPointMake(_margin, (i) * _axisHeight / _verticalGridStep + _margin);

CGContextMoveToPoint(ctx, point.x, point.y);
CGContextAddLineToPoint(ctx, _axisWidth + _margin, point.y);
CGContextStrokePath(ctx);
}

CGContextMoveToPoint(ctx, point.x, point.y);
CGContextAddLineToPoint(ctx, _axisWidth + _margin, point.y);
CGContextStrokePath(ctx);
}

}

}
Expand Down Expand Up @@ -332,6 +342,10 @@ - (void)setDefaultParameters
_valueLabelTextColor = [UIColor grayColor];
_valueLabelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:11];
_valueLabelPosition = ValueLabelRight;

// @mhergon Pull request
_drawHorizontalGrid = YES;
_drawVerticalGrid = YES;
}

- (void)computeBounds
Expand Down
35 changes: 20 additions & 15 deletions FSLineChart/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ @implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];

[self.view addSubview:[self chart1]];
[self.view addSubview:[self chart2]];
//[self.view addSubview:[self chart1]];
[self.view addSubview:[self chart3]];
}

- (void)didReceiveMemoryWarning {
Expand Down Expand Up @@ -75,23 +75,28 @@ -(FSLineChart*)chart2 {
}
-(FSLineChart*)chart3 {
// Generating some dummy data
NSMutableArray* chartData = [NSMutableArray arrayWithCapacity:7];
for(int i=0;i<7;i++) {
NSMutableArray* chartData = [NSMutableArray arrayWithCapacity:13];
for(int i=0;i<13;i++) {
chartData[i] = [NSNumber numberWithFloat: (float)i / 30.0f + (float)(rand() % 100) / 500.0f];
}
NSArray* months = @[@"January", @"February", @"March", @"April", @"May", @"June", @"July"];
NSArray* months = @[@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"11", @"12", @"13"];
// Creating the line chart
FSLineChart* lineChart = [[FSLineChart alloc] initWithFrame:CGRectMake(20, 60, [UIScreen mainScreen].bounds.size.width - 40, 166)];
lineChart.verticalGridStep = 6;
lineChart.horizontalGridStep = 3; // 151,187,205,0.2
lineChart.color = [UIColor colorWithRed:151.0f/255.0f green:187.0f/255.0f blue:205.0f/255.0f alpha:1.0f];
lineChart.fillColor = [lineChart.color colorWithAlphaComponent:0.3];
lineChart.labelForIndex = ^(NSUInteger item) {
return months[item];
};
lineChart.labelForValue = ^(CGFloat value) {
return [NSString stringWithFormat:@"%.02f €", value];
};
lineChart.verticalGridStep = 3;
lineChart.horizontalGridStep = 12; // 151,187,205,0.2
lineChart.color = [UIColor colorWithRed:246.0f/255.0f green:146.0f/255.0f blue:30.0f/255.0f alpha:1.0f];
lineChart.fillColor = [UIColor colorWithRed:246.0f/255.0f green:146.0f/255.0f blue:30.0f/255.0f alpha:0.3];
lineChart.bezierSmoothing = NO;
lineChart.valueLabelBackgroundColor = [UIColor clearColor];
lineChart.valueLabelPosition = ValueLabelLeftInside;
lineChart.drawVerticalGrid = NO;

lineChart.labelForIndex = ^(NSUInteger item) {
return months[item];
};
lineChart.labelForValue = ^(CGFloat value) {
return [NSString stringWithFormat:@"%.0f", value];
};
[lineChart setChartData:chartData];
return lineChart;
}
Expand Down