Skip to content

Commit

Permalink
Merge pull request #5 from sirenum/master
Browse files Browse the repository at this point in the history
Autoscaling
  • Loading branch information
honcheng committed Dec 24, 2011
2 parents 6c590a0 + a393982 commit 374e5c2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
11 changes: 9 additions & 2 deletions iOSPlot/Shared/PCLineChartView.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,22 @@
NSMutableArray *components;
NSMutableArray *xLabels;
UIFont *yLabelFont, *xLabelFont, *valueLabelFont, *legendFont;
int interval;
float interval;
float minValue;
float maxValue;

// Use these to autoscale the y axis to 'nice' values.
// If used, minValue is ignored (0) and interval computed internally
BOOL autoscaleYAxis;
NSUInteger numYIntervals; // Use n*5 for best results
}

@property (nonatomic, assign) int interval;
@property (nonatomic, assign) float interval;
@property (nonatomic, assign) float minValue;
@property (nonatomic, assign) float maxValue;
@property (nonatomic, retain) NSMutableArray *components, *xLabels;
@property (nonatomic, retain) UIFont *yLabelFont, *xLabelFont, *valueLabelFont, *legendFont;
@property (nonatomic, assign) BOOL autoscaleYAxis;
@property (nonatomic, assign) NSUInteger numYIntervals;

@end
31 changes: 25 additions & 6 deletions iOSPlot/Shared/PCLineChartView.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ @implementation PCLineChartView
@synthesize interval, minValue, maxValue;
@synthesize xLabels;
@synthesize yLabelFont, xLabelFont, valueLabelFont, legendFont;
@synthesize autoscaleYAxis, numYIntervals;

- (id)initWithFrame:(CGRect)frame
{
Expand All @@ -60,6 +61,7 @@ - (id)initWithFrame:(CGRect)frame
interval = 20;
maxValue = 100;
minValue = 0;
numYIntervals = 5;
yLabelFont = [[UIFont fontWithName:@"GeezaPro-Bold" size:14] retain];
xLabelFont = [[UIFont fontWithName:@"HiraKakuProN-W6" size:12] retain];
valueLabelFont = [[UIFont fontWithName:@"HiraKakuProN-W6" size:10] retain];
Expand All @@ -75,19 +77,36 @@ - (void)drawRect:(CGRect)rect
UIGraphicsPushContext(ctx);
CGContextSetRGBFillColor(ctx, 0.2f, 0.2f, 0.2f, 1.0f);

int n_div = (maxValue-minValue)/self.interval + 1;
int n_div;
int power;
float scale_min, scale_max, div_height;
float top_margin = 35;
float bottom_margin = 25;
float x_label_height = 20;
float div_height = (self.frame.size.height-top_margin-bottom_margin-x_label_height)/(n_div-1);

if (autoscaleYAxis) {
scale_min = 0.0;
power = floor(log10(maxValue/5));
float increment = maxValue / (5 * pow(10,power));
increment = (increment <= 5) ? ceil(increment) : 10;
increment = increment * pow(10,power);
scale_max = 5 * increment;
self.interval = scale_max / numYIntervals;
} else {
scale_min = minValue;
scale_max = maxValue;
}
n_div = (scale_max-scale_min)/self.interval + 1;
div_height = (self.frame.size.height-top_margin-bottom_margin-x_label_height)/(n_div-1);

for (int i=0; i<n_div; i++)
{
float y_axis = maxValue - i*self.interval;
float y_axis = scale_max - i*self.interval;

int y = top_margin + div_height*i;
CGRect textFrame = CGRectMake(0,y-8,25,20);
NSString *text = [NSString stringWithFormat:@"%.0f", y_axis];
NSString *formatString = [NSString stringWithFormat:@"%%.%if", (power < 0) ? -power : 0];
NSString *text = [NSString stringWithFormat:formatString, y_axis];
[text drawInRect:textFrame
withFont:yLabelFont
lineBreakMode:UILineBreakModeWordWrap
Expand Down Expand Up @@ -142,7 +161,7 @@ - (void)drawRect:(CGRect)rect
CGContextSetLineWidth(ctx, circle_stroke_width);

int x = margin + div_width*x_axis_index;
int y = top_margin + (maxValue-value)/self.interval*div_height;
int y = top_margin + (scale_max-value)/self.interval*div_height;

CGRect circleRect = CGRectMake(x-circle_diameter/2, y-circle_diameter/2, circle_diameter,circle_diameter);
CGContextStrokeEllipseInRect(ctx, circleRect);
Expand Down Expand Up @@ -193,7 +212,7 @@ - (void)drawRect:(CGRect)rect
{
float value = [object floatValue];
int x = margin + div_width*i;
int y = top_margin + (maxValue-value)/self.interval*div_height;
int y = top_margin + (scale_max-value)/self.interval*div_height;
int y1 = y - circle_diameter/2 - valueLabelFont.pointSize;
int y2 = y + circle_diameter/2;

Expand Down

0 comments on commit 374e5c2

Please sign in to comment.