Color of bar chart not displaying in JBChartView

533 Views Asked by At

I'm trying to implement a bar chart into my iOS app. I am using JBChartView. Everything is working fine, but there is no colour to the bars except when I press on them using the touchEvent.

Does anyone have any experience with this particular plugin to help me get it working correctly?

Thanks

  - (void)viewDidLoad
    {


       _barChartView = [[JBBarChartView alloc] init];
        _barChartView.delegate = self;
        _barChartView.dataSource = self;


    _tableView.backgroundColor =[UIColor clearColor];
        _barChartView.frame = CGRectMake(0,0,200,200);
        _barChartView.backgroundColor = [UIColor clearColor];
        [_barChartView reloadData];
        [_barChart addSubview: _barChartView];

    }


    - (UIColor *)barSelectionColorForBarChartView:(JBBarChartView *)barChartView
    {
        return [UIColor greenColor]; // color of selection view
    }
    - (BOOL)slideNavigationControllerShouldDisplayLeftMenu
    {
        return YES;
    }
    - (UIColor *)barChartView:(JBBarChartView *)barChartView colorForBarViewAtIndex:(NSUInteger)index
    {
        return [UIColor greenColor];
    }

    - (NSUInteger)numberOfBarsInBarChartView:(JBBarChartView *)barChartView
    {
        return 4;
    }
    - (CGFloat)barChartView:(JBBarChartView *)barChartView heightForBarViewAtAtIndex:(NSUInteger)index
    {
        return 100.0;
    }
2

There are 2 best solutions below

2
On BEST ANSWER

The problem appears to be with how JBBarChartView "normalizes" the values. Per the header file for JBBarChartView.h:

/**
 *  Height for a bar at a given index (left to right). There is no ceiling on the the height; 
 *  the chart will automatically normalize all values between the overal min and max heights.
 *
 *  @param barChartView     The bar chart object requesting this information.
 *  @param index            The 0-based index of a given bar (left to right, x-axis).
 *
 *  @return The y-axis height of the supplied bar index (x-axis)
 */

Because of this "normalization" when all of your values are the same (100.0f) it normalizes them all to 0, so there is no bar to display. Luckily, it is open-source, so you just need to open up the implementation file and make a little modification:

- (CGFloat)normalizedHeightForRawHeight:(NSNumber*)rawHeight
{
    CGFloat minHeight = [self minimumValue];
    CGFloat maxHeight = [self maximumValue];
    CGFloat value = [rawHeight floatValue];

    if ((maxHeight - minHeight) <= 0)
    {
        return self.availableHeight; // change this line to return the max height instead of 0
    }

    return ((value - minHeight) / (maxHeight - minHeight)) * [self availableHeight];
}
0
On

The correct solution to your problem is to supply a minimum value on the chart.

Please look @ the documentation for JBChartView:

/**
 *  The minimum and maxmimum values of the chart. 
 *  If no value(s) are supplied:
 *  
 *  minimumValue = chart's data source min value. 
 *  maxmimumValue = chart's data source max value.
 *
 *  If value(s) are supplied, they must be >= 0, otherwise an assertion will be thrown. 
 *  The min/max values are clamped to the ceiling and floor of the actual min/max values of the chart's data source;
 *  for example, if a maximumValue of 20 is supplied & the chart's actual max is 100, then 100 will be used.
 *
 *  For min/max modifications to take effect, reloadData must be called.
 */
@property (nonatomic, assign) CGFloat minimumValue;
@property (nonatomic, assign) CGFloat maximumValue;

If all of your data is equal to a single value (ie. 100), supplying a minimum value of 0 will ensure all bars are drawn at equal (visible) height (in relation to 0).

Hope this helps.