如果使用ios-charts只有一个栏,如何在条形图上写入值?

时间:2016-09-02 10:39:22

标签: ios ios-charts

我使用this库制作图表。如果有几个酒吧但是当我只有一个时,一切正常,似乎无法显示该值。如果只有一个条形图(图像)One bar

,则为图表

但是当我放两个条形图时,这些值是可见的。 Two bars

我的代码是:

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

_chartView.delegate = self;

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

_chartView.maxVisibleValueCount = [myQuota.quota doubleValue] * 2;

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; // don't write label values
leftAxis.gridLineWidth = .3;
leftAxis.axisMinValue = 0.0; // this replaces startAtZero = YES

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

rightAxis.drawAxisLineEnabled = YES;
rightAxis.drawGridLinesEnabled = NO;
rightAxis.axisMinValue = 0.0; // this replaces startAtZero = YES
rightAxis.yOffset = 1.0;
rightAxis.axisMaxValue = [myQuota.quota doubleValue] * 2;
[rightAxis removeAllLimitLines];
ChartLimitLine *limit = [[ChartLimitLine alloc] initWithLimit:[myQuota.quota doubleValue] label:[NSString stringWithFormat:@"%d",[myQuota.quota integerValue] ]];
[rightAxis addLimitLine:limit];
rightAxis.valueFormatter = [[NSNumberFormatter alloc] init];
rightAxis.valueFormatter.minimumFractionDigits = 0;

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

[_chartView animateWithYAxisDuration:1.5];
[self setDataCount:(1) range: ([myQuota.quota doubleValue]*2)];
}

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

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

double val = [myQuota.quotaReached doubleValue];
[yVals addObject:[[BarChartDataEntry alloc] initWithValue:([myQuota.quota doubleValue]*2) 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]; // set color

    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.xAxis.labelPosition = Top
    _chartView.data = data;
}
}

0 个答案:

没有答案