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
andi
2015-03-19 11:31:50 +0100
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
df8e253c8226278c366aa76a2a5c5773202cb59e
df8e253c
1 parent
62b50d23
improved legend for line chart, added thousands separator option for y labels
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
39 additions
and
16 deletions
PNChart/PNLineChart.h
PNChart/PNLineChart.m
PNChart/PNLineChart.h
View file @
df8e253
...
...
@@ -48,6 +48,7 @@
@property
(
nonatomic
)
CGFloat
chartMargin
;
@property
(
nonatomic
)
BOOL
showLabel
;
@property
(
nonatomic
)
BOOL
showGenYLabels
;
@property
(
nonatomic
)
BOOL
thousandsSeparator
;
/**
...
...
PNChart/PNLineChart.m
View file @
df8e253
...
...
@@ -56,7 +56,6 @@
{
CGFloat
yStep
=
(
_yValueMax
-
_yValueMin
)
/
_yLabelNum
;
CGFloat
yStepHeight
=
_chartCavanHeight
/
_yLabelNum
;
NSString
*
yLabelFormat
=
self
.
yLabelFormat
?
:
@"%1.f"
;
if
(
_yChartLabels
)
{
for
(
PNChartLabel
*
label
in
_yChartLabels
)
{
...
...
@@ -68,19 +67,19 @@
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
];
minLabel
.
text
=
[
self
formatYLabel
:
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
];
midLabel
.
text
=
[
self
formatYLabel
:
_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
];
maxLabel
.
text
=
[
self
formatYLabel
:
_yValueMax
*
2
];
[
self
setCustomStyleForYLabel
:
maxLabel
];
[
self
addSubview
:
maxLabel
];
[
_yChartLabels
addObject
:
maxLabel
];
...
...
@@ -93,7 +92,7 @@
{
PNChartLabel
*
label
=
[[
PNChartLabel
alloc
]
initWithFrame
:
CGRectMake
(
0
.
0
,
(
NSInteger
)(
_chartCavanHeight
-
index
*
yStepHeight
),
(
NSInteger
)
_chartMargin
,
(
NSInteger
)
_yLabelHeight
)];
[
label
setTextAlignment
:
NSTextAlignmentRight
];
label
.
text
=
[
NSString
stringWithFormat
:
yLabelFormat
,
_yValueMin
+
(
yStep
*
index
)];
label
.
text
=
[
self
formatYLabel
:
_yValueMin
+
(
yStep
*
index
)];
[
self
setCustomStyleForYLabel
:
label
];
[
self
addSubview
:
label
];
[
_yChartLabels
addObject
:
label
];
...
...
@@ -589,8 +588,8 @@
yMin
=
0
.
0
f
;
}
_yValueMin
=
_yFixedValueMin
?
_yFixedValueMin
:
yMin
;
_yValueMax
=
_yFixedValueMax
?
_yFixedValueMax
:
yMax
+
yMax
/
10
.
0
;
_yValueMin
=
(
_yFixedValueMin
>
-
FLT_MAX
)
?
_yFixedValueMin
:
yMin
;
_yValueMax
=
(
_yFixedValueMax
>
-
FLT_MAX
)
?
_yFixedValueMax
:
yMax
+
yMax
/
10
.
0
;
if
(
_showGenYLabels
)
{
[
self
setYLabels
];
...
...
@@ -732,6 +731,8 @@
_endPointsOfPath
=
[[
NSMutableArray
alloc
]
init
];
self
.
userInteractionEnabled
=
YES
;
_yFixedValueMin
=
-
FLT_MAX
;
_yFixedValueMax
=
-
FLT_MAX
;
_yLabelNum
=
5
.
0
;
_yLabelHeight
=
[[[[
PNChartLabel
alloc
]
init
]
font
]
pointSize
];
...
...
@@ -791,6 +792,19 @@
}
-
(
NSString
*
)
formatYLabel
:
(
double
)
value
{
if
(
!
self
.
thousandsSeparator
)
{
NSString
*
format
=
self
.
yLabelFormat
?
:
@"%1.f"
;
return
[
NSString
stringWithFormat
:
format
,
value
];
}
NSNumberFormatter
*
numberFormatter
=
[[
NSNumberFormatter
alloc
]
init
];
[
numberFormatter
setFormatterBehavior
:
NSNumberFormatterBehavior10_4
];
[
numberFormatter
setNumberStyle
:
NSNumberFormatterDecimalStyle
];
return
[
numberFormatter
stringFromNumber
:
[
NSNumber
numberWithDouble
:
value
]];
}
-
(
UIView
*
)
getLegendWithMaxWidth
:
(
CGFloat
)
mWidth
{
if
([
self
.
chartData
count
]
<
1
)
{
return
nil
;
...
...
@@ -805,13 +819,17 @@
/* accumulated height */
CGFloat
totalHeight
=
0
;
CGFloat
totalWidth
=
0
;
NSMutableArray
*
legendViews
=
[[
NSMutableArray
alloc
]
init
];
NSUInteger
numLabelsPerRow
=
ceil
((
float
)[
self
.
chartData
count
]
/
self
.
labelRowsInSerialMode
);
/* Determine the max width of each legend item */
CGFloat
maxLabelWidth
=
self
.
legendStyle
==
PNLegendItemStyleStacked
?
(
mWidth
-
legendLineWidth
)
:
(
mWidth
/
numLabelsPerRow
-
legendLineWidth
);
CGFloat
maxLabelWidth
;
if
(
self
.
legendStyle
==
PNLegendItemStyleStacked
)
{
maxLabelWidth
=
mWidth
-
legendLineWidth
;
}
else
{
maxLabelWidth
=
MAXFLOAT
;
}
/* this is used when labels wrap text and the line
* should be in the middle of the first row */
...
...
@@ -820,6 +838,7 @@
font
:
self
.
legendFont
?
self
.
legendFont
:
[
UIFont
systemFontOfSize
:
12
.
0
f
]].
height
;
NSUInteger
counter
=
0
;
NSUInteger
rowWidth
=
0
;
NSUInteger
rowMaxHeight
=
0
;
for
(
PNLineChartData
*
pdata
in
self
.
chartData
)
{
...
...
@@ -829,12 +848,14 @@
font
:
self
.
legendFont
?
self
.
legendFont
:
[
UIFont
systemFontOfSize
:
12
.
0
f
]];
/* draw lines */
if
(
counter
!=
0
&&
counter
%
numLabelsPerRow
==
0
)
{
if
((
rowWidth
+
labelsize
.
width
+
legendLineWidth
>
mWidth
)
&&
(
self
.
legendStyle
==
PNLegendItemStyleSerial
))
{
rowWidth
=
0
;
x
=
0
;
y
+=
rowMaxHeight
;
rowMaxHeight
=
0
;
}
rowWidth
+=
labelsize
.
width
+
legendLineWidth
;
totalWidth
=
self
.
legendStyle
==
PNLegendItemStyleSerial
?
fmaxf
(
rowWidth
,
totalWidth
)
:
fmaxf
(
totalWidth
,
labelsize
.
width
+
legendLineWidth
);
/* If there is inflection decorator, the line is composed of two lines
* and this is the space that separates two lines in order to put inflection
...
...
@@ -871,19 +892,20 @@
andColor
:
pdata
.
color
andAlpha
:
pdata
.
alpha
]];
UILabel
*
label
=
[[
UILabel
alloc
]
initWithFrame
:
CGRectMake
(
x
+
legendLineWidth
,
y
,
maxLabelW
idth
,
labelsize
.
height
)];
UILabel
*
label
=
[[
UILabel
alloc
]
initWithFrame
:
CGRectMake
(
x
+
legendLineWidth
,
y
,
labelsize
.
w
idth
,
labelsize
.
height
)];
label
.
text
=
pdata
.
dataTitle
;
label
.
textColor
=
self
.
legendFontColor
?
self
.
legendFontColor
:
[
UIColor
blackColor
];
label
.
font
=
self
.
legendFont
?
self
.
legendFont
:
[
UIFont
systemFontOfSize
:
12
.
0
f
];
label
.
lineBreakMode
=
NSLineBreakByWordWrapping
;
label
.
numberOfLines
=
0
;
rowMaxHeight
=
fmaxf
(
rowMaxHeight
,
labelsize
.
height
);
x
+=
self
.
legendStyle
==
PNLegendItemStyleStacked
?
0
:
labelsize
.
width
+
legendLineWidth
;
y
+=
self
.
legendStyle
==
PNLegendItemStyleStacked
?
labelsize
.
height
:
0
;
totalHeight
=
self
.
legendStyle
==
PNLegendItemStyleStacked
?
fmaxf
(
totalHeight
,
rowMaxHeight
+
y
)
:
totalHeight
+
labelsize
.
height
;
totalHeight
=
self
.
legendStyle
==
PNLegendItemStyleSerial
?
fmaxf
(
totalHeight
,
rowMaxHeight
+
y
)
:
totalHeight
+
labelsize
.
height
;
[
legendViews
addObject
:
label
];
counter
++
;
}
...
...
Please
register
or
login
to post a comment