Permalink
Browse files

Fixed the division by zero bug.

  • Loading branch information...
1 parent 3545b9f commit 2459c2b534c8f8770f704900d3cb6d30a59993a4 @yatsu yatsu committed Nov 25, 2010
View
7 .gitignore
@@ -0,0 +1,7 @@
+IPhoneSimpleDemo/*.xcodeproj/project.xcworkspace/xcuserdata/*.xcuserdatad
+IPhoneSimpleDemo/*.xcodeproj/xcuserdata/*.xcuserdatad/
+IPhoneSimpleDemo/*.xcodeproj/*.pbxuser
+IPhoneSimpleDemo/*.xcodeproj/*.mode*
+IPhoneSimpleDemo/build/Debug-*
+IPhoneSimpleDemo/build/Release-*
+IPhoneSimpleDemo/build/*.build
View
17 IPhoneSimpleDemo/IPhoneSimpleDemo.xcodeproj/project.pbxproj
@@ -156,7 +156,14 @@
isa = PBXProject;
buildConfigurationList = C01FCF4E08A954540054247B /* Build configuration list for PBXProject "IPhoneSimpleDemo" */;
compatibilityVersion = "Xcode 3.1";
+ developmentRegion = English;
hasScannedForEncodings = 1;
+ knownRegions = (
+ English,
+ Japanese,
+ French,
+ German,
+ );
mainGroup = 29B97314FDCFA39411CA2CEA /* CustomTemplate */;
projectDirPath = "";
projectRoot = "";
@@ -229,12 +236,13 @@
buildSettings = {
ARCHS = "$(ARCHS_STANDARD_32_BIT)";
BUILD_STYLE = Debug;
- "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
+ "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "Don't Code Sign";
GCC_C_LANGUAGE_STANDARD = c99;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
PREBINDING = NO;
- SDKROOT = iphoneos3.1.3;
+ "PROVISIONING_PROFILE[sdk=iphoneos*]" = "";
+ SDKROOT = iphoneos;
};
name = Debug;
};
@@ -243,13 +251,14 @@
buildSettings = {
ARCHS = "$(ARCHS_STANDARD_32_BIT)";
BUILD_STYLE = Release;
- "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
+ "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "Don't Code Sign";
GCC_C_LANGUAGE_STANDARD = c99;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
OTHER_CFLAGS = "-DNS_BLOCK_ASSERTIONS=1";
PREBINDING = NO;
- SDKROOT = iphoneos3.1.3;
+ "PROVISIONING_PROFILE[sdk=iphoneos*]" = "";
+ SDKROOT = iphoneos;
};
name = Release;
};
View
3 IPhoneSimpleDemo/build/.gitignore
@@ -1,3 +0,0 @@
-*.build
-*-iphonesimulator
-*-iphoneos
View
8 TreemapKit/TreemapView.h
@@ -5,10 +5,10 @@
@protocol TreemapViewDelegate;
@interface TreemapView : UIView <TreemapViewCellDelegate> {
- id <TreemapViewDataSource> dataSource;
- id <TreemapViewDelegate> delegate;
+ id <TreemapViewDataSource> dataSource;
+ id <TreemapViewDelegate> delegate;
- BOOL initialized;
+ BOOL initialized;
}
@property (nonatomic, retain) id <TreemapViewDataSource> dataSource;
@@ -34,7 +34,7 @@
@optional
-- (float)treemapView:(TreemapView *)treemapView separatorWidthForDepth:(NSInteger)depth;
+- (CGFloat)treemapView:(TreemapView *)treemapView separatorWidthForDepth:(NSInteger)depth;
- (NSInteger)treemapView:(TreemapView *)treemapView separationPositionForDepth:(NSInteger)depth;
@end
View
267 TreemapKit/TreemapView.m
@@ -5,174 +5,179 @@ @implementation TreemapView
@synthesize dataSource;
@synthesize delegate;
-- (void)calcNodePositions:(CGRect)rect nodes:(NSArray *)nodes width:(float)width height:(float)height depth:(int)depth withCreate:(BOOL)createNode {
- if (nodes.count <= 1) {
- NSInteger index = [[[nodes objectAtIndex:0] valueForKey:@"index"] intValue];
- if (createNode) {
- TreemapViewCell *cell = [dataSource treemapView:self cellForIndex:index forRect:rect];
- cell.index = index;
- cell.delegate = self;
- [self addSubview:cell];
- }
- else {
- TreemapViewCell *cell = [self.subviews objectAtIndex:index];
- cell.frame = rect;
- if ([delegate respondsToSelector:@selector(treemapView:updateCell:forIndex:forRect:)])
- [delegate treemapView:self updateCell:cell forIndex:index forRect:rect];
- [cell layoutSubviews];
- }
- return;
- }
-
- float total = 0;
- for (NSDictionary *dic in nodes) {
- total += [[dic objectForKey:@"value"] floatValue];
- }
- float half = total / 2.0;
-
- int customSep = NSNotFound;
- if ([dataSource respondsToSelector:@selector(treemapView:separationPositionForDepth:)])
- customSep = [dataSource treemapView:self separationPositionForDepth:depth];
-
- int m;
- if (customSep != NSNotFound) {
- m = customSep;
- }
- else {
- m = nodes.count - 1;
- total = 0.0;
- for (int i = 0; i < nodes.count; i++) {
- if (total > half) {
- m = i;
- break;
- }
- total += [[[nodes objectAtIndex:i] objectForKey:@"value"] floatValue];
- }
- if (m < 1) m = 1;
- }
-
- NSArray *aArray = [nodes subarrayWithRange:NSMakeRange(0, m)];
- NSArray *bArray = [nodes subarrayWithRange:NSMakeRange(m, nodes.count - m)];
-
- float aTotal = 0.0;
- for (NSDictionary *dic in aArray) {
- aTotal += [[dic objectForKey:@"value"] floatValue];
- }
- float bTotal = 0.0;
- for (NSDictionary *dic in bArray) {
- bTotal += [[dic objectForKey:@"value"] floatValue];
- }
- float aRatio = aTotal / (aTotal + bTotal);
-
- CGRect aRect, bRect;
- float aWidth, aHeight, bWidth, bHeight;
-
- BOOL horizontal = (width > height);
-
- float sep = 0.0;
- if ([dataSource respondsToSelector:@selector(treemapView:separatorWidthForDepth:)])
- sep = [dataSource treemapView:self separatorWidthForDepth:depth];
-
- if (horizontal) {
- aWidth = ceil((width - sep) * aRatio);
- bWidth = width - sep - aWidth;
- aHeight = bHeight = height;
- aRect = CGRectMake(rect.origin.x, rect.origin.y, aWidth, aHeight);
- bRect = CGRectMake(rect.origin.x + aWidth + sep, rect.origin.y, bWidth, bHeight);
- }
- else { // vertical layout
- aWidth = bWidth = width;
- aHeight = ceil((height - sep) * aRatio);
- bHeight = height - sep - aHeight;
- aRect = CGRectMake(rect.origin.x, rect.origin.y, aWidth, aHeight);
- bRect = CGRectMake(rect.origin.x, rect.origin.y + aHeight + sep, bWidth, bHeight);
- }
-
- [self calcNodePositions:aRect nodes:aArray width:aWidth height:aHeight depth:depth + 1 withCreate:createNode];
- [self calcNodePositions:bRect nodes:bArray width:bWidth height:bHeight depth:depth + 1 withCreate:createNode];
+- (void)calcNodePositions:(CGRect)rect nodes:(NSArray *)nodes width:(CGFloat)width height:(CGFloat)height depth:(NSInteger)depth withCreate:(BOOL)createNode {
+ if (nodes.count <= 1) {
+ NSInteger index = [[[nodes objectAtIndex:0] valueForKey:@"index"] integerValue];
+ if (createNode) {
+ TreemapViewCell *cell = [dataSource treemapView:self cellForIndex:index forRect:rect];
+ cell.index = index;
+ cell.delegate = self;
+ [self addSubview:cell];
+ }
+ else {
+ TreemapViewCell *cell = [self.subviews objectAtIndex:index];
+ cell.frame = rect;
+ if ([delegate respondsToSelector:@selector(treemapView:updateCell:forIndex:forRect:)])
+ [delegate treemapView:self updateCell:cell forIndex:index forRect:rect];
+ [cell layoutSubviews];
+ }
+ return;
+ }
+
+ CGFloat total = 0;
+ for (NSDictionary *dic in nodes) {
+ total += [[dic objectForKey:@"value"] floatValue];
+ }
+ CGFloat half = total / 2.0;
+
+ NSInteger customSep = NSNotFound;
+ if ([dataSource respondsToSelector:@selector(treemapView:separationPositionForDepth:)])
+ customSep = [dataSource treemapView:self separationPositionForDepth:depth];
+
+ NSInteger m;
+ if (customSep != NSNotFound) {
+ m = customSep;
+ }
+ else {
+ m = nodes.count - 1;
+ total = 0.0;
+ for (NSInteger i = 0; i < nodes.count; i++) {
+ if (total > half) {
+ m = i;
+ break;
+ }
+ total += [[[nodes objectAtIndex:i] objectForKey:@"value"] floatValue];
+ }
+ if (m < 1) m = 1;
+ }
+
+ NSArray *aArray = [nodes subarrayWithRange:NSMakeRange(0, m)];
+ NSArray *bArray = [nodes subarrayWithRange:NSMakeRange(m, nodes.count - m)];
+
+ CGFloat aTotal = 0.0;
+ for (NSDictionary *dic in aArray) {
+ aTotal += [[dic objectForKey:@"value"] floatValue];
+ }
+ CGFloat bTotal = 0.0;
+ for (NSDictionary *dic in bArray) {
+ bTotal += [[dic objectForKey:@"value"] floatValue];
+ }
+
+ CGFloat aRatio;
+ if (aTotal + bTotal > 0.0)
+ aRatio = aTotal / (aTotal + bTotal);
+ else
+ aRatio = 0.5;
+
+ CGRect aRect, bRect;
+ CGFloat aWidth, aHeight, bWidth, bHeight;
+
+ BOOL horizontal = (width > height);
+
+ CGFloat sep = 0.0;
+ if ([dataSource respondsToSelector:@selector(treemapView:separatorWidthForDepth:)])
+ sep = [dataSource treemapView:self separatorWidthForDepth:depth];
+
+ if (horizontal) {
+ aWidth = ceil((width - sep) * aRatio);
+ bWidth = width - sep - aWidth;
+ aHeight = bHeight = height;
+ aRect = CGRectMake(rect.origin.x, rect.origin.y, aWidth, aHeight);
+ bRect = CGRectMake(rect.origin.x + aWidth + sep, rect.origin.y, bWidth, bHeight);
+ }
+ else { // vertical layout
+ aWidth = bWidth = width;
+ aHeight = ceil((height - sep) * aRatio);
+ bHeight = height - sep - aHeight;
+ aRect = CGRectMake(rect.origin.x, rect.origin.y, aWidth, aHeight);
+ bRect = CGRectMake(rect.origin.x, rect.origin.y + aHeight + sep, bWidth, bHeight);
+ }
+
+ [self calcNodePositions:aRect nodes:aArray width:aWidth height:aHeight depth:depth + 1 withCreate:createNode];
+ [self calcNodePositions:bRect nodes:bArray width:bWidth height:bHeight depth:depth + 1 withCreate:createNode];
}
- (NSArray *)getData {
- NSArray *values = [dataSource valuesForTreemapView:self];
- NSMutableArray *nodes = [NSMutableArray arrayWithCapacity:values.count];
- for (int i = 0; i < values.count; i++) {
- NSNumber *value = [values objectAtIndex:i];
- NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:2];
- [dic setValue:[NSNumber numberWithInt:i] forKey:@"index"];
- [dic setValue:value forKey:@"value"];
- [nodes addObject:dic];
- }
- return nodes;
+ NSArray *values = [dataSource valuesForTreemapView:self];
+ NSMutableArray *nodes = [NSMutableArray arrayWithCapacity:values.count];
+ for (NSInteger i = 0; i < values.count; i++) {
+ NSNumber *value = [values objectAtIndex:i];
+ NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:2];
+ [dic setValue:[NSNumber numberWithInt:i] forKey:@"index"];
+ [dic setValue:value forKey:@"value"];
+ [nodes addObject:dic];
+ }
+ return nodes;
}
- (void)createNodes {
- NSArray *nodes = [self getData];
- if (nodes && nodes.count > 0) {
- [self calcNodePositions:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)
- nodes:nodes
- width:ceil(self.frame.size.width)
- height:ceil(self.frame.size.height)
- depth:0
- withCreate:YES];
- }
+ NSArray *nodes = [self getData];
+ if (nodes && nodes.count > 0) {
+ [self calcNodePositions:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)
+ nodes:nodes
+ width:ceil(self.frame.size.width)
+ height:ceil(self.frame.size.height)
+ depth:0
+ withCreate:YES];
+ }
}
- (void)resizeNodes {
- NSArray *nodes = [self getData];
- if (nodes && nodes.count > 0) {
- [self calcNodePositions:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)
- nodes:nodes
- width:ceil(self.frame.size.width)
- height:ceil(self.frame.size.height)
- depth:0
- withCreate:NO];
- }
+ NSArray *nodes = [self getData];
+ if (nodes && nodes.count > 0) {
+ [self calcNodePositions:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)
+ nodes:nodes
+ width:ceil(self.frame.size.width)
+ height:ceil(self.frame.size.height)
+ depth:0
+ withCreate:NO];
+ }
}
- (void)removeNodes {
- for (UIView *view in self.subviews) {
- [view removeFromSuperview];
- }
+ for (UIView *view in self.subviews) {
+ [view removeFromSuperview];
+ }
}
#pragma mark -
#pragma mark Public methods
- (void)reloadData {
- [self resizeNodes];
+ [self resizeNodes];
}
#pragma mark -
#pragma mark TreemapViewCell delegate
- (void)treemapViewCell:(TreemapViewCell *)treemapViewCell tapped:(NSInteger)index {
- if ([delegate respondsToSelector:@selector(treemapView:tapped:)])
- [delegate treemapView:self tapped:index];
+ if ([delegate respondsToSelector:@selector(treemapView:tapped:)])
+ [delegate treemapView:self tapped:index];
}
#pragma mark -
- (id)initWithFrame:(CGRect)frame {
- if ((self = [super initWithFrame:frame])) {
- initialized = NO;
- }
- return self;
+ if ((self = [super initWithFrame:frame])) {
+ initialized = NO;
+ }
+ return self;
}
- (void)layoutSubviews {
- [super layoutSubviews];
+ [super layoutSubviews];
- if (!initialized) {
- [self createNodes];
- initialized = YES;
- }
+ if (!initialized) {
+ [self createNodes];
+ initialized = YES;
+ }
}
- (void)dealloc {
- [dataSource release];
- [delegate release];
+ [dataSource release];
+ [delegate release];
- [super dealloc];
+ [super dealloc];
}
@end

0 comments on commit 2459c2b

Please sign in to comment.