2016-10-11 24 views
0

我使用Core Plot文件來創建我的圖表。 但CorePlot不包含事件「點擊圖例項目」。Core Plot的可點擊圖例

如何檢測用戶點擊圖例項目?

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 
} 

回答

2

核心Plot圖例可以有一個委託,在鼠標向下,鼠標向上和/或被選中(鼠標向下,然後在同一個項目上鼠標向上)時調用。有關可用方法的詳細信息,請參見CPTLegendDelegate protocol。除非您需要某個未被委託人覆蓋的特定行爲,否則您不需要擔心響應者鏈。

+0

謝謝!有用 – Kirill

0

如果你深入研究的核心情節的源代碼,你可以按照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:(nonnull CPTNativeEvent *)事件atPoint:(CGPoint)interactionPoint;

我認爲你可以做的就是繼承CPTLegend並根據需要重寫一個或兩個方法來檢測點擊。

+0

pointingDeviceDownEvent和pointingDeviceUpEvent總是返回false。 如何檢測「圖例」的位置和大小?如果圖例會包含長詞,這個圖例會更長 – Kirill