在X轴上显示重复的标签

时间:2017-08-25 08:43:52

标签: ios objective-c charts linechart ios-charts

我正在使用Charts framework (danielgindi/Charts)。我的折线图显示了x轴上的重复标签,如果我的标签数组中只有一个值可用(在这种情况下为xAxisLabels)。见下图。

enter image description here

如何确保标签不会被复制?因此,如果标签阵列上只有一个条目可用,那么它应该在最左侧只显示一个标签。其他标签将为空白,将根据标签阵列的可用性逐渐填充。

这是我到目前为止尝试过的代码。非常感谢你的帮助。

@property (nonatomic) LineChartView* chartView;
@property (strong, nonatomic) NSArray<NSString *> *xAxisLabels;

- (void)configureChartViewIfNeeded {
    self.chartView.drawGridBackgroundEnabled = NO;
    self.chartView.legend.enabled = NO;
    self.chartView.minOffset = 28;

    self.chartView.xAxis.enabled = YES;
    self.chartView.xAxis.labelPosition = XAxisLabelPositionBottom;
    self.chartView.xAxis.labelTextColor = [UIColor colorWithWhite:1 alpha:0.79];
    self.chartView.xAxis.gridColor = [UIColor colorWithWhite:1 alpha:0.79];
    self.chartView.xAxis.drawAxisLineEnabled = NO;
    self.chartView.xAxis.drawGridLinesEnabled = NO;

    self.chartView.leftAxis.labelTextColor = [UIColor colorWithWhite:1 alpha:0.79];
    self.chartView.leftAxis.gridColor = [UIColor colorWithWhite:1 alpha:0.3];
    self.chartView.leftAxis.gridLineWidth = 1.0f;
    self.chartView.leftAxis.drawZeroLineEnabled = NO;
    self.chartView.leftAxis.drawAxisLineEnabled = NO;
    [self.chartView.leftAxis setLabelCount:leftAxisLabelCount force:NO];
    self.chartView.leftAxis.drawTopYLabelEntryEnabled = YES;
    self.chartView.leftAxis.axisMinimum = 0;
    JVNImpactMetricsFormatter *numberFormatter = [[JVNImpactMetricsFormatter alloc] init];  //My custom formatter
    [numberFormatter setUsesGroupingSeparator:YES];
    [numberFormatter setGroupingSeparator:@","];
    [numberFormatter setGroupingSize:3];
    numberFormatter.maximumFractionDigits = 1;
     self.chartView.xAxis.valueFormatter = numberFormatter; //Will present integer values

    self.chartView.rightAxis.enabled = NO;

    self.chartView.dragEnabled = NO;
    self.chartView.scaleXEnabled = self.chartView.scaleYEnabled = NO;
    self.chartView.drawBordersEnabled = NO;
    self.chartView.doubleTapToZoomEnabled = NO;
    self.chartView.delegate = self;

    self.chartView.descriptionText = @"";
    [self.chartView animateWithXAxisDuration:0.3 yAxisDuration:0.3];
}


#pragma mark - IChartAxisValueFormatter

- (NSString * _Nonnull)stringForValue:(double)value axis:(ChartAxisBase * _Nullable)axis {
    return self.xAxisLabels[[@(value) intValue]];
}

1 个答案:

答案 0 :(得分:0)

尝试使用:

self.chartView.xAxis.granularityEnabled = true