Randika Vishman
Randika Vishman

Reputation: 8124

Grouped Bar chart with JBChartView library

I've needed to draw a grouped bar charts in an iOS Projects I'm doing. So the iOS Third party library I used is JBChartView from Jawbone. Up to the knowledge I gained with my research regarding this I found no any already supported ways within the library.

However I tried doing something like as follows:

- (NSUInteger)numberOfBarsInBarChartView:(JBBarChartView *)barChartView
{
    return [self.chartLegend count]; // number of bars in chart    "BB", "HB", "FB", "RO"
}

- (CGFloat)barChartView:(JBBarChartView *)barChartView heightForBarViewAtIndex:(NSUInteger)index
{
    CGFloat height = (isnan((([[chartData objectAtIndex:index] doubleValue]/total)*100))) ? 0.0 : (([[chartData objectAtIndex:index] doubleValue]/total)*100);
    NSLog(@"height : %f", height);
    return height;
}

- (UIView *)barChartView:(JBBarChartView *)barChartView barViewAtIndex:(NSUInteger)index {
    CGFloat height = (isnan((([[chartData objectAtIndex:index] doubleValue]/total)*100)))
                        ? 0.0
                        : (([[chartData objectAtIndex:index] doubleValue]/total)*100);

    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, height)];
    [view setBackgroundColor:[UIColor grayColor]];

    UIView *firstHalf = [[UIView alloc] initWithFrame:CGRectMake(0, height, 25, height)];
    [firstHalf setBackgroundColor:[UIColor redColor]];

    UIView *secondHalf = [[UIView alloc] initWithFrame:CGRectMake(25, height, 25, height/2)];
    if (index == 0) {
        [secondHalf setBackgroundColor:UIColorFromRGB(0xF15854)];
    } else if (index == 1) {
        [secondHalf setBackgroundColor:UIColorFromRGB(0x5DA5DA)];
    } else if (index == 2) {
        [secondHalf setBackgroundColor:UIColorFromRGB(0xFAA43A)];
    } else if (index == 3) {
        [secondHalf setBackgroundColor:UIColorFromRGB(0x60BD68)];
    }

    [view addSubview:firstHalf];
    [view addSubview:secondHalf];

    return view;
}

Note: Please, ignore the data values which I've put to determine the heights of the Red color and other varying color bars. And UIColorFromRGB is a custom methods I use in my development.

But it gives the graph as follows:

what I get from my implementation of - (UIView *)barChartView:(JBBarChartView *)barChartView barViewAtIndex:(NSUInteger)index; method

But what I really want to draw using JBChartView library is as follows:

expected graph

Any help is accepted with loads of gratitude and respect, but except don't suggest to use any other library to get this done. I like the simplicity of JBChartView library. :-)

Thanks in advance!

Upvotes: 0

Views: 520

Answers (1)

Yanchi
Yanchi

Reputation: 1030

From what I've found out, this is not possible using standard way, but can be worked around. See https://github.com/Jawbone/JBChartView/issues/139

Upvotes: 1

Related Questions