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
Jonathan Tron
2015-07-12 15:59:21 +0200
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
3719489e25f03619e1458e4df55289139c9449b6
3719489e
1 parent
f4c5f550
Allow setting Top/Bottom/Left/Right margins independently in PNBarChart
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
40 additions
and
21 deletions
PNChart/PNBarChart.h
PNChart/PNBarChart.m
PNChartDemo/PCChartViewController.m
PNChart/PNBarChart.h
View file @
3719489
...
...
@@ -46,7 +46,10 @@ typedef NSString *(^PNYLabelFormatter)(CGFloat yLabelValue);
/** Formats the ylabel text. */
@property
(
copy
)
PNYLabelFormatter
yLabelFormatter
;
@property
(
nonatomic
)
CGFloat
chartMargin
;
@property
(
nonatomic
)
CGFloat
chartMarginLeft
;
@property
(
nonatomic
)
CGFloat
chartMarginRight
;
@property
(
nonatomic
)
CGFloat
chartMarginTop
;
@property
(
nonatomic
)
CGFloat
chartMarginBottom
;
/** Controls whether labels should be displayed. */
@property
(
nonatomic
)
BOOL
showLabel
;
...
...
PNChart/PNBarChart.m
View file @
3719489
...
...
@@ -56,7 +56,10 @@
_xLabelSkip
=
1
;
_yLabelSum
=
4
;
_labelMarginTop
=
0
;
_chartMargin
=
25
.
0
;
_chartMarginLeft
=
25
.
0
;
_chartMarginRight
=
25
.
0
;
_chartMarginTop
=
25
.
0
;
_chartMarginBottom
=
25
.
0
;
_barRadius
=
2
.
0
;
_showChartBorder
=
NO
;
_showLevelLine
=
NO
;
...
...
@@ -104,7 +107,7 @@
[
self
processYMaxValue
];
float
sectionHeight
=
(
self
.
frame
.
size
.
height
-
_chartMargin
*
2
-
kXLabelHeight
)
/
_yLabelSum
;
float
sectionHeight
=
(
self
.
frame
.
size
.
height
-
_chartMargin
Top
-
_chartMarginBottom
-
kXLabelHeight
)
/
_yLabelSum
;
for
(
int
i
=
0
;
i
<=
_yLabelSum
;
i
++
)
{
NSString
*
labelText
;
if
(
_yLabels
)
{
...
...
@@ -114,7 +117,7 @@
labelText
=
_yLabelFormatter
((
float
)
_yValueMax
*
(
(
_yLabelSum
-
i
)
/
(
float
)
_yLabelSum
));
}
CGRect
frame
=
(
CGRect
){
0
,
sectionHeight
*
i
+
_chartMargin
-
kYLabelHeight
/
2
.
0
,
_yChartLabelWidth
,
kYLabelHeight
};
CGRect
frame
=
(
CGRect
){
0
,
sectionHeight
*
i
+
_chartMargin
Top
-
kYLabelHeight
/
2
.
0
,
_yChartLabelWidth
,
kYLabelHeight
};
PNChartLabel
*
label
=
[[
PNChartLabel
alloc
]
initWithFrame
:
frame
];
label
.
font
=
_labelFont
;
label
.
textColor
=
_labelTextColor
;
...
...
@@ -153,7 +156,7 @@
_xChartLabels
=
[
NSMutableArray
new
];
}
_xLabelWidth
=
(
self
.
frame
.
size
.
width
-
_chartMargin
*
2
)
/
[
xLabels
count
];
_xLabelWidth
=
(
self
.
frame
.
size
.
width
-
_chartMargin
Left
-
_chartMarginRight
)
/
[
xLabels
count
];
if
(
_showLabel
)
{
int
labelAddCount
=
0
;
...
...
@@ -171,13 +174,13 @@
CGFloat
labelXPosition
;
if
(
_rotateForXAxisText
){
label
.
transform
=
CGAffineTransformMakeRotation
(
M_PI
/
4
);
labelXPosition
=
(
index
*
_xLabelWidth
+
_chartMargin
+
_xLabelWidth
/
1
.
5
);
labelXPosition
=
(
index
*
_xLabelWidth
+
_chartMargin
Left
+
_xLabelWidth
/
1
.
5
);
}
else
{
labelXPosition
=
(
index
*
_xLabelWidth
+
_chartMargin
+
_xLabelWidth
/
2
.
0
);
labelXPosition
=
(
index
*
_xLabelWidth
+
_chartMargin
Left
+
_xLabelWidth
/
2
.
0
);
}
label
.
center
=
CGPointMake
(
labelXPosition
,
self
.
frame
.
size
.
height
-
kXLabelHeight
-
_chartMargin
+
label
.
frame
.
size
.
height
/
2
.
0
+
_labelMarginTop
);
self
.
frame
.
size
.
height
-
kXLabelHeight
-
_chartMargin
Top
+
label
.
frame
.
size
.
height
/
2
.
0
+
_labelMarginTop
);
labelAddCount
=
0
;
[
_xChartLabels
addObject
:
label
];
...
...
@@ -197,7 +200,7 @@
{
//Add bars
CGFloat
chartCavanHeight
=
self
.
frame
.
size
.
height
-
_chartMargin
*
2
-
kXLabelHeight
;
CGFloat
chartCavanHeight
=
self
.
frame
.
size
.
height
-
_chartMargin
Top
-
_chartMarginBottom
-
kXLabelHeight
;
NSInteger
index
=
0
;
for
(
NSNumber
*
valueString
in
_yValues
)
{
...
...
@@ -212,9 +215,9 @@
if
(
_barWidth
)
{
barWidth
=
_barWidth
;
barXPosition
=
index
*
_xLabelWidth
+
_chartMargin
+
_xLabelWidth
/
2
.
0
-
_barWidth
/
2
.
0
;
barXPosition
=
index
*
_xLabelWidth
+
_chartMargin
Left
+
_xLabelWidth
/
2
.
0
-
_barWidth
/
2
.
0
;
}
else
{
barXPosition
=
index
*
_xLabelWidth
+
_chartMargin
+
_xLabelWidth
*
0
.
25
;
barXPosition
=
index
*
_xLabelWidth
+
_chartMargin
Left
+
_xLabelWidth
*
0
.
25
;
if
(
_showLabel
)
{
barWidth
=
_xLabelWidth
*
0
.
5
;
...
...
@@ -226,7 +229,7 @@
}
bar
=
[[
PNBar
alloc
]
initWithFrame
:
CGRectMake
(
barXPosition
,
//Bar X position
self
.
frame
.
size
.
height
-
chartCavanHeight
-
kXLabelHeight
-
_chartMargin
,
//Bar Y position
self
.
frame
.
size
.
height
-
chartCavanHeight
-
kXLabelHeight
-
_chartMargin
Top
,
//Bar Y position
barWidth
,
// Bar witdh
self
.
showLevelLine
?
chartCavanHeight
/
2
.
0
:
chartCavanHeight
)];
//Bar height
...
...
@@ -301,8 +304,8 @@
UIBezierPath
*
progressline
=
[
UIBezierPath
bezierPath
];
[
progressline
moveToPoint
:
CGPointMake
(
_chartMargin
,
self
.
frame
.
size
.
height
-
kXLabelHeight
-
_chartMargin
)];
[
progressline
addLineToPoint
:
CGPointMake
(
self
.
frame
.
size
.
width
-
_chartMargin
,
self
.
frame
.
size
.
height
-
kXLabelHeight
-
_chartMargin
)];
[
progressline
moveToPoint
:
CGPointMake
(
_chartMargin
Left
,
self
.
frame
.
size
.
height
-
kXLabelHeight
-
_chartMarginTop
)];
[
progressline
addLineToPoint
:
CGPointMake
(
self
.
frame
.
size
.
width
-
_chartMargin
Right
,
self
.
frame
.
size
.
height
-
kXLabelHeight
-
_chartMarginTop
)];
[
progressline
setLineWidth
:
1
.
0
];
[
progressline
setLineCapStyle
:
kCGLineCapSquare
];
...
...
@@ -330,8 +333,8 @@
UIBezierPath
*
progressLeftline
=
[
UIBezierPath
bezierPath
];
[
progressLeftline
moveToPoint
:
CGPointMake
(
_chartMargin
,
self
.
frame
.
size
.
height
-
kXLabelHeight
-
_chartMargin
)];
[
progressLeftline
addLineToPoint
:
CGPointMake
(
_chartMargin
,
_chartMargin
)];
[
progressLeftline
moveToPoint
:
CGPointMake
(
_chartMargin
Left
,
self
.
frame
.
size
.
height
-
kXLabelHeight
-
_chartMarginBottom
+
_chartMarginTop
)];
[
progressLeftline
addLineToPoint
:
CGPointMake
(
_chartMargin
Left
,
_chartMarginTop
)];
[
progressLeftline
setLineWidth
:
1
.
0
];
[
progressLeftline
setLineCapStyle
:
kCGLineCapSquare
];
...
...
@@ -360,8 +363,8 @@
UIBezierPath
*
progressline
=
[
UIBezierPath
bezierPath
];
[
progressline
moveToPoint
:
CGPointMake
(
_chartMargin
,
(
self
.
frame
.
size
.
height
-
kXLabelHeight
)
/
2
.
0
)];
[
progressline
addLineToPoint
:
CGPointMake
(
self
.
frame
.
size
.
width
-
_chartMargin
,
(
self
.
frame
.
size
.
height
-
kXLabelHeight
)
/
2
.
0
)];
[
progressline
moveToPoint
:
CGPointMake
(
_chartMargin
Left
,
(
self
.
frame
.
size
.
height
-
kXLabelHeight
)
/
2
.
0
)];
[
progressline
addLineToPoint
:
CGPointMake
(
self
.
frame
.
size
.
width
-
_chartMargin
Left
-
_chartMarginRight
,
(
self
.
frame
.
size
.
height
-
kXLabelHeight
)
/
2
.
0
)];
[
progressline
setLineWidth
:
1
.
0
];
[
progressline
setLineCapStyle
:
kCGLineCapSquare
];
...
...
PNChartDemo/PCChartViewController.m
View file @
3719489
...
...
@@ -91,20 +91,33 @@
}
else
if
([
self
.
title
isEqualToString
:
@"Bar Chart"
])
{
static
NSNumberFormatter
*
barChartFormatter
;
if
(
!
barChartFormatter
){
barChartFormatter
=
[[
NSNumberFormatter
alloc
]
init
];
barChartFormatter
.
numberStyle
=
NSNumberFormatterCurrencyStyle
;
barChartFormatter
.
allowsFloats
=
NO
;
barChartFormatter
.
maximumFractionDigits
=
0
;
}
self
.
titleLabel
.
text
=
@"Bar Chart"
;
self
.
barChart
=
[[
PNBarChart
alloc
]
initWithFrame
:
CGRectMake
(
0
,
135
.
0
,
SCREEN_WIDTH
,
200
.
0
)];
// self.barChart.showLabel = NO;
self
.
barChart
.
backgroundColor
=
[
UIColor
clearColor
];
self
.
barChart
.
yLabelFormatter
=
^
(
CGFloat
yValue
){
CGFloat
yValueParsed
=
yValue
;
NSString
*
labelText
=
[
NSString
stringWithFormat
:
@"%0.f"
,
yValueParsed
];
return
labelText
;
return
[
barChartFormatter
stringFromNumber
:[
NSNumber
numberWithFloat
:
yValue
]];
};
self
.
barChart
.
yChartLabelWidth
=
60
.
0
;
self
.
barChart
.
chartMarginLeft
=
70
.
0
;
self
.
barChart
.
chartMarginRight
=
10
.
0
;
self
.
barChart
.
chartMarginTop
=
5
.
0
;
self
.
barChart
.
chartMarginBottom
=
10
.
0
;
self
.
barChart
.
labelMarginTop
=
5
.
0
;
self
.
barChart
.
showChartBorder
=
YES
;
[
self
.
barChart
setXLabels
:@[
@"2"
,
@"3"
,
@"4"
,
@"5"
,
@"2"
,
@"3"
,
@"4"
,
@"5"
]];
// self.barChart.yLabels = @[@-10,@0,@10];
// [self.barChart setYValues:@[@10000.0,@30000.0,@10000.0,@100000.0,@500000.0,@1000000.0,@1150000.0,@2150000.0]];
[
self
.
barChart
setYValues
:@[
@10.82
,
@1.88
,
@6.96
,
@33.93
,
@10.82
,
@1.88
,
@6.96
,
@33.93
]];
[
self
.
barChart
setStrokeColors
:@[
PNGreen
,
PNGreen
,
PNRed
,
PNGreen
,
PNGreen
,
PNGreen
,
PNRed
,
PNGreen
]];
self
.
barChart
.
isGradientShow
=
NO
;
...
...
Please
register
or
login
to post a comment