1
我正在嘗試在代表各種數據點的圖形視圖(想象Excel中的線圖)上編寫文本標籤。在SWIFT OS X中以編程方式添加和刪除NSTextField
我使用tableView選擇一個數據集。每次選擇一個新的數據集時,圖形都會重新繪製。
我可以使用自定義視圖繪製線條圖。沒問題。
我可以使用)從drawRect中(稱爲下面的函數繪製文本標籤:
func drawDataPointLabels(size: CGSize) { //size is the view.bounds
var dataLabelsArray: [(NSColor, String, NSRect)] = []
// graphDetails contain an array of tuples [(),(),(),()] - this represents one graph type = eg Monthly Fees ** there will be a maximum of 12 items in the array
for graphDetails in graphDetailsArray {
// each dataPoint object is a tuple (graphType: columnLabel: columnColor: columnHeight: columnNumber:) representing one dataPoint
for dataPoint in graphDetails {
let graphHeight = size.height/2
let graphWidth = size.width - graphOrigin.x - rightMargin
let columnAndGapSize = graphWidth/25
let labelX: CGFloat = (graphOrigin.x + columnAndGapSize) + (columnAndGapSize * dataPoint.columnNumber) * 2
let labelY: CGFloat = dataPoint.columnHeight * (graphHeight - topMargin - bottomMargin)
// determine the location and frame for the text label to match dataPoint
let textFrame = NSRect(x: labelX, y: labelY, width: 30, height: 10)
let dataPointTuple = (dataPoint.columnColor, dataPoint.columnLabel, textFrame)
dataLabelsArray.append(dataPointTuple)
}
for dataPoint in dataLabelsArray {
let (color, label, frameRect) = dataPoint
let lblDataPoint = NSTextField(frame: frameRect)
lblDataPoint.stringValue = label
lblDataPoint.backgroundColor = backgroundColor
lblDataPoint.textColor = color
self.addSubview(lblDataPoint)
}
}
}
但是,我不能工作,如何當視圖更新刪除數據標籤和一個新的數據集呈現。圖形重新繪製,但文本標籤仍然來自先前的數據集。
任何指導/建議,將不勝感激。