Skip to content

Prevent division by zero #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
@property (nonatomic, copy) void (^clickHandler)(NSString* identifier, CAShapeLayer* layer);

// Loading functions
- (void)loadMap:(NSString*)mapName forSize:(CGSize)size withColors:(NSDictionary*)colorsDict;
- (void)loadMap:(NSString*)mapName forSize:(CGSize)size withData:(NSDictionary*)data colorAxis:(NSArray*)colors;
- (void)loadMap:(NSString*)mapName withColors:(NSDictionary*)colorsDict;
- (void)loadMap:(NSString*)mapName withData:(NSDictionary*)data colorAxis:(NSArray*)colors;

Expand All @@ -28,4 +30,7 @@
// Layers enumeration
- (void)enumerateLayersUsingBlock:(void(^)(NSString* identifier, CAShapeLayer* layer))block;

// Clear all layers
- (void)clearAll;

@end
64 changes: 61 additions & 3 deletions FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ - (id)initWithFrame:(CGRect)frame
return self;
}

- (void)awakeFromNib {
[super awakeFromNib];

_scaledPaths = [NSMutableArray array];
[self setDefaultParameters];
}

- (void)setDefaultParameters
{
self.fillColor = [UIColor colorWithWhite:0.85 alpha:1];
Expand All @@ -38,6 +45,47 @@ - (void)setDefaultParameters

#pragma mark - SVG map loading

- (void)loadMap:(NSString*)mapName forSize:(CGSize)size withColors:(NSDictionary*)colorsDict {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a log of duplication in that method from - (void)loadMap:(NSString*)mapName withColors:(NSDictionary*)colorsDict. Can you try to factor it?

_svg = [FSSVG svgWithFile:mapName];

for (FSSVGPathElement* path in _svg.paths) {
// Make the map fits inside the frame
float scaleHorizontal = size.width / _svg.bounds.size.width;
float scaleVertical = size.height / _svg.bounds.size.height;
float scale = MIN(scaleHorizontal, scaleVertical);

CGAffineTransform scaleTransform = CGAffineTransformIdentity;
scaleTransform = CGAffineTransformMakeScale(scale, scale);
scaleTransform = CGAffineTransformTranslate(scaleTransform, size.width/2, -_svg.bounds.origin.y);

UIBezierPath* scaled = [path.path copy];
[scaled applyTransform:scaleTransform];

CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = scaled.CGPath;

// Setting CAShapeLayer properties
shapeLayer.strokeColor = self.strokeColor.CGColor;
shapeLayer.lineWidth = 0.5;

if(path.fill) {
if(colorsDict && [colorsDict objectForKey:path.identifier]) {
UIColor* color = [colorsDict objectForKey:path.identifier];
shapeLayer.fillColor = color.CGColor;
} else {
shapeLayer.fillColor = self.fillColor.CGColor;
}

} else {
shapeLayer.fillColor = [[UIColor clearColor] CGColor];
}

[self.layer addSublayer:shapeLayer];

[_scaledPaths addObject:scaled];
}
}

- (void)loadMap:(NSString*)mapName withColors:(NSDictionary*)colorsDict
{
_svg = [FSSVG svgWithFile:mapName];
Expand All @@ -50,7 +98,7 @@ - (void)loadMap:(NSString*)mapName withColors:(NSDictionary*)colorsDict

CGAffineTransform scaleTransform = CGAffineTransformIdentity;
scaleTransform = CGAffineTransformMakeScale(scale, scale);
scaleTransform = CGAffineTransformTranslate(scaleTransform,-_svg.bounds.origin.x, -_svg.bounds.origin.y);
scaleTransform = CGAffineTransformTranslate(scaleTransform, -_svg.bounds.origin.y, -_svg.bounds.origin.y);

UIBezierPath* scaled = [path.path copy];
[scaled applyTransform:scaleTransform];
Expand Down Expand Up @@ -84,6 +132,11 @@ - (void)loadMap:(NSString*)mapName withData:(NSDictionary*)data colorAxis:(NSArr
{
[self loadMap:mapName withColors:[self getColorsForData:data colorAxis:colors]];
}

- (void)loadMap:(NSString*)mapName forSize:(CGSize)size withData:(NSDictionary*)data colorAxis:(NSArray*)colors
{
[self loadMap:mapName forSize:size withColors:[self getColorsForData:data colorAxis:colors]];
}

- (NSDictionary*)getColorsForData:(NSDictionary*)data colorAxis:(NSArray*)colors
{
Expand All @@ -104,8 +157,8 @@ - (NSDictionary*)getColorsForData:(NSDictionary*)data colorAxis:(NSArray*)colors

for (id key in data) {
NSNumber* value = [data objectForKey:key];
float s = ([value floatValue] - min) / (max - min);
float segmentLength = 1.0 / ([colors count] - 1);
float s = ([value floatValue] - min) / ( ((max-min) == 0 ? 1 : (max-min)) );
float segmentLength = 1.0 / ( (([colors count] - 1) == 0 ? 1 : ([colors count] - 1)) );
int minColorIndex = MAX(floorf(s / segmentLength),0);
int maxColorIndex = MIN(ceilf(s / segmentLength), [colors count] - 1);

Expand Down Expand Up @@ -163,6 +216,11 @@ - (void)setData:(NSDictionary*)data colorAxis:(NSArray*)colors
{
[self setColors:[self getColorsForData:data colorAxis:colors]];
}

- (void)clearAll
{
[self.layer.sublayers makeObjectsPerformSelector:@selector(removeFromSuperlayer)];
}

#pragma mark - Layers enumeration

Expand Down