PieChart for ios-charts broken in Swift 2.0?

删除回忆录丶 提交于 2019-12-12 02:28:13

问题


So I upgraded my project, and pods, to work with Swift 2.0 and Xcode 7. But now my PieChart isn't working as expected anymore. My Pies are all starting from the 0 degree angle, which means that they are all beginning in the same place, above each other.

The data is added the same way as before and if I highlight them I can see that they are all there, but not originating from their respective places.

Code for adding my pieChartData:

var dataEntries: [ChartDataEntry] = []
var dataPoints: [String] = []

for var i = 0; i < expensesHistory.count; i++ {
    let dataEntry = ChartDataEntry(value: expensesHistory[i].amount, xIndex: i)
    dataEntries.append(dataEntry)
    dataPoints.append(expensesHistory[i].category.rawValue)
}

let pieChartDataSet = PieChartDataSet(yVals: dataEntries)
let pieChartData = PieChartData(xVals: dataPoints, dataSet: pieChartDataSet)
pieChartView.data = pieChartData
var colors = [UIColor]()
colors.append(Colors.blueColor())
pieChartDataSet.colors = colors
pieChartDataSet.drawValuesEnabled = false
pieChartDataSet.sliceSpace = 4.0

The data gets loaded and added correctly, into different entries (there are 7) but all 7 pies are starting in the same place...so they don't result in a circle at all. Any ideas?

UPDATE #1

I translated the ChartsDemo to Swift as best I could and I end up with the same issue, but further debugging tells me that I'm probably only getting the last Entry pushed into the DataSet. If I set PieChartDataSet.drawValuesEnabled = true and PieChartView.legend.enabled = true I only see 1 value in the legend and 1 value for the DataSet. But I checked and my xVals and yVals contain more than 1 value, but it only displays one, and randomly chosen which one as well. I tried to find a pattern in which entry it chooses, but it just picks one at random.

I have added functionality to my PieChartView which allows me to press a button to highlight the entry associated with it, so that's why I thought that all the pies are placed above each other, but now I'm thinking it might be that the PieChartView is only showing 1 value, since I only get 1 value in the legend displayed, and not all of them on top of one another.

Here is my translation code from the ChartsDemo to Swift, keep in mind that I use Swift 2.0 (which is where this issue first presented itself).

private func setPieChartData() {

    let mult = UInt32(100)
    let count = Int(4)

    let parties = [
    "Party A", "Party B", "Party C", "Party D", "Party E", "Party F",
    "Party G", "Party H", "Party I", "Party J", "Party K", "Party L",
    "Party M", "Party N", "Party O", "Party P", "Party Q", "Party R",
    "Party S", "Party T", "Party U", "Party V", "Party W", "Party X",
    "Party Y", "Party Z"
    ]

    var yVals1 = [ChartDataEntry]()

    // IMPORTANT: In a PieChart, no values (Entry) should have the same xIndex (even if from different DataSets), since no values can be drawn above each other.
    for var i = 0; i < count; i++
    {
        let chartDataEntry = ChartDataEntry(value: Double((arc4random_uniform(mult) + mult/5)), xIndex: i)
        yVals1.append(chartDataEntry)
    }

    var xVals = [String?]()

    for var i = 0; i < count; i++
    {
        xVals.append(parties[i % parties.count])
        //[xVals addObject:parties[i % parties.count]]
    }

    let dataSet = PieChartDataSet(yVals: yVals1, label: "Election Results")
    dataSet.sliceSpace = 3.0

    // add a lot of colors

    var testColors = [UIColor]()
    testColors.append(Colors.blueColor())
    let data = PieChartData(xVals: xVals, dataSet: dataSet)

    pieChartView.data = data

    dataSet.colors = testColors
    dataSet.drawValuesEnabled = true

    dataSet.sliceSpace = 4.0
}

Since this issue started when I updated to the Swift 2.0 compatible version of ios-charts I still think that this issue is connected to that update. I haven't touched this code in months prior to the update, and it works fine with the previous version. And as far as I can tell from a debugging point of view all my values are pushed correctly, but seems to not be handled correctly by the PieChartView.

UPDATE #2

Here is a sample project where I show you the issue I'm having. I did not include functionality for highlighting with buttons since it will take a bit more effort to get it working. Link to sample project

How to:

  1. You need Xcode 7 to run this.
  2. You need CocoaPods installed.
  3. Once you have installed CocoaPods use the terminal to go to the project folder and type "pod install" in root to install the Charts framework.
  4. Use the ChartsIssue.xcworkspace to launch the project, NOT ChartsIssue.xcproj

回答1:


I tried to simulate what your code do in ChartsDemo's pie chart view controller like below, but it does not have problem.

I looked at your code, it is also fine to me. I guess there might be something wrong with your data value, like class type? Anyway, based on current info, I cannot say what's wrong with it, but it seems like it's your project's problem.

There are two ways to move forward:

1.You double check the data value and try debug on your own, to figure out why;

2.Try to reproduce with ChartsDemo code, and post your data sample code, so I can reproduce it on my side; a screenshot is even better.

- (void)setDataCount:(int)count range:(double)range
{
    double mult = range;

    NSMutableArray *yVals1 = [[NSMutableArray alloc] init];

    // IMPORTANT: In a PieChart, no values (Entry) should have the same xIndex (even if from different DataSets), since no values can be drawn above each other.
    for (int i = 0; i < count; i++)
    {
        [yVals1 addObject:[[ChartDataEntry alloc] initWithValue:(arc4random_uniform(mult) + mult / 5) xIndex:i]];
    }

    NSMutableArray *xVals = [[NSMutableArray alloc] init];

    for (int i = 0; i < count; i++)
    {
        [xVals addObject:parties[i % parties.count]];
    }

    PieChartDataSet *dataSet = [[PieChartDataSet alloc] initWithYVals:yVals1 label:@"Election Results"];
    dataSet.sliceSpace = 3.0;

    // add a lot of colors

    NSMutableArray *colors = [[NSMutableArray alloc] init];
    [colors addObject:[UIColor blueColor]];

    PieChartData *data = [[PieChartData alloc] initWithXVals:xVals dataSet:dataSet];
    _chartView.data = data;

    dataSet.colors = colors;
    dataSet.drawValuesEnabled = NO;
    dataSet.sliceSpace = 4.0;
}

Update for question update 1&2:

I have download your demo project and manage to get it work on my side, I do see what you are saying, only one arc stripe while you should have 4 values;

I think you have hit an animation bug here or you mis-use it:

pieChartView.animate(xAxisDuration: 1.5, easingOption: ChartEasingOption.EaseOutBack)
pieChartView.animate(yAxisDuration: 1.5, easingOption: ChartEasingOption.EaseOutBack)

Simply comment one of them out and it will display correctly (not sure if the animation is fine), comment both will display correctly without animation too.

If you want animate both x and y axis, use below func also give you correct chart and animation:

pieChartView.animate(xAxisDuration: 1.5, yAxisDuration: 1.5, easingOption: ChartEasingOption.EaseOutBack)

I will post an issue to ask if using separate methods simultaneously is valid or not.



来源:https://stackoverflow.com/questions/32970306/piechart-for-ios-charts-broken-in-swift-2-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!