0

So I have the following code, followed by the Core plot tutorial by Ray Wenderlich over here but adjusted for my own purposes which looks like this:

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
}
}

But when I run this, all I get greeted with, is an empty screen that looks like this instead of a pie chart.

enter image description here

Can someone tell me what is wrong with my code? I'd this something to do with the Swift version as I am currently on Xcode 9

1 Answer 1

1
  1. Make sure the hosting view is set up properly. You can inspect hostView in the debugger and make sure it is not nil.

  2. The number(for:field:record:) method signature looks ok, but you need to cast the returned values to NSNumber.

  3. Do you really need to completely rebuild the graph in viewDidLayoutSubviews()? Once the graph is built, Core Plot will take care of layout updates whenever the hosting view bounds change.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.