CorePlot 1.5.1中plotAreaViewPointForPlotPoint的用法

时间:2015-06-26 03:30:19

标签: ios swift core-plot

我尝试使用coreplot 1.5.1和swift将类似UIPopoverController的内容添加到barPlot中。像这个Core Plot: How to present popover from a bar selected by the user

所以我们需要知道所选栏的位置,但看起来有些函数不同,比如 plotAreaViewPointForPlotPoint 。在1.5.1中,有两个参数: enter image description here 我尝试找到使用它的正确方法但失败了,这是我的代码:

func barPlot(plot: CPTBarPlot!, barWasSelectedAtRecordIndex idx: UInt, withEvent event: UIEvent!) {
    // Remove all the annotations
    graphView.hostedGraph.plotAreaFrame.plotArea.removeAllAnnotations()

    // Setup a style for the annotation
    let hitAnnotationTextStyle      = CPTMutableTextStyle.textStyle() as! CPTMutableTextStyle
    hitAnnotationTextStyle.color    = CPTColor.whiteColor()
    hitAnnotationTextStyle.fontSize = 12.0;
    hitAnnotationTextStyle.fontName = FONT_HEITI;


    // Determine point of symbol in plot coordinates
    let anchorPoint = [Int(idx),0]
    // Add annotation
    // First make a string for the y value
    let string = "\(idx),values is:\(mArray.objectAtIndex(Int(idx)))"
    // Now add the annotation to the plot area
    let textLayer = CPTTextLayer(text: string, style: hitAnnotationTextStyle)

    // popview, DxPopover is something like UIPopover Controller in iPhone
    var popView = DXPopover()
    annotationLabel.text = string

    var pointers = [NSDecimal](count: 2, repeatedValue: CPTDecimalFromUnsignedInteger(0))
    pointers[0] = CPTDecimalFromUnsignedInteger(idx)

    var plotPointer: UnsafeMutablePointer<NSDecimal> = UnsafeMutablePointer<NSDecimal>.alloc(pointers.count)
    plotPointer.initializeFrom(pointers)

    var popPoint  = graphView.hostedGraph.defaultPlotSpace.plotAreaViewPointForPlotPoint(plotPointer, numberOfCoordinates: idx)
    popView.showAtPoint(popPoint, popoverPostion: DXPopoverPosition.Down, withContentView: self.annotationView, inView: graphView)

    // selectedBarAnnotation = CPTPlotSpaceAnnotation(plotSpace: graphView.hostedGraph.defaultPlotSpace, anchorPlotPoint: anchorPoint)
    // selectedBarAnnotation!.contentLayer = textLayer
    // selectedBarAnnotation!.displacement = CGPointMake(0.0, -15.0)
    // graphView.hostedGraph.plotAreaFrame.plotArea.addAnnotation(selectedBarAnnotation)

}

它会粉碎这条线:

var popPoint  = graphView.hostedGraph.defaultPlotSpace.plotAreaViewPointForPlotPoint(plotPointer, numberOfCoordinates: idx)

那么,我怎样才能获得正确的CGPOINT?

非常感谢!

================= EDIT ===================

我将我的代码更改为此,并且可以得到正确的答案,感谢@Bannings

var popView = DXPopover()
    annotationLabel.text = string

    var pointers = [NSDecimal](count: 2, repeatedValue: CPTDecimalFromUnsignedInteger(0))
    let plotXvalue = self.numberForPlot(plot, field: UInt(CPTScatterPlotFieldX.value), recordIndex: idx)
    pointers[0] = CPTDecimalFromFloat(plotXvalue.floatValue)
    println("\(CPTDecimalFromUnsignedInteger(idx))")

    let plotspace = graphView.hostedGraph.defaultPlotSpace
    println("\(plotspace.numberOfCoordinates)")

    var popPoint  = plotspace.plotAreaViewPointForPlotPoint(&pointers, numberOfCoordinates: plotspace.numberOfCoordinates)
    popView.showAtPoint(popPoint, popoverPostion: DXPopoverPosition.Down, withContentView: self.annotationView, inView: graphView)

1 个答案:

答案 0 :(得分:0)

试试这个:

var popPoint  = graphView.hostedGraph.defaultPlotSpace.plotAreaViewPointForPlotPoint(&pointers, numberOfCoordinates: idx)