Using Dictionaries for Setting Up Bubble Chart Elements

Operating system requirements: iOS 5.0 or later.

Mobile device: iPad.

Description

This example displays application of new settings for data series, chart area, chart plot area and legend with the use of dictionaries:

Source Code

Executing the example requires to place the following code instead of the executeExample method of the ViewController class (see the Creating a Bubble Chart section):

-(void)executeExample {
    // Apply new settings to bubble chart series
    [self applySettingsForBubbleSeries];
    
    // Apply new settings to chart area
    [self applySettingsForChartArea];
    
    // Apply new settings to chart plot area
    [self applySettingsForChartPlotArea];
    
    // Apply new settings to chart legend
    [self applySettingsForChartLegend];
    
}
-(void)applySettingsForBubbleSeries {
    for (BubbleSeries *serie in [chart seriesList]) {
        // Get dictionary for chart bubble fill brush settings
        //NSMutableDictionary *dump = [serie.marker.background dumpConfigurationToDict];
        
        NSMutableDictionary *fullDump = [(RadialGradientBrush *)serie.marker.background dumpConfiguration];
        NSMutableDictionary *dump = [NSMutableDictionary dictionaryWithObject: fullDump
        forKey: @"RadialGradientBrush"];
        // Offset center of radial gradient
        [self moveGradientCenterToPoint: (CGPoint) CGPointMake(0.25, 0.75)
        forRadialGradient: dump];
        // Restore gradient settings from dump
        RadialGradientBrush *brush = [RadialGradientBrush radialGradientBrushWithDict: dump];
        // Set marker fill brush
        [serie.marker setBackground: brush];
    }
}
-(void)applySettingsForChartArea {
    ChartArea *area = [chart chartArea];
    // Create a dictionary for chart area settings
    NSMutableDictionary *areaDump = [area dumpConfigurationToDict];
    // Set a new fill color for chart area
    [self applyBackgroundColor: [UIColor colorWithHex:@"fcf2ea"]
    andOpacity: 1
    forChartObject: areaDump];
    [area configureAreaWithDict: areaDump];
}
-(void)applySettingsForChartPlotArea {
    ChartPlotArea *plotArea = [chart plotArea];
    [plotArea setBorderColor: [UIColor yellowColor]];
    // Create a dictionary for chart plot area settings
    NSMutableDictionary *plotAreaDump = [plotArea dumpConfigurationToDict];
    // Set new fill color for chart plot area
    [self applyBackgroundColor: [UIColor colorWithHex:@"fcf2ea"]
    andOpacity: 0.7
    forChartObject: plotAreaDump];
    [plotArea configurePlotAreaWithDict: plotAreaDump andDefaults: nil];
}
-(void) applySettingsForChartLegend {
    ChartLegend *legend = [chart legend];
    [legend setBorderColor: [UIColor colorWithHex:@"CCCCCC"]];
    // Create a dictionary for chart legend settings
    NSMutableDictionary *legendDump = [legend dumpConfigurationToDict];
    // Set new font settings
    [legendDump addEntriesFromDictionary: [self getFontSettings]];
    // Set new fill color for chart legend
    [self applyBackgroundColor: [UIColor colorWithHex:@"fcf2ea"]
    andOpacity: 0.7
    forChartObject: legendDump];
    // Set color for legend labels
    [self applyTextColor: [UIColor colorWithHex:@"#003300"] forChartObject: legendDump];
    [legend configureLegendWithDict: legendDump andDefaults: nil];
}
// Offsets center of radial gradient
-(void) moveGradientCenterToPoint: (CGPoint) point
forRadialGradient: (NSDictionary *) settings {
    NSDictionary *sourcePoint = [[settings valueForKey:@"RadialGradientBrush"] valueForKey: @"center"];
    [sourcePoint setValue: [NSNumber numberWithFloat: point.x] forKey:@"x"];
    [sourcePoint setValue: [NSNumber numberWithFloat: point.y] forKey:@"y"];
}
// Sets text color for chart elements
-(void)applyTextColor: (UIColor *) color
forChartObject: (NSMutableDictionary *)dump {
    [dump setValue:[NSMutableDictionary dictForColor: color withTitle: @"color"] forKey:@"color"];
}
// Sets fill color for chart elements
-(void) applyBackgroundColor: (UIColor *) color
andOpacity: (double) opacity
forChartObject: (NSMutableDictionary *) dump {
    SolidColorBrush *background = (SolidColorBrush *)[SolidColorBrush brushWithDict:[dump dictForProp:@"background"]];
    [background setColor: color];
    [background setOpacity: opacity];
    NSMutableDictionary *backgroundSettings = [[NSMutableDictionary new] autorelease];
    [backgroundSettings setValue: [NSNumber numberWithInt: 0]
    forKey:@"currentType"];
    [backgroundSettings setValue: [self dumpConfigurationToDictForSolidColorBrush: background]
    forKey:@"SolidColorBrush"];
    [dump setValue: backgroundSettings
    forKey:@"background"];
}
// Creates a dictionary for settings of solid color fill brush settings
- (NSMutableDictionary *)dumpConfigurationToDictForSolidColorBrush:(SolidColorBrush *)brush {
    NSMutableDictionary *dict = [[NSMutableDictionary new] autorelease];
    if (brush != nil) {
        [dict setValue: [NSMutableDictionary dictForColor:[brush color] withTitle: @"color"]
        forKey:@"color"];
        [dict setValue: [NSMutableDictionary dictForDouble: [brush opacity] withMin:0 max:1 title:@"opacity"]
        forKey:@"opacity"];
    }
    return dict;
}
// Creates a dictionary for specified font settings
-(NSMutableDictionary *) dumpConfigurationToDictForFont: (UIFont *) font {
    NSMutableDictionary *fontSettings = [[NSMutableDictionary new] autorelease];
    [fontSettings setValue: [font familyName] forKey:@"family"];
    [fontSettings setValue: [NSNumber numberWithFloat: font.lineHeight] forKey:@"size"];
    return fontSettings;
}
// Returns dictionary for default font settings
-(NSMutableDictionary *) getFontSettings {
    UIFont *font = [UIFont fontWithName:@"Arial" size: 12];
    return [NSMutableDictionary dictionaryWithObject: [self dumpConfigurationToDictForFont: font] forKey:@"font"];
}

After executing the example the radial gradient displayed as a bubble fill on the bubble chart is offset to the left bottom angle. A new fill color is set for chart area and chart plot area. A new fill color, font and label color are set for chart legend:

The example execution result does not change if the string

NSMutableDictionary *dump = [serie.marker.background dumpConfigurationToDict];

is replaced with the following script:

NSMutableDictionary *fullDump = [(RadialGradientBrush *)serie.marker.background dumpConfiguration];
NSMutableDictionary *dump = [NSMutableDictionary dictionaryWithObject: fullDump
forKey: @"RadialGradientBrush"];

These scenario fragments create equal dictionaries for chart bubble fill brush settings.

See also:

Examples of Component Use