iosChart:x轴标签根本没有显示

时间:2016-03-30 11:27:15

标签: objective-c ios-charts

目前我在我的项目中使用iosChart。我使用了下图所示的条形图:

问题是没有显示(第2和第4个)整个x轴标签。当我缩放然后显示。有没有办法证明这个标签?

这是我的代码:

chartView.delegate = self;

chartView.descriptionText = @"";
chartView.noDataTextDescription = GGLocalizeString(@"deshboard.ui.chart.empty");

chartView.drawBarShadowEnabled = NO;
chartView.drawValueAboveBarEnabled = YES;

chartView.maxVisibleValueCount = 60;
chartView.pinchZoomEnabled = NO;
chartView.drawGridBackgroundEnabled = NO;

ChartXAxis *xAxis = chartView.xAxis;
xAxis.labelPosition = XAxisLabelPositionBottom;
xAxis.labelFont = [UIFont systemFontOfSize:10.f];
xAxis.drawGridLinesEnabled = NO;
xAxis.spaceBetweenLabels = 2.0;

ChartYAxis *leftAxis = chartView.leftAxis;
leftAxis.labelFont = [UIFont systemFontOfSize:10.f];
leftAxis.labelCount = 8;
leftAxis.valueFormatter = [[NSNumberFormatter alloc] init];
leftAxis.valueFormatter.maximumFractionDigits = 1;
leftAxis.valueFormatter.negativeSuffix = @"";
leftAxis.valueFormatter.positiveSuffix = @"";
leftAxis.labelPosition = YAxisLabelPositionOutsideChart;
leftAxis.spaceTop = 0.15;

ChartYAxis *rightAxis = chartView.rightAxis;
rightAxis.drawGridLinesEnabled = NO;
rightAxis.labelFont = [UIFont systemFontOfSize:10.f];
rightAxis.labelCount = 8;
rightAxis.valueFormatter = leftAxis.valueFormatter;
rightAxis.spaceTop = 0.15;

chartView.legend.position = ChartLegendPositionBelowChartLeft;
chartView.legend.form = ChartLegendFormSquare;
chartView.legend.formSize = 9.0;
chartView.legend.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:11.f];
chartView.legend.xEntrySpace = 4.0;


if (self.chartData.arrayCityData.count == 0) {
    return cell;
}

//X - VALUE
NSMutableArray *xVals = [[NSMutableArray alloc] init];

for (int i = 0; i < [self.chartData.arrayCityData count]; i++)
{
    DealData *dealData = [self.chartData.arrayCityData objectAtIndex:i];

    [xVals addObject: [NSString stringWithFormat:@"%@(%.2f%%)",dealData.name,[dealData.totalClick floatValue]*100/[self totalCityClicks]]];
}

//Y - VALUE
NSMutableArray *yVals = [[NSMutableArray alloc] init];

for (int i = 0; i < [self.chartData.arrayCityData count]; i++)
{
    DealData *dealData = [self.chartData.arrayCityData objectAtIndex:i];

    [yVals addObject:[[BarChartDataEntry alloc] initWithValue:[dealData.totalClick doubleValue] xIndex:i]];
}

BarChartDataSet *set1 = [[BarChartDataSet alloc] initWithYVals:yVals label:GGLocalizeString(@"deshboard.ui.cell.lblTitle6.cities")];
set1.barSpace = 0.35;

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]];

//remove decimal
NSNumberFormatter *vf = [[NSNumberFormatter alloc] init];
vf.generatesDecimalNumbers = NO;
data.valueFormatter = vf;

chartView.data = data;
    chartView.legend.position = ChartLegendPositionBelowChartLeft;
    chartView.legend.form = ChartLegendFormSquare;
    chartView.legend.formSize = 9.0;
    chartView.legend.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:11.f];
    chartView.legend.xEntrySpace = 4.0;


    if (self.chartData.arrayCityData.count == 0) {
        return cell;
    }

    //X - VALUE
    NSMutableArray *xVals = [[NSMutableArray alloc] init];

    for (int i = 0; i < [self.chartData.arrayCityData count]; i++)
    {
        DealData *dealData = [self.chartData.arrayCityData objectAtIndex:i];

        [xVals addObject: [NSString stringWithFormat:@"%@(%.2f%%)",dealData.name,[dealData.totalClick floatValue]*100/[self totalCityClicks]]];
    }

    //Y - VALUE
    NSMutableArray *yVals = [[NSMutableArray alloc] init];

    for (int i = 0; i < [self.chartData.arrayCityData count]; i++)
    {
        DealData *dealData = [self.chartData.arrayCityData objectAtIndex:i];

        [yVals addObject:[[BarChartDataEntry alloc] initWithValue:[dealData.totalClick doubleValue] xIndex:i]];
    }

    BarChartDataSet *set1 = [[BarChartDataSet alloc] initWithYVals:yVals label:GGLocalizeString(@"deshboard.ui.cell.lblTitle6.cities")];
    set1.barSpace = 0.35;

    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]];

    //remove decimal
    NSNumberFormatter *vf = [[NSNumberFormatter alloc] init];
    vf.generatesDecimalNumbers = NO;
    data.valueFormatter = vf;

    chartView.data = data;

2 个答案:

答案 0 :(得分:2)

好吧,默认情况下,ios-charts会计算x轴的模数,以决定绘制x轴标签的间隔。这是你的情况。因为每个标签都比条形长,所以为了避免标签重叠,我们必须这样做。

如果您不想这样,请致电xAxis.setLabelsToSkip(0)

答案 1 :(得分:1)

如果Label彼此重叠,那么只需添加下面的代码,看看我认为它是有效的。

[xAxis setLabelsToSkip:0];
[xAxis setWordWrapEnabled:YES];