Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HorizontalBarChart X axe labels duplicated #3300

Closed
siyu6974 opened this issue Feb 26, 2018 · 2 comments
Closed

HorizontalBarChart X axe labels duplicated #3300

siyu6974 opened this issue Feb 26, 2018 · 2 comments

Comments

@siyu6974
Copy link

siyu6974 commented Feb 26, 2018

After #3114, my labels are duplicated.
screen shot 2018-02-26 at 14 03 34

Here is my configuration

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _chartView.fitBars = YES;
    NSArray * values = @[@1,@2,@3,@4,@5,@6,@7,@8];
    labels = [NSMutableArray new];
    [values enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *s = [NSString stringWithFormat:@"entries %@", obj];
        [labels addObject:s];
    }];
    [self setChart:self.chartView withLabels:labels AndValues:values];
}

-(void)setChart:(BarChartView *)barChartView withLabels:(NSArray *)labels AndValues:(NSArray *)values{
    
    NSUInteger count = [labels count];
    double barWidth = 7.0;
    double spaceForBar = 10.0;

    ChartXAxis *xAxis = barChartView.xAxis;
    xAxis.labelCount = count;
    xAxis.valueFormatter = self;
    
    NSMutableArray *dataEntries = [[NSMutableArray alloc] init];
    for (int i = 0; i < count; i++)
    {
        NSNumber *value = [values objectAtIndex:i];
        BarChartDataEntry *dataEntry = [[BarChartDataEntry alloc] initWithX:i*spaceForBar y:[value doubleValue]];
        [dataEntries addObject:dataEntry];
    }
    
    BarChartDataSet *chartDataSet = [[BarChartDataSet alloc] initWithValues:dataEntries];
    
    BarChartData *chartData = [[BarChartData alloc] initWithDataSets:@[chartDataSet]];
    chartData.barWidth = barWidth;
    [chartData setValueFont:[UIFont systemFontOfSize:9.f]];
    
    barChartView.data = chartData;
    [barChartView setScaleMinima:1.f scaleY:count/4.0];
}

- (NSString *)stringForValue:(double)value axis:(ChartAxisBase *)axis
{
    return labels[(int)value /10];
}

Some remarks:

@thierryH91200
Copy link
Contributor

add

xAxis.granularity = 1

@siyu6974
Copy link
Author

@thierryH91200 It does not work. In fact I believe 1 is the default value for xAxis.granularity.
However change it to 10 (spaceForBar) fix the problem!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants