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

Could not cast value of type 'Charts.BarChartData' (0x109bd1010) to 'Charts.PieChartData' (0x109be0320). #1778

Closed
durul opened this issue Nov 3, 2016 · 0 comments

Comments

@durul
Copy link

durul commented Nov 3, 2016

Here is my code.

//
// ChartsViewController.swift
// iOSChartsDemo
//

import UIKit
import Charts

class ChartsViewController: UIViewController {

@IBOutlet weak var lineChartView: LineChartView!
@IBOutlet weak var pieChartView: PieChartView!
var months: [String]!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    
    months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
    let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0]
    
    setChart(dataPoints: months, values: unitsSold)
    
}

func setChart(dataPoints: [String], values: [Double]) {
    
    
    var dataEntries: [BarChartDataEntry] = []
    var counter = 0.0
    
    for i in 0..<dataPoints.count {
        counter += 1.0
        let dataEntry = BarChartDataEntry(x: values[i], y: counter)
        dataEntries.append(dataEntry)
    }
    
    let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")
    let chartData = BarChartData()
    chartData.addDataSet(chartDataSet)
    pieChartView.data = chartData
    
    var colors: [UIColor] = []
    
    for _ in 0..<dataPoints.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)
        colors.append(color)
    }
    
    chartDataSet.colors = colors
    
    
    let lineDataSet = LineChartDataSet(values: dataEntries, label: "Units Sold")
    let lineData = LineChartData()
    lineData.addDataSet(lineDataSet)
    lineChartView.data = lineData
    
}

}

@durul durul closed this as completed Nov 3, 2016
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

1 participant