Mpandroidchart Open Source Chart Library line chart

Source: Internet
Author: User

Reference: http://blog.csdn.net/shineflowers/article/details/44704723

1. Adding components to the layout file

<com.github.mikephil.charting.charts.linechart

Android:id= "@+id/char1"

Android:layout_width= "Match_parent"

android:layout_height= "Match_parent"

Android:layout_margin= "10DP"

Android:layout_weight= "1.0"

/>

2. Set the properties of the chart

Chart1 = (Linechart) Findviewbyid (R.ID.CHAR1);

Linechart.setdrawborders (FALSE); Whether to add a border on a line chart

Linechart.setdescription ("");//Data description

Linechart.setdescriptiontypeface (mTf);//font

If there is no data, it will show this, like the ListView Emtpyview

Linechart.setnodatatextdescription ("need to provide data for the chart.");

Linechart.setdrawgridbackground (FALSE); Whether to show Table colors

Linechart.setgridbackgroundcolor (Color.White & 0X70FFFFFF); The color of the table here is to give the color set a transparency

Linechart.settouchenabled (TRUE); Sets whether you can touch

Enable scaling and dragging

Linechart.setdragenabled (TRUE);//Can not be dragged

Linechart.setscaleenabled (TRUE);//Whether you can scale

Linechart.setpinchzoom (FALSE);

Linechart.setbackgroundcolor (Color.White);//Set background

Set a callout line for maximum and minimum values

Limitline ll1 = new Limitline (100f, "Maximum value");

Ll1.setlinewidth (4f);

Ll1.enabledashedline (10f, 10f, 0f);//set to dashed.

Ll1.setlabelposition (Limitlabelposition.pos_left);

Ll1.settextsize (10f);

Limitline ll2 = new Limitline (10f, "min");

Ll2.setlinewidth (4f);

Ll2.enabledashedline (10f, 10f, 0f);

Ll2.setlabelposition (Limitlabelposition.pos_left);

Ll2.settextsize (10f);

X-axis style.

Xaxis Xaxis = Linechart.getxaxis ();

Xaxis.setposition (Xaxisposition.bottom);

Xaxis.settypeface (MTF);

Xaxis.setdrawgridlines (FALSE);

Xaxis.setdrawaxisline (TRUE);

Y-axis style.

YAxis Leftaxis = Linechart.getaxisleft ();

Leftaxis.settypeface (MTF);

Leftaxis.setlabelcount (10);

Leftaxis.addlimitline (LL1);

Leftaxis.addlimitline (LL2);

Sets the legend style.

Legend mlegend = Linechart.getlegend (); Set up the scale chart, which is the value of the set of Y

Mlegend.setform (legendform.circle);//Style

Mlegend.setformsize (6f);//font

Mlegend.settextcolor (Color.Black);//Color

Mlegend.settypeface (mTf);//font

Chart animations.

Linechart.animatex (2000); Animate now, X-axis

Linechart.animatey (1000); Animate now, y-axis

3. Generate data for a chart

The x-axis displays the data, which is displayed by default using the digital subscript

arraylist<string> xValues = new arraylist<string> ();

for (int i = 0; i < count; i++) {

Xvalues.add ("" + i);

}

Y-Axis data

arraylist<entry> yvalues = new arraylist<entry> ();

for (int i = 0; i < count; i++) {

Float value = (float) (Math.random () * range) + 3;

Yvalues.add (New Entry (value, i));

}

Create a dataset and give it a type

Data collection for y-axis

Linedataset linedataset = new Linedataset (yvalues, "Test Line Chart"/* is displayed on the scale chart */);

Mlinedataset.setfillalpha (110);

Mlinedataset.setfillcolor (color.red);

Set the style of data lines and data points with a collection of Y-axes

Linedataset.setlinewidth (1.75f); Line width

Linedataset.setcirclesize (4.5f);//the circular size shown

Linedataset.setcolor (color);//Color of line

Linedataset.setcirclecolor (color);//Round Color

Linedataset.sethighlightcolor (Color.rgb (244, 117, 117)); The color of the highlighted line

Create a data object with the datasets

Linedata linedata = new Linedata (xValues, linedataset);//Generate Data

============================================================

Adds a collection of data to ArrayList. The resulting linedata can generate multiple data lines on a single chart.

arraylist<linedataset> linedatasets = new arraylist<linedataset> ();

Linedatasets.add (Linedataset); Add a DataSet

Linedata linedata = new Linedata (xValues, linedatasets); Use the ArrayList setting to generate data.

==============================================================

4. Bind the data to the chart.

Linechart.setdata (Linedata); Setting up data

Reference: http://blog.csdn.net/shineflowers/article/details/44704723

Mpandroidchart Open Source Chart Library line chart

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.