Android Statistics Chart Mpandroidchart

Source: Internet
Author: User
Tags polyline set background



Android Statistics Chart Mpandroidchart

Mpandroidchart is an open source third-party statistical Chart library on the Android platform that can draw a variety of complex and rich statistical charts, such as common line charts, pie charts, histogram, scatter charts, candle charts used in financial stocks, bubble chart, Radar-like statistics pie chart and so on. In short, Androidmpchart can basically meet the needs of daily statistical charting development on the Android platform.
Androidmpchart Project on GitHub home: Https://github.com/PhilJay/MPAndroidChart
In the Libs of your project, you can use Mpandroidchart to import its published jar packages. Mpandroidchart released Jar package page in: https://github.com/PhilJay/MPAndroidChart/releases
Androidmpchart How to use: Download the latest jar package from the releases page above and copy it to your project Libs.


Note: Writing this article is based on Mpandroidchart version: Mpandroidchartlibrary-2-1-3.jar


Now, to do a basic Androidmpchart line chart, line chart in the usual statistical chart of the most applied, Androidmpchart provides a wealth of features to support.

As follows:


The entire code for implementing the above statistical chart is given.

Mainactivity.java's Code:

Package Zhangphil.linechart;import Java.util.arraylist;import Com.github.mikephil.charting.charts.LineChart; Import Com.github.mikephil.charting.components.legend;import Com.github.mikephil.charting.components.legend.legendform;import Com.github.mikephil.charting.components.legend.legendposition;import Com.github.mikephil.charting.data.Entry; Import Com.github.mikephil.charting.data.linedata;import Com.github.mikephil.charting.data.linedataset;import Com.github.mikephil.charting.utils.valueformatter;import Android.support.v7.app.actionbaractivity;import Android.graphics.color;import Android.os.bundle;public class Mainactivity extends Actionbaractivity {@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); Linechart chart = (Linechart) Findviewbyid (R.id.chart);//production of 7 data points (along X axis) Linedata Mlinedata = Makelinedata (7); Setchartstyle (chart, Mlinedata, Color.White);} Sets the style of the chart display private void Setchartstyle (Linechart Mlinechart, Linedata linedata,int color) {//whether to add a border Mlinechart.setdrawborders (false) on a line chart; Mlinechart.setdescription ( "Description @zhangphil");//Data Description//If there is no data, it will show this, similar to the ListView emtpyviewmlinechart.setnodatatextdescription (" If the data passed to Mpandroidchart is empty, you will see this text. @Zhang Phil ");//whether to draw the background color. If Mlinechart.setdrawgridbackground (false),//Then Mlinechart.setgridbackgroundcolor (Color.cyan) will fail; Mlinechart.setdrawgridbackground (false); Mlinechart.setgridbackgroundcolor (Color.cyan);// Touch mlinechart.settouchenabled (TRUE);//Drag mlinechart.setdragenabled (TRUE);//Zoom mlinechart.setscaleenabled (TRUE); Mlinechart.setpinchzoom (false);//Set Background mlinechart.setbackgroundcolor (color);//Set the X, Y axis data mlinechart.setdata ( Linedata);//Set the scale graph indicator, which is the value of the group y legend mlegend = Mlinechart.getlegend (); Mlegend.setposition ( Legendposition.below_chart_center); Mlegend.setform (legendform.circle);//Style mlegend.setformsize (15.0f);// Font Mlegend.settextcolor (color.blue);//color//along x-axis animation, time 2000 milliseconds. Mlinechart.animatex (2000);} /** * @param count * data pointsThe quantity.  * @return */private linedata makelinedata (int count) {arraylist<string> x = new arraylist<string> (); for (int i = 0; I < count; i++) {//X-axis displays data X.add ("x:" + i);} Y-axis data arraylist<entry> y = new arraylist<entry> (); for (int i = 0; i < count; i++) {Float val = (float) (M Ath.random () * 100); Entry Entry = new Entry (val, i); Y.add (Entry);} Y-axis DataSet Linedataset Mlinedataset = new Linedataset (y, "test data set. by Zhangphil ");//Use the y-axis collection to set the parameter//line width mlinedataset.setlinewidth (3.0f);//Display the circular size mlinedataset.setcirclesize (5.0f);// The color of the polyline mlinedataset.setcolor (Color.dkgray);//The Color of the ball mlinedataset.setcirclecolor (color.green);// After setting Mlinedataset.setdrawhighlightindicators (false),//highlight cross line will not be displayed,//At the same time, Mlinedataset.sethighlightcolor (Color.cyan) failed. Mlinedataset.setdrawhighlightindicators (TRUE);//After hitting, the cross-line Color Mlinedataset.sethighlightcolor (Color.cyan);// Sets the font size of the data points displayed on this item. Mlinedataset.setvaluetextsize (10.0f);//Mlinedataset.setdrawcirclehole (TRUE);//change the line style, use the curve. Mlinedataset.setdrawCubic (TRUE);//The smoothness of the line//curve is the default, and the larger the value the smoother. Mlinedataset.setcubicintensity (0.2f);//The area below the fill curve, red, translucent. Mlinedataset.setdrawfilled (TRUE);//Mlinedataset.setfillalpha;//Mlinedataset.setfillcolor (Color.RED);// Fills the color of the data points on the polyline, the center space of the inside of the sphere. Mlinedataset.setcirclecolorhole (Color.yellow);//sets the format for displaying data on a polyline. If not set, the float data format is displayed by default. Mlinedataset.setvalueformatter (New Valueformatter () {@Overridepublic String getformattedvalue (float value) {int n = ( int) value; String s = "y:" + n;return s;}); arraylist<linedataset> mlinedatasets = new arraylist<linedataset> (); Mlinedatasets.add (MLineDataSet); Linedata mlinedata = new Linedata (x, mlinedatasets); return mlinedata;}}

Activity_main.xml required by Mainactivity.java:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent " ><com.github.mikephil.charting.charts.linechart        android:id= "@+id/chart"        android:layout_width= " Match_parent "        android:layout_height=" match_parent "/>    </RelativeLayout>


Mpandroidchart provides a rich set of parametric design criteria that can be designed to enrich statistical charts, such as in this case, if the commented out code is re-enabled:

Change the line style, using curves. Mlinedataset.setdrawcubic (TRUE);//The smoothness of the line//curve is the default, and the larger the value the smoother. Mlinedataset.setcubicintensity (0.2f);//The area below the fill curve, red, translucent. Mlinedataset.setdrawfilled (TRUE);//Mlinedataset.setfillalpha (+);//Mlinedataset.setfillcolor (Color.RED);


Then the style of the line chart becomes this:


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. Reprint Please specify source: Http://blog.csdn.net/zhangphil

Android Statistics Chart Mpandroidchart

Related Article

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.