Skip to content

Commit 8fe5d13

Browse files
committed
specify or read the range of the values, ....
specify or read the max min... useful if you want to make 2 graph based on the same values, otherwise they would be inconsistent between them, for example graph of download has max 20 min 10 , and graph of updates has max 5 and min 0, without that the 20 and the 5 would be in the same place MPGraphValuesRange downloadRange=[MPPlot rangeForValues:values]; MPGraphValuesRange updateRange=[MPPlot rangeForValues:upValues]; MPGraphValuesRange range=MPGetBiggestRange(downloadRange, updateRange); graph.valueRanges=range; updateGraph.valueRanges=range;
1 parent 6b0e83f commit 8fe5d13

File tree

3 files changed

+79
-23
lines changed

3 files changed

+79
-23
lines changed

MPPlot.h

+32-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,34 @@ typedef NS_ENUM(NSUInteger, MPPlotType) {
2424
typedef CGFloat(^GraphPointsAlgorithm)(CGFloat x);
2525

2626

27+
struct _MPValuesRange {
28+
CGFloat max;
29+
CGFloat min;
30+
};
31+
32+
typedef struct _MPValuesRange MPGraphValuesRange;
33+
34+
NS_INLINE MPGraphValuesRange MPMakeGraphValuesRange(CGFloat min, CGFloat max) {
35+
MPGraphValuesRange r;
36+
r.min = min;
37+
r.max = max;
38+
return r;
39+
}
40+
41+
NS_INLINE BOOL MPValuesRangeNULL(MPGraphValuesRange r) {
42+
return r.min==CGFLOAT_MIN && r.max==CGFLOAT_MAX;
43+
}
44+
45+
NS_INLINE MPGraphValuesRange MPGetBiggestRange(MPGraphValuesRange r1,MPGraphValuesRange r2) {
46+
47+
MPGraphValuesRange r;
48+
49+
r.min=(r1.min<r2.min ? r1.min : r2.min);
50+
r.max=(r1.max>r2.max ? r1.max : r2.max);
51+
52+
return r;
53+
}
54+
2755
@protocol MPDetailView <NSObject>
2856

2957
- (void)setText:(NSString *)text;
@@ -54,24 +82,25 @@ typedef CGFloat(^GraphPointsAlgorithm)(CGFloat x);
5482
// Abstract Class
5583

5684
+ (id)plotWithType:(MPPlotType)type frame:(CGRect)frame;
57-
85+
+ (MPGraphValuesRange)rangeForValues:(NSArray *)values;
5886

5987

6088
@property (nonatomic,copy) NSArray *values; // array of NSNumber or NSString
6189
@property (nonatomic,retain) UIColor *graphColor; // color of the line
6290
@property (nonatomic,assign) CGFloat lineWidth;
6391
@property (nonatomic,assign) BOOL waitToUpdate;
6492
@property (nonatomic,assign) CGFloat animationDuration;
93+
@property (nonatomic,assign) MPGraphValuesRange valueRanges; // specify or read the max min... useful if you want to make 2 graph based on the same values, otherwise they would be inconsistent between them, for example graph of download has max 20 min 10 , and graph of updates has max 5 and min 0, without that the 20 and the 5 would be in the same place
6594

66-
@property (nonatomic,readwrite) UIView<MPDetailView> *detailView;
6795

6896
// detail View customization
97+
@property (nonatomic,readwrite) UIView<MPDetailView> *detailView;
6998
@property (nonatomic,retain) UIColor *detailBackgroundColor;
7099
@property (nonatomic,retain) UIColor *detailTextColor;
71-
72100
@property (nonatomic,retain) NSNumberFormatter *detailLabelFormatter;
73101

74102

103+
75104
- (void)animate;
76105

77106
- (void)setAlgorithm:(GraphPointsAlgorithm)customAlgorithm numberOfPoints:(NSUInteger)numberOfPoints;

MPPlot.m

+47-20
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,39 @@
1212

1313
@implementation MPPlot
1414

15+
+ (MPGraphValuesRange)rangeForValues:(NSArray *)values{
16+
17+
CGFloat min,max;
18+
19+
min=max=[[values firstObject] floatValue];
20+
21+
22+
for (NSInteger i=0; i<values.count; i++) {
23+
24+
CGFloat val=[[values objectAtIndex:i] floatValue];
25+
26+
if (val>max) {
27+
max=val;
28+
}
29+
30+
if (val<min) {
31+
min=val;
32+
}
33+
34+
}
35+
36+
37+
38+
return MPMakeGraphValuesRange(min, max);
39+
}
40+
41+
1542

1643
- (id)init
1744
{
1845
self = [super init];
1946
if (self) {
47+
self.valueRanges=MPMakeGraphValuesRange(CGFLOAT_MIN, CGFLOAT_MAX);
2048
NSAssert(![self isMemberOfClass:[MPPlot class]], @"You shouldn't init MPPlot directly, use the class method plotWithType:frame:");
2149
}
2250
return self;
@@ -26,6 +54,7 @@ - (id)initWithFrame:(CGRect)frame
2654
{
2755
self = [super initWithFrame:frame];
2856
if (self) {
57+
self.valueRanges=MPMakeGraphValuesRange(CGFLOAT_MIN, CGFLOAT_MAX);
2958
NSAssert(![self isMemberOfClass:[MPPlot class]], @"You shouldn't init MPPlot directly, use the class method plotWithType:frame:");
3059
}
3160
return self;
@@ -71,35 +100,25 @@ - (void)drawRect:(CGRect)rect{
71100

72101

73102

74-
- (NSMutableArray *)pointsForArray:(NSArray *)dict{
75-
76-
CGFloat min,max,avg;
103+
- (NSMutableArray *)pointsForArray:(NSArray *)values{
77104

78-
min=max=[[dict firstObject] floatValue];
105+
CGFloat min,max;
79106

80107

81-
for (NSInteger i=0; i<dict.count; i++) {
82-
83-
CGFloat val=[[dict objectAtIndex:i] floatValue];
84-
85-
if (val>max) {
86-
max=val;
87-
}
88-
89-
if (val<min) {
90-
min=val;
91-
}
92-
93-
avg+=val;
108+
if (MPValuesRangeNULL(self.valueRanges)) {
109+
_valueRanges=[MPPlot rangeForValues:values];
110+
min=_valueRanges.min;
111+
max=_valueRanges.max;
112+
}else{
113+
max=_valueRanges.max;
114+
min=_valueRanges.min;
94115
}
95116

96-
avg/=dict.count;
97-
98117

99118
NSMutableArray *pointsArray=[[NSMutableArray alloc] init];
100119

101120
if(max!=min){
102-
for (NSString *p in dict) {
121+
for (NSString *p in values) {
103122

104123
CGFloat val=[p floatValue];
105124

@@ -133,6 +152,14 @@ - (void)setValues:(NSArray *)values{
133152

134153
}
135154

155+
- (void)setValueRanges:(MPGraphValuesRange)valueRanges{
156+
157+
_valueRanges=valueRanges;
158+
159+
if (!MPValuesRangeNULL(valueRanges) && self.values) {
160+
points=[self pointsForArray:self.values];
161+
}
162+
}
136163

137164

138165
- (void)setAlgorithm:(GraphPointsAlgorithm)customAlgorithm numberOfPoints:(NSUInteger)numberOfPoints{

0 commit comments

Comments
 (0)