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
Zhang Hang
2015-03-01 18:58:32 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
9c2c94b3c80aed26c41f7be72a0d4ebbf64cd675
9c2c94b3
1 parent
12924184
Format code
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
15 additions
and
13 deletions
PNChart/PNPieChart.m
PNChart/PNPieChart.m
View file @
9c2c94b
...
...
@@ -10,7 +10,7 @@
@interface
PNPieChart
()
@property
(
nonatomic
,
readwrite
)
NSArray
*
items
;
@property
(
nonatomic
)
NSArray
*
items
;
@property
(
nonatomic
)
NSArray
*
endPercentages
;
@property
(
nonatomic
)
CGFloat
outerCircleRadius
;
...
...
@@ -28,6 +28,7 @@
-
(
CGFloat
)
startPercentageForItemAtIndex
:(
NSUInteger
)
index
;
-
(
CGFloat
)
endPercentageForItemAtIndex
:(
NSUInteger
)
index
;
-
(
CGFloat
)
ratioForItemAtIndex
:(
NSUInteger
)
index
;
-
(
CAShapeLayer
*
)
newCircleLayerWithRadius
:(
CGFloat
)
radius
borderWidth
:(
CGFloat
)
borderWidth
fillColor
:(
UIColor
*
)
fillColor
...
...
@@ -45,8 +46,8 @@
self
=
[
self
initWithFrame
:
frame
];
if
(
self
){
_items
=
[
NSArray
arrayWithArray
:
items
];
_outerCircleRadius
=
CGRectGetWidth
(
self
.
bounds
)
/
2
;
_innerCircleRadius
=
CGRectGetWidth
(
self
.
bounds
)
/
6
;
_outerCircleRadius
=
CGRectGetWidth
(
self
.
bounds
)
/
2
;
_innerCircleRadius
=
CGRectGetWidth
(
self
.
bounds
)
/
6
;
_descriptionTextColor
=
[
UIColor
whiteColor
];
_descriptionTextFont
=
[
UIFont
fontWithName
:
@"Avenir-Medium"
size
:
18
.
0
];
...
...
@@ -66,10 +67,10 @@
NSMutableArray
*
endPercentages
=
[
NSMutableArray
new
];
[
_items
enumerateObjectsUsingBlock
:
^
(
PNPieChartDataItem
*
item
,
NSUInteger
idx
,
BOOL
*
stop
)
{
if
(
total
==
0
){
[
endPercentages
addObject
:
@
(
1
.
0
/
_items
.
count
*
(
idx
+
1
))];
[
endPercentages
addObject
:
@
(
1
.
0
/
_items
.
count
*
(
idx
+
1
))];
}
else
{
currentTotal
+=
item
.
value
;
[
endPercentages
addObject
:
@
(
currentTotal
/
total
)];
[
endPercentages
addObject
:
@
(
currentTotal
/
total
)];
}
}];
self
.
endPercentages
=
[
endPercentages
copy
];
...
...
@@ -96,7 +97,7 @@
CGFloat
startPercnetage
=
[
self
startPercentageForItemAtIndex
:
i
];
CGFloat
endPercentage
=
[
self
endPercentageForItemAtIndex
:
i
];
CGFloat
radius
=
_innerCircleRadius
+
(
_outerCircleRadius
-
_innerCircleRadius
)
/
2
;
CGFloat
radius
=
_innerCircleRadius
+
(
_outerCircleRadius
-
_innerCircleRadius
)
/
2
;
CGFloat
borderWidth
=
_outerCircleRadius
-
_innerCircleRadius
;
CAShapeLayer
*
currentPieLayer
=
[
self
newCircleLayerWithRadius
:
radius
borderWidth
:
borderWidth
...
...
@@ -119,7 +120,7 @@
-
(
UILabel
*
)
descriptionLabelForItemAtIndex
:
(
NSUInteger
)
index
{
PNPieChartDataItem
*
currentDataItem
=
[
self
dataItemForIndex
:
index
];
CGFloat
distance
=
_innerCircleRadius
+
(
_outerCircleRadius
-
_innerCircleRadius
)
/
2
;
CGFloat
centerPercentage
=
([
self
startPercentageForItemAtIndex
:
index
]
+
[
self
endPercentageForItemAtIndex
:
index
])
/
2
;
CGFloat
centerPercentage
=
([
self
startPercentageForItemAtIndex
:
index
]
+
[
self
endPercentageForItemAtIndex
:
index
])
/
2
;
CGFloat
rad
=
centerPercentage
*
2
*
M_PI
;
UILabel
*
descriptionLabel
=
[[
UILabel
alloc
]
initWithFrame
:
CGRectMake
(
0
,
0
,
100
,
80
)];
...
...
@@ -139,8 +140,7 @@
descriptionLabel
.
font
=
_descriptionTextFont
;
CGSize
labelSize
=
[
descriptionLabel
.
text
sizeWithAttributes
:@{
NSFontAttributeName
:
descriptionLabel
.
font
}];
descriptionLabel
.
frame
=
CGRectMake
(
descriptionLabel
.
frame
.
origin
.
x
,
descriptionLabel
.
frame
.
origin
.
y
,
descriptionLabel
.
frame
=
CGRectMake
(
descriptionLabel
.
frame
.
origin
.
x
,
descriptionLabel
.
frame
.
origin
.
y
,
descriptionLabel
.
frame
.
size
.
width
,
labelSize
.
height
);
descriptionLabel
.
numberOfLines
=
0
;
descriptionLabel
.
textColor
=
_descriptionTextColor
;
...
...
@@ -198,12 +198,11 @@
circle
.
lineWidth
=
borderWidth
;
circle
.
path
=
path
.
CGPath
;
return
circle
;
}
-
(
void
)
maskChart
{
CGFloat
radius
=
_innerCircleRadius
+
(
_outerCircleRadius
-
_innerCircleRadius
)
/
2
;
CGFloat
radius
=
_innerCircleRadius
+
(
_outerCircleRadius
-
_innerCircleRadius
)
/
2
;
CGFloat
borderWidth
=
_outerCircleRadius
-
_innerCircleRadius
;
CAShapeLayer
*
maskLayer
=
[
self
newCircleLayerWithRadius
:
radius
borderWidth
:
borderWidth
...
...
@@ -223,8 +222,11 @@
[
maskLayer
addAnimation
:
animation
forKey
:
@"circleAnimation"
];
}
-
(
void
)
createArcAnimationForLayer
:
(
CAShapeLayer
*
)
layer
ForKey
:
(
NSString
*
)
key
fromValue
:
(
NSNumber
*
)
from
toValue
:
(
NSNumber
*
)
to
Delegate
:
(
id
)
delegate
{
-
(
void
)
createArcAnimationForLayer
:
(
CAShapeLayer
*
)
layer
forKey
:
(
NSString
*
)
key
fromValue
:
(
NSNumber
*
)
from
toValue
:
(
NSNumber
*
)
to
delegate
:
(
id
)
delegate
{
CABasicAnimation
*
arcAnimation
=
[
CABasicAnimation
animationWithKeyPath
:
key
];
arcAnimation
.
fromValue
=
@0
;
arcAnimation
.
toValue
=
to
;
...
...
Please
register
or
login
to post a comment