我使用核心绘图文件来创建我的图表。但是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
}发布于 2016-10-12 00:39:13
核心情节传说可以有一个委托,它在鼠标向下、鼠标向上和/或被选中(鼠标向下,在同一项上鼠标向上)事件上调用。有关可用方法的详细信息,请参阅CPTLegendDelegate协议。除非您需要一些委托不包括的特定行为,否则您不需要担心响应者链。
发布于 2016-10-11 13:17:09
如果您深入研究核心图的源代码,您可以遵循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:(非空CPTNativeEvent *)事件atPoint:(CGPoint)interactionPoint;
-(BOOL)pointingDeviceUpEvent:(非空CPTNativeEvent *)事件atPoint:(CGPoint)interactionPoint;
我认为您可能可以做的是将CPTLegend子类化,并根据检测单击的需要覆盖一个或两个方法。
https://stackoverflow.com/questions/39977800
复制相似问题