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

Can't add two pie charts in a uiviewcontroller at same time #3670

Closed
danaamiri opened this issue Sep 29, 2018 · 3 comments
Closed

Can't add two pie charts in a uiviewcontroller at same time #3670

danaamiri opened this issue Sep 29, 2018 · 3 comments

Comments

@danaamiri
Copy link

danaamiri commented Sep 29, 2018

class StatisticsViewController: UIViewController {
    let payChart:PieChartView = {
        let p = PieChartView()
        //p.translatesAutoresizingMaskIntoConstraints = false
        p.noDataText = "No Data"
        p.legend.enabled = false
        p.chartDescription?.text = ""
        return p
    }()
    let incomeChart:PieChartView = {
        let pi = PieChartView()
        //p.translatesAutoresizingMaskIntoConstraints = false
        pi.noDataText = "No Data"
        pi.legend.enabled = false
        pi.chartDescription?.text = ""
        return pi
    }()
    @IBOutlet weak var paymentCat: UIStackView!
    @IBOutlet weak var incomeCat: UIStackView!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.paymentCat.addArrangedSubview(self.payChart)
        self.setChart(data: ["cat": 20, "dog": 30, "both": 5, "neither": 45], chart: self.payChart)
        
        self.incomeCat.addArrangedSubview(self.incomeChart)
        
        setChart(data: ["cat": 20, "dog": 30, "both": 5], chart: self.incomeChart)
        
    
    }

    
    func setChart(data:[String:Int],chart:PieChartView){
        var dataEntries = [PieChartDataEntry]()
        for (key, val) in data {
            let entry = PieChartDataEntry(value: Double(val), label: key)
            dataEntries.append(entry)
        }
        let chartDataSet = PieChartDataSet(values: dataEntries, label: "")
        chartDataSet.colors = ChartColorTemplates.material()
        chartDataSet.sliceSpace = 2
        chartDataSet.selectionShift = 5
        
        let chartData = PieChartData(dataSet: chartDataSet)
        chart.data = chartData
    }
}

//////This show just payChart and doesn't add the incomechart
//////please help if you know how to do this

@furiosFast
Copy link

use two different container view

@pmairoldi
Copy link
Collaborator

Something is wrong with your Interface builder stuff. Just tried this and it works

class ViewController: UIViewController {

    let payChart:PieChartView = {
        let p = PieChartView()
        //p.translatesAutoresizingMaskIntoConstraints = false
        p.noDataText = "No Data"
        p.legend.enabled = false
        p.chartDescription?.text = ""
        return p
    }()
    
    let incomeChart:PieChartView = {
        let pi = PieChartView()
        //p.translatesAutoresizingMaskIntoConstraints = false
        pi.noDataText = "No Data"
        pi.legend.enabled = false
        pi.chartDescription?.text = ""
        return pi
    }()

    let paymentCat: UIStackView = {
        let stackView = UIStackView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))

        return stackView
    }()
    let incomeCat: UIStackView = {
        let stackView =  UIStackView(frame: CGRect(x: 0, y: 300, width: 300, height: 300))

        return stackView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.paymentCat.addArrangedSubview(self.payChart)
        self.setChart(data: ["cat": 20, "dog": 30, "both": 5, "neither": 45], chart: self.payChart)

        self.incomeCat.addArrangedSubview(self.incomeChart)
        setChart(data: ["cat": 20, "dog": 30, "both": 5], chart: self.incomeChart)

        self.view.addSubview(self.paymentCat)
        self.view.addSubview(self.incomeCat)
    }


    func setChart(data:[String:Int],chart:PieChartView){
        var dataEntries = [PieChartDataEntry]()
        for (key, val) in data {
            let entry = PieChartDataEntry(value: Double(val), label: key)
            dataEntries.append(entry)
        }
        let chartDataSet = PieChartDataSet(values: dataEntries, label: "")
        chartDataSet.colors = ChartColorTemplates.material()
        chartDataSet.sliceSpace = 2
        chartDataSet.selectionShift = 5

        let chartData = PieChartData(dataSet: chartDataSet)
        chart.data = chartData
    }
}

simulator screen shot - iphone xs - 2018-10-08 at 16 16 27

@PrashantKT
Copy link

PrashantKT commented Jan 3, 2019

How to make colors in both charts same ? Like look at Yellow color in both chart and red color. dog and both are switched Any solution for this ?.

I mean to say dog should be yellow color in both and both should have red color in both is it possible ?

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

4 participants