DanielGindi IOS水平条形图值标签被切除

时间:2019-02-12 10:14:45

标签: ios objective-c ios-charts

我已经设置了水平条形图,并在每个条形图中放置了自定义值。 不幸的是,大多数较小的值不能正确地渲染到条形图中。 有没有一种方法可以设置钢筋的最小长度来避免出现此问题(或其他问题)。

我尝试了多种设置偏移量和minValue的方法,但没有成功。 如果可以的话,有人可以指出正确的方向。不胜感激。 谢谢

enter image description here

请在下面的代码中重现此行为。 字典由名称和值组成(例如569、714、1444等)

- (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];
}

0 个答案:

没有答案