Last active
May 5, 2020 07:36
-
-
Save prabakarviji/5cdaa1d073af3645ecb4cf7b2942926a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import CorePlot | |
class ViewController: UIViewController { | |
var plotData = [Double](repeating: 0.0, count: 1000) | |
@IBOutlet var graphView: CPTGraphHostingView! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
initializeGraph() | |
} | |
func initializeGraph(){ | |
configureGraphtView() | |
} | |
func configureGraphtView(){ | |
graphView.allowPinchScaling = false | |
self.plotData.removeAll() | |
self.currentIndex = 0 | |
//Configure graph | |
let graph = CPTXYGraph(frame: hostView.bounds) | |
graph.plotAreaFrame?.masksToBorder = false | |
hostView.hostedGraph = graph | |
graph.backgroundColor = UIColor.black.cgColor | |
graph.paddingBottom = 40.0 | |
graph.paddingLeft = 40.0 | |
graph.paddingTop = 30.0 | |
graph.paddingRight = 15.0 | |
//Style for graph title | |
let titleStyle = CPTMutableTextStyle() | |
titleStyle.color = CPTColor.white() | |
titleStyle.fontName = "HelveticaNeue-Bold" | |
titleStyle.fontSize = 20.0 | |
titleStyle.textAlignment = .center | |
graph.titleTextStyle = titleStyle | |
//Set graph title | |
let title = "CorePlot" | |
graph.title = title | |
graph.titlePlotAreaFrameAnchor = .top | |
graph.titleDisplacement = CGPoint(x: 0.0, y: 0.0) | |
} | |
} | |
extension ViewController: CPTScatterPlotDataSource, CPTScatterPlotDelegate { | |
func numberOfRecords(for plot: CPTPlot) -> UInt { | |
return UInt(self.plotData.count) | |
} | |
func number(for plot: CPTPlot, field: UInt, record: UInt) -> Any? { | |
switch CPTScatterPlotField(rawValue: Int(field))! { | |
case .X: | |
return NSNumber(value: Int(record) + self.currentIndex-self.plotData.count) | |
case .Y: | |
return self.plotData[Int(record)] as NSNumber | |
default: | |
return 0 | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment