50 lines
2.2 KiB
Swift
50 lines
2.2 KiB
Swift
//
|
|
// LossDistributionPie.swift
|
|
// Graphic Analysis 2
|
|
//
|
|
// Created by Kilian Hofmann on 16.06.17.
|
|
// Copyright © 2017 Kilian Hofmann. All rights reserved.
|
|
//
|
|
|
|
import Cocoa
|
|
|
|
class LossDistributionPie: NSView {
|
|
|
|
var distribution: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
|
|
|
override func draw(_ dirtyRect: NSRect) {
|
|
super.draw(dirtyRect)
|
|
let center: CGPoint = CGPoint(x: 18.5, y: 18.5)
|
|
let startAngle: CGFloat = (1/2) * CGFloat.pi
|
|
let increment: CGFloat = CGFloat.pi / 6
|
|
for i in 0..<12 {
|
|
if NSGraphicsContext.currentContextDrawingToScreen() {
|
|
let layer: CAShapeLayer = CAShapeLayer()
|
|
layer.fillColor = CGColor(red: 1, green: 0, blue: 0, alpha: CGFloat(distribution[i]) / 60)
|
|
layer.strokeColor = NSColor.controlShadowColor.cgColor
|
|
layer.lineWidth = 0.5
|
|
layer.frame = dirtyRect
|
|
let arc: CGMutablePath = CGMutablePath()
|
|
arc.move(to: center)
|
|
arc.addArc(center: center, radius: 18, startAngle: startAngle - CGFloat(i)*increment, endAngle: startAngle - CGFloat(i+1)*increment, clockwise: true)
|
|
arc.closeSubpath()
|
|
layer.path = arc
|
|
if self.layer?.sublayers?.count == 12 {
|
|
self.layer?.replaceSublayer((self.layer?.sublayers?[i])!, with: layer)
|
|
} else{
|
|
self.layer?.addSublayer(layer)
|
|
}
|
|
} else {
|
|
let arc: CGMutablePath = CGMutablePath()
|
|
arc.move(to: center)
|
|
arc.addArc(center: center, radius: 18, startAngle: startAngle - CGFloat(i)*increment, endAngle: startAngle - CGFloat(i+1)*increment, clockwise: true)
|
|
arc.closeSubpath()
|
|
NSGraphicsContext.current!.cgContext.addPath(arc)
|
|
NSGraphicsContext.current!.cgContext.setFillColor(CGColor(red: 1, green: 0, blue: 0, alpha: CGFloat(distribution[i]) / 60))
|
|
NSGraphicsContext.current!.cgContext.setStrokeColor(NSColor.controlShadowColor.cgColor)
|
|
NSGraphicsContext.current!.cgContext.drawPath(using: CGPathDrawingMode.fillStroke)
|
|
}
|
|
}
|
|
}
|
|
}
|