Search code examples
androidchartsxamarinshinobi

Programmatically show CrossHair in Shinobi Charts for Android (Xamarin)


I'm trying to display the crosshairs on a Shinobi Chart programmatically (via a separate button) on Android and can't figure a way to. I've seen the iOS way which basically involves calling moveToFloatingPosition on the chart.Crosshair.

The closest I've come is calling Focus on Chart.Crosshair, but IsActive and IsShown are false until I long-press on the chart. I'm assuming this is because TrackedSeries is null, so maybe there's a way to set the TrackedSeries programmatically?

shinobiChart.Crosshair.Style.LineColor = Color.Fuchsia.ToAndroid();
shinobiChart.Crosshair.CrosshairMode = Crosshair.Mode.SingleSeries;
lineSeries.CrosshairEnabled = true;
shinobiChart.Crosshair.Focus = lineSeries.DataAdapter.Get(500);  // arbitrary DataPoint - it's in the middle of the chart
shinobiChart.RedrawChart();

Solution

  • In the Android version of shinobicharts the API doesn't allow for programmatically displaying the Crosshair - this has to be done via a user gesture (specifically a long press).

    What you can do is take advantage of the MotionEvent API in the Android SDK. This allows you to programmatically create user gestures and so you can "perform" a long press on the chart in order to display the Crosshair.

    You use the obtain() method to create the MotionEvents - there are a number of overloads of this method so take a look at the docs to see which is the most appropriate to your needs, but the one linked above is the simplest.

    Additionally, you can use the conversion methods on the Axis class to go from data values to pixel values, which might help in making sure your MotionEvent is performed directly on the plot area. See Axis.getPixelValueForUserValue(). Note, this method will only return a valid result after the chart has been laid out (but as you're doing this from a button press that shouldn't be a problem).

    I appreciate this is a bit fiddly; you'll have to manage the "touch gestures" and make sure you're not permanently "pressing down" on the screen but on the whole it's pretty straight forward. Programmatic control of the Crosshair is definitely something that has been requested before and as such is on our product roadmap though at this stage I'm not able to say when this feature will be available.

    Full disclosure - I work for shinobicontrols