0

Using shinobi charts

Looking for examples how to add gesture recognizers (on touch up) to Tick Marks and schart annoations

I see the documentation for interacting with a series data series, but I need to add GestureRecognizers to tick marks and annotation events

I tried this for the tickMark/datapoint labels with no luck:

 func sChart(chart: ShinobiChart!, alterTickMark tickMark: SChartTickMark!, beforeAddingToAxis axis: SChartAxis!) {

    if let label = tickMark.tickLabel {
        //added a gesture recognizer here but it didn't work
    }

For the SchartAnnotations no idea how to go about adding one there

MobileMon
  • 8,341
  • 5
  • 56
  • 75

1 Answers1

0

I think you're nearly there with labels. I found I just needed to set userInteractionEnabled = true e.g.

func sChart(chart: ShinobiChart!, alterTickMark tickMark: SChartTickMark!, beforeAddingToAxis axis: SChartAxis!) {
    if let label = tickMark.tickLabel {

        let tapRecognizer = UITapGestureRecognizer(target: self, action: "labelTapped")
        tapRecognizer.numberOfTapsRequired = 1
        label.addGestureRecognizer(tapRecognizer)

        label.userInteractionEnabled = true
    }
}

Annotations are a little trickier, as they're on a view below SChartCanvasOverlay (responsible for listening for gestures). This results in the gestures being 'swallowed' before they get to the annotation.

It is possible however, but you'll need to add a UITapGestureRecognizer to your chart and then loop through the chart's annotations to check whether the touch point was inside an annotation. E.g.:

In viewDidLoad:

let chartTapRecognizer = UITapGestureRecognizer(target: self, action: "annotationTapped:")
chartTapRecognizer.numberOfTapsRequired = 1
chart.addGestureRecognizer(chartTapRecognizer)

And then the annotationTapped function:

func annotationTapped(recognizer: UITapGestureRecognizer) {

    var touchPoint: CGPoint?

    // Grab the first annotation so we can grab its superview for later use
    if let firstAnnotation = chart.getAnnotations().first as? UIView {
        // Convert touch point to position on annotation's superview

        let glView = firstAnnotation.superview!

        touchPoint = recognizer.locationInView(glView)
    }

    if let touchPoint = touchPoint {

        // Loop through the annotations
        for item in chart.getAnnotations() {
            let annotation: SChartAnnotation = item as SChartAnnotation

            if (CGRectContainsPoint(annotation.frame, touchPoint as CGPoint)) {
                chart.removeAnnotation(annotation)
            }
        }
    }
}
sburnstone
  • 586
  • 3
  • 8
  • Works for tick marks, however doesn't work for SChartDataPointLabel. Trying to add it in this method with no luck: "func sChart(chart: ShinobiChart!, alterDataPointLabel label: SChartDataPointLabel!, forDataPoint dataPoint: SChartDataPoint!, inSeries series: SChartSeries!) {" – MobileMon Aug 06 '15 at 12:00