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

Pie chart data labels not in correct position. #436

Closed
PrajeetShrestha opened this issue Oct 2, 2015 · 6 comments
Closed

Pie chart data labels not in correct position. #436

PrajeetShrestha opened this issue Oct 2, 2015 · 6 comments

Comments

@PrajeetShrestha
Copy link

Hello,
I am using ur library and the data labels are not correctly rendered in the PieChart. Shouldn't be it in the middle of the PieChart slices?

I initialized data set as :
PieChartData *data = [[PieChartData alloc] initWithXVals:nil dataSet:dataSet];
Is there any other way to remove the data labels as well?
HELLO THERE

@danielgindi
Copy link
Collaborator

This is very weird, as it works in the demo perfectly. Can you show us your code?

Or maybe play with the demo a little until you find the line that makes all the difference?

@PrajeetShrestha
Copy link
Author

- (void)chartSetup {

    _chartView.delegate = self;
    _chartView.usePercentValuesEnabled = NO;
    _chartView.holeTransparent = YES;
    _chartView.centerTextFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:12.f];
    _chartView.holeRadiusPercent = 0.58;
    _chartView.transparentCircleRadiusPercent = 0.61;
    _chartView.descriptionText = @"";
    _chartView.drawCenterTextEnabled = YES;
    _chartView.drawHoleEnabled = YES;
    _chartView.rotationAngle = 0.0;
    _chartView.rotationEnabled = NO;
    _chartView.centerText = @"14 Total Reports";
    [_chartView animateWithXAxisDuration:1.5 yAxisDuration:1.5 easingOption:ChartEasingOptionEaseOutBack];

    NSArray *dataSet = @[@1, @3 , @34];
    [self setPieData:dataSet];
}

- (void)setPieData:(NSArray *)values
{

    NSMutableArray *yVals1 = [[NSMutableArray alloc] init];
    for (int i = 0; i < values.count; i++)
    {
        double sliceValue = [values[i] doubleValue];
        [yVals1 addObject:[[BarChartDataEntry alloc] initWithValue:sliceValue xIndex:i]];
    }

    PieChartDataSet *dataSet = [[PieChartDataSet alloc] initWithYVals:yVals1 label:@""];
    dataSet.sliceSpace = 0.0;
    dataSet.colors = @[UIColorFromRGB(0xD37D7C),kAppThemeColor,UIColorFromRGB(0x6592CA)];

    PieChartData *data = [[PieChartData alloc] initWithXVals:nil dataSet:dataSet];
    NSNumberFormatter *pFormatter = [[NSNumberFormatter alloc] init];
    pFormatter.numberStyle = kCFNumberFormatterNoStyle;
    pFormatter.maximumFractionDigits = 0;
    pFormatter.multiplier = @1.f;
    [data setValueFormatter:pFormatter];
    [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:14.0f]];
    [data setValueTextColor:UIColor.whiteColor];
    _chartView.data = data;
}

This is my exact code for displaying pie. Playing around with it since a while couldn't figure out :)

@PrajeetShrestha
Copy link
Author

Oh I managed to fix this by removing the line where we set the rotationAngle of chart to :

_chartView.rotationAngle = 0.0;
May be some calculation error on setting the rotation of Pie ?

@danielgindi
Copy link
Collaborator

No, it's not that at all.

You've not set x-values to the data. It tries to render the labels for the x-values below the values, but has nothing to render.
If you want to avoid rendering of x-values, you have to:

_chartView.drawSliceTextEnabled = NO;

@PrajeetShrestha
Copy link
Author

That solved the issue! :) Thank you! And it's awesome library ! 👍

@anfriis
Copy link

anfriis commented Jul 14, 2016

I have the same problem, but am unable to fix it using the solution above.

I want the labels on the screenshot to also be aligned in the middle of the slice.
skaermbillede 2016-07-14 16 18 38

Here is my Swift code for setting up the Pie Chart:

        usePercentValuesEnabled = false
        drawHoleEnabled = false
        drawCenterTextEnabled = false
        drawSliceTextEnabled = false

        legend.enabled = false
        rotationEnabled = false

        highlightPerTapEnabled = false

        usePercentValuesEnabled = true

        noDataText = ""
        descriptionText = ""

        setNeedsDisplay()

And when i set values in the chart:

        var yVals = [ChartDataEntry]()
        var colors = [UIColor]()

        if let noAnswer = p.noAnswer where noAnswer != 0 {
            yVals.append(BarChartDataEntry(value: Double(noAnswer), xIndex: 0) as ChartDataEntry)
            colors.append(Answer.noColor)
        }

        if let yesAnswer = p.yesAnswer where yesAnswer != 0 {
            let index = (yVals.count == 0) ? 0 : 1
            yVals.append(BarChartDataEntry(value: Double(yesAnswer), xIndex: index) as ChartDataEntry)
            colors.append(Answer.yesColor)
        }

        // Create the data set with y values
        let dataSet = PieChartDataSet(yVals: yVals, label: "")
        dataSet.colors = colors
        dataSet.sliceSpace = 0


        // Get empty string for each y val, so the num of xVals equals the num of yVals
        let xVals = yVals.map { _ in "" }

        let data = PieChartData(xVals: xVals, dataSets: [dataSet])

        data.setValueFont(UIFont.defaultFont(.Regular, size: 9))
        data.setValueTextColor(UIColor.whiteColor())

        self.data = data
        self.highlightValues(nil)

        self.animate(xAxisDuration: 1.1, yAxisDuration: 1.1, easingOption: ChartEasingOption.EaseOutCirc)

Would be fantastic if someone could help :)

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