Class Breaks Renderer

Download Sample Application

A class breaks renderer allows you to define a single symbol for each feature whose attribute value falls within a specific range. In this sample each State has been provided with a color depending on whether the value of its ‘Population in 2007’ fell into a particular range. For example a state with a ‘Population in 2007’ value of 9 to 37 million has been given the polygon fill color of dark blue.

//init class breaks renderer, set field and minValue
AGSClassBreaksRenderer *classBreaksRenderer = [[AGSClassBreaksRenderer alloc] init];
classBreaksRenderer.field = @"POP2007";
classBreaksRenderer.minValue = 523174;

//init class breaks array
NSMutableArray *classBreaksArray = [NSMutableArray array];

//define a class break and add it to class breaks array
AGSSimpleFillSymbol *symbol1 = [AGSSimpleFillSymbol simpleFillSymbol];
symbol1.color = [NSColor colorWithRed:239/255.0f green:243/255.0f blue:255/255.0f alpha:0.6];
[classBreaksArray addObject:[AGSClassBreak classBreakInfoWithLabel:@"523,174 - 1,085,885" description:@"523,174 - 1,085,885" maxValue:1085885 symbol:symbol1]];
//repeat for all class breaks

//set class break array to renderer
classBreaksRenderer.classBreaks = classBreaksArray;

//set the class breaks renderer to the feature layer's renderer
featureLayer.renderer = classBreaksRenderer;

Sample Code

//SWIFT SAMPLE CODE
/*
Copyright 2014 Esri

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import Cocoa
import ArcGIS

//layer urls
let kCBBaseMapURL = "http://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"
let kCBFeatureLayerURL = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5"

class ClassBreaksRendererSwiftSample: NSViewController, AGSLayerDelegate {

    @IBOutlet weak var mapView:AGSMapView!
    
    //MARK: - awakeFromNib
    
    // -------------------------------------------------------------------------------
    //  awakeFromNib
    // -------------------------------------------------------------------------------
    override func awakeFromNib() {
    
        //enable wrap around
        self.mapView.enableWrapAround()
        
        //add base layer to map and set delegate to know when layer loads or fails to load
        let baseMapLayer = AGSTiledMapServiceLayer(URL: NSURL(string: kCBBaseMapURL))
        baseMapLayer.delegate = self
        self.mapView.addMapLayer(baseMapLayer, withName:"Base Layer")
        
        //add feature layer to map and set delegate to know when layer loads or fails to load
        //set outFields to return all fields
        //set feature layer's renderer so features will be rendered with accordingly
        let featureLayer = AGSFeatureLayer(URL: NSURL(string: kCBFeatureLayerURL), mode: .OnDemand)
        featureLayer.delegate = self
        featureLayer.outFields = ["*"]
        featureLayer.renderer = self.classBreaksRenderer()
        self.mapView.addMapLayer(featureLayer, withName:"US States")
        
        //zoom to predefined extend with known spatial reference of the map
        let envelope = AGSEnvelope(xmin: -19839095.588617, ymin:2145730.010627, xmax:-7454984.983701, ymax:11542629.109590, spatialReference:AGSSpatialReference.webMercatorSpatialReference())
        self.mapView.zoomToEnvelope(envelope, animated:true)
    }
    
    //MARK: - Renderer
    
    // -------------------------------------------------------------------------------
    //  classBreaksRenderer
    // -------------------------------------------------------------------------------
    func classBreaksRenderer() -> AGSClassBreaksRenderer {
    
        //init class breaks recderer, set field and minValue
        let classBreaksRenderer = AGSClassBreaksRenderer()
        classBreaksRenderer.field = "POP2007"
        classBreaksRenderer.minValue = 523174
        
        //create default symbol and set to renderer
        let defaultSymbol = AGSSimpleFillSymbol()
        defaultSymbol.color = NSColor(red: 0, green:0, blue:0, alpha:0.6)
        classBreaksRenderer.defaultSymbol = defaultSymbol
        
        //init class breaks array
        var classBreaks = Array<AGSClassBreak>()
        
        //deine class breaks and add to classBreaks array
        let symbol1 = AGSSimpleFillSymbol()
        symbol1.color = NSColor(red: 239/255.0, green:243/255.0, blue:255/255.0, alpha:0.6)
        classBreaks.append(AGSClassBreak(label: "523,174 - 1,085,885", description:"523,174 - 1,085,885", maxValue:1085885, symbol:symbol1))
        
        let symbol2 = AGSSimpleFillSymbol()
        symbol2.color = NSColor(red: 186/255.0, green:215/255.0, blue:239/255.0, alpha:0.6)
        classBreaks.append(AGSClassBreak(label:"1,085,885 - 2,645,277", description:"1,085,885 - 2,645,277", maxValue:2645277, symbol:symbol2))
        
        let symbol3 = AGSSimpleFillSymbol()
        symbol3.color = NSColor(red: 133/255.0, green:188/255.0, blue:222/255.0, alpha:0.6)
        classBreaks.append(AGSClassBreak(label:"2,645,277 - 4,385,281", description:"2,645,277 - 4,385,281", maxValue:4385281, symbol:symbol3))
        
        let symbol4 = AGSSimpleFillSymbol()
        symbol4.color = NSColor(red: 87/255.0, green:155/255.0, blue:202/255.0, alpha:0.6)
        classBreaks.append(AGSClassBreak(label:"4,385,281 - 6,185,390", description:"4,385,281 - 6,185,390", maxValue:6185390, symbol:symbol4))
        
        let symbol5 = AGSSimpleFillSymbol()
        symbol5.color = NSColor(red: 48/255.0, green:118/255.0, blue:179/255.0, alpha:0.6)
        classBreaks.append(AGSClassBreak(label:"6,185,390 - 9,654,958", description:"6,185,390 - 9,654,958", maxValue:9654958, symbol:symbol5))
        
        let symbol6 = AGSSimpleFillSymbol()
        symbol6.color = NSColor(red: 8/255.0, green:81/255.0, blue:156/255.0, alpha:0.6)
        classBreaks.append(AGSClassBreak(label:"9,654,958 - 37,483,448", description:"9,654,958 - 37,483,448", maxValue:37483448, symbol:symbol6))
        
        //set class breaks to renderer
        classBreaksRenderer.classBreaks = classBreaks
        
        //return renderer
        return classBreaksRenderer
    }
    
    //MARK: - Layer Delegate
    
    // -------------------------------------------------------------------------------
    //  layerDidLoad:layer
    // -------------------------------------------------------------------------------
    func layerDidLoad(layer: AGSLayer!) {
        println("Layer \(layer.name) loaded..")
    }
    
    // -------------------------------------------------------------------------------
    //  layer:didFailToLoadWithError:error
    // -------------------------------------------------------------------------------
    func layer(layer: AGSLayer!, didFailToLoadWithError error: NSError!) {
        if let viewWindow = self.view.window {
            let alert = NSAlert()
            alert.messageText = "Layer '\(layer.name)' failed to load"
            alert.informativeText = error.localizedDescription
            alert.beginSheetModalForWindow(viewWindow, modalDelegate: self, didEndSelector: nil, contextInfo: nil)
        }
    }
    
}
//OBJECTIVE C SAMPLE CODE
/*
 Copyright 2013 Esri
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 
 http://www.apache.org/licenses/LICENSE-2.0
 
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

#import "ClassBreaksRendererSample.h"

//layer urls
#define kBaseMapURL @"http://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"
#define kFeatureLayerURL @"http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5"

@interface ClassBreaksRendererSample ()

@end

@implementation ClassBreaksRendererSample

#pragma mark - awakeFromNib

// -------------------------------------------------------------------------------
//  awakeFromNib
// -------------------------------------------------------------------------------
- (void)awakeFromNib {
    
    //enable wrap around
    [self.mapView enableWrapAround];
        
    //add base layer to map and set delegate to know when layer loads or fails to load
	AGSTiledMapServiceLayer *baseMapLayer = [[AGSTiledMapServiceLayer alloc] initWithURL:[NSURL URLWithString:kBaseMapURL]];
    baseMapLayer.delegate = self;
    [self.mapView addMapLayer:baseMapLayer withName:@"Base Layer"];
    
    //add feature layer to map and set delegate to know when layer loads or fails to load
    //set outFields to return all fields
    //set feature layer's renderer so features will be rendered with accordingly
    AGSFeatureLayer *featureLayer = [AGSFeatureLayer featureServiceLayerWithURL:[NSURL URLWithString:kFeatureLayerURL] mode:AGSFeatureLayerModeOnDemand];
    featureLayer.delegate = self;
    featureLayer.outFields = @[@"*"];
    featureLayer.renderer = [self classBreaksRenderer];
    [self.mapView addMapLayer:featureLayer withName:@"US States"];
    
    //zoom to predefined extend with known spatial reference of the map
    AGSEnvelope *envelope = [AGSEnvelope envelopeWithXmin:-19839095.588617 ymin:2145730.010627 xmax:-7454984.983701 ymax:11542629.109590 spatialReference:[AGSSpatialReference webMercatorSpatialReference]];
    [self.mapView zoomToEnvelope:envelope animated:YES];
}

#pragma mark - Renderer

// -------------------------------------------------------------------------------
//  classBreaksRenderer
// -------------------------------------------------------------------------------
- (AGSClassBreaksRenderer*)classBreaksRenderer {

    //init class breaks renderer, set field and minValue
    AGSClassBreaksRenderer *classBreaksRenderer = [[AGSClassBreaksRenderer alloc] init];
    classBreaksRenderer.field = @"POP2007";
    classBreaksRenderer.minValue = 523174;
    
    //create default symbol and set to renderer
    AGSSimpleFillSymbol *defaultSymbol = [AGSSimpleFillSymbol simpleFillSymbol];
    defaultSymbol.color = [NSColor colorWithRed:0 green:0 blue:0 alpha:0.6];
    classBreaksRenderer.defaultSymbol = defaultSymbol;
    
    //init class breaks array
    NSMutableArray *classBreaks = [NSMutableArray array];
    
    //deine class breaks and add to classBreaks array
    AGSSimpleFillSymbol *symbol1 = [AGSSimpleFillSymbol simpleFillSymbol];
    symbol1.color = [NSColor colorWithRed:239/255.0f green:243/255.0f blue:255/255.0f alpha:0.6];
    [classBreaks addObject:[AGSClassBreak classBreakInfoWithLabel:@"523,174 - 1,085,885" description:@"523,174 - 1,085,885" maxValue:1085885 symbol:symbol1]];
    
    AGSSimpleFillSymbol *symbol2 = [AGSSimpleFillSymbol simpleFillSymbol];
    symbol2.color = [NSColor colorWithRed:186/255.0f green:215/255.0f blue:239/255.0f alpha:0.6];
    [classBreaks addObject:[AGSClassBreak classBreakInfoWithLabel:@"1,085,885 - 2,645,277" description:@"1,085,885 - 2,645,277" maxValue:2645277 symbol:symbol2]];
    
    AGSSimpleFillSymbol *symbol3 = [AGSSimpleFillSymbol simpleFillSymbol];
    symbol3.color = [NSColor colorWithRed:133/255.0f green:188/255.0f blue:222/255.0f alpha:0.6];
    [classBreaks addObject:[AGSClassBreak classBreakInfoWithLabel:@"2,645,277 - 4,385,281" description:@"2,645,277 - 4,385,281" maxValue:4385281 symbol:symbol3]];
    
    AGSSimpleFillSymbol *symbol4 = [AGSSimpleFillSymbol simpleFillSymbol];
    symbol4.color = [NSColor colorWithRed:87/255.0f green:155/255.0f blue:202/255.0f alpha:0.6];
    [classBreaks addObject:[AGSClassBreak classBreakInfoWithLabel:@"4,385,281 - 6,185,390" description:@"4,385,281 - 6,185,390" maxValue:6185390 symbol:symbol4]];
    
    AGSSimpleFillSymbol *symbol5 = [AGSSimpleFillSymbol simpleFillSymbol];
    symbol5.color = [NSColor colorWithRed:48/255.0f green:118/255.0f blue:179/255.0f alpha:0.6];
    [classBreaks addObject:[AGSClassBreak classBreakInfoWithLabel:@"6,185,390 - 9,654,958" description:@"6,185,390 - 9,654,958" maxValue:9654958 symbol:symbol5]];

    AGSSimpleFillSymbol *symbol6 = [AGSSimpleFillSymbol simpleFillSymbol];
    symbol6.color = [NSColor colorWithRed:8/255.0f green:81/255.0f blue:156/255.0f alpha:0.6];
    [classBreaks addObject:[AGSClassBreak classBreakInfoWithLabel:@"9,654,958 - 37,483,448" description:@"9,654,958 - 37,483,448" maxValue:37483448 symbol:symbol6]];
    
    //set class breaks to renderer
    classBreaksRenderer.classBreaks = classBreaks;
    
    //return renderer
    return classBreaksRenderer;
}

#pragma mark - Layer Delegate

// -------------------------------------------------------------------------------
//  layerDidLoad:layer
// -------------------------------------------------------------------------------
- (void)layerDidLoad:(AGSLayer *)layer {
    NSLog(@"Layer %@ loaded..",layer.name);
}

// -------------------------------------------------------------------------------
//  layer:didFailToLoadWithError:error
// -------------------------------------------------------------------------------
- (void)layer:(AGSLayer *)layer didFailToLoadWithError:(NSError *)error {
    NSAlert *alert = [[NSAlert alloc] init];
    [alert setMessageText:[NSString stringWithFormat:@"Layer '%@' failed to load",layer.name]];
    [alert setInformativeText:[NSString stringWithFormat:@"%@",error]];
    [alert beginSheetModalForWindow:self.view.window modalDelegate:self didEndSelector:nil contextInfo:nil];
}

@end
//OBJECTIVE C SAMPLE CODE
/*
 Copyright 2013 Esri
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 
 http://www.apache.org/licenses/LICENSE-2.0
 
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

#import <Cocoa/Cocoa.h>

@interface ClassBreaksRendererSample : NSViewController <AGSLayerDelegate>

@property (strong) IBOutlet AGSMapView *mapView;

@end
Feedback on this topic?