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
neo
2014-01-27 16:36:42 +0100
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
ae484652a7026700eb7829a634485e8ea2b4710a
ae484652
1 parent
75bc30cb
removed defines from PNLineChart.h (not typesafe)
fixed issue #24
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
65 additions
and
34 deletions
PNChartDemo/PNChart/PNLineChart.h
PNChartDemo/PNChart/PNLineChart.m
PNChartDemo/PNChart/PNLineChart.h
View file @
ae48465
...
...
@@ -11,9 +11,8 @@
#import <QuartzCore/QuartzCore.h>
#import "PNChartDelegate.h"
#define chartMargin 10
#define yLabelMargin 15
#define yLabelHeight 11
@interface
PNLineChart
:
UIView
...
...
@@ -30,19 +29,31 @@
@property
(
strong
,
nonatomic
)
NSArray
*
yLabels
;
/**
* Array of `LineChartData` objects, one for each line.
*/
* Array of `LineChartData` objects, one for each line.
*/
@property
(
strong
,
nonatomic
)
NSArray
*
chartData
;
@property
(
strong
,
nonatomic
)
NSMutableArray
*
pathPoints
;
@property
(
nonatomic
)
CGFloat
xLabelWidth
;
@property
(
nonatomic
)
float
yValueMax
;
@property
(
nonatomic
)
CGFloat
xLabelHeight
;
@property
(
nonatomic
)
CGFloat
yValueMax
;
@property
(
nonatomic
)
CGFloat
yValueMin
;
@property
(
nonatomic
)
NSInteger
yLabelNum
;
@property
(
nonatomic
)
CGFloat
yLabelHeight
;
@property
(
nonatomic
)
CGFloat
chartCavanHeight
;
@property
(
nonatomic
)
CGFloat
chartCavanWidth
;
@property
(
nonatomic
)
CGFloat
chartMargin
;
@property
(
nonatomic
)
float
chartCavanHeight
;
@property
(
nonatomic
)
float
xLabelHeight
;
@property
(
nonatomic
)
BOOL
showLabel
;
...
...
PNChartDemo/PNChart/PNLineChart.m
View file @
ae48465
...
...
@@ -55,15 +55,16 @@
-
(
void
)
setYLabels
:(
NSArray
*
)
yLabels
{
float
level
=
_yValueMax
/
5
.
0
;
float
yStep
=
(
_yValueMax
-
_yValueMin
)
/
_yLabelNum
;
CGFloat
yStepHeight
=
_chartCavanHeight
/
_yLabelNum
;
NSInteger
index
=
0
;
NSInteger
num
=
[
yLabels
count
]
+
1
;
NSInteger
num
=
_yLabelNum
+
1
;
while
(
num
>
0
)
{
CGFloat
levelHeight
=
_chartCavanHeight
/
5
.
0
;
PNChartLabel
*
label
=
[[
PNChartLabel
alloc
]
initWithFrame
:
CGRectMake
(
0
.
0
,
_chartCavanHeight
-
index
*
levelHeight
+
(
levelHeight
-
yLabelHeight
)
,
20
.
0
,
yLabelHeight
)];
PNChartLabel
*
label
=
[[
PNChartLabel
alloc
]
initWithFrame
:
CGRectMake
(
0
.
0
,
(
_chartCavanHeight
-
index
*
yStepHeight
),
_chartMargin
,
_yLabelHeight
)];
[
label
setTextAlignment
:
NSTextAlignmentRight
];
label
.
text
=
[
NSString
stringWithFormat
:
@"%1.f"
,
level
*
index
];
label
.
text
=
[
NSString
stringWithFormat
:
@"%1.f"
,
_yValueMin
+
(
yStep
*
index
)
];
[
self
addSubview
:
label
];
index
+=
1
;
num
-=
1
;
...
...
@@ -76,12 +77,12 @@
_xLabels
=
xLabels
;
if
(
_showLabel
){
_xLabelWidth
=
(
self
.
frame
.
size
.
width
-
chartMargin
-
30
.
0
)
/
[
xLabels
count
];
_xLabelWidth
=
_chartCavanWidth
/
[
xLabels
count
];
for
(
int
index
=
0
;
index
<
xLabels
.
count
;
index
++
)
{
NSString
*
labelText
=
xLabels
[
index
];
PNChartLabel
*
label
=
[[
PNChartLabel
alloc
]
initWithFrame
:
CGRectMake
(
index
*
_xLabelWidth
+
30
.
0
,
self
.
frame
.
size
.
height
-
30
.
0
,
_xLabelWidth
,
20
.
0
)];
PNChartLabel
*
label
=
[[
PNChartLabel
alloc
]
initWithFrame
:
CGRectMake
(
2
*
_chartMargin
+
(
index
*
_xLabelWidth
)
-
(
_xLabelWidth
/
2
),
_chartMargin
+
_chartCavanHeight
,
_xLabelWidth
,
_chartMargin
)];
[
label
setTextAlignment
:
NSTextAlignmentCenter
];
label
.
text
=
labelText
;
[
self
addSubview
:
label
];
...
...
@@ -142,39 +143,42 @@
UIBezierPath
*
progressline
=
[
UIBezierPath
bezierPath
];
[
_chartPath
addObject
:
progressline
];
PNLineChartDataItem
*
firstDataItem
=
chartData
.
getData
(
0
);
CGFloat
firstValue
=
firstDataItem
.
y
;
CGFloat
xPosition
=
_xLabelWidth
;
CGFloat
xPosition
=
_chartMargin
;
if
(
!
_showLabel
){
_chartCavanHeight
=
self
.
frame
.
size
.
height
-
_xLabelHeight
*
2
;
//heuristic don't know why
_yLabelHeight
=
2
;
_chartCavanHeight
=
self
.
frame
.
size
.
height
-
2
*
_yLabelHeight
;
_chartCavanWidth
=
self
.
frame
.
size
.
width
;
_yLabelHeight
=
5
.
0
;
_chartMargin
=
0
.
0
;
_xLabelWidth
=
(
_chartCavanWidth
/
([
_xLabels
count
]
-
1
));
xPosition
=
0
;
}
CGFloat
grade
=
(
float
)
firstValue
/
_yValueMax
;
NSMutableArray
*
linePointsArray
=
[[
NSMutableArray
alloc
]
init
];
[
progressline
moveToPoint
:
CGPointMake
(
xPosition
,
_chartCavanHeight
-
grade
*
_chartCavanHeight
+
_xLabelHeight
)];
[
linePointsArray
addObject
:[
NSValue
valueWithCGPoint
:
CGPointMake
(
xPosition
,
_chartCavanHeight
-
grade
*
_chartCavanHeight
+
_xLabelHeight
)]];
[
progressline
setLineWidth
:
3
.
0
];
[
progressline
setLineCapStyle
:
kCGLineCapRound
];
[
progressline
setLineJoinStyle
:
kCGLineJoinRound
];
PNLineChartDataItem
*
dataItem
;
NSInteger
index
=
0
;
for
(
NSUInteger
i
=
0
;
i
<
chartData
.
itemCount
;
i
++
)
{
PNLineChartDataItem
*
dataItem
=
chartData
.
getData
(
i
);
dataItem
=
chartData
.
getData
(
i
);
float
value
=
dataItem
.
y
;
CGFloat
innerGrade
=
value
/
_yValueMax
;
if
(
index
!=
0
)
{
CGPoint
point
=
CGPointMake
(
index
*
_xLabelWidth
+
30
.
0
+
_xLabelWidth
/
2
.
0
,
_chartCavanHeight
-
(
innerGrade
*
_chartCavanHeight
)
+
_xLabelHeight
);
[
linePointsArray
addObject
:[
NSValue
valueWithCGPoint
:
point
]];
CGFloat
innerGrade
=
(
value
-
_yValueMin
)
/
(
_yValueMax
-
_yValueMin
);
CGPoint
point
=
CGPointMake
(
2
*
_chartMargin
+
(
i
*
_xLabelWidth
),
_chartCavanHeight
-
(
innerGrade
*
_chartCavanHeight
)
+
(
_yLabelHeight
/
2
));
if
(
i
!=
0
)
{
[
progressline
addLineToPoint
:
point
];
[
progressline
moveToPoint
:
point
];
}
index
+=
1
;
[
progressline
moveToPoint
:
point
];
[
linePointsArray
addObject
:[
NSValue
valueWithCGPoint
:
point
]];
}
[
_pathPoints
addObject
:[
linePointsArray
copy
]];
// setup the color of the chart line
...
...
@@ -206,7 +210,7 @@
NSMutableArray
*
yLabelsArray
=
[
NSMutableArray
arrayWithCapacity
:
data
.
count
];
CGFloat
yMax
=
0
.
0
f
;
CGFloat
yMin
=
MAXFLOAT
;
// remove all shape layers before adding new ones
for
(
CALayer
*
layer
in
self
.
chartLineArray
)
{
[
layer
removeFromSuperlayer
];
...
...
@@ -230,6 +234,7 @@
CGFloat
yValue
=
dataItem
.
y
;
[
yLabelsArray
addObject
:[
NSString
stringWithFormat
:
@"%2f"
,
yValue
]];
yMax
=
fmaxf
(
yMax
,
dataItem
.
y
);
yMin
=
fminf
(
yMin
,
dataItem
.
y
);
}
}
...
...
@@ -237,6 +242,12 @@
if
(
yMax
<
5
)
{
yMax
=
5
.
0
f
;
}
if
(
yMin
<
0
){
yMin
=
0
.
0
f
;
}
NSLog
(
@"%f"
,
yMin
);
_yValueMin
=
yMin
;
_yValueMax
=
yMax
;
_chartData
=
data
;
...
...
@@ -259,8 +270,17 @@
_showLabel
=
YES
;
_pathPoints
=
[[
NSMutableArray
alloc
]
init
];
self
.
userInteractionEnabled
=
YES
;
_xLabelHeight
=
20
.
0
;
_chartCavanHeight
=
self
.
frame
.
size
.
height
-
chartMargin
*
2
-
_xLabelHeight
*
2
;
_yLabelNum
=
5
.
0
;
_yLabelHeight
=
[[[[
PNChartLabel
alloc
]
init
]
font
]
pointSize
];
_chartMargin
=
30
;
_xLabelHeight
=
_chartMargin
;
_chartCavanWidth
=
self
.
frame
.
size
.
width
-
_chartMargin
*
2
;
_chartCavanHeight
=
self
.
frame
.
size
.
height
-
_chartMargin
*
2
;
}
@end
...
...
Please
register
or
login
to post a comment