ashmi123 ashmi123 - 9 days ago 6
iOS Question

How to set the X-values to Bar chart view X-axis using Daniel Chart?

I have use the Daniel Chart library.I want to show the xAxis value on the simple Bar Graph due to change in new library not able to set xValues,below is the previous code to set the xValues as string in but in the latest code not able to set same.

enter image description here
Old code: Check the last line i.e set the barchartdata with xValues.

- (void)setDataCount:(int)count range:(float)range
{

NSArray *tempXvals = [self.graphDict valueForKey:@"xVals"];
NSArray *xvals = [[tempXvals reverseObjectEnumerator]allObjects];

NSMutableArray *xVals;

if ([xvals count]>0) {
xVals = [[NSMutableArray alloc] initWithArray:xvals];
}
else
{
xVals = [[NSMutableArray alloc] init];
}

NSArray *tempYvals = [self.graphDict valueForKey:@"yVals"];
NSArray *yvals = [[tempYvals reverseObjectEnumerator]allObjects];
NSMutableArray *yVals = [[NSMutableArray alloc] init];

int xValsCount = [xVals count];
for (int i = 0; i < xValsCount; i++)
{
int val = [[yvals objectAtIndex:i]integerValue];
[yVals addObject:[[BarChartDataEntry alloc]initWithValue:(val) xIndex:i]];
}


NSString *graphTitle = [self.graphDict valueForKey:@"title"];
BarChartDataSet *set1;
if (graphTitle != nil) {
set1 = [[BarChartDataSet alloc] initWithYVals:yVals label:graphTitle];
}
graphTitle = nil;


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

NSNumberFormatter *formatter = [[NSNumberFormatter alloc]init];
formatter.minimumFractionDigits = 0;

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


New Code : In new library only datasets use.but how to pass xvalues.

- (void)setDataCount:(int)count range:(double)range
{
double start = 1.0;

NSMutableArray *yVals = [[NSMutableArray alloc] init];

for (int i = start; i < start + count + 1; i++)
{
double mult = (range + 1);
double val = (double) (arc4random_uniform(mult));
[yVals addObject:[[BarChartDataEntry alloc] initWithX:(double)i y:val]];
}

BarChartDataSet *set1 = nil;
if (_chartView.data.dataSetCount > 0)
{
set1 = (BarChartDataSet *)_chartView.data.dataSets[0];
set1.values = yVals;
[_chartView.data notifyDataChanged];
[_chartView notifyDataSetChanged];
}
else
{
set1 = [[BarChartDataSet alloc] initWithValues:yVals label:@"The year 2017"];
[set1 setColors:ChartColorTemplates.material];

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

BarChartData *data = [[BarChartData alloc] initWithDataSets:dataSets];
[data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:10.f]];

data.barWidth = 0.9f;

_chartView.data = data;
}
}

Answer

This issue resolve.set the delegate for that i.e IChartAxisValueFormatter,Also set the delegate to xaxis as below so that the method will be call.

xAxis.valueFormatter = self;

I use the below code to change the value formatter.

- (NSString *)stringForValue:(double)value
                        axis:(ChartAxisBase *)axis
{
    return months[(int)value % months.count];
}