Skip to content

stokatyan/Plot3D

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Plot3D

Version License Platform

Installation

Plot3d is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'Plot3d'

Usage

Plot3d is a framework for plotting data in 3D. The data is plotted using SceneKit and the entire scene is contained in a PlotView, which is a subclass of a UIView. A PlotView creates a 3D plot using a data source and delegate pattern similar to a UITableView's so, hopefully, it is easy to get started.

Check out this Medium article for a more detailed example on how to use Plot3d.

PlotView

A scene for a 3D plot can be created and added to a view controller much using a PlotView.
/// 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)

Plotting Points

Points are plotted using a pattern similar to 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)
        }
    }
}

Connecting Points

Points can be connected to help with data visualization. Connections can be made by implementing optional PlotDataSource and PlotDelegate functions.
// 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)
}

About

Plot3d is an iOS framework for plotting data in 3D.

Resources

Stars

Watchers

Forks

Packages

No packages published