Search code examples
iosobjective-cios-charts

How to set horizontal bar length in ios-charts?


I've used the ios-charts project on github and used the HorizontalCharts example. It works good in the example but when I try to set the horizontal bar value, the bar just gets too long. Example visible here: enter image description here

My code is this:

-(void)setupChart{
myQuota = [[DataManager shared] myQuota];
[self setupBarLineChartView:_chartView];

_chartView.delegate = self;

_chartView.drawBarShadowEnabled = NO;
_chartView.drawValueAboveBarEnabled = YES;

_chartView.maxVisibleValueCount = 60;

ChartXAxis *xAxis = _chartView.xAxis;
xAxis.labelPosition = XAxisLabelPositionBottom;
xAxis.labelFont = [UIFont systemFontOfSize:10.f];
xAxis.drawAxisLineEnabled = YES;
xAxis.drawGridLinesEnabled = YES;
xAxis.gridLineWidth = .3;

ChartYAxis *leftAxis = _chartView.leftAxis;
leftAxis.labelFont = [UIFont systemFontOfSize:10.f];
leftAxis.drawAxisLineEnabled = YES;
leftAxis.drawGridLinesEnabled = YES;
leftAxis.drawLabelsEnabled = NO; 
leftAxis.gridLineWidth = .3;
leftAxis.axisMinValue = 0.0; 

ChartYAxis *rightAxis = _chartView.rightAxis;
rightAxis.enabled = YES;
rightAxis.labelFont = [UIFont systemFontOfSize:10.f];

rightAxis.drawAxisLineEnabled = YES;
rightAxis.drawGridLinesEnabled = NO;
rightAxis.axisMinValue = 0.0; 
rightAxis.yOffset = 1.0;
rightAxis.axisMaxValue = [myQuota.quota doubleValue]; // here is 10

_chartView.doubleTapToZoomEnabled = NO;
_chartView.highlightPerTapEnabled = NO;
_chartView.legend.enabled = NO;

[_chartView animateWithYAxisDuration:2.5];
[self updateChartData];
}

- (void)updateChartData
{
if (self.shouldHideData)
{
    _chartView.data = nil;
    return;
}

if(myQuota != nil && myQuota.quota != nil)
{
    [self setDataCount:(1) range: [myQuota.quota doubleValue]];
}

}

- (void)setDataCount:(int)count range:(double)range
{
NSMutableArray *xVals = [[NSMutableArray alloc] init];
[xVals addObject:myQuota.rowName];

NSMutableArray *yVals = [[NSMutableArray alloc] init];

double val = [myQuota.quotaReached doubleValue];  // here is 2
[yVals addObject:[[BarChartDataEntry alloc] initWithValue:val xIndex:0]];

BarChartDataSet *set1 = nil;
if (_chartView.data.dataSetCount > 0)
{
    set1 = (BarChartDataSet *)_chartView.data.dataSets[0];
    set1.yVals = yVals;
    _chartView.data.xValsObjc = xVals;
    [_chartView notifyDataSetChanged];
}
else
{
    set1 = [[BarChartDataSet alloc] initWithYVals:yVals label:@""];
    set1.barSpace = 0.35;
    set1.colors = [NSArray arrayWithObjects:[UIColor greenColor], nil]; 

    NSMutableArray *dataSets = [[NSMutableArray alloc] init];
    [dataSets addObject:set1];

    BarChartData *data = [[BarChartData alloc] initWithXVals:xVals     dataSets:dataSets];
    [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:10.f]];

    _chartView.data = data;
}
}

Why is the bar so long?


Solution

  • Okay, since nobody offered an answer I tried a HACK. I've added an invisible bar/row for which I set the value to the maximum of the bar chart. This way the axis shows the maximum even though the bar is invisible.

    I welcome anybody who knows a better way but until then I'll do it like this:

    - (void)setDataCount:(int)count range:(double)range{
    
    MY_MAX_VALUE = 10; // put HERE your MAX value for the axis
    
    NSMutableArray *xVals = [[NSMutableArray alloc] init];
    [xVals addObject:myQuota.rowName];
    
    NSMutableArray *yVals = [[NSMutableArray alloc] init];
    
    double val = [myQuota.quotaReached doubleValue];  // here is 2
    [yVals addObject:[[BarChartDataEntry alloc] initWithValue:val xIndex:0]];
    [yVals addObject:[[BarChartDataEntry alloc] initWithValue: MY_MAX_VALUE xIndex:1]];
    }