iosChart:x 轴标签根本不显示

iosChart: x-axis label not showing at all

目前我在我的项目中使用 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;

嗯,默认情况下,ios-charts 将计算 x 轴的模数来决定绘制 x 轴标签的间隔。那是你的情况。因为每个标签都比条长,所以为了避免标签重叠,我们不得不这样做。

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

如果 Label 彼此重叠,则只需添加以下代码,看看我认为它是否有效。

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