Swift不绘制图表(XYPieChart)

时间:2016-04-14 12:15:51

标签: swift cocoa

我正在使用XYPieChart库在我的项目中绘制饼图,但是当我在模拟器中运行它时它不会绘制图表。我在UITableViewController课程中尝试过,但没有将结果切换到UIViewController,但结果相同。 我在这里做错了什么?

import Foundation
import XYPieChart

class MainVC:UIViewController,XYPieChartDelegate,XYPieChartDataSource{

    let z = Share.sharedInstance
    let dbm = DatabaseManager()
    var chart_dNameArr = [""]
    var chart_dAmountArr = [0.0]

    override func viewDidLoad() {
        self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
        makeChart()
    }

    func makeChart(){
        let pieChart = XYPieChart()
        let viewWidth: Float = Float(pieChart.bounds.size.width / 2)
        let viewHeight: Float = Float(pieChart.bounds.size.height / 2)
        pieChart.delegate = self
        pieChart.dataSource = self
        pieChart.startPieAngle = CGFloat(M_PI_2)
        pieChart.animationSpeed = 1.5
        pieChart.labelColor = UIColor.whiteColor()
        pieChart.labelShadowColor = UIColor.blackColor()
        pieChart.showPercentage = true
        pieChart.backgroundColor = UIColor.whiteColor()
        //To make the chart at the center of view
        pieChart.pieCenter = CGPointMake(pieChart.bounds.origin.x + CGFloat(viewWidth), pieChart.bounds.origin.y + CGFloat(viewHeight))
        //Method to display the pie chart with values.
        pieChart.reloadData()

        print("made a chart")
    }

    func numberOfSlicesInPieChart(pieChart: XYPieChart!) -> UInt {
      return 2
    }

    func pieChart(pieChart: XYPieChart!, valueForSliceAtIndex index: UInt) -> CGFloat {
        var value: CGFloat = 0.0
        if index % 2 == 0 {
            value = 25
        }
        else {
            value = 75
        }
        return value
    }

    func pieChart(pieChart: XYPieChart!, colorForSliceAtIndex index: UInt) -> UIColor! {
        var color: UIColor
        if index % 2 == 0 {
            color = UIColor.redColor()
        }
        else {
            color = UIColor.greenColor()
        }
        return color
    }     
}

1 个答案:

答案 0 :(得分:0)

我认为您没有将饼图添加到视图

func makeChart(){
   //....
   pieChart.reloadData()
   addSubView(pieChart) //<- Add this line
}