dullgrass

add show negative rate barChart

... ... @@ -11,6 +11,7 @@
@interface PNBar : UIView
- (void)rollBack;
@property (nonatomic) float grade;
... ... @@ -23,5 +24,6 @@
@property (nonatomic) CAShapeLayer *gradeLayer;
@property (nonatomic) CATextLayer* textLayer;
@property (nonatomic, assign) BOOL isNegative; //!< 是否是负数
@end
... ...
... ... @@ -208,6 +208,23 @@
}
- (void)setIsNegative:(BOOL)isNegative{
if (isNegative) {
[self.textLayer setString:[[NSString alloc]initWithFormat:@"-%0.f",_grade*100]];
CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI];
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
rotationAnimation.duration = 0.1;
rotationAnimation.repeatCount = 0;//你可以设置到最大的整数值
rotationAnimation.cumulative = NO;
rotationAnimation.removedOnCompletion = NO;
rotationAnimation.fillMode = kCAFillModeForwards;
[self.textLayer addAnimation:rotationAnimation forKey:@"Rotation"];
}
}
-(CABasicAnimation*)fadeAnimation
{
CABasicAnimation* fadeAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
... ...
... ... @@ -54,9 +54,15 @@ typedef NSString *(^PNYLabelFormatter)(CGFloat yLabelValue);
/** Controls whether the chart border line should be displayed. */
@property (nonatomic) BOOL showChartBorder;
/** Controls whether the chart Horizontal separator should be displayed. */
@property (nonatomic, assign) BOOL showLevelLine;
/** Chart bottom border, co-linear with the x-axis. */
@property (nonatomic) CAShapeLayer * chartBottomLine;
/** Chart bottom border, level separator-linear with the x-axis. */
@property (nonatomic) CAShapeLayer * chartLevelLine;
/** Chart left border, co-linear with the y-axis. */
@property (nonatomic) CAShapeLayer * chartLeftLine;
... ... @@ -97,4 +103,5 @@ typedef NSString *(^PNYLabelFormatter)(CGFloat yLabelValue);
@property (nonatomic, weak) id<PNChartDelegate> delegate;
@end
... ...
... ... @@ -56,9 +56,10 @@
_xLabelSkip = 1;
_yLabelSum = 4;
_labelMarginTop = 0;
_chartMargin = 15.0;
_chartMargin = 25.0;
_barRadius = 2.0;
_showChartBorder = NO;
_showLevelLine = NO;
_yChartLabelWidth = 18;
_rotateForXAxisText = false;
}
... ... @@ -80,6 +81,7 @@
[self viewCleanupForCollection:_yChartLabels];
NSArray *yAxisValues = _yLabels ? _yLabels : _yValues;
_yLabelSum = _yLabels ? _yLabels.count - 1 :_yLabelSum;
if (_yMaxValue) {
_yValueMax = _yMaxValue;
} else {
... ... @@ -92,7 +94,7 @@
}
float sectionHeight = (self.frame.size.height - _chartMargin * 2 - kXLabelHeight) / _yLabelSum;
for (int i = 0; i < _yLabelSum; i++) {
for (int i = 0; i <= _yLabelSum; i++) {
NSString *labelText;
if (_yLabels) {
float yAsixValue = [_yLabels[_yLabels.count - i - 1] floatValue];
... ... @@ -214,10 +216,10 @@
}
bar = [[PNBar alloc] initWithFrame:CGRectMake(barXPosition, //Bar X position
self.frame.size.height - chartCavanHeight - kXLabelHeight - _chartMargin, //Bar Y position
self.frame.size.height - chartCavanHeight - kXLabelHeight - _chartMargin , //Bar Y position
barWidth, // Bar witdh
chartCavanHeight)]; //Bar height
self.showLevelLine ? chartCavanHeight/2.0:chartCavanHeight)]; //Bar height
//Change Bar Radius
bar.barRadius = _barRadius;
... ... @@ -243,12 +245,21 @@
//Height Of Bar
float value = [valueString floatValue];
float grade = (float)value / (float)_yValueMax;
float grade =ABS((float)value / (float)_yValueMax);
if (isnan(grade)) {
grade = 0;
}
bar.grade = grade;
CGRect originalFrame = bar.frame;
if (value<0) {
CGAffineTransform transform =CGAffineTransformMakeRotation(M_PI);
[bar setTransform:transform];
originalFrame.origin.y = bar.frame.origin.y + bar.frame.size.height;
bar.frame = originalFrame;
bar.isNegative = YES;
}
index += 1;
}
... ... @@ -297,7 +308,7 @@
_chartBottomLine.strokeEnd = 1.0;
[self.layer addSublayer:_chartBottomLine];
//Add left Chart Line
_chartLeftLine = [CAShapeLayer layer];
... ... @@ -330,6 +341,39 @@
[self.layer addSublayer:_chartLeftLine];
}
// Add Level Separator Line
if (_showLevelLine) {
_chartLevelLine = [CAShapeLayer layer];
_chartLevelLine.lineCap = kCALineCapButt;
_chartLevelLine.fillColor = [[UIColor whiteColor] CGColor];
_chartLevelLine.lineWidth = 1.0;
_chartLevelLine.strokeEnd = 0.0;
UIBezierPath *progressline = [UIBezierPath bezierPath];
[progressline moveToPoint:CGPointMake(_chartMargin, (self.frame.size.height - kXLabelHeight )/2.0)];
[progressline addLineToPoint:CGPointMake(self.frame.size.width - _chartMargin, (self.frame.size.height - kXLabelHeight )/2.0)];
[progressline setLineWidth:1.0];
[progressline setLineCapStyle:kCGLineCapSquare];
_chartLevelLine.path = progressline.CGPath;
_chartLevelLine.strokeColor = PNLightGrey.CGColor;
CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
pathAnimation.duration = 0.5;
pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
pathAnimation.fromValue = @0.0f;
pathAnimation.toValue = @1.0f;
[_chartLevelLine addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
_chartLevelLine.strokeEnd = 1.0;
[self.layer addSublayer:_chartLevelLine];
}
}
... ...
... ... @@ -21,11 +21,10 @@
0AF7A880182AA9F6003645C4 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 0AF7A87F182AA9F6003645C4 /* Images.xcassets */; };
0AF7A8AF182AAEEF003645C4 /* PCChartViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 0AF7A8AE182AAEEF003645C4 /* PCChartViewController.m */; };
5C728F7B8AACCC0864A63B26 /* libPods-PNChartTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = B0A0D7DDAB496680487BF1E5 /* libPods-PNChartTests.a */; };
5C9B4AA31B05BBCB00093EBE /* PNBarChart.m in Sources */ = {isa = PBXBuildFile; fileRef = 5C9B4AA21B05BBCB00093EBE /* PNBarChart.m */; };
6E984E5D1AE2B00600E817A0 /* PNBarChartTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 6E984E5C1AE2B00600E817A0 /* PNBarChartTests.m */; };
6E984E5E1AE2B03800E817A0 /* PNBar.m in Sources */ = {isa = PBXBuildFile; fileRef = 91177EC9198DFAC400017E27 /* PNBar.m */; };
6E984E5F1AE2B03E00E817A0 /* PNBarChart.m in Sources */ = {isa = PBXBuildFile; fileRef = 91177ECB198DFAC400017E27 /* PNBarChart.m */; };
91177ED8198DFAC400017E27 /* PNBar.m in Sources */ = {isa = PBXBuildFile; fileRef = 91177EC9198DFAC400017E27 /* PNBar.m */; };
91177EDA198DFAC400017E27 /* PNBarChart.m in Sources */ = {isa = PBXBuildFile; fileRef = 91177ECB198DFAC400017E27 /* PNBarChart.m */; };
91177EDC198DFAC400017E27 /* PNCircleChart.m in Sources */ = {isa = PBXBuildFile; fileRef = 91177ECD198DFAC400017E27 /* PNCircleChart.m */; };
91177EDE198DFAC400017E27 /* PNLineChart.m in Sources */ = {isa = PBXBuildFile; fileRef = 91177ECF198DFAC400017E27 /* PNLineChart.m */; };
91177EE0198DFAC400017E27 /* PNLineChartData.m in Sources */ = {isa = PBXBuildFile; fileRef = 91177ED1198DFAC400017E27 /* PNLineChartData.m */; };
... ... @@ -75,13 +74,13 @@
2980CA20C29DC029B2D0B926 /* Pods.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.release.xcconfig; path = "Pods/Target Support Files/Pods/Pods.release.xcconfig"; sourceTree = "<group>"; };
3BA6321352024B1FBA0158B0 /* libPods.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libPods.a; sourceTree = BUILT_PRODUCTS_DIR; };
4D54B84CA1CAEB2BBBC9DCFA /* Pods-PNChartTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-PNChartTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-PNChartTests/Pods-PNChartTests.release.xcconfig"; sourceTree = "<group>"; };
5C9B4AA21B05BBCB00093EBE /* PNBarChart.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PNBarChart.m; sourceTree = "<group>"; };
6E984E511AE2AF2D00E817A0 /* PNChartTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = PNChartTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
6E984E541AE2AF2D00E817A0 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
6E984E5C1AE2B00600E817A0 /* PNBarChartTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PNBarChartTests.m; sourceTree = "<group>"; };
91177EC8198DFAC400017E27 /* PNBar.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PNBar.h; sourceTree = "<group>"; };
91177EC9198DFAC400017E27 /* PNBar.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PNBar.m; sourceTree = "<group>"; };
91177ECA198DFAC400017E27 /* PNBarChart.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PNBarChart.h; sourceTree = "<group>"; };
91177ECB198DFAC400017E27 /* PNBarChart.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PNBarChart.m; sourceTree = "<group>"; };
91177ECC198DFAC400017E27 /* PNCircleChart.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PNCircleChart.h; sourceTree = "<group>"; };
91177ECD198DFAC400017E27 /* PNCircleChart.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PNCircleChart.m; sourceTree = "<group>"; };
91177ECE198DFAC400017E27 /* PNLineChart.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PNLineChart.h; sourceTree = "<group>"; };
... ... @@ -249,7 +248,7 @@
91177EC8198DFAC400017E27 /* PNBar.h */,
91177EC9198DFAC400017E27 /* PNBar.m */,
91177ECA198DFAC400017E27 /* PNBarChart.h */,
91177ECB198DFAC400017E27 /* PNBarChart.m */,
5C9B4AA21B05BBCB00093EBE /* PNBarChart.m */,
91177ECC198DFAC400017E27 /* PNCircleChart.h */,
91177ECD198DFAC400017E27 /* PNCircleChart.m */,
91177ECE198DFAC400017E27 /* PNLineChart.h */,
... ... @@ -442,7 +441,6 @@
0AF7A8AF182AAEEF003645C4 /* PCChartViewController.m in Sources */,
0A29228A1A423FB300A42BC4 /* PNScatterChartDataItem.m in Sources */,
A9C75FA61A9F1DA900A54638 /* PNGenericChart.m in Sources */,
91177EDA198DFAC400017E27 /* PNBarChart.m in Sources */,
91177EE6198DFAC400017E27 /* PNPieChartDataItem.m in Sources */,
9FA23B10184A5944002DBBA4 /* PCChartsTableViewController.m in Sources */,
91177EE0198DFAC400017E27 /* PNLineChartData.m in Sources */,
... ... @@ -450,6 +448,7 @@
0AF7A874182AA9F6003645C4 /* main.m in Sources */,
91177EE4198DFAC400017E27 /* PNPieChart.m in Sources */,
0AF7A878182AA9F6003645C4 /* PCAppDelegate.m in Sources */,
5C9B4AA31B05BBCB00093EBE /* PNBarChart.m in Sources */,
0A2922891A423FB300A42BC4 /* PNScatterChartData.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
... ... @@ -458,7 +457,6 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
6E984E5F1AE2B03E00E817A0 /* PNBarChart.m in Sources */,
6E984E5E1AE2B03800E817A0 /* PNBar.m in Sources */,
6E984E5D1AE2B00600E817A0 /* PNBarChartTests.m in Sources */,
);
... ...
... ... @@ -101,14 +101,16 @@
return labelText;
};
self.barChart.labelMarginTop = 5.0;
[self.barChart setXLabels:@[@"SEP 1",@"SEP 2",@"SEP 3",@"SEP 4",@"SEP 5",@"SEP 6",@"SEP 7"]];
self.barChart.showLevelLine = YES;
self.barChart.showChartBorder = YES;
[self.barChart setXLabels:@[@"2",@"3",@"4",@"5"]];
self.barChart.rotateForXAxisText = true ;
self.barChart.yLabelSum=5;
self.barChart.yMaxValue=100;
[self.barChart setYValues:@[@1,@24,@12,@18,@30,@10,@21]];
// self.barChart.yLabels = @[@0,@20,@40,@60];
self.barChart.yLabelSum=5;
self.barChart.yMaxValue=100;
self.barChart.yLabels = @[@-10,@0,@10];
[self.barChart setYValues:@[@1,@24,@(0-12),@18]];
[self.barChart setStrokeColors:@[PNGreen,PNGreen,PNRed,PNGreen,PNGreen,PNYellow,PNGreen]];
// Adding gradient
self.barChart.barColorGradientStart = [UIColor blueColor];
... ...
... ... @@ -10,4 +10,4 @@ SPEC CHECKSUMS:
Expecta: 8c507baf13211207b1e9d0a741480600e6b4ed15
UICountingLabel: 1db4e7d023e1762171eb226d6dff47a7a84f27aa
COCOAPODS: 0.36.4
COCOAPODS: 0.37.1
... ...