3

Previously we can set the x-Axis of bar chart data as below:-

BarChartData *data = [[BarChartData alloc] initWithXVals:xvals dataSets:dataSets];

In latest updated of ios-charts library, the syntax changed to below:- BarChartData *data = [[BarChartData alloc] initWithDataSets:dataSets];

How to set the x axis on BarChartData?

Hasya
  • 9,792
  • 4
  • 31
  • 46
pkc456
  • 8,350
  • 38
  • 53
  • 109
  • What you need to show in XAxis can you tell me ? or provide screenshot what you are trying to achieve so I can get proper idea? – CodeChanger Jan 27 '17 at 12:23
  • I come to know that x values will be shown by `IAxisValueFormatter` and its delegate method `stringForValue`. When I tried to use IAxisValueFormatter in my viewController, then I get the error:- `cannot find protocol declaration`. [This answer](http://stackoverflow.com/a/39149070/988169) shared the approach while working with swift. Can you tell me the same in objective c. – pkc456 Jan 27 '17 at 12:27

2 Answers2

3

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;
}
Community
  • 1
  • 1
pkc456
  • 8,350
  • 38
  • 53
  • 109
  • But how can that possible as you are setting value in BarChartDataEntry and while in delegate method you are using as object something is wrong with this i suspect. – CodeChanger Jan 30 '17 at 09:35
1

You can use below same method of IAxisValueFormatter with value as index and fetch data from xArray and show your custom X values in it.

#pragma mark - IAxisValueFormatter

- (NSString *)stringForValue:(double)value
                        axis:(ChartAxisBase *)axis
{
    NSString *xValue = [xArray objectAtIndex:value];
    return xValue;
}

You can use this as delegate methods of IAxisValueFormatter.

Hope this will helps you to set xAxis in your chart.

CodeChanger
  • 7,953
  • 5
  • 49
  • 80