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

Horizontal Bar Chart Value Labels Cut Off #3836

Closed
TommyPrax opened this issue Feb 11, 2019 · 3 comments
Closed

Horizontal Bar Chart Value Labels Cut Off #3836

TommyPrax opened this issue Feb 11, 2019 · 3 comments

Comments

@TommyPrax
Copy link

Hi

I set up a horizontal bar chart and placed custom values in each chart.
Unfortunately most of the small values are not properly rendered into the bar.
Is there a way to set the minimum length of a bar to avid this problem(or something else)

I tried out lots of variations to set the offset and minValue, but with no success.
If please someone could point me in the right direction. It would be appreciated a lot.
Thank you

bildschirmfoto 2019-02-11 um 22 11 08

(I am using the Charts-Version [v3.2.0]

@LiubovFedorchuk
Copy link

Hi, @TommyPrax, perhaps, you can add companion code, this can speed up and simplify the problem solving process. Or maybe you tried to reproduce your problem on ChartsDemo?

@TommyPrax
Copy link
Author

TommyPrax commented Feb 13, 2019

Dear LiubivFedorchuk,
thanks for your answer <3.
Please find below the code to reproduce this behaviour.
The dictionary consists of names and values (e.g. 569, 714, 1444, etc)

- (void)showBarlineReportDoctorsWithView:(HorizontalBarChartView *)viewChart andDataSource:(NSDictionary *)dictDataSource
{
    viewChart.data = nil;
    viewChart.noDataText = @"";
    viewChart.chartDescription.enabled = NO;
    viewChart.drawGridBackgroundEnabled = NO;
    viewChart.dragEnabled = YES;
    [viewChart setScaleEnabled:NO];
    viewChart.pinchZoomEnabled = NO;
    viewChart.delegate = self;
    viewChart.drawBarShadowEnabled = NO;
    viewChart.drawValueAboveBarEnabled = NO;
    viewChart.legend.enabled = NO;
    viewChart.highlightPerDragEnabled = NO;
    viewChart.highlightFullBarEnabled = NO;
    viewChart.highlightPerTapEnabled = NO;
    
    viewChart.leftAxis.drawAxisLineEnabled = NO;
    viewChart.leftAxis.drawGridLinesEnabled = NO;
    viewChart.leftAxis.drawZeroLineEnabled = NO;
    viewChart.leftAxis.drawLabelsEnabled = NO;
    viewChart.leftAxis.drawBottomYLabelEntryEnabled = NO;
    viewChart.leftAxis.drawLimitLinesBehindDataEnabled = NO;
    viewChart.leftAxis.axisMinimum = 0.0;
    viewChart.leftAxis.enabled = NO;
    
    viewChart.rightAxis.drawAxisLineEnabled = NO;
    viewChart.rightAxis.drawGridLinesEnabled = NO;
    viewChart.rightAxis.drawZeroLineEnabled = YES;
    viewChart.rightAxis.drawLabelsEnabled = NO;
    viewChart.rightAxis.axisMinimum = 0.0;
    viewChart.rightAxis.enabled = YES;
    
    viewChart.xAxis.drawAxisLineEnabled = YES;
    viewChart.xAxis.drawGridLinesEnabled = NO;
    viewChart.xAxis.labelFont = [UIFont fontWithName:@"Helvetica" size:10.5];
    viewChart.xAxis.labelTextColor = [UIColor blackColor];
    viewChart.xAxis.labelPosition = XAxisLabelPositionBottom;
    viewChart.xAxis.labelWidth = 300;
    viewChart.xAxis.labelHeight = 30;
    
    viewChart.xAxis.drawLabelsEnabled = YES;
    viewChart.xAxis.drawLimitLinesBehindDataEnabled = NO;
    viewChart.xAxis.granularity = 1.0;
    viewChart.xAxis.labelCount = dictDataSource.count;
    
    //Sort the dictionary to its values descending
    NSArray *blockSortedKeys = [dictDataSource keysSortedByValueUsingComparator: ^(id obj1, id obj2)
                                {
                                    // Switching the order of the operands reverses the sort direction
                                    return [obj2 compare:obj1];
                                }];
    NSMutableArray *yVals = NSMutableArray.new;
    NSMutableArray<NSString *> *xLabels = NSMutableArray.new;
    int i = 0;
    for (NSString *key in blockSortedKeys) {
        double dblValue = [dictDataSource[key] doubleValue];
        [yVals addObject:[[BarChartDataEntry alloc] initWithX:i y:dblValue]];
        NSString *strKeyAbbr = [IOSUtilities CutTextToRect:key MaxWidth:viewChart.xAxis.labelWidth withFont:[UIFont fontWithName:@"Helvetica" size:10.5]];
        [xLabels addObject:strKeyAbbr];
        i++;
    }

    if (yVals.count > 0)
    {
        viewChart.xAxis.valueFormatter = [[ChartIndexAxisValueFormatter alloc] initWithValues:xLabels];
        BarChartDataSet *set1 = nil;
        set1 = [[BarChartDataSet alloc] initWithValues:yVals label:@"DataSet"];
        set1.drawIconsEnabled = YES;
        set1.drawValuesEnabled = YES;
        set1.valueTextColor = [UIColor colorWithRed:229.0 green:252.0 blue:255.0 alpha:1.0];
        set1.valueFont = [UIFont fontWithName:@"Helvetica" size:20.0];
        
        NSNumberFormatter *pFormatter = [[NSNumberFormatter alloc] init];
        pFormatter.numberStyle = NSNumberFormatterCurrencyStyle;
        pFormatter.currencySymbol = @"€ ";
        pFormatter.maximumFractionDigits = 0;
        
        ChartDefaultValueFormatter *chartFormatter = [[ChartDefaultValueFormatter alloc] init];
        chartFormatter.formatter = pFormatter;
        [set1 setColor:[IOSUtilities getBlueIPraxColor]];
        
        NSArray *dataSets = @[set1];
        BarChartData *data = [[BarChartData alloc] initWithDataSets:dataSets];
        [data setValueFont:[UIFont fontWithName:@"Helvetica" size:20.0]];
         [data groupBarsFromX:0.0 groupSpace:10.0 barSpace:10.0];
        [data setValueFormatter:chartFormatter];
        viewChart.data = data;
    }
    [viewChart notifyDataSetChanged];
}

@liuxuan30
Copy link
Member

maybe you should draw the vale above the bar. There is no simple solution, as you can see the text is longer than the bar. What you expect to draw?

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