Usage for plotAreaViewPointForPlotPoint in CorePlot 1.5.1

173 views Asked by At

I'm trying to add something like UIPopoverController into the barPlot with coreplot 1.5.1 and swift. Like this one Core Plot: How to present popover from a bar selected by the user

So we need to know the point where the selected bar is ,But looks like some functions are different, like plotAreaViewPointForPlotPoint. In 1.5.1, there are two parameters: enter image description here And I'm try to find the right way to use it but failed, here is my code:

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)

}

And it will crush at this line:

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

SO, HOW CAN I GET THE RIGHT CGPOINT?

Thanks very much!

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

I change my codes to this, and can get the right point, Thanks to @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

There are 1 answers

0
Bannings On BEST ANSWER

Try this:

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