Programmatically showing chart crosshair + tooltip


#1

Hi All,

I am trying to programatically show the crosshair and tooltip for the last datapoint in a chart after it loads. The below code is what I have so far. The crosshair shows in the right place, but the tooltip shows up way to the left and is too small to fit the text.

int num = [self.activeTrendsResult sChart:self.activeChart numberOfDataPointsForSeriesAtIndex:0];
    id<SChartData>data = [self.activeTrendsResult sChart:self.activeChart dataPointAtIndex:num-1 forSeriesAtIndex:0];
    double x = [[data sChartXValue] doubleValue];
    double y = [[data sChartYValue] doubleValue];
    SChartPoint p;
    p.x = x;
    p.y = y;
    
    [self.activeChart.crosshair moveToFloatingPosition:p onXAxis:self.activeChart.xAxis onYAxis:self.activeChart.yAxis];
    [self.activeChart.crosshair.tooltip setResolvedDataPoint:p fromSeries:self.activeChart.series[0] fromChart:self.activeChart];
    [self.activeChart.crosshair.tooltip setPosition:self.activeChart.crosshair.trackingPoint onCanvas:self.activeChart.canvas];

Any help would be much appreciated. 

Thanks!


#2

One other related question. Can anyone tell me if there is a way to make the crosshair appear on single tap, rather than tap and hold? I’m assuming that if I can get the above code working, I can just call it from the chart delegate did select point, or is there a better way?

Thanks


#3

Your first question can be answered here:


#4

To answer your second question, add the following category for SChartCanvasOverlay:

@interface SChartCanvasOverlay (CrosshairDuration)

- (void) changeLongPressDelay:(CFTimeInterval)time;

@end

@implementation SChartCanvasOverlay (LongPress)

- (void) changeLongPressDelay:(CFTimeInterval)time

{

    [longPressGesture setMinimumPressDuration:time];

}

@end

This exposes the long press gesture recognizer, and sets the minimum press duration. Now simply call changeLongPressDelay: on your canvas overlay and set the delay to 0. Bear in mind that once you do this you won’t be able to dismiss the crosshair with a tap, so if you want it to disappear you’ll have to remove it programatically:

[chart.crosshair removeCrosshair];

It is worth bearing in mind, that this is an internal implementation detail and it is not actually something we expose on our API, thus it might break in the future. Thought if it does, we’ll have probably added a way to do this onto our API.

Thanks,
Jan


#5

Hello, I can not find the SChartCanvasOverlay in version 2.6.0. Is there a new solution for the tooltip to open with a single tap?


#6

Add me to the list of folks who would like to see tooltips with a single tap.  I was able to find a way to make it work, but it was messy.


#7

Added you :slight_smile:


#8

I’m also interested in being able to show the crosshair with a single tap.


#9

Hi,

This should be fairly simple to accomplish if you’re using version 2.8.0 or later.

First of all we need to import a few header files that aren’t included in the ‘umbrella’ ShinobiChart.h header:

#import <ShinobiCharts/SChartCanvas.h>
#import <ShinobiCharts/SChartCanvasOverlay.h>

Then we create the UITapGestureRecognizer to call a method when a tap gesture is detected

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showCrosshair:)];

[chart.canvas.overlay addGestureRecognizer:tapGesture];

Finally, within that showCrosshair: method, we just need to convert get the user’s tap location relative to the chart and then show the crosshair.

- (void)showCrosshair:(UITapGestureRecognizer *)tapGestureRecognizer {
    CGPoint positionOfTap = [tapGestureRecognizer locationInView:chart];
    
    [chart.crosshair showAtPoint:positionOfTap inChart:chart];
}

I hope that helps  smiley lol

Thanks,

Sam


#10

@sburnstone I like your approach. But then the crosshair does not follow when zooming in/out for some reason. Do you (or anyone) know how to solve this?


#11

@yfujiki, sadly that’s a bug in ShinobiChart, I asked the same question here.

https://www.shinobicontrols.com/root/forum/shinobicontrols/2015/8/[chartcrosshair-showatpointinchart]-has-strange-1st-time-behavior/