ios - Core Plot 饼图不显示

标签 ios swift3 core-plot

所以我有以下代码,然后是 Ray Wenderlich 在 here 上的核心情节教程但根据我自己的目的进行了调整,如下所示:

class CategoryAnalysis:UIViewController{
var collection:String?
var categories = [Category](){
    didSet{
    CoreDataHelper.retrieveCategories()
    }
}
var categoryTotals:[Double] = []
var colors:[CPTColor] = [CPTColor.red(),CPTColor.blue(),CPTColor.cyan(),CPTColor.orange(),CPTColor.yellow(),CPTColor.darkGray(),CPTColor.green(),CPTColor.purple(),CPTColor.lightGray(),CPTColor.brown(),CPTColor.darkGray()]
@IBOutlet weak var hostView: CPTGraphHostingView!

override func viewDidLoad() {
    self.categories = CoreDataHelper.retrieveCategories()
    categoryTotals.removeAll()
    print (collection)
    for category in categories{
        categoryTotals.append(ExpensesAdditions().retrieveCategoryAnalysis(collectionName: collection!, category: String(describing: category)))
    }

    super.viewDidLoad()
    print(categoryTotals)
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(false)

}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(false)
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    initPlot()
}

func initPlot() {
    configureHostView()
    configureGraph()
    configureChart()
    configureLegend()
}

func configureHostView() {
    hostView.allowPinchScaling = false
}

func configureGraph() {
    let graph = CPTXYGraph(frame: hostView.bounds)
    hostView.hostedGraph = graph
    graph.paddingLeft = 0.0
    graph.paddingTop = 0.0
    graph.paddingRight = 0.0
    graph.paddingBottom = 0.0
    graph.axisSet = nil

    // 2 - Create text style
    let textStyle: CPTMutableTextStyle = CPTMutableTextStyle()
    textStyle.color = CPTColor.black()
    textStyle.fontName = "HelveticaNeue-Bold"
    textStyle.fontSize = 16.0
    textStyle.textAlignment = .center

    // 3 - Set graph title and text style
    graph.title = "Category Analysis"
    graph.titleTextStyle = textStyle
    graph.titlePlotAreaFrameAnchor = CPTRectAnchor.top
}

func configureChart() {
    let graph = hostView.hostedGraph!

    // 2 - Create the chart
    let pieChart = CPTPieChart()
    pieChart.delegate = self
    pieChart.dataSource = self
    pieChart.pieRadius = (min(hostView.bounds.size.width, hostView.bounds.size.height) * 0.7) / 2
    pieChart.identifier = NSString(string: graph.title!)
    pieChart.startAngle = CGFloat(M_PI_4)
    pieChart.sliceDirection = .clockwise
    pieChart.labelOffset = -0.6 * pieChart.pieRadius

    // 3 - Configure border style
    let borderStyle = CPTMutableLineStyle()
    borderStyle.lineColor = CPTColor.white()
    borderStyle.lineWidth = 2.0
    pieChart.borderLineStyle = borderStyle

    // 4 - Configure text style
    let textStyle = CPTMutableTextStyle()
    textStyle.color = CPTColor.white()
    textStyle.textAlignment = .center
    pieChart.labelTextStyle = textStyle

    // 5 - Add chart to graph
    graph.add(pieChart)
}

func configureLegend() {
}


}

extension CategoryAnalysis: CPTPieChartDataSource, CPTPieChartDelegate {

func numberOfRecords(for plot: CPTPlot) -> UInt {
        return UInt(categories.count)
}

func number(for plot: CPTPlot, field fieldEnum: UInt, record idx: UInt) -> Any? {
    if categoryTotals[Int(idx)] == 0{
        return 0
    }
    else {
    return categoryTotals[Int(idx)]
    }
}

func dataLabel(for plot: CPTPlot, record idx: UInt) -> CPTLayer? {
    let value = ExpensesAdditions().convertToMoney(categoryTotals[Int(idx)])
    if categoryTotals[Int(idx)] == 0{
        let layer2 = CPTTextLayer(text: "")
        return layer2
    }
    else{
    let name = String(describing:categories[Int(idx)])
    let layer = CPTTextLayer(text: String(format: name, value))
    layer.textStyle = plot.labelTextStyle
    return layer
    }
}

func sliceFill(for pieChart: CPTPieChart, record idx: UInt) -> CPTFill? {
    if categoryTotals[Int(idx)]==0{
        return CPTFill(color: CPTColor.black())
    }
    else {
    return CPTFill(color: colors[Int(idx)])
    }
}

func legendTitle(for pieChart: CPTPieChart, record idx: UInt) -> String? {
    return nil
}
}

但是当我运行它时,我看到的只是一个看起来像这样的空屏幕,而不是饼图。

enter image description here

有人能告诉我我的代码有什么问题吗?我认为这与 Swift 版本有关,因为我目前使用的是 Xcode 9

最佳答案

  1. 确保托管 View 设置正确。您可以在调试器中检查 hostView 并确保它不是 nil

  2. number(for:field:record:) 方法签名看起来不错,但您需要将返回值转换为 NSNumber

  3. 您真的需要在 viewDidLayoutSubviews() 中完全重建图形吗?图表构建完成后,Core Plot 将在托管 View bounds 更改时负责布局更新。

关于ios - Core Plot 饼图不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48109838/

相关文章:

ios - 使用 CorePlot 的散点图中的可变大小圆圈

iOS核心剧情: Difference between CPPlot and CPTPlot

ios - 如何使用 Xcode 制作一个 imessage 扩展包以便可以导入

iphone - iPhone编程如何给UIImageview添加注解

ios - 找不到 Swift 3.0 stringFromStringNumberOrNil 替换。应用给出错误 "Ambiguous reference to member ' 值'或 'value' 的模糊使用“

ios - 如何获取当月的开始日期和结束日期(Swift 3)

ios - 核心图-iOS触摸事件很难在图形上选择点

ios - iOS 8 横向纵向模式下的自定义键盘

ios - ios-无法上传新版本

swift - 在 Swift 3 中向后迭代时安全地删除项目