PowerPlotPercolation logo

German Federal Election 2009

This is a bar chart with individually colored bars. The data consists of the results of the Federal Election 2009 in Germany.

This page describes the class “ElectionBars” in the PowerPlot demo app.

German Federal Elections 2009

To begin, we use a factory method to produce a bar chart with individually colored bars:

    // Create data set.
    WSData *barData = [[DemoData electionResults2009] indexedData];

    // Create and configure a bar plot.
    WSChart *electionChart = [WSChart barPlotWithFrame:[self.view bounds]
                                                  data:barData
                                             barColors:[NSArray
                                                        arrayWithObjects:[UIColor CSSColorBlack],
                                                        [UIColor CSSColorYellow],
                                                        [UIColor CSSColorRed],
                                                        [UIColor CSSColorGreen],
                                                        [UIColor CSSColorTeal],
                                                        [UIColor CSSColorGray], nil]
                                                 style:kChartBarPlain
                                           colorScheme:kColorWhite];
    [electionChart scaleAllAxisYD:NARangeMake(-10, 45)];
    [electionChart setAllAxisLocationXD:-0.5];
    [electionChart setAllAxisLocationYD:0];    

The class DemoData provides the actual election results (taken from the German Wikipedia entry) and we apply the method indexedData which sets the x-values to indices starting at zero. This is a typical pattern for bar charts of labeled objects – like in this case the different political parties.

Now we apply a couple of further customizations. The first is a very minor one: Setting a black color to outline all bars. As we are dealing with bars with individual styles here, we have to set the customData style property of every bar in the chart. Fortunately, PowerPlot is fully KVC-compliant and we can thus simply employ Key-Value Coding to accomplish this task:

    // Customization of the resulting chart.
    WSPlotBar *bar = (WSPlotBar *)[[electionChart plotAtIndex:0] view];
    [bar setValue:[UIColor blackColor]
       forKeyPath:@"dataDelegate.dataD.values.customDatum.outlineColor"];

For the x-axis we simply use the annotation of the WSData objects describing each bar. However, for the y-axis we would like to have percentage-values in the form “20%” etc. Here we simply set the labels manually via strings. This could also be accomplished by a custom formatter, but the approach below is simpler.

    WSPlotAxis *axis = [electionChart firstPlotAxis];
    [[axis ticksX] setTicksStyle:kTicksLabels];
    [[axis ticksY] setTicksStyle:kTicksLabels];
    [[axis ticksY] ticksWithNumbers:[NSArray arrayWithObjects:
                                     [NSNumber numberWithFloat:0],
                                     [NSNumber numberWithFloat:10],
                                     [NSNumber numberWithFloat:20],
                                     [NSNumber numberWithFloat:30],
                                     nil]
                             labels:[NSArray arrayWithObjects:@"",
                                     @"10%", @"20%", @"30%", nil]];
    [electionChart setChartTitle:NSLocalizedString(@"Bundestagselection 2009", @"")];

Finally, the chart title is set to “Bundestagselection 2009” which completes the chart.