Использование справочников для настройки элементов пузырьковой диаграммы

Требования к операционной системе: iOS 5.0 и выше.

Мобильное устройство: iPad.

Описание

В данном примере для рядов данных, области диаграммы, области построения диаграммы и легенды применяются новые настройки с использованием справочников:

Исходный код

Для выполнения примера необходимо разместить взамен метода executeExample класса ViewController (см. раздел «Создание пузырьковой диаграммы») следующий код:

-(void)executeExample {
    // Применяем новые настройки для рядов пузырьковой диаграммы
    [self applySettingsForBubbleSeries];
    
    // Применяем новые настройки для области диаграммы
    [self applySettingsForChartArea];
    
    // Применяем новые настройки для области построения диаграммы
    [self applySettingsForChartPlotArea];
    
    // Применяем новые настройки для легенды диаграммы
    [self applySettingsForChartLegend];
    
}
-(void)applySettingsForBubbleSeries {
    for (BubbleSeries *serie in [chart seriesList]) {
        // Получаем справочник настроек кисти для заливки пузырьков диаграмме
        //NSMutableDictionary *dump = [serie.marker.background dumpConfigurationToDict];
        
        NSMutableDictionary *fullDump = [(RadialGradientBrush *)serie.marker.background dumpConfiguration];
        NSMutableDictionary *dump = [NSMutableDictionary dictionaryWithObject: fullDump
        forKey: @"RadialGradientBrush"];
        // Смещаем центр радиального градиента
        [self moveGradientCenterToPoint: (CGPoint) CGPointMake(0.25, 0.75)
        forRadialGradient: dump];
        // Восстанавливаем настройки градиента из дампа
        RadialGradientBrush *brush = [RadialGradientBrush radialGradientBrushWithDict: dump];
        // Устанавливаем кисть для заливки маркера
        [serie.marker setBackground: brush];
    }
}
-(void)applySettingsForChartArea {
    ChartArea *area = [chart chartArea];
    // Создаём справочник настроек для области диаграммы
    NSMutableDictionary *areaDump = [area dumpConfigurationToDict];
    // Устанавливаем новый цвет заливки области диаграммы
    [self applyBackgroundColor: [UIColor colorWithHex:@"fcf2ea"]
    andOpacity: 1
    forChartObject: areaDump];
    [area configureAreaWithDict: areaDump];
}
-(void)applySettingsForChartPlotArea {
    ChartPlotArea *plotArea = [chart plotArea];
    [plotArea setBorderColor: [UIColor yellowColor]];
    // Создаём справочник настроек для области построения диаграммы
    NSMutableDictionary *plotAreaDump = [plotArea dumpConfigurationToDict];
    // Устанавливаем новый цвет заливки области построения диаграммы
    [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"]];
    // Создаём справочник настроек для легенды диаграммы
    NSMutableDictionary *legendDump = [legend dumpConfigurationToDict];
    // Устанавливаем новые настройки шрифта
    [legendDump addEntriesFromDictionary: [self getFontSettings]];
    // Устанавливаем новый цвет заливки легенды диаграммы
    [self applyBackgroundColor: [UIColor colorWithHex:@"fcf2ea"]
    andOpacity: 0.7
    forChartObject: legendDump];
    // Устанавливаем цвет для подписей легенды
    [self applyTextColor: [UIColor colorWithHex:@"#003300"] forChartObject: legendDump];
    [legend configureLegendWithDict: legendDump andDefaults: nil];
}
// Смещает центр радиального градиента
-(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"];
}
// Устанавливаем цвет текста для элементов диаграммы
-(void)applyTextColor: (UIColor *) color
forChartObject: (NSMutableDictionary *)dump {
    [dump setValue:[NSMutableDictionary dictForColor: color withTitle: @"color"] forKey:@"color"];
}
// Устанавливает цвет заливки для элементов диаграммы
-(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"];
}
// Создаёт справочник настроек для кисти, используемой для заливки фона одним цветом
- (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;
}
// Создаёт справочник настроек указанного шрифта
-(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;
}
// Возвращает справочник настроек шрифта по умолчанию
-(NSMutableDictionary *) getFontSettings {
    UIFont *font = [UIFont fontWithName:@"Arial" size: 12];
    return [NSMutableDictionary dictionaryWithObject: [self dumpConfigurationToDictForFont: font] forKey:@"font"];
}

В результате выполнения примера радиальный градиент, отображаемый в качестве заливки пузырьков на пузырьковой диаграмме, смещается в левый нижний угол. Для области диаграммы и области её построения устанавливается новый цвет заливки. Для легенды диаграммы устанавливаются новый цвет заливки, шрифт и цвет подписей:

Результат выполнения примера не изменится, если заменить строку

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

на следующий сценарий:

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

Данные фрагменты сценария создают одинаковые справочники настроек кисти для заливки пузырьков на диаграмме.

См. также:

Примеры использования компонентов