如何在水平条形图(ios-charts)中为每个条形写限制?

时间:2016-09-12 08:58:24

标签: ios charts ios-charts

有没有办法为水平条形图中的每个条形创建限制,以便限制不是从上到下。我使用this library

Chart

例如,这里的限制线为10.我想仅显示7月份的这一行(因此它从6月开始到8月),并为其他月份添加其他行。有办法吗?

编辑: 我添加了我的代码,但我现在只生成一行。它被称为" limitLine"。 这是我的代码:

-(void)setupChart{
[self setupBarLineChartView:_chartView showDescription:YES];

double qLimit = 0.0;
for (Quota *q in self.report.arrayOfQuotas) {
    double temp = [q.quota doubleValue];
    if(temp > qLimit)
        qLimit = temp;
}
double maxValuesForTopChart = qLimit*2;

_chartView.delegate = self;
_chartView.drawBarShadowEnabled = NO;
_chartView.drawValueAboveBarEnabled = YES;
_chartView.maxVisibleValueCount = maxValuesForTopChart;

_chartViewTotal.delegate = self;
_chartViewTotal.drawBarShadowEnabled = NO;
_chartViewTotal.drawValueAboveBarEnabled = YES;
_chartViewTotal.maxVisibleValueCount = [self.report.quotaSum doubleValue]*2;

//top chart
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
leftAxis.axisMaxValue = maxValuesForTopChart;

ChartYAxis *rightAxis = _chartView.rightAxis;
rightAxis.enabled = YES;
rightAxis.labelTextColor = [HelperClass colorWithHexString:@"1e1e1e"];

rightAxis.drawAxisLineEnabled = YES;
rightAxis.drawGridLinesEnabled = YES;
rightAxis.axisMinValue = 0.0; // this replaces startAtZero = YES
rightAxis.yOffset = 1.0;
rightAxis.axisMaxValue = maxValuesForTopChart;
// set the limit lines
[rightAxis removeAllLimitLines];
ChartLimitLine *limit = [[ChartLimitLine alloc] initWithLimit:qLimit label:[NSString stringWithFormat:@"%d",(int)qLimit  ]];
[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 updateChartData];
}

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

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

- (void)setDataCount:(int)count range:(double)range
{
NSMutableArray *xVals = [[NSMutableArray alloc] init];
NSArray *arrayOfQuotas;
int rowCount = 0;

arrayOfQuotas = self.report.arrayOfQuotas;
rowCount = [arrayOfQuotas count];
double qLimit= 0.0;
for (Quota *q in self.report.arrayOfQuotas) {
    double temp = [q.quota doubleValue];
    if(temp > qLimit)
        qLimit = temp;
}
double maxValuesForTopChart = qLimit*2;

self.constraintChartViewHeight.constant = rowCount * chartRowSize;

NSMutableArray *yVals = [[NSMutableArray alloc] init];
int run = 0;
for (int i = [arrayOfQuotas count]-1; i > -1; i--)
{
    Quota *quota = arrayOfQuotas[i];
    double val = [quota.quotaReached doubleValue];
    [yVals addObject:[[BarChartDataEntry alloc] initWithValue:val xIndex:run]];
    [xVals addObject: quota.rowName];
    run++;
}

[yVals addObject:[[BarChartDataEntry alloc] initWithValue:
                  maxValuesForTopChart xIndex:[arrayOfQuotas count]+1]];

BarChartDataSet *set1 = nil;
if (_chartView.data.dataSetCount > 0)
{
    NSLog(@"ChartView first if");
    set1 = (BarChartDataSet *)_chartView.data.dataSets[0];
    set1.yVals = yVals;
    set1.valueFormatter = [[NSNumberFormatter alloc] init];
    set1.valueFormatter.minimumFractionDigits = 0;
    set1.valueFormatter.alwaysShowsDecimalSeparator = false;

    _chartView.data.xValsObjc = xVals;
    [_chartView notifyDataSetChanged];
}
else
{
    NSLog(@"ChartView second if");
    set1 = [[BarChartDataSet alloc] initWithYVals:yVals label:@""];
    set1.barSpace = 0.35;
    set1.valueFormatter = [[NSNumberFormatter alloc] init];
    set1.valueFormatter.minimumFractionDigits = 0;
    set1.valueFormatter.alwaysShowsDecimalSeparator = false;

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

    BarChartData *data = [[BarChartData alloc] initWithXVals:xVals dataSets:dataSets];

    [_chartView clear];
    [_chartView clearValues];
    _chartView.data = data;
    [_chartView notifyDataSetChanged];
}
}

- (void)setupBarLineChartView:(BarLineChartViewBase *)chartView
{
chartView.descriptionText = @"";
chartView.noDataTextDescription = @"No chart data available.";

chartView.drawGridBackgroundEnabled = NO;

chartView.dragEnabled = NO;
[chartView setScaleEnabled:YES];
chartView.pinchZoomEnabled = NO;

ChartXAxis *xAxis = chartView.xAxis;
xAxis.labelPosition = XAxisLabelPositionBottom;

chartView.rightAxis.enabled = NO;
}

0 个答案:

没有答案