Rewrite the display tag of MPAndroidChart

Source: Internet
Author: User

Rewrite the display tag of MPAndroidChart

MPAndroidChart is an excellent control for chart functions and can meet most of the drawing requirements. for modifying third-party libraries, an excellent architecture is inheritance development, rather than splitting the source code. an exception occurs when displaying the MarkView control. As a result, the flag is not fully displayed at the edge of the chart, you need to override the control to solve the problem.

Inherit LineChart and extract coordinates of highlighted positionsgetHighLightPos, Redraw markdrawMarkers.

/*** Chart line chart of the data center, inheriting the line chart of MPChart *

* Created by wangchenlong on 15/10/13. */public class CYDataLineChart extends LineChart {@ SuppressWarnings ("unused") private static final String 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 contex T, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle);} // obtain the high-intensity coordinate public float [] getHighLightPos (Entry e, Highlight highlight) {return getMarkerPosition (e, highlight);} // rewrite this method to fix the Bug @ 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 (-80, 0); // make the rect larger canvas. clipRect (newRect, Region. op. REPLACE); // noinspection ForLoopReplaceableByForEach for (int I = 0; I <mIndicesToHighlight. length; I ++) {Highlight highlight = mIndicesToHighlight [I]; int xIndex = highlight. getXIndex (); if (xIndex <= mDeltaX & xIndex <= mDeltaX * mAnimator. getPhas EX () {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 float tmpY = pos [1]-8 * AppUtils. getPerDp (); Paint paint = new Paint (); paint. setStyle (Paint. style. FILL); paint. setAntiAlias (true); paint. setStrokeWidth (5); // noinspection deprecation paint. setColor (getResources (). getColor (R. color. chart_circle); canvas. drawCircle (pos [0], pos [1], 2 * AppUti Ls. 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) {float y = mMarkerView. getHeight ()-tmpY; mMarkerView. draw (canvas, pos [0], tmpY + y);} else {mMarkerView. draw (canvas, pos [0], tmpY );}}}}}

getMarkerPositionIs the protected method in the LineChart class. It inherits the class and exports it using the public method.
float tmpY = pos[1] - 8 * AppUtils.getPerDp();, Re-calculate the Y coordinate and deviate from the original canvas.

However, when moving MarkView, the parent control remains. How can this problem be solved? The method is to re-paint the canvas of the parent control when moving, and useinvalidate()Function.

// Set the chart click event to monitor the highlighted mLcChart. setOnChartValueSelectedListener (new OnChartValueSelectedListener () {@ Override public void onValueSelected (Entry e, int dataSetIndex, Highlight h) {int index = 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 (); // re-paint the parent control to avoid residue} @ Override public void onNothingSelected () {// call this when you click again, or do not highlight mLcChart. highlightValue (mIndex, 0 );}});
// Externally set the chart to highlight private void setChartHighlight (int index) {if (mLcChart. getData () = null) return; mMarkerView. setDateText (mMarkers. get (index); mLcChart. highlightValue (index, 0); mLlContainer. invalidate (); // re-paint the parent control to avoid residue}

In the chart control, the highlighted position is triggered externally.

OK, Enjoy It!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.