android - aChartEngine; getting SeriesSelection from within onTouchListener -
i'm creating line chart, using xyseries, etc. working great, except i'm finding impossible graphicalview.getcurrentseriesandpoint() within callbacks of ontouchllistener. have both , onclick , ontouch on chart, need handle other gestures data point onclicks.
when use onclicklistener, works expected; calls chartview.getcurrentseries... returns seriesselection. however, once set ontouchelistener, breaks.
i started standard means of mixing two, maintain touch state within ontouchlistener, , when resolves motionevent.action_up, if determine it's click, invoke performclick on passed in view, chart/graphicalview, results in null return getcurrentseriesandpoint call. here's code; nothing special going on here:
case motionevent.action_up: if (onclick) { log.d(tag, "actionup onclick should clicking, " + "and mcurrentss: " + mcurrentseriesselection); seriesselection ss = ((graphicalview)v).getcurrentseriesandpoint(); //always null if (ss == null) { log.d(tag, "no point clicked"); } else { log.d(tag, "handle overlays on point: " + ss); mlistener.userdidclickpoint(ss); } return true; }...
ss null. tried call on reference have chart/graph...view directly, , still null. next idea making call during ontouch's motionevent.action_down block, , tried both direct call on view, , performclick on view see if grab ss there, this:
case motionevent.action_down: onclick = true; //mcurrentseriesselection var hang onto results; ends null mcurrentseriesselection = mchart.getcurrentseriesandpoint(); //this doesn't work either, call in onclick return null v.performclick(); log.d(tag, "the current series selection in ontouch action down: " + mcurrentseriesselection); break;
now thoroughly confused, search more knowledge, , come across wonderful post, involves getting current series, , iterating on each of it's points, comparing view position coordinate (while accounting point size set on renderer, even), wrap on method of own:
private seriesdatapoint seriespointforclickedviewwithmotionevent(view v, motionevent event) { seriesdatapoint sdp = null; seriesselection ss = null; point screencoordinate = null; (int = 0; < mseries.getitemcount(); i++) { log.d(tag, "mseries.getx i: " + mseries.getx(i)); log.d(tag, "mseries.gety i: " + mseries.gety(i)); double[] xy = mxychart.toscreenpoint(new double[]{mseries.getx(i), mseries.gety(i)}, 0); double dx = (xy[0] - event.getx()); double dy = (xy[1] - event.gety()); double distance = math.sqrt(dx * dx + dy * dy); if (distance <= 2 * mrenderer.getpointsize()) { screencoordinate = new point((float) xy[0], (float) xy[1]); ss = mxychart.getseriesandpointforscreencoordinate(screencoordinate); sdp = new seriesdatapoint(ss, screencoordinate); break; } } return sdp; }
(where seriesdatapoint simple class i'm using pass stuff need caller; ignore it) problem is:
double[] xy = mxychart.toscreenpoint(new double[]{mseries.getx(i), mseries.gety(i)}, 0);
crashed npe, though mxychart, , mseries , not null , valid. total bummer...
so, i'm out of ideas on how accomplish need do, getting seriesselection corresponding clicked chart point. thanks.
my error thinking referencing same line chart graphical view using, , not. ended redoing way instantiate graphicalview, using (context, abstractchart) constructor, , passing in linechart instance. once done, problem line:
double[] xy = mxychart.toscreenpoint(new double[]{mseries.getx(i), mseries.gety(i)}, 0);
no longer crashed, , able determine series point clicked within ontouch handling. so, in short, have make sure linechart you're calling method on in fact 1 being rendered part of constructing graphicalview. know that's been mentioned elsewhere, didn't connect @ first, so, reiterating here. thanks.
Comments
Post a Comment