Search code examples
iosswiftdelegatesresearchkit

ResearchKit ORKLineGraphChartView Get data at touchpoint


I am trying to figure out how to detect the datapoint touched within a ORKLineGraphChartView so that I can take action on the datapoint selected, for example, more data for a date datapoint. I implemented the protocol ORKGraphChartViewDelegate, but can not find any functions on the delegate that return a touchpoint/datapoint for the chart. I can add a tap gesture to the chart view to get the X/Y of the touchpoint, but then I'm not quite sure how to get the datapoint for that location within the chart. Any help is appreciated.


Solution

  • For this, what I ended up doing was digging through the Objective-C classes and found a function that did exactly what I needed, but it hadn't been implemented in swift yet. So I created a bridging header in my application:

    Bridging Header import:

    #import "ORKLineGraphChartView+ORKLineGraphChartView_PointIndex.h"
    

    And then I created another file which I used to expose the Objective-C function on the class ORKLineGraphChartView:

    #import <ResearchKit/ResearchKit.h>
    
    @interface ORKLineGraphChartView (ORKLineGraphChartView_PointIndex)
        - (NSInteger)pointIndexForXPosition:(CGFloat)xPosition plotIndex:(NSInteger)plotIndex;
    @end
    

    This worked perfectly until hopefully swift contains all the functions from research kit. Hope this helps someone.