Skip to content

Commit

Permalink
Merge pull request Boris-Em#98 from kattrali/fill-gradients
Browse files Browse the repository at this point in the history
Add ability to fill the top and/or bottom of a graph with a gradient
  • Loading branch information
Boris-Em committed Nov 11, 2014
2 parents 7baa81d + 8dc0924 commit 4d1241a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
8 changes: 7 additions & 1 deletion Classes/BEMLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,22 @@ typedef NS_ENUM(NSInteger, BEMLineAnimation) {
/// The color of the area above the line, inside of its superview
@property (strong, nonatomic) UIColor *topColor;

/// A color gradient applied to the area above the line, inside of its superview. If set, it will be drawn on top of the fill from the \p topColor property.
@property (assign, nonatomic) CGGradientRef topGradient;

/// The color of the area below the line, inside of its superview
@property (strong, nonatomic) UIColor *bottomColor;

/// A color gradient applied to the area below the line, inside of its superview. If set, it will be drawn on top of the fill from the \p bottomColor property.
@property (assign, nonatomic) CGGradientRef bottomGradient;

@property (strong, nonatomic) UIColor *xAxisBackgroundColor;

@property (nonatomic) CGFloat xAxisBackgroundAlpha;

/** A color gradient to be applied to the line. If this property is set, it will mask (override) the \p color property.
@todo This property is non-functional at this point in time. It only serves as a marker for further implementation. */
@property (assign, nonatomic) CGGradientRef gradient;
@property (assign, nonatomic) CGGradientRef lineGradient;

/// The reference line color. Defaults to `color`.
@property (strong, nonatomic) UIColor *refrenceLineColor;
Expand Down
19 changes: 18 additions & 1 deletion Classes/BEMLine.m
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,26 @@ - (void)drawRect:(CGRect)rect {
// ---------------------------//
[self.topColor set];
[fillTop fillWithBlendMode:kCGBlendModeNormal alpha:self.topAlpha];

[self.bottomColor set];
[fillBottom fillWithBlendMode:kCGBlendModeNormal alpha:self.bottomAlpha];

CGContextRef ctx = UIGraphicsGetCurrentContext();
if (self.topGradient != nil) {
CGContextSaveGState(ctx);
CGContextAddPath(ctx, [fillTop CGPath]);
CGContextClip(ctx);
CGContextDrawLinearGradient(ctx, self.topGradient, CGPointZero, CGPointMake(0, CGRectGetMaxY(fillTop.bounds)), 0);
CGContextRestoreGState(ctx);
}

if (self.bottomGradient != nil) {
CGContextSaveGState(ctx);
CGContextAddPath(ctx, [fillBottom CGPath]);
CGContextClip(ctx);
CGContextDrawLinearGradient(ctx, self.bottomGradient, CGPointZero, CGPointMake(0, CGRectGetMaxY(fillBottom.bounds)), 0);
CGContextRestoreGState(ctx);
}


// ---------------------------//
Expand Down
7 changes: 7 additions & 0 deletions Classes/BEMSimpleLineGraphView.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@
@property (nonatomic) CGFloat alphaBottom;


/// Fill gradient of the bottom part of the graph (between the line and the X-axis). When set, it will draw a gradient over top of the fill provided by the \p colorBottom and \p alphaBottom properties.
@property (assign, nonatomic) CGGradientRef gradientBottom;


/// Color of the top part of the graph (between the line and the top of the view the graph is drawn in).
@property (strong, nonatomic) UIColor *colorTop;

Expand All @@ -205,6 +209,9 @@
@property (nonatomic) CGFloat alphaTop;


/// Fill gradient of the top part of the graph (between the line and the top of the view the graph is drawn in). When set, it will draw a gradient over top of the fill provided by the \p colorTop and \p alphaTop properties.
@property (assign, nonatomic) CGGradientRef gradientTop;

/// Color of the line of the graph.
@property (strong, nonatomic) UIColor *colorLine;

Expand Down
2 changes: 2 additions & 0 deletions Classes/BEMSimpleLineGraphView.m
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,8 @@ - (void)drawLine {
line.bottomColor = self.colorBottom;
line.topAlpha = self.alphaTop;
line.bottomAlpha = self.alphaBottom;
line.topGradient = self.gradientTop;
line.bottomGradient = self.gradientBottom;
line.lineWidth = self.widthLine;
line.lineAlpha = self.alphaLine;
line.bezierCurveIsEnabled = self.enableBezierCurve;
Expand Down
8 changes: 8 additions & 0 deletions Sample Project/SimpleLineChart/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ - (void)viewDidLoad {
[self.view addSubview:myGraph]; */

// Customization of the graph
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
size_t num_locations = 2;
CGFloat locations[2] = { 0.0, 1.0 };
CGFloat components[8] = {
1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 0.0
};
self.myGraph.gradientBottom = CGGradientCreateWithColorComponents(colorspace, components, locations, num_locations);
self.myGraph.colorTop = [UIColor colorWithRed:31.0/255.0 green:187.0/255.0 blue:166.0/255.0 alpha:1.0];
self.myGraph.colorBottom = [UIColor colorWithRed:31.0/255.0 green:187.0/255.0 blue:166.0/255.0 alpha:1.0];
self.myGraph.colorLine = [UIColor whiteColor];
Expand Down

0 comments on commit 4d1241a

Please sign in to comment.