I have a subclassed NSView drawing column graphs in a portion of another NSView object.
The drawRect() function below draws the columns based on passed parameters correctly.
However, when I adjust the parameters and update the view (through myView.needDisplay = true) the next version of the columns draw over the top of the previous versions without removing them.
How do I remove the existing columns to redraw the next set? I was expecting the
backgroundColor.set()
NSBezierPath.fillRect(bounds)
portion of the code to overwrite the columns on each call to drawRect() - from the custom NSView.
override func drawRect(dirtyRect: NSRect) {
backgroundColor.set()
NSBezierPath.fillRect(bounds)
println("Drawn")
if checkBoxCurrentMonth == true {
for object in columnDetails{
drawChartColumn(bounds.size, parameters: object)
}
}
}
The answer here -> Clear NSView programmatically in swift
doesn't work as I don't have an NSWindow object.
The two images below show excerpts from the storyboard and the runtime output showing the view hierarchy and that the graphs plot ok.
Other relevant items of code might be:
The call from the viewController to an instance of the custom NSView object (GraphView in the storyboard).
// Call the view methods to draw the new columns
graphView.confirmCurrentMonth()
graphView.determineColumnColor(currentMonthColor)
graphView.determineColumnParameters(columnDetailsArray)
graphView.needsDisplay = true
Two of the relevant functions from the GraphView():
func drawChartColumn(size: CGSize, parameters: (columnHeight: CGFloat, columnTotalNumber: CGFloat, thisColumnNumber: CGFloat)) {
let (colHeight, columnFrame) = determineColumnSize(size, columnTotalNumber: parameters.columnTotalNumber, columnHeight: parameters.columnHeight, thisColumn: parameters.thisColumnNumber)
columnColor.set()
NSBezierPath(rect: columnFrame).fill()
}
func determineColumnSize(size: CGSize, columnTotalNumber: CGFloat, columnHeight: CGFloat, thisColumn: CGFloat) ->(columnHeight: CGFloat, columnFrame: CGRect) {
// determine total size of the graphview
let graphHeight = size.height / 2
let graphWidth = size.width - graphOrigin.x - rightMargin
let columnAndGapSize = graphWidth / 25
let columnX: CGFloat = (graphOrigin.x + columnAndGapSize) + (columnAndGapSize * thisColumn) * 2
let columnY: CGFloat = (graphOrigin.y) + columnHeight * (graphHeight - topMargin - bottomMargin)
// determine the drawing bounds and the column frame assuming that the full bounds are used (ie no padding)
let drawingBounds = CGRectMake(columnX, graphOrigin.y, columnAndGapSize, columnY)//= CGRect(x: columnX , y: graphOrigin.y, width: columnAndGapSize , height: columnY)
let columnFrame = drawingBounds
println("\(columnHeight) is the columnHeight. \(columnFrame) is the columnFrame.")
return (columnHeight, columnFrame)
}