【问题标题】:Clickable legend at Core PlotCore Plot 上的可点击图例
【发布时间】:2016-10-11 12:57:56
【问题描述】:

我使用 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
}

【问题讨论】:

    标签: swift macos core-plot legend clickable


    【解决方案1】:

    Core Plot 图例可以有一个在鼠标按下、鼠标向上和/或选中(鼠标按下,然后鼠标在同一项目上向上)事件时调用的委托。有关可用方法的详细信息,请参阅CPTLegendDelegate protocol。除非您需要代理未涵盖的某些特定行为,否则您无需担心响应者链。

    【讨论】:

      【解决方案2】:

      如果你深入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 *)event atPoint:(CGPoint)interactionPoint;

      -(BOOL)pointingDeviceUpEvent:(nonnull CPTNativeEvent *)event atPoint:(CGPoint)interactionPoint;

      我认为您可能可以做的是将 CPTLegend 子类化并根据您检测点击的需要覆盖一种或两种方法。

      【讨论】:

      • pointingDeviceDownEvent 和 pointingDeviceUpEvent 总是返回 false。如何检测“图例”位​​置和大小?如果图例包含长字,则此图例会更长
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多