Plot3D
安装
Plot3d可通过CocoaPods获取。要安装它,只需将以下行添加到Podfile中
pod 'Plot3d'
使用
Plot3d是一个用于在3D中绘制数据的框架。数据使用SceneKit绘制,整个场景包含在一个PlotView中,它是一个UIView的子类。PlotView使用数据源和委托模式创建3D绘图,类似于UITableView的,因此应该很容易上手。
查看这篇Medium文章,了解如何使用Plot3d的更详细的示例。
PlotView可以使用PlotView在视图控制器中创建和添加一个3D绘图场景。
/// Initialize a view containing a 3-D plot with the given frame and a default configuration.
let plotView = PlotView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height),
configuration: PlotConfiguration())
/// A PlotView is child of UIView, so add it as a subview.
view.addSubview(plotView)
绘制点使用类似于UITableView的模式绘制点。
// A plot view delegates tasks similar to a UITableView.
plotView.dataSource = self
plotView.delegate = self
plotView.reloadData()
// Set the axis titles.
plotView.setAxisTitle(.x, text: "x axis", textColor: .white)
plotView.setAxisTitle(.y, text: "y axis", textColor: .white)
plotView.setAxisTitle(.z, text: "z axis", textColor: .white)
extension ViewController: PlotDataSource {
func numberOfPoints() -> Int {
return 16
}
}
extension ViewController: PlotDelegate {
func plot(_ plotView: PlotView, pointForItemAt index: Int) -> PlotPoint {
let v = CGFloat(index)
return PlotPoint(v, sqrt(v) * 3, v)
}
func plot(_ plotView: PlotView, geometryForItemAt index: Int) -> SCNGeometry? {
let geo = SCNSphere(radius: 0.15)
geo.materials.first!.diffuse.contents = UIColor.red
return geo
}
func plot(_ plotView: PlotView, textAtTickMark index: Int, forAxis axis: PlotAxis) -> PlotText? {
let config = PlotConfiguration()
switch axis {
case .x:
return PlotText(text: "\(Int(CGFloat(index + 1) * config.xTickInterval))", fontSize: 0.3, offset: 0.25)
case .y:
return PlotText(text: "\(Int(CGFloat(index + 1) * config.yTickInterval))", fontSize: 0.3, offset: 0.1)
case .z:
return PlotText(text: "\(Int(CGFloat(index + 1) * config.zTickInterval))", fontSize: 0.3, offset: 0.25)
}
}
}
连接点可以通过实现可选的PlotDataSource和PlotDelegate函数来连接点,以便于数据可视化。
// Optional PlotDataSource function for providing the number of connections to make.
func numberOfConnections() -> Int {
return 15
}
// Optional PlotDelegate function for specifying which points to connect.
func plot(_ plotView: PlotView, pointsToConnectAt index: Int) -> (p0: Int, p1: Int)? {
return (p0: index, p1: index + 1)
}
// Optional PlotDelegate function for specifying how each connection looks.
func plot(_ plotView: PlotView, connectionAt index: Int) -> PlotConnection? {
return PlotConnection(radius: 0.025, color: .red)
}