折线图 - 在底部显示xAxis

时间:2016-03-10 09:07:54

标签: ios-charts

我到处寻找并尝试了各种方法,无法弄清楚这一点。 如何将xAxis定位在折线图的底部?

在viewDidLoad中:

[self initChartProperties];
[self updateChartData:5];

initChartProperties:

- (void)initChartProperties2 {
    _chartView.delegate = self;
    _chartView.descriptionText = @"";
    _chartView.noDataTextDescription = @"You need to provide data for the chart.";
    _chartView.dragEnabled = YES;
    _chartView.pinchZoomEnabled = YES;
    _chartView.legend.enabled = NO;
    [_chartView setScaleEnabled:YES];

    _chartView.leftAxis.customAxisMax = 21;
    _chartView.leftAxis.customAxisMin = 0;
    _chartView.leftAxis.drawZeroLineEnabled = NO;
    _chartView.leftAxis.enabled = YES;

    _chartView.xAxis.axisLineWidth = 2.0f;
    _chartView.xAxis.drawAxisLineEnabled = YES;

    _chartView.rightAxis.enabled = NO;

    [_chartView.viewPortHandler setMaximumScaleY: 2.f];
    [_chartView.viewPortHandler setMaximumScaleX: 2.f];
}

updateChartData:

- (void)updateChartData:(int)count {
    NSMutableArray *xVals = [[NSMutableArray alloc] init];
    NSMutableArray *yVals = [[NSMutableArray alloc] init];

    for (int i = 0; i < count; i++) {
        [xVals addObject:[@(i) stringValue]];
    }

    for (int i = 0; i < count; i++) {
        [yVals addObject:[[ChartDataEntry alloc] initWithValue:i xIndex:i]];
    }

    LineChartDataSet *set1 = [[LineChartDataSet alloc] initWithYVals:yVals label:@""];

    set1.lineWidth = 1.0;
    set1.circleRadius = 0.0;
    set1.drawCircleHoleEnabled = NO;
    set1.valueFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:9.0f];

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

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

    _chartView.data = data;
}

结果: line graph

我只想让xAxis值呈现在图形的底部,而不是顶部。 抱歉看似简单的问题。 感谢任何帮助。

2 个答案:

答案 0 :(得分:4)

没关系 - 终于想通了

_chartView.xAxis.labelPosition = XAxisLabelPositionBottom;

答案 1 :(得分:0)

在最新版本(目前为3.0)中,您可以使用

执行此操作
_chartView.xAxis.labelPosition = XAxis.LabelPosition.bottom