Stacked bar graph on iOS


#1

I’m trying to figure out how to create a stacked bar graph. I was able to create normal version of the bar graph with 4 series, but once I set the stackIndex, the graph shows a lot of glitches and the values that are showing are not accurate. Some of the areas are even showing with gradient color, even if I set it not to show.

It also shows a lot of this kind of warning on the console:

ShinobiCharts: Unable to get category name: invalid value 2
From: <SChartCategoryAxis: 0x7fe5e1eb0ca0; range = { 0.000000, 1.000000 }; defaultRange = (null); dataRange = (null)>

My code consists on setting some test values on viewDidLoad:

NSDictionary *serie0 = @{@"Mon" : @10, @"Tue" : @20, @"Wed" : @30, @"Thu" : @20, @"Fri" : @10, @"Sat" : @20, @"Sun" : @10};

NSDictionary *serie1 = @{@"Mon" : @5, @"Tue" : @1, @"Wed" : @15, @"Thu" : @10, @"Fri" : @5, @"Sat" : @10, @"Sun" : @10};

NSDictionary *serie2 = @{@"Mon" : @1.5, @"Tue" : @1, @"Wed" : @20, @"Thu" : @10, @"Fri" : @15, @"Sat" : @3, @"Sun" : @10};

NSDictionary *serie3 = @{@"Mon" : @5, @"Tue" : @1, @"Wed" : @15, @"Thu" : @10, @"Fri" : @5, @"Sat" : @10, @"Sun" : @10};

self.dataToPlot = [[NSArray alloc] initWithObjects:serie0, serie1, serie2, serie3, nil];

And then the data source methods:

- (NSInteger)numberOfSeriesInSChart:(ShinobiChart *)chart {
    return self.dataToPlot.count;
}

- (NSInteger)sChart:(ShinobiChart *)chart numberOfDataPointsForSeriesAtIndex:(NSInteger)seriesIndex {
    
    NSDictionary *thisSerie = self.dataToPlot[seriesIndex];
    return thisSerie.count;
}

-(SChartSeries *)sChart:(ShinobiChart *)chart seriesAtIndex:(NSInteger)index {
    
    SChartBarSeries *barSeries = [SChartBarSeries new];
    barSeries.stackIndex = @1;
    barSeries.style.showArea = YES;
    barSeries.style.showAreaWithGradient = NO;
    return barSeries;
}

- (id<SChartData>)sChart:(ShinobiChart *)chart dataPointAtIndex:(NSInteger)dataIndex forSeriesAtIndex:(NSInteger)seriesIndex {
    SChartDataPoint *datapoint = [SChartDataPoint new];
    NSDictionary *thisSerie = self.dataToPlot[seriesIndex];
    NSString* key = thisSerie.allKeys[dataIndex];
    datapoint.yValue = key;
    datapoint.xValue = thisSerie[key];
    return datapoint;
}

What am I missing?


#2

Hi Tati,

After playing around with one of our samples I’ve managed to identify there is a bug on our category axis that appears to be causing this. I’ve raised this as an issue to our development team which should be looked at shortly.

As a temporary work-around you can convert the Mon, Tue … Sat, Sun values to 0-6 and then make your category axis an SChartNumberAxis. This will then allow you to make use of our sChart:alterTickMark:beforeAddingToAxis: to alter the tick labels to whatever you want them to be.

Apologies for any inconvenience this may cause you, and I hope this work-around helps you with what you’re trying to achieve.

If you have any further questions please let me know.

Kind regards,

Matt / iOS Developer