With the help of this Answer (swift version), I am able to show xAxis string values on bar chart in Objective-C
.
NSMutableArray *xValsDietArray;
Set up your chart data. Basic example is as follows:-
for (int i=0; i<dietArray.count; i++) {
DietInput *obj = [dietArray objectAtIndex:i];
NSString *dateStr = @"Jan";
[xValsDietArray addObject:dateStr];//Maintain this xValsDietArray. It will be used later on while setting x values. This contains array of x-axis values
[yvals addObject:[[BarChartDataEntry alloc]initWithX:[[xValsDietArray objectAtIndex:i] doubleValue] y:obj.calorieInput.doubleValue data:xValsDietArray] ];
}
BarChartDataSet *set1 = [[BarChartDataSet alloc] initWithValues:yvals label:@"Water Consumed"];
NSMutableArray *dataSets = [[NSMutableArray alloc] init];
[dataSets addObject:set1];
BarChartData *data = [[BarChartData alloc] initWithDataSets:dataSets];
cell.chartView.data=data;
cell.chartView.xAxis.valueFormatter = self;// Set the delegate to self. THIS IS THE MAIN ADDITION IN NEW CHART LIBRARY
Implement the stringForValue
delegate method as follows:-
- (NSString * _Nonnull)stringForValue:(double)value axis:(ChartAxisBase * _Nullable)axis
{
NSString *xAxisStringValue = @"";
int myInt = (int)value;
if(xValsDietArray.count > myInt)
xAxisStringValue = [xValsDietArray objectAtIndex:myInt];
return xAxisStringValue;
}