Android uses open source table framework Mpandroidchart

Source: Internet
Author: User

Address: Https://github.com/PhilJay/MPAndroidChart

1. Gradle Dependency (recommended)

    • Add the following to your project level build.gradle :
allprojects {    repositories {        maven {URL "Https://jitpack.io"}}}    

    • Add this to your app build.gradle :
dependencies {    implementation ' com.github.philjay:mpandroidchart:v3.0.3 '}

2. Create a Chartevent.java file

 Public classCharteventImplementsOnchartgesturelistener, Onchartvalueselectedlistener {Private  intSpecmount;//number of lanes to display    PrivateLinechart Mchart; Privatearraylist<entry> values =NewArraylist<entry>(); Private int[] specarray;  PublicChartevent (Linechart Wchart,intspecnum) {Specmount=Specnum; Specarray=New int[Specmount]; Mchart=Wchart; Mchart.setonchartgesturelistener ( This); Mchart.setonchartvalueselectedlistener ( This); Mchart.setdrawgridbackground (false); Mchart.getdescription (). SetText (""); Mchart.settouchenabled (true); Mchart.setdragenabled (true); Mchart.setscaleenabled (true); Mchart.setpinchzoom (true); Mchart.setdoubletaptozoomenabled (false); Mymarkerview MV=NewMymarkerview (Myapplication.getcontext (), R.layout.custom_marker_view); Mv.setchartview (Mchart); //For bounds controlMchart.setmarker (MV);//Set The marker to the chartXaxis Xaxis=Mchart.getxaxis ();        Xaxis.setposition (XAxis.XAxisPosition.BOTTOM); Xaxis.setvalueformatter (NewIaxisvalueformatter () {@Override PublicString GetFormattedValue (floatvalue, Axisbase axis) {                return((int) value) *3000/specmount+ "KeV";        }        }); YAxis Leftaxis=Mchart.getaxisleft (); Leftaxis.setaxisminimum (0); YAxis Rightaxis=mchart.getaxisright (); Rightaxis.setenabled (false);//Mchart.setbackgroundcolor (color.gray);SetData (); Mchart.animatex (100); Legend L=Mchart.getlegend ();    L.setform (Legend.LegendForm.LINE); }     Public voidUpdatechart (int[] specdata) {Specarray=Specdata;  for(inti=0;i<specmount;i++) {Values.set (I,NewEntry (i, specarray[i]));        } setData ();    Mchart.invalidate (); }    Private voidSetData () {Linedataset set1; if(Mchart.getdata ()! =NULL&&mchart.getdata (). Getdatasetcount ()> 0) {Set1= (Linedataset) mchart.getdata (). Getdatasetbyindex (0);            Set1.setvalues (values);            Mchart.getdata (). notifydatachanged ();        Mchart.notifydatasetchanged (); } Else {             for(inti = 0; i < Specmount; i++) {Values.add (NewEntry (i, 0)); }            //Create a dataset and give it a typeSet1 =NewLinedataset (values, "real-time spectral lines")); Set1.setdrawicons (false); //Set the line to being drawn like this "- -----"//set1.enabledashedline (10f, 0f, 0f);//set1.enabledashedhighlightline (10f, 0f, 0f);Set1.sethighlightcolor (color.red);            Set1.disabledashedline ();            Set1.setcolor (Color.Black);            Set1.setcirclecolor (Color.Black); Set1.setlinewidth (0.5f);            Set1.setcircleradius (1f); Set1.setdrawcirclehole (false);            Set1.setvaluetextsize (9f); Set1.setdrawfilled (true);            Set1.setformlinewidth (1f); Set1.setformlinedasheffect (NewDashpatheffect (New float[]{10f, 5f}, 0f]); Set1.setformsize (15. f); if(Utils.getsdkint () >= 18) {                //fill drawable only supported in API level and aboveDrawable drawable =contextcompat.getdrawable (Myapplication.getcontext (), r.drawable.fade_red);            Set1.setfilldrawable (drawable); }            Else{set1.setfillcolor (color.black); } ArrayList<ILineDataSet> dataSets =NewArraylist<ilinedataset>(); Datasets.add (SET1); //Add the Datasets//Create a data object with the datasetsLinedata data =NewLinedata (dataSets); //Set Datamchart.setdata (data); }} @Override Public voidOnchartgesturestart (motionevent me, Charttouchlistener.chartgesture lastperformedgesture) {} @Override  Public voidOnchartgestureend (motionevent me, Charttouchlistener.chartgesture lastperformedgesture) {} @Override Public voidonchartlongpressed (motionevent me) {} @Override Public voidonchartdoubletapped (motionevent me) {} @Override Public voidonchartsingletapped (motionevent me) {} @Override Public voidOnchartfling (motionevent me1, Motionevent me2,floatVelocityx,floatvelocityy) {} @Override Public voidOnchartscale (motionevent me,floatScaleX,floatScaleY) {} @Override Public voidOncharttranslate (motionevent me,floatDxfloatDY) {} @Override Public voidonvalueselected (Entry E, Highlight h) {} @Override Public voidonnothingselected () {}}

3. Create a Mymarkerview.java file

/*** Custom implementation of the Markerview. * *@authorPhilipp Jahoda*/ Public classMymarkerviewextendsMarkerview {PrivateTextView tvcontent;  PublicMymarkerview (Context context,intLayoutresource) {        Super(context, layoutresource); Tvcontent=(TextView) Findviewbyid (r.id.tvcontent); }    //callbacks Everytime the Markerview is redrawn, can be used to update the//content (User-interface)@Override Public voidrefreshcontent (Entry E, Highlight Highlight) {if(Einstanceofcandleentry) {candleentry CE=(Candleentry) e; Tvcontent.settext ("" + Utils.formatnumber (Ce.gethigh (), 0,true)); } Else{Tvcontent.settext ("" + Utils.formatnumber (e.gety (), 0,true)); }        Super. Refreshcontent (E, highlight); } @Override Publicmppointf GetOffset () {return Newmppointf (-(GetWidth ()/2),-getheight ()); }}

4. Create a Layout.custom_marker_view.xml file in the Layout folder

<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Wrap_content"Android:layout_height= "40DP"Android:background= "@drawable/marker2" >    <TextViewAndroid:id= "@+id/tvcontent"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_centerhorizontal= "true"Android:layout_margintop= "7DP"Android:layout_marginleft= "5DP"Android:layout_marginright= "5DP"Android:text=""android:textsize= "12DP"Android:textcolor= "@android: Color/white"android:ellipsize= "End"Android:singleline= "true"android:textappearance= "? Android:attr/textappearancesmall" /></Relativelayout>

5. In the drawable folder configuration, marker2.png pictures, fade_red.xml files, these two files can also be used, one is to click the Display icon prompt, one is the icon line color fill.

Android uses open source table framework Mpandroidchart

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.