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-01 23:28:16 +0100
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
9eda59f4dedeb387bb9545599051c1adb80c400d
9eda59f4
1 parent
72346578
added some documentation minor changes
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
76 additions
and
48 deletions
PNChart/PNLineChart.h
PNChart/PNLineChart.m
PNChartDemo/PCChartViewController.m
PNChart/PNLineChart.h
View file @
9eda59f
...
...
@@ -71,5 +71,14 @@
-
(
void
)
updateChartData
:(
NSArray
*
)
data
;
/**
* returns the Legend View, or nil if no chart data is present.
* The origin of the legend frame is 0,0 but you can set it with setFrame:(CGRect)
*
* @param mWidth Maximum width of legend. Height will depend on this and font size
*
* @return UIView of Legend
*/
-
(
UIView
*
)
getLegendWithMaxWidth
:(
CGFloat
)
mWidth
;
@end
...
...
PNChart/PNLineChart.m
View file @
9eda59f
...
...
@@ -736,91 +736,108 @@
if
([
self
.
chartData
count
]
<
1
)
{
return
nil
;
}
/* This is a short line that refers to the chart data */
CGFloat
legendLineWidth
=
40
;
/* x and y are the coordinates of the starting point of each legend item */
CGFloat
x
=
0
;
CGFloat
y
=
0
;
/* accumulated width and height */
CGFloat
totalWidth
=
0
;
CGFloat
totalHeight
=
0
;
NSMutableArray
*
legendLabels
=
[[
NSMutableArray
alloc
]
init
];
NSMutableArray
*
legendLines
=
[[
NSMutableArray
alloc
]
init
];
NSMutableArray
*
legendInflexion
=
[[
NSMutableArray
alloc
]
init
];
for
(
PNLineChartData
*
pdata
in
self
.
chartData
)
{
NSMutableArray
*
legendViews
=
[[
NSMutableArray
alloc
]
init
];
CGFloat
maxLabelWidth
=
self
.
legendStyle
==
PNLegendItemStyleStacked
?
(
mWidth
-
legendLineWidth
)
:
(
mWidth
/
[
self
.
chartData
count
]
-
legendLineWidth
);
/* Determine the max width of each legend item */
CGFloat
maxLabelWidth
=
self
.
legendStyle
==
PNLegendItemStyleStacked
?
(
mWidth
-
legendLineWidth
)
:
(
mWidth
/
[
self
.
chartData
count
]
-
legendLineWidth
);
/* this is used when labels wrap text and the line
* should be in the middle of the first row */
CGFloat
singleRowHeight
=
[
PNLineChart
sizeOfString
:
@"Test"
withWidth
:
MAXFLOAT
font
:
[
UIFont
systemFontOfSize
:
self
.
legendFontSize
]].
height
;
for
(
PNLineChartData
*
pdata
in
self
.
chartData
)
{
/* Expected label size*/
CGSize
labelsize
=
[
PNLineChart
sizeOfString
:
pdata
.
dataTitle
withWidth
:
maxLabelWidth
font
:
[
UIFont
systemFontOfSize
:
self
.
legendFontSize
]];
/* draw line */
/* draw lines */
/* 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
* decorator */
CGFloat
inflexionWidthSpacer
=
pdata
.
inflexionPointStyle
==
PNLineChartPointStyleTriangle
?
pdata
.
inflexionPointWidth
/
2
:
pdata
.
inflexionPointWidth
;
CGFloat
t1
=
(
legendLineWidth
*
0
.
8
-
inflexionWidthSpacer
)
/
2
;
UIView
*
line
=
[[
UIView
alloc
]
initWithFrame
:
CGRectMake
(
x
+
legendLineWidth
*
0
.
1
,
y
+
(
labelsize
.
height
-
pdata
.
lineWidth
)
/
2
,
t1
,
pdata
.
lineWidth
)];
line
.
backgroundColor
=
pdata
.
color
;
line
.
alpha
=
pdata
.
alpha
;
[
legendLines
addObject
:
line
];
CGFloat
halfLineLength
;
if
(
pdata
.
inflexionPointStyle
!=
PNLineChartPointStyleNone
)
{
halfLineLength
=
(
legendLineWidth
*
0
.
8
-
inflexionWidthSpacer
)
/
2
;
}
else
{
halfLineLength
=
legendLineWidth
*
0
.
8
;
}
UIView
*
line
=
[[
UIView
alloc
]
initWithFrame
:
CGRectMake
(
x
+
legendLineWidth
*
0
.
1
,
y
+
(
singleRowHeight
-
pdata
.
lineWidth
)
/
2
,
halfLineLength
,
pdata
.
lineWidth
)];
line
=
[[
UIView
alloc
]
initWithFrame
:
CGRectMake
(
x
+
legendLineWidth
*
0
.
1
+
t1
+
inflexionWidthSpacer
,
y
+
(
labelsize
.
height
-
pdata
.
lineWidth
)
/
2
,
t1
,
pdata
.
lineWidth
)];
line
.
backgroundColor
=
pdata
.
color
;
line
.
alpha
=
pdata
.
alpha
;
[
legendViews
addObject
:
line
];
[
legendLines
addObject
:
line
];
if
(
pdata
.
inflexionPointStyle
!=
PNLineChartPointStyleNone
)
{
line
=
[[
UIView
alloc
]
initWithFrame
:
CGRectMake
(
x
+
legendLineWidth
*
0
.
1
+
halfLineLength
+
inflexionWidthSpacer
,
y
+
(
singleRowHeight
-
pdata
.
lineWidth
)
/
2
,
halfLineLength
,
pdata
.
lineWidth
)];
line
.
backgroundColor
=
pdata
.
color
;
line
.
alpha
=
pdata
.
alpha
;
[
legendViews
addObject
:
line
];
}
// Add inflexion type
[
legend
Inflexion
addObject
:[
self
drawInflexion
:
pdata
.
inflexionPointWidth
center
:
CGPointMake
(
x
+
legendLineWidth
/
2
,
y
+
labelsize
.
h
eight
/
2
)
strokeWidth
:
2
[
legend
Views
addObject
:[
self
drawInflexion
:
pdata
.
inflexionPointWidth
center
:
CGPointMake
(
x
+
legendLineWidth
/
2
,
y
+
singleRowH
eight
/
2
)
strokeWidth
:
pdata
.
lineWidth
inflexionStyle
:
pdata
.
inflexionPointStyle
andColor
:
pdata
.
color
andAlpha
:
pdata
.
alpha
]];
UILabel
*
label
=
[[
UILabel
alloc
]
initWithFrame
:
CGRectMake
(
x
+
legendLineWidth
,
y
,
maxLabelWidth
,
labelsize
.
height
)];
label
.
text
=
pdata
.
dataTitle
;
label
.
font
=
[
UIFont
systemFontOfSize
:
self
.
legendFontSize
];
label
.
lineBreakMode
=
NSLineBreakByWordWrapping
;
label
.
numberOfLines
=
0
;
x
+=
self
.
legendStyle
==
PNLegendItemStyleStacked
?
0
:
labelsize
.
width
+
legendLineWidth
;
y
+=
self
.
legendStyle
==
PNLegendItemStyleStacked
?
labelsize
.
height
:
0
;
totalWidth
=
self
.
legendStyle
==
PNLegendItemStyleStacked
?
fmaxf
(
totalWidth
,
labelsize
.
width
+
legendLineWidth
)
:
totalWidth
+
labelsize
.
width
+
legendLineWidth
;
totalHeight
=
self
.
legendStyle
==
PNLegendItemStyleStacked
?
fmaxf
(
totalHeight
,
labelsize
.
height
)
:
totalHeight
+
labelsize
.
height
;
[
legend
Label
s
addObject
:
label
];
[
legend
View
s
addObject
:
label
];
}
UIView
*
legend
=
[[
UIView
alloc
]
initWithFrame
:
CGRectMake
(
100
,
400
,
totalWidth
,
totalHeight
)];
for
(
UILabel
*
l
in
legendLabels
)
{
[
legend
addSubview
:
l
];
}
for
(
UIView
*
v
in
legendLines
)
{
UIView
*
legend
=
[[
UIView
alloc
]
initWithFrame
:
CGRectMake
(
0
,
0
,
totalWidth
,
totalHeight
)];
for
(
UIView
*
v
in
legendViews
)
{
[
legend
addSubview
:
v
];
}
for
(
UIImageView
*
iv
in
legendInflexion
)
{
[
legend
addSubview
:
iv
];
}
return
legend
;
}
//// PaintCode Trial Version
//// www.paintcodeapp.com
-
(
UIImageView
*
)
drawInflexion
:
(
CGFloat
)
size
center
:
(
CGPoint
)
center
strokeWidth
:
(
CGFloat
)
sw
inflexionStyle
:
(
PNLineChartPointStyle
)
type
andColor
:
(
UIColor
*
)
color
andAlpha
:
(
CGFloat
)
alfa
{
//
this is an arbitrary size for exampl
e
//
Make the size a little bigger so it includes also border strok
e
CGSize
aSize
=
CGSizeMake
(
size
+
sw
,
size
+
sw
);
//this can take any CGSize
//it works like the frame.size would in the drawRect: method
//in the way that it represents the context's size
UIGraphicsBeginImageContextWithOptions
(
aSize
,
NO
,
0
.
0
);
//this gets the graphic context
CGContextRef
context
=
UIGraphicsGetCurrentContext
();
if
(
type
==
PNLineChartPointStyleCircle
)
{
//// Oval Drawing
CGContextAddArc
(
context
,
(
size
+
sw
)
/
2
,
(
size
+
sw
)
/
2
,
size
/
2
,
0
,
M_PI
*
2
,
YES
);
}
else
if
(
type
==
PNLineChartPointStyleSquare
){
CGContextAddRect
(
context
,
CGRectMake
(
sw
/
2
,
sw
/
2
,
size
,
size
));
...
...
@@ -832,12 +849,12 @@
CGContextClosePath
(
context
);
}
//Set
the width of the pen mark
//Set
some stroke properties
CGContextSetLineWidth
(
context
,
sw
);
CGContextSetAlpha
(
context
,
alfa
);
CGContextSetStrokeColorWithColor
(
context
,
color
.
CGColor
);
//Finally draw
CGContextDrawPath
(
context
,
kCGPathStroke
);
//now get the image from the context
...
...
@@ -845,14 +862,13 @@
UIGraphicsEndImageContext
();
////
Variable Declarations
////
Translate origin
CGFloat
originX
=
center
.
x
-
(
size
+
sw
)
/
2
.
0
;
CGFloat
originY
=
center
.
y
-
(
size
+
sw
)
/
2
.
0
;
UIImageView
*
squareImageView
=
[[
UIImageView
alloc
]
initWithImage
:
squareImage
];
[
squareImageView
setFrame
:
CGRectMake
(
originX
,
originY
,
size
+
sw
,
size
+
sw
)];
return
squareImageView
;
}
@end
...
...
PNChartDemo/PCChartViewController.m
View file @
9eda59f
...
...
@@ -47,11 +47,11 @@
// Line Chart #2
NSArray
*
data02Array
=
@[
@20.1
,
@180.1
,
@26.4
,
@202.2
,
@126.2
,
@167.2
,
@276.2
];
PNLineChartData
*
data02
=
[
PNLineChartData
new
];
data02
.
dataTitle
=
@"Beta"
;
data02
.
dataTitle
=
@"Beta
Beta Beta Beta
"
;
data02
.
color
=
PNTwitterColor
;
data02
.
alpha
=
0
.
5
f
;
data02
.
itemCount
=
data02Array
.
count
;
data02
.
inflexionPointStyle
=
PNLineChartPointStyle
Circl
e
;
data02
.
inflexionPointStyle
=
PNLineChartPointStyle
Non
e
;
data02
.
getData
=
^
(
NSUInteger
index
)
{
CGFloat
yValue
=
[
data02Array
[
index
]
floatValue
];
return
[
PNLineChartDataItem
dataItemWithY
:
yValue
];
...
...
@@ -63,9 +63,12 @@
[
self
.
view
addSubview
:
self
.
lineChart
];
self
.
lineChart
.
legendStyle
=
PNLegendItemStyleSerial
;
self
.
lineChart
.
legendFontSize
=
17
.
0
;
[
self
.
view
addSubview
:[
self
.
lineChart
getLegendWithMaxWidth
:
200
]];
self
.
lineChart
.
legendStyle
=
PNLegendItemStyleStacked
;
self
.
lineChart
.
legendFontSize
=
12
.
0
;
UIView
*
legend
=
[
self
.
lineChart
getLegendWithMaxWidth
:
200
];
[
legend
setFrame
:
CGRectMake
(
100
,
400
,
legend
.
frame
.
size
.
width
,
legend
.
frame
.
size
.
width
)];
[
self
.
view
addSubview
:
legend
];
}
else
if
([
self
.
title
isEqualToString
:
@"Bar Chart"
])
{
...
...
Please
register
or
login
to post a comment