GithubHelp home page GithubHelp logo

iosfreak57 / plot3d Goto Github PK

View Code? Open in Web Editor NEW

This project forked from stokatyan/plot3d

0.0 0.0 0.0 102 KB

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

Objective-C 0.70% Swift 89.70% Ruby 9.60%

plot3d's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.