我正在尝试在表示各种数据点的图形视图上编写文本标签(想象一下Excel中的折线图)。
我使用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)
}
}
}
字符串
但是,我不知道如何在视图更新和呈现新数据集时删除数据标签。图形将重新绘制,但文本标签仍保留以前的数据集。
1条答案
按热度按时间n53p2ov01#
在函数的顶部添加以下代码已经解决了这个问题。
字符串