Mpandroidchart is an excellent control that implements the charting function and can accomplish most of the drawing needs. For modifying third-party libraries, a good architecture is to inherit the development, rather than splitting out the source code. MP will have an exception when displaying the markup control (Markview), causing the flag to appear incomplete at the edge of the chart, and you will need to override the control to resolve the issue.
Inherits the Linechart, extracts the highlighted position coordinates getHighLightPos
, redraws the marker drawMarkers
.
/** * Data Center Chart Line chart, inherit Mpchart line chart * <p> * Created by Wangchenlong on 15/10/13. */ Public class cydatalinechart extends linechart { @SuppressWarnings("Unused")Private Static FinalString TAG ="DEBUG-WCL:"+ CYDataLineChart.class.getSimpleName ();//default constructor Public Cydatalinechart(Context context) {Super(context); } Public Cydatalinechart(context context, AttributeSet attrs) {Super(context, attrs); } Public Cydatalinechart(context context, AttributeSet attrs,intDefstyle) {Super(Context, attrs, Defstyle); }//Get high-brightness coordinates Public float[]Gethighlightpos(Entry E, Highlight Highlight) {returnGetmarkerposition (e, highlight); }//Override this method to fix bugs @Override protected void drawmarkers(Canvas canvas) {//If there is no marker view or drawing marker is disabled if(Mmarkerview = =NULL|| !mdrawmarkerviews | | !valuestohighlight ())return; Rect newrect = Canvas.getclipbounds (); Newrect.inset (- the,0);//make the rect largerCanvas.cliprect (Newrect, Region.Op.REPLACE);//noinspection Forloopreplaceablebyforeach for(inti =0; i < mindicestohighlight.length; i++) {Highlight Highlight = mindicestohighlight[i];intXindex = Highlight.getxindex ();if(Xindex <= mdeltax && xindex <= mdeltax * manimator.getphasex ()) {Entry E = mdata.getentryforhighlight (Mindicestohighlight[i]);//Make sure entry not NULL if(E = =NULL|| E.getxindex ()! = Mindicestohighlight[i].getxindex ())Continue;float[] pos = Getmarkerposition (e, highlight);//Marker offset floatTmpy = pos[1] -8* APPUTILS.GETPERDP (); Paint paint =NewPaint (); Paint.setstyle (Paint.Style.FILL); Paint.setantialias (true); Paint.setstrokewidth (5);//Noinspection deprecationPaint.setcolor (Getresources (). GetColor (r.color.chart_circle)); Canvas.drawcircle (pos[0], pos[1],2* APPUTILS.GETPERDP (), paint);//Check bounds if(!mviewporthandler.isinbounds (pos[0], Tmpy))Continue; Mmarkerview.refreshcontent (e, highlight); Mmarkerview.measure (Measurespec.makemeasurespec (0, measurespec.unspecified), Measurespec.makemeasurespec (0, measurespec.unspecified)); Mmarkerview.layout (0,0, Mmarkerview.getmeasuredwidth (), Mmarkerview.getmeasuredheight ());if(Tmpy-mmarkerview.getheight () <=0) {floaty = mmarkerview.getheight ()-tmpy; Mmarkerview.draw (Canvas, pos[0], tmpy + y); }Else{Mmarkerview.draw (canvas, pos[0], tmpy); } } } }}
getMarkerPosition
is the protected method in the Linechart class, inheriting the class and exporting using the public method.
float tmpY = pos[1] - 8 * AppUtils.getPerDp();
, recalculate the y-coordinate, and deviate from the original canvas.
However, there is a problem with the parent control remaining when moving Markview. How to solve it? The way to do this is to redraw the parent control's canvas and use the function when moving invalidate()
.
//Set Chart Click event, monitor highlight positionMlcchart.setonchartvalueselectedlistener (NewOnchartvalueselectedlistener () {@Override Public void onvalueselected(Entry E,intDatasetindex, Highlight h) {intindex = E.getxindex (); LOG.E (TAG,"index ="+ index); Setchartindex (index); Mcallback.setcurindex (index); Mindex = index;float[] pos = Mlcchart.gethighlightpos (E, h); LOG.E (TAG,"x:"+ pos[0] +", Y:"+ pos[1]); Mllcontainer.invalidate ();//Redraw the parent control to avoid residue}@Override Public void onnothingselected() {//Call this when you click again, or not highlightMlcchart.highlightvalue (Mindex,0); } });
// 外部设置图表高亮 privatevoidsetChartHighlight(int index) { ifnullreturn; mMarkerView.setDateText(mMarkers.get(index)); 0); // 重绘父控件, 避免残留 }
In a chart control, a highlighted position is triggered inside and outside.
OK, Enjoy it!
overriding Mpandroidchart Display markup