Search code examples
iosobjective-cios-charts

iOS Charts Library - clipping the last value in X Axis


I'm working on a line chart made by iOS Charts Library.

The values of X-axis are from 1980 to 2000, but the last value "2000" is missed from the X-axis, as the screenshot below:

enter image description here

Codes are as follow:

#import <Charts/Charts-Swift.h>

@interface ViewController () <ChartViewDelegate> {
    IBOutlet UITextField *tfInput;
    IBOutlet LineChartView *chartView;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    chartView.delegate = self;
    chartView.chartDescription.enabled = NO;
    chartView.drawGridBackgroundEnabled = YES;
    chartView.legend.enabled = NO;

    // X-axis
    chartView.xAxis.gridLineDashLengths = @[@10.0, @10.0];
    chartView.xAxis.gridLineDashPhase = 0.f;
    chartView.xAxis.labelPosition = XAxisLabelPositionBottom;

    // Y-axis
    chartView.rightAxis.enabled = NO;
    NSMutableArray *theDataSet = [NSMutableArray array];
    // ( logic of adding data into data set here )
    [self updateChartDataWithData:theDataSet];
}

- (void)updateChartDataWithdata:(NSMutableArray *)data {
    LineChartDataSet *dataSet = nil;
    if(chartView.data.dataSetCount > 0) {
        // Load existing data set
        dataSet = (LineChartDataSet *)chartView.data.dataSets[0];
        // ...and update it
        dataSet.values = data;
        [chartView.data notifyDataChanged];
        [chartView notifyDataSetChanged];
    } else {
        // Create new dataset
        dataSet = [[LineChartDataSet alloc] initWithValues:data label:@"Dataset"];
        dataSet.axisDependency = AxisDependencyLeft;
        [dataSet setColor:[UIColor colorWithRed:51/255.f green:181/255.f blue:229/255.f alpha:1.f]];
        [dataSet setCircleColor:UIColor.whiteColor];
        dataSet.lineWidth = 2.0;
        dataSet.circleRadius = 3.0;
        dataSet.fillAlpha = 66/255.0;
        dataSet.fillColor = [UIColor colorWithRed:51/255.f green:181/255.f blue:229/255.f alpha:1.f];
        dataSet.highlightColor = [UIColor colorWithRed:244/255.f green:117/255.f blue:117/255.f alpha:1.f];
        dataSet.drawCircleHoleEnabled = NO;
        dataSet.drawValuesEnabled = NO;

        dataSet.drawCirclesEnabled = NO;

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

        LineChartData *data = [[LineChartData alloc] initWithDataSets:dataSets];
        [data setValueTextColor:UIColor.whiteColor];
        [data setValueFont:[UIFont systemFontOfSize:9.f]];

        chartView.data = data;
    }
}

The actual data used is as follow:

X: 1980 Y: 0.000000115
X: 1981 Y: 0.000000028
X: 1982 Y: 0.000000038
X: 1983 Y: 0.000000016
X: 1984 Y: 0.000000032
X: 1985 Y: 0.000000038
X: 1986 Y: 0.000000045
X: 1987 Y: 0.000000069
X: 1988 Y: 0.000000045
X: 1989 Y: 0.000000074
X: 1990 Y: 0.000000054
X: 1991 Y: 0.000000058
X: 1992 Y: 0.000000049
X: 1993 Y: 0.000000066
X: 1994 Y: 0.000000065
X: 1995 Y: 0.000000059
X: 1996 Y: 0.000000050
X: 1997 Y: 0.000000048
X: 1998 Y: 0.000000062
X: 1999 Y: 0.000000081
X: 2000 Y: 0.000000079

What did I miss / misconfigure the chart view?

p.s. the chart view has plenty of spaces on the right, but the last value is just not displaying.


Solution

  • Your x-axis values are going up in 3's and 2000 is not a multiple of 3 given its starting point of 1980. The next value on the x axis would be 2001 but that is outside of your data set so is not shown. You can change this using the granularity property.

    chartView.xAxis.granularity = 4
    

    I dont have access at the moment to test but I'm pretty sure.