From 62fef997d44df9da5744d444156b6c7a13769179 Mon Sep 17 00:00:00 2001 From: Cao Tri DO Date: Tue, 28 Jun 2016 16:14:13 +0200 Subject: [PATCH 1/6] Add interface builder support --- FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.m | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.m b/FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.m index fc2db0e..f4512ad 100644 --- a/FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.m +++ b/FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.m @@ -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]; From 07d6c7f5052ce39fba6aad44afd2b29a5b9bd084 Mon Sep 17 00:00:00 2001 From: Cao Tri DO Date: Thu, 7 Jul 2016 16:17:50 +0200 Subject: [PATCH 2/6] Fix divide by zero cases --- FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.m b/FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.m index f4512ad..6dd9636 100644 --- a/FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.m +++ b/FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.m @@ -111,8 +111,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); From b65228e99bdecf9ec54f65f5142b51922048bfb0 Mon Sep 17 00:00:00 2001 From: Cao Tri DO Date: Wed, 31 Aug 2016 01:25:11 +0200 Subject: [PATCH 3/6] Center svg horizontally --- FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.m b/FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.m index 6dd9636..7411338 100644 --- a/FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.m +++ b/FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.m @@ -57,7 +57,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, self.frame.size.width/2, -_svg.bounds.origin.y); UIBezierPath* scaled = [path.path copy]; [scaled applyTransform:scaleTransform]; From b2bce65a3645e1657fb95315733084410551d430 Mon Sep 17 00:00:00 2001 From: Cao Tri DO Date: Thu, 1 Sep 2016 00:40:01 +0200 Subject: [PATCH 4/6] Load the svg in specified size --- .../FSInteractiveMap/FSInteractiveMapView.h | 1 + .../FSInteractiveMap/FSInteractiveMapView.m | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.h b/FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.h index 9fa3ff9..0ed1a65 100644 --- a/FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.h +++ b/FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.h @@ -18,6 +18,7 @@ @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 withColors:(NSDictionary*)colorsDict; - (void)loadMap:(NSString*)mapName withData:(NSDictionary*)data colorAxis:(NSArray*)colors; diff --git a/FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.m b/FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.m index 7411338..8c628ee 100644 --- a/FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.m +++ b/FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.m @@ -45,6 +45,47 @@ - (void)setDefaultParameters #pragma mark - SVG map loading +- (void)loadMap:(NSString*)mapName forSize:(CGSize)size withColors:(NSDictionary*)colorsDict { + _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]; From 91ce787dc2e7d5da59f4ad098c56e7ada3a7e9af Mon Sep 17 00:00:00 2001 From: Cao Tri DO Date: Tue, 6 Sep 2016 15:16:32 +0200 Subject: [PATCH 5/6] Minor fix --- FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.m b/FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.m index 8c628ee..6e2dd2d 100644 --- a/FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.m +++ b/FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.m @@ -98,7 +98,7 @@ - (void)loadMap:(NSString*)mapName withColors:(NSDictionary*)colorsDict CGAffineTransform scaleTransform = CGAffineTransformIdentity; scaleTransform = CGAffineTransformMakeScale(scale, scale); - scaleTransform = CGAffineTransformTranslate(scaleTransform, self.frame.size.width/2, -_svg.bounds.origin.y); + scaleTransform = CGAffineTransformTranslate(scaleTransform, -_svg.bounds.origin.y, -_svg.bounds.origin.y); UIBezierPath* scaled = [path.path copy]; [scaled applyTransform:scaleTransform]; From 8ced52f58f8f2fad7fc0aff9274a10d7f5d1b1b9 Mon Sep 17 00:00:00 2001 From: Cao Tri DO Date: Wed, 1 Mar 2017 17:04:57 +0100 Subject: [PATCH 6/6] Various improvements --- .../FSInteractiveMap/FSInteractiveMapView.h | 4 ++++ .../FSInteractiveMap/FSInteractiveMapView.m | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.h b/FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.h index 0ed1a65..c6e4218 100644 --- a/FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.h +++ b/FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.h @@ -19,6 +19,7 @@ // 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; @@ -29,4 +30,7 @@ // Layers enumeration - (void)enumerateLayersUsingBlock:(void(^)(NSString* identifier, CAShapeLayer* layer))block; +// Clear all layers +- (void)clearAll; + @end diff --git a/FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.m b/FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.m index 6e2dd2d..281e217 100644 --- a/FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.m +++ b/FSInteractiveMap/FSInteractiveMap/FSInteractiveMapView.m @@ -132,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 { @@ -211,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