ashmi123 ashmi123 - 1 year ago 144
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];
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 ( > 0)
set1 = (BarChartDataSet *)[0];
set1.values = yVals;
[ notifyDataChanged];
[_chartView notifyDataSetChanged];
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; = data;

Answer Source

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];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download