Skip to content

Commit

Permalink
Improve demo app
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro Isaza committed Feb 14, 2016
1 parent 11e7841 commit 591a097
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions PlotKitDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,49 @@ import PlotKit
import Upsurge

class ViewController: NSViewController {
var plotView: PlotView!

override func viewDidLoad() {
super.viewDidLoad()
createPlotView()
createSinePlot()
}

func createPlotView() {
plotView = PlotView()
view.addSubview(plotView)

plotView.translatesAutoresizingMaskIntoConstraints = false
plotView.leadingAnchor.constraintEqualToAnchor(view.leadingAnchor).active = true
plotView.trailingAnchor.constraintEqualToAnchor(view.trailingAnchor).active = true
plotView.topAnchor.constraintEqualToAnchor(view.topAnchor).active = true
plotView.bottomAnchor.constraintEqualToAnchor(view.bottomAnchor).active = true
}

func createSinePlot() {
let font = NSFont(name: "Optima", size: 16)!

let π = M_PI
let count = 1024
let t = (0..<count).map{ 2*π * Double($0) / Double(count-1) }
let y = sin(RealArray(t))

let plot = plotPoints((0..<count).map{ Point(x: t[$0], y: y[$0]) }, hTicks: .Fit(6), vTicks: .Fit(4))
plot.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(plot)
let ticks = [
TickMark(π/2, label: "π/2"),
TickMark(π, label: "π"),
TickMark(3*π/2, label: "3π/2"),
TickMark(2*π, label: ""),
]
var xaxis = Axis(orientation: .Horizontal, ticks: .List(ticks))
xaxis.position = .Value(0)
xaxis.labelAttributes = [NSFontAttributeName: font]
plotView.addAxis(xaxis)

var yaxis = Axis(orientation: .Vertical, ticks: .Fit(4))
yaxis.labelAttributes = [NSFontAttributeName: font]
plotView.addAxis(yaxis)

plot.leadingAnchor.constraintEqualToAnchor(view.leadingAnchor).active = true
plot.trailingAnchor.constraintEqualToAnchor(view.trailingAnchor).active = true
plot.topAnchor.constraintEqualToAnchor(view.topAnchor).active = true
plot.bottomAnchor.constraintEqualToAnchor(view.bottomAnchor).active = true
let pointSet = PointSet(points: (0..<count).map{ Point(x: t[$0], y: y[$0]) })
plotView.addPointSet(pointSet)
}
}

0 comments on commit 591a097

Please sign in to comment.