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
kevinzhow
2014-12-13 05:19:44 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
57d98275bf1168c723585ccd6fe0f62b5a785c9c
57d98275
1 parent
5e2a809a
Update Y labels
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
27 deletions
PNChart/PNLineChart.h
PNChart/PNLineChart.m
PNChart/PNLineChart.h
View file @
57d9827
...
...
@@ -29,6 +29,8 @@
@property
(
nonatomic
)
NSMutableArray
*
pathPoints
;
@property
(
nonatomic
)
NSMutableArray
*
xChartLabels
;
@property
(
nonatomic
)
NSMutableArray
*
yChartLabels
;
@property
(
nonatomic
)
CGFloat
xLabelWidth
;
@property
(
nonatomic
)
UIFont
*
xLabelFont
;
@property
(
nonatomic
)
UIColor
*
xLabelColor
;
...
...
PNChart/PNLineChart.m
View file @
57d9827
...
...
@@ -58,21 +58,32 @@
CGFloat
yStepHeight
=
_chartCavanHeight
/
_yLabelNum
;
NSString
*
yLabelFormat
=
self
.
yLabelFormat
?
:
@"%1.f"
;
if
(
_yChartLabels
)
{
for
(
PNChartLabel
*
label
in
_yChartLabels
)
{
[
label
removeFromSuperview
];
}
}
else
{
_yChartLabels
=
[
NSMutableArray
new
];
}
if
(
yStep
==
0
.
0
)
{
PNChartLabel
*
minLabel
=
[[
PNChartLabel
alloc
]
initWithFrame
:
CGRectMake
(
0
.
0
,
(
NSInteger
)
_chartCavanHeight
,
(
NSInteger
)
_chartMargin
,
(
NSInteger
)
_yLabelHeight
)];
minLabel
.
text
=
[
NSString
stringWithFormat
:
yLabelFormat
,
0
.
0
];
[
self
setCustomStyleForYLabel
:
minLabel
];
[
self
addSubview
:
minLabel
];
[
_yChartLabels
addObject
:
minLabel
];
PNChartLabel
*
midLabel
=
[[
PNChartLabel
alloc
]
initWithFrame
:
CGRectMake
(
0
.
0
,
(
NSInteger
)(
_chartCavanHeight
/
2
),
(
NSInteger
)
_chartMargin
,
(
NSInteger
)
_yLabelHeight
)];
midLabel
.
text
=
[
NSString
stringWithFormat
:
yLabelFormat
,
_yValueMax
];
[
self
setCustomStyleForYLabel
:
midLabel
];
[
self
addSubview
:
midLabel
];
[
_yChartLabels
addObject
:
midLabel
];
PNChartLabel
*
maxLabel
=
[[
PNChartLabel
alloc
]
initWithFrame
:
CGRectMake
(
0
.
0
,
0
.
0
,
(
NSInteger
)
_chartMargin
,
(
NSInteger
)
_yLabelHeight
)];
maxLabel
.
text
=
[
NSString
stringWithFormat
:
yLabelFormat
,
_yValueMax
*
2
];
[
self
setCustomStyleForYLabel
:
maxLabel
];
[
self
addSubview
:
maxLabel
];
[
_yChartLabels
addObject
:
maxLabel
];
}
else
{
NSInteger
index
=
0
;
...
...
@@ -85,6 +96,7 @@
label
.
text
=
[
NSString
stringWithFormat
:
yLabelFormat
,
_yValueMin
+
(
yStep
*
index
)];
[
self
setCustomStyleForYLabel
:
label
];
[
self
addSubview
:
label
];
[
_yChartLabels
addObject
:
label
];
index
+=
1
;
num
-=
1
;
}
...
...
@@ -439,10 +451,6 @@
-
(
void
)
setChartData
:
(
NSArray
*
)
data
{
if
(
data
!=
_chartData
)
{
NSMutableArray
*
yLabelsArray
=
[
NSMutableArray
arrayWithCapacity
:
data
.
count
];
CGFloat
yMax
=
0
.
0
f
;
CGFloat
yMin
=
MAXFLOAT
;
CGFloat
yValue
;
// remove all shape layers before adding new ones
for
(
CALayer
*
layer
in
self
.
chartLineArray
)
{
...
...
@@ -475,43 +483,60 @@
pointLayer
.
lineWidth
=
chartData
.
lineWidth
;
[
self
.
layer
addSublayer
:
pointLayer
];
[
self
.
chartPointArray
addObject
:
pointLayer
];
for
(
NSUInteger
i
=
0
;
i
<
chartData
.
itemCount
;
i
++
)
{
yValue
=
chartData
.
getData
(
i
).
y
;
[
yLabelsArray
addObject
:[
NSString
stringWithFormat
:
@"%2f"
,
yValue
]];
yMax
=
fmaxf
(
yMax
,
yValue
);
yMin
=
fminf
(
yMin
,
yValue
);
}
}
// Min value for Y label
if
(
yMax
<
5
)
{
yMax
=
5
.
0
f
;
}
if
(
yMin
<
0
)
{
yMin
=
0
.
0
f
;
}
_yValueMin
=
yMin
;
_yValueMax
=
yMax
;
_chartData
=
data
;
if
(
_showLabel
)
{
[
self
setYLabels
:
yLabelsArray
];
}
[
self
prepareYLabelsWithData
:
data
];
[
self
setNeedsDisplay
];
}
}
-
(
void
)
prepareYLabelsWithData
:
(
NSArray
*
)
data
{
CGFloat
yMax
=
0
.
0
f
;
CGFloat
yMin
=
MAXFLOAT
;
NSMutableArray
*
yLabelsArray
=
[
NSMutableArray
new
];
for
(
PNLineChartData
*
chartData
in
data
)
{
// create as many chart line layers as there are data-lines
for
(
NSUInteger
i
=
0
;
i
<
chartData
.
itemCount
;
i
++
)
{
CGFloat
yValue
=
chartData
.
getData
(
i
).
y
;
[
yLabelsArray
addObject
:[
NSString
stringWithFormat
:
@"%2f"
,
yValue
]];
yMax
=
fmaxf
(
yMax
,
yValue
);
yMin
=
fminf
(
yMin
,
yValue
);
}
}
// Min value for Y label
if
(
yMax
<
5
)
{
yMax
=
5
.
0
f
;
}
if
(
yMin
<
0
)
{
yMin
=
0
.
0
f
;
}
_yValueMin
=
yMin
;
_yValueMax
=
yMax
+
yMax
/
10
.
0
;
if
(
_showLabel
)
{
[
self
setYLabels
:
yLabelsArray
];
}
}
#pragma mark - Update Chart Data
-
(
void
)
updateChartData
:
(
NSArray
*
)
data
{
_chartData
=
data
;
[
self
prepareYLabelsWithData
:
data
];
[
self
calculateChartPath
:
_chartPath
andPointsPath
:
_pointPath
andPathKeyPoints
:
_pathPoints
];
for
(
NSUInteger
lineIndex
=
0
;
lineIndex
<
self
.
chartData
.
count
;
lineIndex
++
)
{
...
...
Please
register
or
login
to post a comment