The Android mpandroidcharts frame draws a histogram that can be viewed by sliding

Source: Internet
Author: User

1. It took 1.2 days to study the Mpandroidcharts framework because of the company's project needs. But found a lot of blogs to me have no help, waste a lot of time on Baidu, have to say Google is really stronger than Baidu too much.

Requirements: Statistics on the number of 56 family members

The original author's Github:https://github.com/philjay/mpandroidchart

Mpandroidcharts API Address: https://jitpack.io/com/github/PhilJay/MPAndroidChart/v2.2.5/javadoc/


2. The frame used is mpandroidcharts. The dependencies introduced:

And then in the introduction warehouse



3. Formal commencement of Use

XML file:

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    xmlns:tools=" Http://schemas.android.com/tools "    android:id=" @+id/activity_main "    android: Layout_width= "Match_parent"    android:layout_height= "match_parent"    tools:context= " Aas.androidmpcharts.MainActivity ">    <com.github.mikephil.charting.charts.barchart        android:id=" @+ Id/mbarchart "        android:layout_width=" match_parent "        android:layout_height=" 300DP "        /></ Relativelayout>


Mainactivity Code:

public class Mainactivity extends appcompatactivity {barchart mbarchart;    Arraylist<string> mlist=new arraylist<string> ();        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Mbarchart = (barchart) Findviewbyid (R.id.mbarchart);     Mbarchart.setdrawbarshadow (FALSE);        Table Do not shadow Mbarchart.setdrawvalueabovebar (true);        Description description=new Description ();        Description.settext ("National People");  Mbarchart.setdescription (description);        Table description Information Mbarchart.setpinchzoom (FALSE); Mbarchart.setmaxvisiblevaluecount (60); The maximum number of displays.     More than 60 will no longer show mbarchart.setscaleenabled (false); Disable scaling mbarchart.setdragenabled (true);//Can be dragged mbarchart.sethighlightperdragenabled (true);//Drag over the icon when drawing the canvas is highlighted Mbarchart.setdrawgridbackground (false);//////* mbarchart.setautoscaleminmaxenabled (TRUE); */* Mbarchar T.aniMateX (500);//data display animation, from left to right display */* mbarchart.getaxisright (). setenabled (FALSE); *//*mbarchart.setdragdecelerati Onenabled (true); *///drag-and-drop scrolling, the hand will continue to scroll, the default is True (False is dragged to where, true after the drag will be buffered) Mbarchart.zoom (2.5f,1f,0,0);//Display is Zoom in by how much to display 1f means not zoom out//My default phone screen shows 10 remaining slide histogram and then displays: If you want to show 25, then divide by 10 to enlarge 2.5f.    similarly//56 nationalities so enlarge 5.6f Draw ();        } public void Draw () {//x axis style final Xaxis Xaxis = Mbarchart.getxaxis ();        Xaxis.setposition (XAxis.XAxisPosition.BOTTOM);        Xaxis.setlabelrotationangle (90);//The following description of the column is rotated 90 degrees xaxis.setdrawlabels (true);        Xaxis.setdrawgridlines (FALSE); Xaxis.settypeface (Typeface.createfromasset (Getassets (), "Opensans-light.ttf"));//font-related Settings xaxis.setgranularity (        1f);//Set the minimum interval to prevent duplicate labels from appearing when zoomed in. Xaxis.setcenteraxislabels (TRUE);//The label below the font appears in the middle of each histogram xaxis.setlabelcount (11,true);//An interface displays 10 lable.        So here to set 11 xaxis.settextsize (10f); Y-axis style yaxis Leftaxis = MbarchArt.getaxisleft ();        Leftaxis.setlabelcount (10);        Leftaxis.setposition (YAxis.YAxisLabelPosition.OUTSIDE_CHART);        Leftaxis.setspacetop (15f);        Leftaxis.setstartatzero (FALSE);        Leftaxis.setyoffset (10f);        This replaces Setstartatzero (true) Leftaxis.setaxismaxvalue (50f);        Leftaxis.setaxisminvalue (0f); Leftaxis.setdrawgridlines (TRUE);//Background Line Leftaxis.setaxislinecolor (Getresources (). GetColor (        R.color.colorprimarydark));        . Set the display of the scale icon hidden Legend l = mbarchart.getlegend ();        L.setverticalalignment (Legend.LegendVerticalAlignment.TOP);        L.sethorizontalalignment (Legend.LegendHorizontalAlignment.LEFT);        L.setorientation (Legend.LegendOrientation.HORIZONTAL);        L.setdrawinside (FALSE);        Style L.setform (Legend.LegendForm.CIRCLE);        Font l.setformsize (10f);        Size l.settextsize (13f);        L.setformtotextspace (10f);        L.setxentryspace (10f); Analog Data Arraylist<barentry> yVals1 = new arraylist<barentry> ();        Yvals1.add (New Barentry (1,23));        Yvals1.add (New Barentry (2, 20));        Yvals1.add (New Barentry (3, 30));        Yvals1.add (New Barentry (4, 10));        Yvals1.add (New Barentry (5, 45));        Yvals1.add (New Barentry (6, 50));        Yvals1.add (New Barentry (7, 35));        Yvals1.add (New Barentry (8, 26));        Yvals1.add (New Barentry (9, 14));        Yvals1.add (New Barentry (10, 20));        Yvals1.add (New Barentry (11, 33));        Yvals1.add (New Barentry (12, 44));        Yvals1.add (New Barentry (13, 42));        Yvals1.add (New Barentry (14, 41));        Yvals1.add (New Barentry (15, 12));        Yvals1.add (New Barentry (16, 31));        Yvals1.add (New Barentry (17, 21));        Yvals1.add (New Barentry (18, 20));        Yvals1.add (New Barentry (19, 44));        Yvals1.add (New Barentry (20, 42));        Yvals1.add (New Barentry (21, 41));        Yvals1.add (New Barentry (22, 12));        Yvals1.add (New Barentry (23, 31)); Yvals1.add(New Barentry (24, 21));        Yvals1.add (New Barentry (25, 20));    SetData (YVALS1);        } private void SetData (ArrayList yVals1) {for (int i=1;i<=yvals1.size (); i++) {Mlist.add ("" +i);        } iaxisvalueformatter ix=new Myxaxisvalueformatter (mlist);        Mbarchart.getxaxis (). Setvalueformatter (ix);        Bardataset Set1; if (mbarchart.getdata () = null && mbarchart.getdata (). Getdatasetcount () > 0) {Set1            = (Bardataset) mbarchart.getdata (). Getdatasetbyindex (0);            Set1.setvalues (YVALS1);            Mbarchart.getdata (). notifydatachanged ();        Mbarchart.notifydatasetchanged ();            } else {Set1 = new Bardataset (YVALS1, "the year 2017");            Set1.setcolors (colortemplate.material_colors);            arraylist<ibardataset> dataSets = new arraylist<ibardataset> ();            Datasets.add (SET1);            BarData data = new BarData (dataSets); Data.setvalUetextsize (10f);            Data.setbarwidth (1f);        Mbarchart.setdata (data);        }} public class Myxaxisvalueformatter implements Iaxisvalueformatter {private list<string> mvalues;        Public Myxaxisvalueformatter (list<string> values) {this.mvalues = values;            } @Override Public String getformattedvalue (float value, axisbase axis) {int x= (int) (value);            if (x<0) x=0;            if (X>=mvalues.size ()) x=mvalues.size ()-1;        return Mvalues.get (x); }    }}

The final result:



In the process of doing some of the pits encountered said:

1 . xaxis.setaxismaximum (500f); Sets the maximum axis of the x-axis. At first I was set up like this: Then, when adding barentry, fill in the coordinates of the x-axis. But we finally found a bug .

Damn it. Tangled for a long time. Then see the original author's demo, found that the author did not call Xaxis.setaxismaximum (500f). The coordinates of each histogram are from 1, beginning with the number of backwards. Remember to be sure to start from 1
Begin.. You will experience problems starting with 0.
2. How to add tags to each histogram: The main use is  iaxisvalueformatter Rewrite
String getformattedvalue (value, Axisbase axis) obtains the label from this value: But I debug found that this value is not strictly 1
, 2,3,4,5, and so on in order to traverse. So I don't quite understand this approach. Look at the author's code also do not understand. Keng

Demo Address: http://download.csdn.net/detail/qq_25984015/9794208


The Android mpandroidcharts frame draws a histogram that can be viewed by sliding

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.