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
xujunwen
2014-05-10 17:37:37 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
e876f8c347b6e86085f66b6bd9900801f8b33609
e876f8c3
1 parent
1596857b
[Mod] : Add Coodinate Axis to PNLineChart
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
111 additions
and
1 deletions
PNChart/PNLineChart/PNLineChart.h
PNChart/PNLineChart/PNLineChart.m
PNChart/PNLineChart/PNLineChart.h
View file @
e876f8c
...
...
@@ -52,4 +52,15 @@
@property
(
nonatomic
)
BOOL
showLabel
;
/**
* show CoordinateAxis ornot, Default is not
*/
@property
(
nonatomic
,
getter
=
isShowCoordinateAxis
)
BOOL
showCoordinateAxis
;
@property
(
nonatomic
)
UIColor
*
axisColor
;
@property
(
nonatomic
)
CGFloat
axisWidth
;
@property
(
nonatomic
,
strong
)
NSString
*
xUnit
;
@property
(
nonatomic
,
strong
)
NSString
*
yUnit
;
@end
...
...
PNChart/PNLineChart/PNLineChart.m
View file @
e876f8c
...
...
@@ -239,7 +239,7 @@
pathAnimation
.
duration
=
1
.
0
;
pathAnimation
.
timingFunction
=
[
CAMediaTimingFunction
functionWithName
:
kCAMediaTimingFunctionEaseInEaseOut
];
pathAnimation
.
fromValue
=
@0.0f
;
pathAnimation
.
toValue
=
@1.0f
;
pathAnimation
.
toValue
=
@1.0f
;
[
chartLine
addAnimation
:
pathAnimation
forKey
:
@"strokeEndAnimation"
];
chartLine
.
strokeEnd
=
1
.
0
;
...
...
@@ -305,6 +305,53 @@
}
}
#define IOS7_OR_LATER [[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0
-
(
void
)
drawRect
:
(
CGRect
)
rect
{
if
(
self
.
isShowCoordinateAxis
)
{
CGContextRef
ctx
=
UIGraphicsGetCurrentContext
();
UIGraphicsPushContext
(
ctx
);
CGContextSetLineWidth
(
ctx
,
self
.
axisWidth
);
CGContextSetStrokeColorWithColor
(
ctx
,
[
self
.
axisColor
CGColor
]);
// draw coordinate axis
CGContextMoveToPoint
(
ctx
,
_chartMargin
+
10
,
0
);
CGContextAddLineToPoint
(
ctx
,
_chartMargin
+
10
,
_chartMargin
+
_chartCavanHeight
);
CGContextAddLineToPoint
(
ctx
,
CGRectGetWidth
(
rect
)
-
_chartMargin
,
_chartMargin
+
_chartCavanHeight
);
CGContextStrokePath
(
ctx
);
// draw y axis arrow
CGContextMoveToPoint
(
ctx
,
_chartMargin
+
6
,
8
);
CGContextAddLineToPoint
(
ctx
,
_chartMargin
+
10
,
0
);
CGContextAddLineToPoint
(
ctx
,
_chartMargin
+
14
,
8
);
CGContextStrokePath
(
ctx
);
// draw x axis arrow
CGContextMoveToPoint
(
ctx
,
CGRectGetWidth
(
rect
)
-
_chartMargin
-
8
,
_chartMargin
+
_chartCavanHeight
-
4
);
CGContextAddLineToPoint
(
ctx
,
CGRectGetWidth
(
rect
)
-
_chartMargin
,
_chartMargin
+
_chartCavanHeight
);
CGContextAddLineToPoint
(
ctx
,
CGRectGetWidth
(
rect
)
-
_chartMargin
-
8
,
_chartMargin
+
_chartCavanHeight
+
4
);
CGContextStrokePath
(
ctx
);
UIFont
*
font
=
[
UIFont
systemFontOfSize
:
11
];
// draw y unit
if
([
self
.
yUnit
length
])
{
CGFloat
height
=
[
PNLineChart
heightOfString
:
self
.
yUnit
withWidth
:
30
.
f
font
:
font
];
CGRect
drawRect
=
CGRectMake
(
_chartMargin
+
10
+
5
,
0
,
30
.
f
,
height
);
[
self
drawTextInContext
:
ctx
text
:
self
.
yUnit
inRect
:
drawRect
font
:
font
];
}
// draw x unit
if
([
self
.
xUnit
length
])
{
CGFloat
height
=
[
PNLineChart
heightOfString
:
self
.
xUnit
withWidth
:
30
.
f
font
:
font
];
CGRect
drawRect
=
CGRectMake
(
CGRectGetWidth
(
rect
)
-
_chartMargin
+
5
,
_chartMargin
+
_chartCavanHeight
-
height
/
2
,
25
.
f
,
height
);
[
self
drawTextInContext
:
ctx
text
:
self
.
xUnit
inRect
:
drawRect
font
:
font
];
}
}
[
super
drawRect
:
rect
];
}
#pragma mark private methods
...
...
@@ -325,6 +372,58 @@
_chartCavanWidth
=
self
.
frame
.
size
.
width
-
_chartMargin
*
2
;
_chartCavanHeight
=
self
.
frame
.
size
.
height
-
_chartMargin
*
2
;
// Coordinate Axis Default Values
_showCoordinateAxis
=
NO
;
_axisColor
=
[
UIColor
colorWithRed
:
0
.
4
f
green
:
0
.
4
f
blue
:
0
.
4
f
alpha
:
1
.
f
];
_axisWidth
=
1
.
f
;
}
#pragma mark - tools
+
(
float
)
heightOfString
:
(
NSString
*
)
text
withWidth
:
(
float
)
width
font
:
(
UIFont
*
)
font
{
NSInteger
ch
;
//设置字体
CGSize
size
=
CGSizeMake
(
width
,
MAXFLOAT
);
if
([
text
respondsToSelector
:
@selector
(
boundingRectWithSize
:
options
:
attributes
:
context
:
)])
{
NSDictionary
*
tdic
=
[
NSDictionary
dictionaryWithObjectsAndKeys
:
font
,
NSFontAttributeName
,
nil
];
size
=
[
text
boundingRectWithSize
:
size
options
:
NSStringDrawingUsesLineFragmentOrigin
|
NSStringDrawingUsesFontLeading
attributes
:
tdic
context
:
nil
].
size
;
}
else
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
size
=
[
text
sizeWithFont
:
font
constrainedToSize
:
size
lineBreakMode
:
NSLineBreakByCharWrapping
];
//ios7以上已经摒弃的这个方法
#pragma clang diagnostic pop
}
ch
=
size
.
height
;
return
ch
;
}
-
(
void
)
drawTextInContext
:
(
CGContextRef
)
ctx
text
:
(
NSString
*
)
text
inRect
:
(
CGRect
)
rect
font
:
(
UIFont
*
)
font
{
if
(
IOS7_OR_LATER
)
{
NSMutableParagraphStyle
*
priceParagraphStyle
=
[[
NSParagraphStyle
defaultParagraphStyle
]
mutableCopy
];
priceParagraphStyle
.
lineBreakMode
=
NSLineBreakByTruncatingTail
;
priceParagraphStyle
.
alignment
=
NSTextAlignmentLeft
;
[
text
drawInRect
:
rect
withAttributes
:@{
NSParagraphStyleAttributeName
:
priceParagraphStyle
,
NSFontAttributeName
:
font
}];
}
else
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[
text
drawInRect
:
rect
withFont
:
font
lineBreakMode
:
NSLineBreakByTruncatingTail
alignment
:
NSTextAlignmentLeft
];
#pragma clang diagnostic pop
}
}
...
...
Please
register
or
login
to post a comment