Your code contains something unclear: loadFromJSON, someFunction and the type having the method someFunction.
And there is no sample data.
So, we readers cannot test it and hard to say something sure.
But some parts of your code are clearly bad to use CAShapeLayer.
You create only one CAShapeLayer with this line let shapeLayer = CAShapeLayer(),
but shapeLayer is not added as sublayer to any other layer in your code.
And, if you want multiple shape layers having different color each, you need to create multiple shape layers.
As already written, your code has many missing parts, so I have written a simplified example.
Please try this:
import AppKit
class AnalysisViewController: NSViewController {
@IBOutlet var analysisView: NSView!
func genrateGraph() {
for bar: (CGFloat, CGFloat, CGFloat, ClosedRange<CGFloat>) in [
(1.0, 0.0, 0.0, 0...100),
(0.0, 1.0, 0.0, 100...200),
(1.0, 1.0, 0.0, 200...300),
(0.0, 0.0, 1.0, 300...400),
(1.0, 0.0, 1.0, 400...500),
(0.0, 1.0, 1.0, 500...600),
(1.0, 1.0, 1.0, 600...700),
] {
let color = CGColor(srgbRed: bar.0, green: bar.1, blue: bar.2, alpha: 1.0)
let x = bar.3.lowerBound
let width = bar.3.upperBound - bar.3.lowerBound
//
//Create `CAShapeLayer` for each color
let layer = CAShapeLayer()
analysisView.layer?.addSublayer(layer)
let height: CGFloat = 320
let rect = CGRect(x: x, y: 0, width: width, height: height)
print(rect)
let path = CGMutablePath()
path.addRect(rect)
layer.path = path
layer.fillColor = color
}
}
override func viewDidLoad() {
super.viewDidLoad()
view.frame = CGRect(x: 0, y: 0, width: 840, height: 640)
analysisView.wantsLayer = true
analysisView.frame = CGRect(x: 0, y: 0, width: 840, height: 640)
genrateGraph()
}
}
If this code does show you a grey window, you need to check if you properly setup AnalysisViewController on your storyboard.
By the way,
To work with CAShapeLayer, using CGPath (including CGMutablePath) is the simpler way.
CGPath (or CGMutablePath) does not have a method stroke() and you have no need to call it. (In fact, you cannot!)