[英]Rendering basic bar chart with core plot in swift
我正在尝试使用以下代码呈现基本的条形图:
import UIKit
import CorePlot
class BarChart: CPTGraphHostingView, CPTPlotDataSource, CPTPlotDelegate, CPTPlotSpaceDelegate
{
let data: [[UInt]] = [
[30, 40, 100],
[10, 44, 35]
]
func renderData() {
let barGraph = CPTXYGraph(frame: self.bounds)
self.hostedGraph = barGraph
barGraph.axisSet = nil
var space: CPTXYPlotSpace = barGraph.defaultPlotSpace as! CPTXYPlotSpace
barGraph.addPlotSpace(space)
space.yRange = CPTPlotRange(locationDecimal: CPTDecimalFromFloat(0), lengthDecimal: CPTDecimalFromFloat(50))
space.xRange = CPTPlotRange(locationDecimal: CPTDecimalFromFloat(0), lengthDecimal: CPTDecimalFromFloat(3))
space.delegate = self
var bar = CPTBarPlot(frame: barGraph.bounds)
bar.dataSource = self
bar.barWidth = 3
barGraph.addPlot(bar, toPlotSpace: space)
}
func numberForPlot(plot: CPTPlot, field: UInt, recordIndex: UInt ) -> AnyObject? {
return data[0][Int(idx)]
}
func barFillForBarPlot(barPlot: CPTBarPlot, recordIndexRange indexRange: NSRange) -> AnyObject? {
return CPTFill(color: CPTColor(componentRed: 0, green: 0, blue: 255, alpha: 1))
}
func numberOfRecordsForPlot(plot: CPTPlot) -> UInt {
return 3
}
}
此类与情节提要中的视图关联。 不幸的是,我无法显示任何数据(但是当我使用barGraph.axisSet = nil
删除线时,轴是可见的)。 我想念什么吗? 谢谢!
使用Swift时,建议从GitHub的“ release-2.0”分支上获取Core Plot的版本。 它进行了一些API更改,使使用Swift更加容易。 最大的方法是提供使用NSDecimal
值的方法和属性的替代方法,例如绘图范围。 我希望Xcode 7最终发布后能尽快完成2.0版发布。
无需向图形添加默认的绘图空间; 它已经在那里。
-numberForPlot:field:recordIndex:
函数应将值作为NSNumber
返回。 返回值时强制转换值,例如, return data[0][Int(idx)] as NSNumber
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.