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
Kevin
2015-05-24 22:29:12 +0800
Browse Files
Options
Browse Files
Download
Plain Diff
Commit
12d35ba0a6fecce12e788439d798f68db7b057b5
12d35ba0
2 parents
dbbbd0ae
dbfb6649
Merge pull request #197 from dullgrass/master
Added the new feature support custom yLabels
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
52 additions
and
41 deletions
PNChart/PNBar.m
PNChart/PNBarChart.m
PNChartDemo/PCChartViewController.m
PNChart/PNBar.m
View file @
12d35ba
...
...
@@ -39,8 +39,8 @@
-
(
void
)
setGrade
:(
float
)
grade
{
NSLog
(
@"New garde %f"
,
grade
);
//
NSLog(@"New garde %f",grade);
CGFloat
startPosY
=
(
1
-
grade
)
*
self
.
frame
.
size
.
height
;
UIBezierPath
*
progressline
=
[
UIBezierPath
bezierPath
];
...
...
@@ -201,8 +201,8 @@
[
_chartLine
addSublayer
:
self
.
textLayer
];
[
self
.
textLayer
setFontSize
:
textheigt
/
2
];
[
self
.
textLayer
setString
:[[
NSString
alloc
]
initWithFormat
:
@"%
ld"
,(
NSInteger
)(
grade
*
100
)
]];
[
self
.
textLayer
setString
:[[
NSString
alloc
]
initWithFormat
:
@"%
0.f"
,
grade
*
100
]];
[
self
.
textLayer
setFrame
:
CGRectMake
(
0
,
textStartPosY
,
textWidth
,
textheigt
)];
self
.
textLayer
.
contentsScale
=
[
UIScreen
mainScreen
].
scale
;
...
...
PNChart/PNBarChart.m
View file @
12d35ba
...
...
@@ -10,7 +10,6 @@
#import "PNColor.h"
#import "PNChartLabel.h"
@interface
PNBarChart
()
{
NSMutableArray
*
_xChartLabels
;
NSMutableArray
*
_yChartLabels
;
...
...
@@ -67,48 +66,55 @@
-
(
void
)
setYValues
:
(
NSArray
*
)
yValues
{
_yValues
=
yValues
;
//make the _yLabelSum value dependant of the distinct values of yValues to avoid duplicates on yAxis
if
(
_showLabel
)
{
[
self
__addYCoordinateLabelsValues
];
}
}
//make the _yLabelSum value dependant of the distinct values of yValues to avoid duplicates on yAxis
int
yLabelsDifTotal
=
(
int
)[
NSSet
setWithArray
:
yValues
].
count
;
_yLabelSum
=
yLabelsDifTotal
%
2
==
0
?
yLabelsDifTotal
:
yLabelsDifTotal
+
1
;
if
(
_yMaxValue
)
{
_yValueMax
=
_yMaxValue
;
#pragma mark - Private Method
#pragma mark - 添加柱状图的Y轴坐标
-
(
void
)
__addYCoordinateLabelsValues
{
[
self
viewCleanupForCollection
:
_yChartLabels
];
NSArray
*
yAxisValues
=
_yLabels
?
_yLabels
:
_yValues
;
if
(
_yMaxValue
)
{
_yValueMax
=
_yMaxValue
;
}
else
{
[
self
getYValueMax
:
yAxisValues
];
}
if
(
_yLabelSum
==
4
)
{
_yLabelSum
=
yAxisValues
.
count
;
(
_yLabelSum
%
2
==
0
)
?
_yLabelSum
:
_yLabelSum
++
;
}
float
sectionHeight
=
(
self
.
frame
.
size
.
height
-
_chartMargin
*
2
-
kXLabelHeight
)
/
_yLabelSum
;
for
(
int
i
=
0
;
i
<
_yLabelSum
;
i
++
)
{
NSString
*
labelText
;
if
(
_yLabels
)
{
float
yAsixValue
=
[
_yLabels
[
_yLabels
.
count
-
i
-
1
]
floatValue
];
labelText
=
_yLabelFormatter
(
yAsixValue
);
}
else
{
[
self
getYValueMax
:
yValues
]
;
labelText
=
_yLabelFormatter
((
float
)
_yValueMax
*
(
(
_yLabelSum
-
i
)
/
(
float
)
_yLabelSum
))
;
}
if
(
_yChartLabels
)
{
[
self
viewCleanupForCollection
:
_yChartLabels
];
}
else
{
_yLabels
=
[
NSMutableArray
new
];
}
CGRect
frame
=
(
CGRect
){
0
,
sectionHeight
*
i
+
_chartMargin
-
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
];
if
(
_showLabel
)
{
//Add y labels
float
yLabelSectionHeight
=
(
self
.
frame
.
size
.
height
-
_chartMargin
*
2
-
kXLabelHeight
)
/
_yLabelSum
;
for
(
int
index
=
0
;
index
<
_yLabelSum
;
index
++
)
{
NSString
*
labelText
=
_yLabelFormatter
((
float
)
_yValueMax
*
(
(
_yLabelSum
-
index
)
/
(
float
)
_yLabelSum
));
PNChartLabel
*
label
=
[[
PNChartLabel
alloc
]
initWithFrame
:
CGRectMake
(
0
,
yLabelSectionHeight
*
index
+
_chartMargin
-
kYLabelHeight
/
2
.
0
,
_yChartLabelWidth
,
kYLabelHeight
)];
label
.
font
=
_labelFont
;
label
.
textColor
=
_labelTextColor
;
[
label
setTextAlignment
:
NSTextAlignmentRight
];
label
.
text
=
labelText
;
[
_yChartLabels
addObject
:
label
];
[
self
addSubview
:
label
];
}
}
[
_yChartLabels
addObject
:
label
];
}
}
-
(
void
)
updateChartData
:
(
NSArray
*
)
data
{
self
.
yValues
=
data
;
[
self
updateBar
];
...
...
@@ -238,7 +244,7 @@
float
value
=
[
valueString
floatValue
];
float
grade
=
(
float
)
value
/
(
float
)
_yValueMax
;
if
(
isnan
(
grade
))
{
grade
=
0
;
}
...
...
PNChartDemo/PCChartViewController.m
View file @
12d35ba
...
...
@@ -103,7 +103,12 @@
self
.
barChart
.
labelMarginTop
=
5
.
0
;
[
self
.
barChart
setXLabels
:@[
@"SEP 1"
,
@"SEP 2"
,
@"SEP 3"
,
@"SEP 4"
,
@"SEP 5"
,
@"SEP 6"
,
@"SEP 7"
]];
self
.
barChart
.
rotateForXAxisText
=
true
;
self
.
barChart
.
yLabelSum
=
5
;
self
.
barChart
.
yMaxValue
=
100
;
[
self
.
barChart
setYValues
:@[
@1
,
@24
,
@12
,
@18
,
@30
,
@10
,
@21
]];
// self.barChart.yLabels = @[@0,@20,@40,@60];
[
self
.
barChart
setStrokeColors
:@[
PNGreen
,
PNGreen
,
PNRed
,
PNGreen
,
PNGreen
,
PNYellow
,
PNGreen
]];
// Adding gradient
self
.
barChart
.
barColorGradientStart
=
[
UIColor
blueColor
];
...
...
Please
register
or
login
to post a comment