Toggle navigation
Toggle navigation
This project
Loading...
Sign in
iOS
/
PNChart
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
Jonathan Tron
2015-07-12 15:59:21 +0200
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
3719489e25f03619e1458e4df55289139c9449b6
3719489e
1 parent
f4c5f550
Allow setting Top/Bottom/Left/Right margins independently in PNBarChart
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
82 additions
and
63 deletions
PNChart/PNBarChart.h
PNChart/PNBarChart.m
PNChartDemo/PCChartViewController.m
PNChart/PNBarChart.h
View file @
3719489
...
...
@@ -46,7 +46,10 @@ typedef NSString *(^PNYLabelFormatter)(CGFloat yLabelValue);
/** Formats the ylabel text. */
@property
(
copy
)
PNYLabelFormatter
yLabelFormatter
;
@property
(
nonatomic
)
CGFloat
chartMargin
;
@property
(
nonatomic
)
CGFloat
chartMarginLeft
;
@property
(
nonatomic
)
CGFloat
chartMarginRight
;
@property
(
nonatomic
)
CGFloat
chartMarginTop
;
@property
(
nonatomic
)
CGFloat
chartMarginBottom
;
/** Controls whether labels should be displayed. */
@property
(
nonatomic
)
BOOL
showLabel
;
...
...
PNChart/PNBarChart.m
View file @
3719489
...
...
@@ -24,7 +24,7 @@
-
(
id
)
initWithCoder
:(
NSCoder
*
)
aDecoder
{
self
=
[
super
initWithCoder
:
aDecoder
];
if
(
self
)
{
[
self
setupDefaultValues
];
}
...
...
@@ -56,7 +56,10 @@
_xLabelSkip
=
1
;
_yLabelSum
=
4
;
_labelMarginTop
=
0
;
_chartMargin
=
25
.
0
;
_chartMarginLeft
=
25
.
0
;
_chartMarginRight
=
25
.
0
;
_chartMarginTop
=
25
.
0
;
_chartMarginBottom
=
25
.
0
;
_barRadius
=
2
.
0
;
_showChartBorder
=
NO
;
_showLevelLine
=
NO
;
...
...
@@ -73,7 +76,7 @@
{
_yValues
=
yValues
;
//make the _yLabelSum value dependant of the distinct values of yValues to avoid duplicates on yAxis
if
(
_showLabel
)
{
[
self
__addYCoordinateLabelsValues
];
}
else
{
...
...
@@ -89,7 +92,7 @@
}
else
{
[
self
getYValueMax
:
yAxisValues
];
}
if
(
_yLabelSum
==
4
)
{
_yLabelSum
=
yAxisValues
.
count
;
(
_yLabelSum
%
2
==
0
)
?
_yLabelSum
:
_yLabelSum
++
;
...
...
@@ -99,12 +102,12 @@
#pragma mark - Private Method
#pragma mark - 添加柱状图的Y轴坐标
-
(
void
)
__addYCoordinateLabelsValues
{
[
self
viewCleanupForCollection
:
_yChartLabels
];
[
self
processYMaxValue
];
float
sectionHeight
=
(
self
.
frame
.
size
.
height
-
_chartMargin
*
2
-
kXLabelHeight
)
/
_yLabelSum
;
float
sectionHeight
=
(
self
.
frame
.
size
.
height
-
_chartMargin
Top
-
_chartMarginBottom
-
kXLabelHeight
)
/
_yLabelSum
;
for
(
int
i
=
0
;
i
<=
_yLabelSum
;
i
++
)
{
NSString
*
labelText
;
if
(
_yLabels
)
{
...
...
@@ -113,15 +116,15 @@
}
else
{
labelText
=
_yLabelFormatter
((
float
)
_yValueMax
*
(
(
_yLabelSum
-
i
)
/
(
float
)
_yLabelSum
));
}
CGRect
frame
=
(
CGRect
){
0
,
sectionHeight
*
i
+
_chartMargin
-
kYLabelHeight
/
2
.
0
,
_yChartLabelWidth
,
kYLabelHeight
};
CGRect
frame
=
(
CGRect
){
0
,
sectionHeight
*
i
+
_chartMargin
Top
-
kYLabelHeight
/
2
.
0
,
_yChartLabelWidth
,
kYLabelHeight
};
PNChartLabel
*
label
=
[[
PNChartLabel
alloc
]
initWithFrame
:
frame
];
label
.
font
=
_labelFont
;
label
.
textColor
=
_labelTextColor
;
[
label
setTextAlignment
:
NSTextAlignmentRight
];
label
.
text
=
labelText
;
[
self
addSubview
:
label
];
[
_yChartLabels
addObject
:
label
];
}
}
...
...
@@ -146,20 +149,20 @@
-
(
void
)
setXLabels
:
(
NSArray
*
)
xLabels
{
_xLabels
=
xLabels
;
if
(
_xChartLabels
)
{
[
self
viewCleanupForCollection
:
_xChartLabels
];
}
else
{
_xChartLabels
=
[
NSMutableArray
new
];
}
_xLabelWidth
=
(
self
.
frame
.
size
.
width
-
_chartMargin
*
2
)
/
[
xLabels
count
];
if
(
_showLabel
)
{
_xLabelWidth
=
(
self
.
frame
.
size
.
width
-
_chartMargin
Left
-
_chartMarginRight
)
/
[
xLabels
count
];
if
(
_showLabel
)
{
int
labelAddCount
=
0
;
for
(
int
index
=
0
;
index
<
_xLabels
.
count
;
index
++
)
{
labelAddCount
+=
1
;
if
(
labelAddCount
==
_xLabelSkip
)
{
NSString
*
labelText
=
[
_xLabels
[
index
]
description
];
PNChartLabel
*
label
=
[[
PNChartLabel
alloc
]
initWithFrame
:
CGRectMake
(
0
,
0
,
_xLabelWidth
,
kXLabelHeight
)];
...
...
@@ -171,15 +174,15 @@
CGFloat
labelXPosition
;
if
(
_rotateForXAxisText
){
label
.
transform
=
CGAffineTransformMakeRotation
(
M_PI
/
4
);
labelXPosition
=
(
index
*
_xLabelWidth
+
_chartMargin
+
_xLabelWidth
/
1
.
5
);
labelXPosition
=
(
index
*
_xLabelWidth
+
_chartMargin
Left
+
_xLabelWidth
/
1
.
5
);
}
else
{
labelXPosition
=
(
index
*
_xLabelWidth
+
_chartMargin
+
_xLabelWidth
/
2
.
0
);
labelXPosition
=
(
index
*
_xLabelWidth
+
_chartMargin
Left
+
_xLabelWidth
/
2
.
0
);
}
label
.
center
=
CGPointMake
(
labelXPosition
,
self
.
frame
.
size
.
height
-
kXLabelHeight
-
_chartMargin
+
label
.
frame
.
size
.
height
/
2
.
0
+
_labelMarginTop
);
self
.
frame
.
size
.
height
-
kXLabelHeight
-
_chartMargin
Top
+
label
.
frame
.
size
.
height
/
2
.
0
+
_labelMarginTop
);
labelAddCount
=
0
;
[
_xChartLabels
addObject
:
label
];
[
self
addSubview
:
label
];
}
...
...
@@ -195,44 +198,44 @@
-
(
void
)
updateBar
{
//Add bars
CGFloat
chartCavanHeight
=
self
.
frame
.
size
.
height
-
_chartMargin
*
2
-
kXLabelHeight
;
CGFloat
chartCavanHeight
=
self
.
frame
.
size
.
height
-
_chartMargin
Top
-
_chartMarginBottom
-
kXLabelHeight
;
NSInteger
index
=
0
;
for
(
NSNumber
*
valueString
in
_yValues
)
{
PNBar
*
bar
;
if
(
_bars
.
count
==
_yValues
.
count
)
{
bar
=
[
_bars
objectAtIndex
:
index
];
}
else
{
CGFloat
barWidth
;
CGFloat
barXPosition
;
if
(
_barWidth
)
{
barWidth
=
_barWidth
;
barXPosition
=
index
*
_xLabelWidth
+
_chartMargin
+
_xLabelWidth
/
2
.
0
-
_barWidth
/
2
.
0
;
barXPosition
=
index
*
_xLabelWidth
+
_chartMargin
Left
+
_xLabelWidth
/
2
.
0
-
_barWidth
/
2
.
0
;
}
else
{
barXPosition
=
index
*
_xLabelWidth
+
_chartMargin
+
_xLabelWidth
*
0
.
25
;
barXPosition
=
index
*
_xLabelWidth
+
_chartMargin
Left
+
_xLabelWidth
*
0
.
25
;
if
(
_showLabel
)
{
barWidth
=
_xLabelWidth
*
0
.
5
;
}
else
{
barWidth
=
_xLabelWidth
*
0
.
6
;
}
}
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
Top
,
//Bar Y position
barWidth
,
// Bar witdh
self
.
showLevelLine
?
chartCavanHeight
/
2
.
0
:
chartCavanHeight
)];
//Bar height
//Change Bar Radius
bar
.
barRadius
=
_barRadius
;
//Change Bar Background color
bar
.
backgroundColor
=
_barBackgroundColor
;
//Bar StrokColor First
...
...
@@ -241,23 +244,23 @@
}
else
{
bar
.
barColor
=
[
self
barColorAtIndex
:
index
];
}
// Add gradient
if
(
self
.
isGradientShow
)
{
bar
.
barColorGradientStart
=
bar
.
barColor
;
}
//For Click Index
bar
.
tag
=
index
;
[
_bars
addObject
:
bar
];
[
self
addSubview
:
bar
];
}
//Height Of Bar
float
value
=
[
valueString
floatValue
];
float
grade
=
fabsf
((
float
)
value
/
(
float
)
_yValueMax
);
if
(
isnan
(
grade
))
{
grade
=
0
;
}
...
...
@@ -266,14 +269,14 @@
bar
.
isShowNumber
=
self
.
isShowNumbers
;
CGRect
originalFrame
=
bar
.
frame
;
NSString
*
currentNumber
=
[
NSString
stringWithFormat
:
@"%f"
,
value
];
if
([[
currentNumber
substringToIndex
:
1
]
isEqualToString
:
@"-"
]
&&
self
.
showLevelLine
)
{
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
;
}
...
...
@@ -287,9 +290,9 @@
//Update Bar
[
self
updateBar
];
//Add chart border lines
if
(
_showChartBorder
)
{
...
...
@@ -301,8 +304,8 @@
UIBezierPath
*
progressline
=
[
UIBezierPath
bezierPath
];
[
progressline
moveToPoint
:
CGPointMake
(
_chartMargin
,
self
.
frame
.
size
.
height
-
kXLabelHeight
-
_chartMargin
)];
[
progressline
addLineToPoint
:
CGPointMake
(
self
.
frame
.
size
.
width
-
_chartMargin
,
self
.
frame
.
size
.
height
-
kXLabelHeight
-
_chartMargin
)];
[
progressline
moveToPoint
:
CGPointMake
(
_chartMargin
Left
,
self
.
frame
.
size
.
height
-
kXLabelHeight
-
_chartMarginTop
)];
[
progressline
addLineToPoint
:
CGPointMake
(
self
.
frame
.
size
.
width
-
_chartMargin
Right
,
self
.
frame
.
size
.
height
-
kXLabelHeight
-
_chartMarginTop
)];
[
progressline
setLineWidth
:
1
.
0
];
[
progressline
setLineCapStyle
:
kCGLineCapSquare
];
...
...
@@ -319,7 +322,7 @@
_chartBottomLine
.
strokeEnd
=
1
.
0
;
[
self
.
layer
addSublayer
:
_chartBottomLine
];
//Add left Chart Line
_chartLeftLine
=
[
CAShapeLayer
layer
];
...
...
@@ -330,8 +333,8 @@
UIBezierPath
*
progressLeftline
=
[
UIBezierPath
bezierPath
];
[
progressLeftline
moveToPoint
:
CGPointMake
(
_chartMargin
,
self
.
frame
.
size
.
height
-
kXLabelHeight
-
_chartMargin
)];
[
progressLeftline
addLineToPoint
:
CGPointMake
(
_chartMargin
,
_chartMargin
)];
[
progressLeftline
moveToPoint
:
CGPointMake
(
_chartMargin
Left
,
self
.
frame
.
size
.
height
-
kXLabelHeight
-
_chartMarginBottom
+
_chartMarginTop
)];
[
progressLeftline
addLineToPoint
:
CGPointMake
(
_chartMargin
Left
,
_chartMarginTop
)];
[
progressLeftline
setLineWidth
:
1
.
0
];
[
progressLeftline
setLineCapStyle
:
kCGLineCapSquare
];
...
...
@@ -349,7 +352,7 @@
[
self
.
layer
addSublayer
:
_chartLeftLine
];
}
// Add Level Separator Line
if
(
_showLevelLine
)
{
_chartLevelLine
=
[
CAShapeLayer
layer
];
...
...
@@ -357,27 +360,27 @@
_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
moveToPoint
:
CGPointMake
(
_chartMargin
Left
,
(
self
.
frame
.
size
.
height
-
kXLabelHeight
)
/
2
.
0
)];
[
progressline
addLineToPoint
:
CGPointMake
(
self
.
frame
.
size
.
width
-
_chartMargin
Left
-
_chartMarginRight
,
(
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
];
}
else
{
if
(
_chartLevelLine
)
{
...
...
PNChartDemo/PCChartViewController.m
View file @
3719489
...
...
@@ -91,20 +91,33 @@
}
else
if
([
self
.
title
isEqualToString
:
@"Bar Chart"
])
{
static
NSNumberFormatter
*
barChartFormatter
;
if
(
!
barChartFormatter
){
barChartFormatter
=
[[
NSNumberFormatter
alloc
]
init
];
barChartFormatter
.
numberStyle
=
NSNumberFormatterCurrencyStyle
;
barChartFormatter
.
allowsFloats
=
NO
;
barChartFormatter
.
maximumFractionDigits
=
0
;
}
self
.
titleLabel
.
text
=
@"Bar Chart"
;
self
.
barChart
=
[[
PNBarChart
alloc
]
initWithFrame
:
CGRectMake
(
0
,
135
.
0
,
SCREEN_WIDTH
,
200
.
0
)];
// self.barChart.showLabel = NO;
self
.
barChart
.
backgroundColor
=
[
UIColor
clearColor
];
self
.
barChart
.
yLabelFormatter
=
^
(
CGFloat
yValue
){
CGFloat
yValueParsed
=
yValue
;
NSString
*
labelText
=
[
NSString
stringWithFormat
:
@"%0.f"
,
yValueParsed
];
return
labelText
;
return
[
barChartFormatter
stringFromNumber
:[
NSNumber
numberWithFloat
:
yValue
]];
};
self
.
barChart
.
yChartLabelWidth
=
60
.
0
;
self
.
barChart
.
chartMarginLeft
=
70
.
0
;
self
.
barChart
.
chartMarginRight
=
10
.
0
;
self
.
barChart
.
chartMarginTop
=
5
.
0
;
self
.
barChart
.
chartMarginBottom
=
10
.
0
;
self
.
barChart
.
labelMarginTop
=
5
.
0
;
self
.
barChart
.
showChartBorder
=
YES
;
[
self
.
barChart
setXLabels
:@[
@"2"
,
@"3"
,
@"4"
,
@"5"
,
@"2"
,
@"3"
,
@"4"
,
@"5"
]];
// self.barChart.yLabels = @[@-10,@0,@10];
// [self.barChart setYValues:@[@10000.0,@30000.0,@10000.0,@100000.0,@500000.0,@1000000.0,@1150000.0,@2150000.0]];
[
self
.
barChart
setYValues
:@[
@10.82
,
@1.88
,
@6.96
,
@33.93
,
@10.82
,
@1.88
,
@6.96
,
@33.93
]];
[
self
.
barChart
setStrokeColors
:@[
PNGreen
,
PNGreen
,
PNRed
,
PNGreen
,
PNGreen
,
PNGreen
,
PNRed
,
PNGreen
]];
self
.
barChart
.
isGradientShow
=
NO
;
...
...
Please
register
or
login
to post a comment