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 not accepting more than one dataSet #2489

Closed
yazankhayyat opened this issue May 30, 2017 · 1 comment
Closed

Pie chart not accepting more than one dataSet #2489

yazankhayyat opened this issue May 30, 2017 · 1 comment

Comments

@yazankhayyat
Copy link

yazankhayyat commented May 30, 2017

Hello,

This issue was addressed in January as a bug , and it is yet to be resolved.
I am trying to add 2 PieChartDataSets to my PieChartView, but I keep running into the issue where one of the datasets unfolds as a nil when the renderer is creating the Legend object.

Here is my code (I know its super ugly but its a dummy project)

Here is a link to the same addressed issue - #2099

func setChart() {

    for value in stocks1 {
        let entry = PieChartDataEntry()
        entry.y = Double(value.amount)
        entry.label = value.name
        
        dataEntries1.append(entry)
    }
    for value in stocks2 {
        let entry = PieChartDataEntry()
        entry.y = Double(value.amount)
        entry.label = value.name
        
        dataEntries2.append(entry)
    }
    
    var colors1: [UIColor] = []
    var colors2: [UIColor] = []
    
    for _ in 0..<stocks1.count {
        let red = Double(arc4random_uniform(256))
        let green = Double(arc4random_uniform(256))
        let blue = Double(arc4random_uniform(256))
        
        let color = UIColor(red: CGFloat(red/255), green: CGFloat(green/255), blue: CGFloat(blue/255), alpha: 1)
        colors1.append(color)
    }
    
    for _ in 0..<stocks2.count {
        let red = Double(arc4random_uniform(256))
        let green = Double(arc4random_uniform(256))
        let blue = Double(arc4random_uniform(256))
        
        let color = UIColor(red: CGFloat(red/255), green: CGFloat(green/255), blue: CGFloat(blue/255), alpha: 1)
        colors2.append(color)
    }
    let set1 = ChartDataSet(values: dataEntries1, label: "Companies")
    let set2 = ChartDataSet(values: dataEntries2, label: "Companies2")
    let data1 = PieChartData(dataSets: [set1, set2])
    set1.colors = colors1
    set2.colors = colors2
    pieChartView.data = data1
}
@liuxuan30
Copy link
Member

liuxuan30 commented Jun 5, 2017

As far as I remember, pie chart only allow 1 data set.

    open override func getDataSetByIndex(_ index: Int) -> IChartDataSet?
    {
        if index != 0
        {
            return nil
        }
        return super.getDataSetByIndex(index)
    }

will add a guard to avoid crash

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

2 participants