Description: a map is built by obtained data, contains two arrows and three tooltips.
#import <MapCharting/MapChart.h> #import <MapCharting/MapChart2D.h> #import "MapDataSource.h" @interface ViewController : UIViewController<MapChartDelegate>{ MapChart2D *m_view; // 2D map MapDataSource *datasource; // Data source } @end
#import "ViewController.h" #import <MapCharting/MapTopobase.h> #import <MapCharting/MapAreaVisual.h> #import "SolidColorBrush.h" #import "DataDependency.h" #import <MapCharting/MapTooltip.h> #import <MapCharting/MapLabel.h> #import <MapCharting/MapLegend.h> #import <MapCharting/MapShape.h> #import "UIColor+transition.h" #import <MapCharting/MapLabel.h> #import <MapCharting/MapChartSign.h> #import <MapCharting/MapArrow.h> #import <MapCharting/MapFilledArrow.h> #import <MapCharting/MapBarVisual.h> #import <MapCharting/MapPieVisual.h> #import "UIColor+hex.h" @implementation ViewController - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (void)viewDidLoad { [super viewDidLoad]; // Load data from source datasource = [[MapDataSource alloc]initWithFileName:@"data.txt"]; // Determine minimum and maximum data values [datasource prepare]; // Create a map based on loaded data [self dataSourceFinishedLoadData]; } // Creates a map based on loaded data - (void)dataSourceFinishedLoadData { // Create a 2D map m_view = [[MapChart2D alloc] init]; // Load file with topobase NSData *file = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"world.svg" ofType:nil]]; // Create an object for working with map topobase MapTopobase *topobase = [[MapTopobase alloc] initWithData:file]; if([MapTopobase isSVG:file]) { NSLog(@"Map topobase is in the SVG format"); } else { NSLog(@"Map topobase is not in the SVG format"); } // Create an object for working with map layer MapLayer *layer = [topobase rootLayer]; // Create a tooltip for layer layer.tooltip = [MapTooltip new]; // Set object for working with map topobase [m_view setTopobase:[layer topobase]]; // Set object containing methods for working with map [m_view setDelegate: self]; // Determine afffine transformation applied to all map areas [[m_view topobase] setTransform: CGAffineTransformIdentity]; [m_view setClipsToBounds:YES]; // Enable map zoom [m_view setZoomEnabled:YES]; // Enable automatic legend hiding [m_view setAutoHideLegend: YES]; // Create an object for working with map factor MapAreaVisual *visual = [[MapAreaVisual alloc] init]; /* Determine whether map area bakcground fill parameters depend on data source */ DataDependency *dataDependency = [DataDependency dependency]; [dataDependency setIsDependent:YES]; // Set data source [visual setDataSource: datasource]; [dataDependency setDataSource:datasource]; // Create a label for map layer areas MapLabel *label = [MapLabel new]; // Hide label label.visibility = NO; label.font = [UIFont fontWithName:@"Arial" size:16]; // Value is set automatically and contains map layer area name label.text = @""; label.maxWidth = 150.0; label.minWidth = 100.0; label.textColor = [UIColor blackColor]; label.textWrapping = TextWrappingNoWrap; // Set label for layer [layer setLabel: label]; // Set map layer area border [layer setStrokeThickness: 1.0]; [layer setStrokeColor: [UIColor darkGrayColor]]; // Set colors for map layer area bakcground fill NSArray *colors = [NSArray arrayWithObjects:@"ff462c", @"ebaf36", @"ffd900", @"b1ca40", @"6a8535", nil]; // Determine minimum map value double value = [datasource minValue]; // Determine map scale step double step = (datasource.maxValue - datasource.minValue) / [colors count]; // Determine correspondence between map scale intervals and layer area background fill colors ValueScale *scale = [[ValueScale new] autorelease]; for (int i = 1; i <[colors count]; i++) { value += step; [scale.value addObject:[NSNumber numberWithDouble:value]]; // Determine colors for the Less value UIColor *lessColor = [UIColor colorWithHex:[colors objectAtIndex:(i - 1)]]; [scale.less addObject:[SolidColorBrush solidColorBrushWithColor:lessColor]]; // Determine colors for the Equal or Greate value UIColor *equalColor = [UIColor colorWithHex:[colors objectAtIndex:(i)]]; [scale.equal addObject:[SolidColorBrush solidColorBrushWithColor:equalColor]]; [scale.greater addObject:[SolidColorBrush solidColorBrushWithColor:equalColor]]; } // Set map scale [dataDependency setScale:scale]; [visual setBackground:dataDependency]; // Set brush for map background fill UIColor *backgroundColor = [[[Brush new] autorelease] mainColor]; [m_view setBackground:[SolidColorBrush solidColorBrushWithColor:backgroundColor]]; // Set map title color m_view.caption.textColor = [UIColor blackColor]; // Set label text for the map layer areas, for which there is no data [m_view setNoDataText: NSLocalizedString(@"NO_DATA", nil)]; // Create a brush for filling map layer areas that have no data SolidColorBrush *brush = [[[SolidColorBrush alloc] init] autorelease]; // Set brush transparency [brush setOpacity: 0.2]; // Set brush color [brush setColor: [UIColor grayColor]]; // Set this brush [[[visual background] scale] setNoData: brush]; Thickness th = {0}; th.top = 30; // Determine title text [[m_view caption] setText: NSLocalizedString(@"WORLD_MAP", nil)]; // Set title margin [[m_view caption] setMargin: th]; // Determine font parameters [[m_view caption] setFont:[UIFont systemFontOfSize:18]]; // Use text direction from left to right [m_view setUsingRightToLeft: NO]; // Get map legend MapLegend *legend = [m_view legend]; // Set scale [legend setValueScale: scale]; // Align legend by map center [legend setBlockAlignment: NWLegendBlockAlignmentBottomCenter]; // Set legend margins Thickness margin = {0,20,0,0}; [legend setMargin:margin]; // Set legend text color and font parameters [legend setTextColor:[UIColor blackColor]]; [legend setFont:[UIFont systemFontOfSize:12]]; // Set the Less interval record format NSString *lessFormat = [NSMutableString stringWithString:NSLocalizedString(@"LESS_FORMAT", nil)]; [legend setLessFormat:lessFormat]; // Set the Greater interval record format NSString *greaterFormat = [NSMutableString stringWithString:NSLocalizedString(@"GREATER_FORMAT", nil)]; [legend setGreaterFormat:greaterFormat]; // Create legend top text MapLabel *header = [[MapLabel new] autorelease]; [header setText: NSLocalizedString(@"HEADER_TEXT", nil)]; // Set text font parameters [header setFont: [UIFont systemFontOfSize:14]]; [header setTextColor:[UIColor blackColor]]; // Set text margins Thickness headerMargin = {5, 0, 0, 0}; [header setMargin:headerMargin]; // Set legend top text [[m_view legend] setHeader:header]; // Set marker shape for map legend [legend setMarkerShape: MarkerShapeRectangle]; // Set legend orientation [legend setOrientation: NWLegendOrientationFreeVertical]; // Set legend position CGPoint pt = {[m_view legend].origin.x, 325}; [legend setOrigin: pt]; // Get timeline MapTimeAxis *timeAxis = [m_view timeAxis]; // Display timeline [timeAxis setHidden: NO]; // Set object containing methods for working with timeline [timeAxis setDelegate: m_view]; // Set data source [timeAxis setDataSource:datasource]; // Set up animation delay [timeAxis setDelayTime: AnimateTypeSlow]; // Set timeline font parameters [timeAxis setFont:[UIFont fontWithName:@"Arial" size:16]]; // Set minimum distance in points between two neighbor timeline tick marks [timeAxis setMinTickSpacing:1.0]; // Set blue color for timeline tick marks [timeAxis setTickColor: [UIColor blueColor]]; /* Set time of movement from one timeline value to another during animation playback */ [timeAxis setJumpTime: AnimateTypeNormal]; // Set current timeline slider position [timeAxis setIndex:0]; // Create and display label over the current timeline slider position MapTooltip *caption = [[MapTooltip new] autorelease]; [caption setVisibility:YES]; [timeAxis setLabel: caption]; // Create a pie factor MapPieVisual *pieVisual = [MapPieVisual new]; // Specify data source [pieVisual setDataSource:datasource]; pieVisual.maxPiesCount = 6; // Set pie factor radius pieVisual.radius.value = [NSNumber numberWithDouble:20.0]; // Set minimum radius pieVisual.leastRadiusPart = 10.0; pieVisual.isProportional = NO; // Set border thickness pieVisual.borderThickness = 1.0; pieVisual.borderColor.value = [UIColor blackColor]; // Create a bar factor MapBarVisual *barVisual = [MapBarVisual new]; // Specify data source [barVisual setDataSource:datasource]; // Set distance between bars barVisual.barDistance = 1.0; // Determine birder thickness barVisual.borderThickness = 1.0; // set minimum barcolumn height barVisual.leastHeightPart = 1.0; // Set minimum column width barVisual.leastWidthPart = 1.0; // Set column ratio in percents barVisual.isPercentage = NO; // Determine column border color barVisual.borderColor.value = [UIColor whiteColor]; // Determine maximum number of columns barVisual.maxBarsCount = 6; barVisual.height.isDependent = YES; barVisual.width.isDependent = YES; // Display map factors [layer.visuals addObject:pieVisual]; [layer.visuals addObject:barVisual]; [layer.visuals addObject: visual]; [m_view setShowCharts:YES]; barVisual.height.scale.value = visual.background.scale.value; barVisual.height.scale.less = visual.background.scale.value; barVisual.height.scale.equal = visual.background.scale.value; barVisual.height.scale.greater = visual.background.scale.value; barVisual.background.scale.value = visual.background.scale.value; barVisual.background.scale.less = visual.background.scale.less; barVisual.background.scale.equal = visual.background.scale.equal; barVisual.background.scale.greater = visual.background.scale.greater; pieVisual.background.scale.value = visual.background.scale.value; pieVisual.background.scale.less = visual.background.scale.less; pieVisual.background.scale.equal = visual.background.scale.equal ; pieVisual.background.scale.greater = visual.background.scale.greater; /* Add an object for working with map factor into array of visual elements corresponding to chidl layers and areas */ [[layer visuals] addObject: visual]; // Add map layer into array of displayed layers [[m_view layers] addObject:layer]; [self setView: m_view]; // Execute custom example [self executeExample]; } // Executes custom example located in the body of this method -(void) executeExample { }; - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear: animated]; [self mapChartDidAppear: m_view]; } // Called after map layer area touch - (void) mapChart:(MapChart2D *)mapChart touchDownInShape:(MapShape *)shape { [self mapChart:mapChart touchDownInShapeWithID:[shape ID]]; } // Called after map layer area touch - (void) mapChart:(MapChart2D *)mapChart touchDownInShapeWithID:(NSString *)shapeId { // Get map layer area by identifier MapShape *shape = [[[mapChart layers] objectAtIndex:0] shapeWithId:shapeId]; /* Zoom and scroll to display specified map layer area with animation playback */ [mapChart zoomToShape:shape animated:YES]; // Display tooltip for map layer area [m_view showPopupInShape:shape]; // Fully redraw map [mapChart invalidate]; } - (void)mapChartDidAppear:(MapChart *)mapChart { // Set new component size struct CGRect frame; frame = CGRectMake(0, 0, 375, 470); [mapChart setFrame:frame]; // Set gray color border [[mapChart layer] setBorderColor: [[UIColor colorWithRed:0.75 green:0.75 blue:0.75 alpha:1] CGColor]]; [[mapChart layer] setBorderWidth: 1.0f]; } // Called after timeline index change -(void)mapChart:(MapChart *)mapChart setDoubleTimeAxisIndex:(double)idx { // Determine map transparency depending on the current timeline index double coef = idx - floor(idx); double alpha = 1 - coef; if (coef > 0.5) { alpha = coef; } // Set new value of map transparency [mapChart setAlpha:alpha]; // Fully redraw map layers [mapChart invalidateOverlay]; } @end
See also: