所以我有以下代码,然后是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
}
}
但是当我运行这个时,所有我都会受到欢迎,是一个看起来像这样的空屏幕而不是饼图。
有人可以告诉我我的代码有什么问题吗?我和Swift版本有关,因为我目前正在使用Xcode 9
答案 0 :(得分:1)
确保托管视图已正确设置。您可以在调试器中检查hostView
,并确保它不是nil
。
number(for:field:record:)
方法签名看起来没问题,但您需要将返回的值转换为NSNumber
。
您真的需要在viewDidLayoutSubviews()
中完全重建图表吗?构建图表后,只要主机视图bounds
发生更改,Core Plot就会负责布局更新。