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
13 changes: 11 additions & 2 deletions FSLineChart/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="9531" systemVersion="14F27" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="vXZ-lx-hvc">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10117" systemVersion="15E65" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="vXZ-lx-hvc">
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9529"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
</dependencies>
<scenes>
Expand Down Expand Up @@ -32,13 +32,21 @@
<constraint firstAttribute="height" constant="166" id="mPh-F7-Spf"/>
</constraints>
</view>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Fb3-cl-ajH" userLabel="Chart 3" customClass="FSLineChart">
<rect key="frame" x="36" y="434" width="528" height="151"/>
<color key="backgroundColor" white="0.33333333333333331" alpha="1" colorSpace="calibratedWhite"/>
</view>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="bTN-h0-UGb" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leadingMargin" constant="16" id="4KJ-j1-ScK"/>
<constraint firstItem="2fi-mo-0CV" firstAttribute="top" secondItem="Fb3-cl-ajH" secondAttribute="bottom" constant="15" id="WGa-R5-xyQ"/>
<constraint firstItem="Fb3-cl-ajH" firstAttribute="leading" secondItem="bTN-h0-UGb" secondAttribute="leading" id="bNy-E5-dfw"/>
<constraint firstItem="d4w-YB-QSH" firstAttribute="top" secondItem="jyV-Pf-zRb" secondAttribute="bottom" constant="32" id="cWF-L9-Wca"/>
<constraint firstItem="d4w-YB-QSH" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leadingMargin" constant="16" id="d38-ev-f0M"/>
<constraint firstItem="2fi-mo-0CV" firstAttribute="top" secondItem="d4w-YB-QSH" secondAttribute="bottom" constant="32" id="dD7-ui-USm"/>
<constraint firstItem="Fb3-cl-ajH" firstAttribute="trailing" secondItem="bTN-h0-UGb" secondAttribute="trailing" id="fNG-Ca-OcO"/>
<constraint firstItem="Fb3-cl-ajH" firstAttribute="top" secondItem="bTN-h0-UGb" secondAttribute="bottom" constant="18" id="sKr-3m-8eU"/>
<constraint firstAttribute="trailingMargin" secondItem="bTN-h0-UGb" secondAttribute="trailing" constant="16" id="uPN-93-Auf"/>
<constraint firstAttribute="trailingMargin" secondItem="d4w-YB-QSH" secondAttribute="trailing" constant="16" id="v3s-0y-rmP"/>
<constraint firstItem="bTN-h0-UGb" firstAttribute="top" secondItem="d4w-YB-QSH" secondAttribute="bottom" constant="32" id="yE7-pt-gi9"/>
Expand All @@ -52,6 +60,7 @@
<connections>
<outlet property="chart" destination="d4w-YB-QSH" id="ga1-cN-Dgv"/>
<outlet property="chartWithDates" destination="bTN-h0-UGb" id="cRc-De-bfT"/>
<outlet property="chartWithGradient" destination="Fb3-cl-ajH" id="Uu5-hy-iEB"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
Expand Down
6 changes: 4 additions & 2 deletions FSLineChart/FSLineChart/FSLineChart.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ typedef NS_ENUM(NSInteger, ValueLabelPositionType) {
// Get the bounds of the chart
- (CGFloat)minVerticalBound;
- (CGFloat)maxVerticalBound;


/**
* gradient in fillcolor mode
*/
@property (nonatomic , strong) CAGradientLayer *gradientLayer;
@end
6 changes: 6 additions & 0 deletions FSLineChart/FSLineChart/FSLineChart.m
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,12 @@ - (void)strokeChart
fillAnimation.fromValue = (id)noFill.CGPath;
fillAnimation.toValue = (id)fill.CGPath;
[fillLayer addAnimation:fillAnimation forKey:@"path"];

if (_gradientLayer) {
_gradientLayer.frame = fillLayer.frame;
_gradientLayer.mask = fillLayer;
[self.layer addSublayer:_gradientLayer];
}
}

CAShapeLayer *pathLayer = [CAShapeLayer layer];
Expand Down
47 changes: 47 additions & 0 deletions FSLineChart/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ @interface ViewController ()

@property (nonatomic, strong) IBOutlet FSLineChart *chart;
@property (nonatomic, strong) IBOutlet FSLineChart *chartWithDates;
@property (nonatomic, strong) IBOutlet FSLineChart *chartWithGradient;

@end

Expand All @@ -24,6 +25,7 @@ - (void)viewDidLoad {

[self loadSimpleChart];
[self loadChartWithDates];
[self loadGradientChart];
}

#pragma mark - Setting up the charts
Expand Down Expand Up @@ -82,4 +84,49 @@ - (void)loadChartWithDates {
[_chartWithDates setChartData:chartData];
}

- (void)loadGradientChart {
_chartWithGradient.verticalGridStep = 4;
_chartWithGradient.horizontalGridStep = 10;

_chartWithGradient.labelForIndex = ^(NSUInteger item) {
return [NSString stringWithFormat:@"%lu",(unsigned long)item];
};

_chartWithGradient.labelForValue = ^(CGFloat value) {
return [NSString stringWithFormat:@"%.f", value];
};

CAGradientLayer *gradientLayer = [CAGradientLayer layer];
//gradient direction
gradientLayer.startPoint = CGPointMake(0, 0);
gradientLayer.endPoint = CGPointMake(0, 1);

//gradient colors
gradientLayer.colors = @[(__bridge id)[UIColor redColor].CGColor,
(__bridge id)[UIColor yellowColor].CGColor,
(__bridge id)[UIColor greenColor].CGColor
];
//gradient position for colors
gradientLayer.locations = @[@(0.3f), @(0.7f),@(0.9f)];

_chartWithGradient.fillColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5];
/**
* gradient only effect when fillColor isn't nil;
*/
_chartWithGradient.gradientLayer = gradientLayer;
_chartWithGradient.lineWidth = 2;
_chartWithGradient.color = [UIColor fsLightBlue];
_chartWithGradient.valueLabelPosition = ValueLabelLeft;
_chartWithGradient.valueLabelTextColor = [UIColor fsDarkGray];
_chartWithGradient.indexLabelTextColor = [UIColor fsDarkGray];
_chartWithGradient.bezierSmoothing = YES;

NSMutableArray* chartData = [NSMutableArray arrayWithCapacity:101];
for(int i=0;i<101;i++) {
chartData[i] = [NSNumber numberWithFloat:(float)i / 30.0f + (float)(rand() % 100) / 200.0f];
}

[_chartWithGradient setChartData:chartData];
}

@end
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Screenshots
---
<img src="Screenshots/fslinechart.png" width="320px" />&nbsp;
<img src="Screenshots/fslinechart2.png" width="320px" />
<img src="Screenshots/lineGradient.png" width="320px" />

Installing FSLineChart
---
Expand Down
Binary file added Screenshots/lineGradient.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.