Mpandroidchart Open Source Chart Library---line chart

Source: Internet
Author: User
Tags set background

Project Address: Click Open, Original Reference address: Click to open

1. Copy the Mpandroidchartlibrary-2-0-8.jar package to the project's Libs

2. Defining an XML file

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "vertical" >    <Com.github.mikephil.charting.charts.LineChartAndroid:id= "@+id/chart"Android:layout_width= "Match_parent"Android:layout_height= "200DP" /></LinearLayout>

3. The main Java logic code is as follows, and the comments have been added.

 PackageCom.example.sensorchart;Importjava.util.ArrayList;Importandroid.app.Activity;ImportAndroid.graphics.Color;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportCom.github.mikephil.charting.charts.LineChart;ImportCom.github.mikephil.charting.components.Legend;ImportCom.github.mikephil.charting.components.Legend.LegendForm;Importcom.github.mikephil.charting.components.XAxis.XAxisPosition;ImportCom.github.mikephil.charting.data.Entry;ImportCom.github.mikephil.charting.data.LineData;ImportCom.github.mikephil.charting.data.LineDataSet;ImportCom.shelwee.uilistview_demo. R; Public classLinechartshowextendsActivity {PrivateLinechart Mlinechart; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.line_chart); Mlinechart=(Linechart) Findviewbyid (R.id.spread_line_chart); //mTf = Typeface.createfromasset (Getassets (), "Opensans-bold.ttf");} @Overrideprotected voidOnStart () {//TODO auto-generated Method StubLinedata Mlinedata = Getlinedata (); Showchart (Mlinechart, Mlinedata, Color.rgb (114, 188, 223)); Super. OnStart (); }    //set the displayed style    Private voidShowchart (Linechart Linechart, Linedata Linedata,intcolor) {Linechart.setdrawborders (false);//whether to add a border on a line chart//No Description textLinechart.setdescription ("");//Data Description//If there is no data, it will show this, like the ListView EmtpyviewLinechart. Setnodatatextdescription ("You need-provide data for the chart."); //enable/disable Grid BackgroundLinechart.setdrawgridbackground (false);//whether to show table colorsLinechart.setgridbackgroundcolor (Color.White & 0X70FFFFFF);//The color of the table here is to give the color set a transparency//Enable Touch gesturesLinechart.settouchenabled (true);//sets whether you can touch//enable scaling and draggingLinechart.setdragenabled (true);//Whether you can drag and dropLinechart.setscaleenabled (true);//Whether you can scaleLinechart.getaxisright (). setenabled (false);//Hide the right axisLinechart.getxaxis (). SetPosition (Xaxisposition.bottom); //Set the horizontal axis at the bottom linechart.getxaxis (). Setgridcolor (color.transparent); //Remove the display of vertical bars in the grid //if disabled, scaling can be do on X-and y-axis separatelyLinechart.setpinchzoom (false);//linechart.setbackgroundcolor (color);//Set Background//Add DataLinechart.setdata (Linedata);//Setting up data//Get the legend (only possible after setting data)Legend mlegend = Linechart.getlegend ();//set up the scale chart, which is the value of the set of Y//Modify the Legend ...//mlegend.setposition (legendposition.left_of_chart);Mlegend.setform (legendform.circle);//styleMlegend.setformsize (6f);//FontMlegend.settextcolor (Color.White);//Color//Mlegend.settypeface (mTf);//FontLinechart.animatex (2500);//animate now, x-axis    }    /*** Generate a data * *@paramCount * Indicates how many coordinate points are in a chart *@paramrange * To generate a random number within range *@return     */    PrivateLinedata Getlinedata () {string[] xData=Conductivity.getdevicename ();         //data obtained, the same as Float[] Ydata=Broadcastcome.getbtdata (); //          for(inti = 0; i < ydata.length; i++) {System.out.println ("Linechart_ydata---:" +ydata); }        intDatalength =xdata.length; ArrayList<String> xValues =NewArraylist<string>();  for(inti = 0; i < datalength; i++) {            //The X-axis displays the data, which is displayed by default using the digital subscript//Xvalues.add ("" + i);Xvalues.add (Xdata[i]); }        //y-Axis dataArraylist<entry> yvalues =NewArraylist<entry>();  for(inti = 0; i < datalength; i++) {            //Float value = (float) (Math.random () * range) + 3; //Yvalues.add (New Entry (value, i));Yvalues.add (NewEntry (Ydata[i], i)); }        //Create a dataset and give it a type//data collection for y-axisLinedataset Linedataset =NewLinedataset (yvalues, "Temperature data list"/*displayed on the scale chart*/); //Mlinedataset.setfillalpha (110); //Mlinedataset.setfillcolor (color.red); //set parameters with the y-axis collectionLinedataset.setlinewidth (1.75f);//line widthLinedataset.setcirclesize (3f);//the circular size shownLinedataset.setcolor (Color.White);//Display ColorLinedataset.setcirclecolor (Color.White);//the color of the circleLinedataset.sethighlightcolor (Color.White);//The color of the highlighted lineArrayList<LineDataSet> linedatasets =NewArraylist<linedataset>(); Linedatasets.add (Linedataset); //Add the DatasetsLinedataset.setdrawcircles (false); Linedataset.setdrawcubic (true); Linedataset.setcubicintensity (0.6f); Linedataset.setdrawfilled (true); Linedataset.setfillcolor (Color.rgb (0, 255, 255)); //Create a data object with the datasetsLinedata Linedata =NewLinedata (xValues, linedatasets); returnLinedata; }    /**     *      * @paramstrings string converted to float*/     Publicfloat[] Stringtofloat (string[] strings) {float[] data=NewFloat[strings.length];  for(inti = 0; i < strings.length; i++) {Data[i]= Float.valueof (Strings[i].trim (). Replace ("℃", "" ")); System.out.println ("Converted data:" +Data[i]); }        returndata; } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.        return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//Handle Action Bar item clicks here. The Action Bar would//automatically handle clicks on the Home/up button, so long//As you specify a the parent activity in Androidmanifest.xml.        return Super. onoptionsitemselected (item); }}

To borrow a picture from the original:

Mpandroidchart Open Source Chart Library---line chart

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.