-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGraphView.swift
More file actions
41 lines (31 loc) · 1.12 KB
/
GraphView.swift
File metadata and controls
41 lines (31 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//
// GraphView.swift
// AudioTest
//
// Created by hirochin on 08/12/2017.
// Copyright © 2017 Thel. All rights reserved.
//
import Cocoa
class GraphView: NSView {
var data: [Float] = []
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
// Drawing code here.
let context = NSGraphicsContext.current?.cgContext
drawGraphInContext(context: context, rect: self.bounds)
}
private func drawGraphInContext(context: CGContext?, rect: CGRect) -> () {
let path = CGMutablePath()
for (i, v) in data.enumerated() {
let width = rect.width / CGFloat(data.count)
let height = rect.height * CGFloat(fabs(v) * 2.0)
let x = CGFloat(i) * width
let y: CGFloat = 0//v > 0 ? rect.height / 2 : rect.height / 2 - height
path.addRect(CGRect(x: x, y: y, width: width, height: height))
}
context?.setLineWidth(1.0)
context?.setFillColor(CGColor(red: 0.8, green: 0.0, blue: 0.0, alpha: 1.0))
context?.addPath(path)
context?.drawPath(using: .fillStroke)
}
}