我使用Core Plot文件来创建我的图表。 但CorePlot并不包含事件"点击图例项目"。
如何检测用户点击图例项?
class ViewController: NSViewController, CPTPlotDataSource, CPTPieChartDataSource {
@IBOutlet weak var graphView: CPTGraphHostingView!
override func viewDidLoad() {
super.viewDidLoad()
let graph = CPTXYGraph(frame: CGRect.zero)
var axes = graph.axisSet as! CPTXYAxisSet
var lineStyle = CPTMutableLineStyle()
lineStyle.lineWidth = 0
axes.xAxis?.axisLineStyle = lineStyle
axes.yAxis?.axisLineStyle = lineStyle
var pie = CPTPieChart()
pie.pieRadius = 100
pie.dataSource = self
pie.centerAnchor = CGPoint(x: 0.18, y: 0.75)
graph.add(pie)
var theLegend=CPTLegend(graph: graph)
var legendFill=CPTFill(color: CPTColor.gray())
theLegend.fill = legendFill
var legendLineStyle = CPTMutableLineStyle()
legendLineStyle.lineColor = CPTColor.white()
theLegend.borderLineStyle = legendLineStyle
theLegend.cornerRadius = 2.0
theLegend.frame = CGRect(x: 0, y: 0, width: 0.1, height: 50)
theLegend.numberOfColumns = 1
graph.legend = theLegend
graph.legendDisplacement = CGPoint(x: 0.5, y: 300)
self.graphView.hostedGraph = graph
}
答案 0 :(得分:2)
Core Plot图例可以有一个委托,在鼠标按下,鼠标按下和/或选中(鼠标按下,然后鼠标按下同一项目)事件时调用该委托。有关可用方法的详细信息,请参阅CPTLegendDelegate protocol。除非您需要代理未涵盖的某些特定行为,否则您无需担心响应者链。
答案 1 :(得分:0)
如果你深入研究Core Plot的源代码,你可以跟随CPTLegend的继承链一直到CPTLayer:
https://github.com/core-plot/core-plot/blob/master/framework/Source/CPTLegend.h https://github.com/core-plot/core-plot/blob/master/framework/Source/CPTBorderedLayer.h https://github.com/core-plot/core-plot/blob/master/framework/Source/CPTAnnotationHostLayer.h https://github.com/core-plot/core-plot/blob/master/framework/Source/CPTLayer.h
如果您检查CPTLayer的头文件,它符合CPTResponder,如果您转到CPTResponder的源:
https://github.com/core-plot/core-plot/blob/master/framework/Source/CPTResponder.h
您会注意到其他两种相关方法:
- (BOOL)pointingDeviceDownEvent :( nonnull CPTNativeEvent *)事件atPoint:(CGPoint)interactionPoint;
- (BOOL)pointingDeviceUpEvent :( nonnull CPTNativeEvent *)事件atPoint:(CGPoint)interactionPoint;
我认为您可以做的是将CPTLegend子类化并覆盖一种或两种方法,具体取决于您检测点击的需求。