Mpchartandroid Use explanation

Source: Internet
Author: User

<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " > The most of the project to add more complex chart, really do not want to go to their own realization, want to find a third-party open source Chart library to achieve, previously encapsulated Achartenginee this Apache graphics engine, but after seeing this mpchartandroid, feel The achartengine is simply a weak explosion. Let's compare the advantages of Mpchartenginee and its use. </span>

Mpchartenginee Advantages: 1. More beautiful appearance, from the official to the respective two demo can be seen, mpchartandroid painted chart more soft, beautiful.

                                            2. More Perfect event conflict handling, used Achartenginee know, Achartenginee in the event of conflict processing is really not perfect, Achartenginee chart to                             &N Bsp                  scrollview will cause the scrollview to slip, and Mpchartenginee does not. The official demo has a chart in the ListView, with ScrollView                                                   in the chart, it can be said that the handling of events can be considered perfect.

3. More powerful customization, when learning and using Achartenginee, found that the chart has a lot of properties is not provided by the set method to set, resulting in many cases, do not conform to the chart The actual requirements, the style is particularly ugly, and mpchartenginee almost all of the visible properties are provided with a set method, you can easily customize the appearance of the chart, which is also my use of this framework The main reason.

4. Powerful animated effects can add a lot to our app. (There is a little bug, not a required).


Based on the above points, we can later abandon the Achartenginee and other chart frames, and all use Mpchartenginee. Below on my understanding of the framework source code, first explain the source of the composition of the structure. Look at the source code structure diagram:

Source structure, we just focus on the chart package, components, Data,listener package and animation package can be. Among them, the chart package defines all the types of charts that can be drawn, mainly for drawing the overall properties of the chart, defining event handling, and so on, which defines the different data types required for different charts in the data package, which defines all chart-related component definitions in the package. The listener defines the interaction with the user's events.

Official Demo Explanation:

Package Com.xxmassdeveloper.mpchartexample;public class LineChartActivity1 extends Demobase{private Linechart Mchart; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (    R.layout.activity_linechart); Mchart = (Linechart) Findviewbyid (R.ID.CHART1); Mchart.setdescription (""); Set the chart description information mchart.setscaleenabled (FALSE); Sets whether the chart can be scaled Mchart.setbackgroundcolor (color.white);  Sets the chart background color mchart.setdrawgridbackground (false);     Sets whether to show table Xaxis Xaxis = Mchart.getxaxis ();  Get the X-axis instance of the chart xaxis.setposition (xaxisposition.bottom);//Set the x-axis display position xaxis.setdrawgridlines (false); Sets whether to display the X-axis table xaxis.setavoidfirstlastclipping (TRUE);           Set X-axis start and end point label does not exceed screen xaxis.setdrawaxisline (true); Set display x-axis xaxis.setspacebetweenlabels (0);  Set X axis label not spaced yaxis Leftaxis = Mchart.getaxisleft (); Get the left y-axis instance of the chart leftaxis.setaxismaxvalue (3.4482f); Sets the y-axis maximum value leftaxis.setaxisminvalue (3.1872f);//sets the y-axis minimum value.   Leftaxis.setstartatzero (FALSE); Set the beginning of the chart starting from 0 leftaxis.enablegriddashedline (10f, 10f, 0f); Set the horizontal table as dashed//limit lines is drawn behind data (and not on top) Leftaxis.setdrawlimitlinesbehinddata (True ); Mchart.getaxisright (). setenabled (false); The right y-axis is not available (you can get the right y-axis instance to get to the left y-axis)//YAxis Rightaxis = Mchart.getaxisright ();
                Rightaxis.settextsize ();
                Rightaxis.settextcolor (); A series of settings
                Add data
SetData (7, 3);//Mchart.setvisiblexrange (5, axisdependency.left);//Mchart.setvisibleyrangemaximum (5f, Axisdependency.left);//dont forget to refresh the Drawingmchart.invalidate ();} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.line, menu); return true;} @Overridepublic boolean onoptionsitemselected (MenuItem item) {switch (Item.getitemid ()) {case R.id.actiontogglevalues : {for (dataset<?> set:mChart.getData (). Getdatasets ()) Set.setdrawvalues (!set.isdrawvaluesenabled ()); Mchart.invalidate (); break;} Case R.id.actiontogglehighlight:{if (mchart.ishighlightenabled ()) mchart.sethighlightenabled (false); else Mchart.sethighlightenabled (True); Mchart.invalidate (); break;} Case R.id.actiontogglefilled:{arraylist<linedataset> sets = (arraylist<linedataset>) mChart.getData (). Getdatasets (); for (Linedataset set:sets) {if (set.isdrawfilledenabled ()) set.setdrawfilled (false); else Set.setdrawfilled (TRUE);} Mchart.invalidate (); break;} Case R.id.actiontogglecircles:{arraylist<linedataset> sets = (arraylist<linedataset>) mchart.getdata (). GetDataSets (); for ( Linedataset set:sets) {if (set.isdrawcirclesenabled ()) Set.setdrawcircles (false); else Set.setdrawcircles (true);} Mchart.invalidate (); break;} Case R.id.actiontogglecubic:{arraylist<linedataset> sets = (arraylist<linedataset>) mChart.getData (). Getdatasets (); for (Linedataset set:sets) {if (set.isdrawcubicenabled ()) Set.setdrawcubic (false); else Set.setdrawcubic (TRUE);} Mchart.invalidate (); break;} Case R.id.actiontogglestartzero:{mchart.getaxisleft (). Setstartatzero (!mchart.getaxisleft (). isStartAtZeroEnabled ()); Mchart.getaxisright (). Setstartatzero (!mchart.getaxisright (). isstartatzeroenabled ()); Mchart.invalidate (); break;} Case R.id.actiontogglepinch:{if (mchart.ispinchzoomenabled ()) Mchart.setpinchzoom (false); else Mchart.setpinchzoom ( true); Mchart.invalidate (); break;} Case r.id.actiontoggleautoscaleminmax:{mchart.setautoscaleminmaxenabled (!mchart.isautoscaleminmaxenabled ()); McharT.notifydatasetchanged (); break;} Case R.id.animatex:{mchart.animatex (n); Case R.id.animatey:{mchart.animatey (Easing.EasingOption.EaseInCubic); Case R.id.animatexy:{mchart.animatexy (n.); Case r.id.actiontogglefilter:{//The angle of filtering is 35°approximator a = new Approximator (approximatortype.douglas_ Peucker), if (!mchart.isfilteringenabled ()) {mchart.enablefiltering (a);} Else{mchart.disablefiltering ();} Mchart.invalidate ();////for (int i = 0; i < i++) {//Mchart.addentry (New Entry ((float) (Math.random () *),//i+2 ), 0);//Mchart.invalidate ();//}////Toast.maketext (Getapplicationcontext (), "Valcount:" +//mchart.getdataoriginal ( ). Getyvalcount () + ", Valsum:" +//mchart.getdataoriginal (). Getyvaluesum (),//Toast.length_short). Show ();//break;} Case R.id.actionsave:{if (Mchart.savetopath ("title" + System.currenttimemillis (), "")) {Toast.maketext ( Getapplicationcontext (), "Saving successful!", Toast.length_short). Show (); Else Toast.maketext (gEtapplicationcontext (), "Saving failed!", Toast.length_short). Show ();//Mchart.savetogallery ("title" + System.currenttimemillis ()) break;}} return true;} private void SetData (int count, float range) {arraylist<string> xvals = new arraylist<string> (); arraylist<entry> yvals = new arraylist<entry> (); for (int i = 0; i < count; i++) {Xvals.add ((i% 30) + "/" + (i) + "/14"), Yvals.add (New Entry ((float) (Math.random () * 4), i)); Create a dataset and give it a typelinedataset Set1 = new Linedataset (yvals, "DataSet 1"); Set1.setlinewidth (1.5f); Set1. Setcirclesize (4f); Set1.setcirclecolor (Color.parsecolor ("#fd4634"));//Create a data object with the Datasetslinedata data = new Linedata (xvals, Set1);//Set Datamchart.setdata (data);}}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Mpchartandroid Use explanation

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.