MPAndroidChart open source chart Library (2) line chart, mpandroidchart Open Source
Undertake the previous article, please refer to http://blog.csdn.net/shineflowers/article/details/44701645
1. copy the mpandroidchartlibrary-2-0-8.jar package to the project's libs
2. Define an xml file
3. The main Java logic code is as follows, and comments have been added.
Package com. example. mpandroidlinechart; 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. data. entry; import com. github. mikephil. charting. data. lineData; import com. github. mikephil. charting. data. lineDataSet; import android. support. v7.app. actionBarActivity; import android. graphics. color; import android. OS. bundle; public class MainActivity extends ActionBarActivity {private LineChart mLineChart; // private Typeface mTf; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mLineChart = (LineChart) findViewById (R. id. spread_line_chart); // mTf = Typeface. createFromAsset (getAssets (), "OpenSans-Bold.ttf"); LineData mLineData = getLineData (36,100); showChart (mLineChart, mLineData, Color. rgb (114,188,223);} // set the display style private void showChart (LineChart lineChart, LineData lineData, int color) {lineChart. setDrawBorders (false); // whether to add a border on the line chart // no description text lineChart. setDescription (""); // data description // This is displayed if no data exists, similar to the emtpyview lineChart of listview. setNoDataTextDescription ("You need to provide data for the chart. "); // enable/disable grid background lineChart. setDrawGridBackground (false); // whether to display the table color lineChart. setGridBackgroundColor (Color. WHITE & 0x70FFFFFF); // table color. Here, we set a transparency for the color. // enable touch gestures lineChart. setTouchEnabled (true); // you can set whether to touch or enable scaling and dragging lineChart. setDragEnabled (true); // whether lineChart can be dragged. setScaleEnabled (true); // whether scaling is allowed // if disabled, scaling can be done on x-and y-axis separately lineChart. setPinchZoom (false); // lineChart. setBackgroundColor (color); // set the background // add data lineChart. setData (lineData); // set data // get the legend (only possible after setting data) Legend mLegend = lineChart. getLegend (); // set the ratio icon, which is the value of the group of y // modify the legend... // mLegend. setPosition (LegendPosition. LEFT_OF_CHART); mLegend. setForm (LegendForm. CIRCLE); // style mLegend. setFormSize (6f); // font mLegend. setTextColor (Color. WHITE); // color // mLegend. setTypeface (mTf); // font lineChart. animateX (2500); // animation to be executed immediately, X axis}/*** generate a data * @ param count indicates the number of coordinate points in the chart * @ param range is used to generate a random number within the range * @ return */private LineData getLineData (int count, float range) {ArrayList <String> xValues = new ArrayList <String> (); for (int I = 0; I <count; I ++) {// data displayed on the X axis. xValues is displayed by default using a numerical subscript. add ("" + I);} // ArrayList of Y axis data <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 // LineDataSet lineDataSet = new LineDataSet (yValues, "test line chart"/* displayed on the scale chart */); // mLineDataSet. setFillAlpha (110); // mLineDataSet. setFillColor (Color. RED); // use the Y axis set to set the lineDataSet parameter. setLineWidth (1.75f); // lineDataSet. setCircleSize (3f); // display the circular lineDataSet. setColor (Color. WHITE); // display color lineDataSet. setCircleColor (Color. WHITE); // lineDataSet in the circular color. setHighLightColor (Color. WHITE); // The ArrayList color of the highlighted line <LineDataSet> lineDataSets = new ArrayList <LineDataSet> (); lineDataSets. add (lineDataSet); // add the datasets // create a data object with the datasets LineData lineData = new LineData (xValues, lineDataSets); return lineData ;}}
As follows:
Another form of line chart is the smooth line chart, which can be filled with the desired color between the line chart and the X axis. The Code is as follows:
In the above getLineData () function, add your own settings:
As follows:
There are very few posts on the MPAndroidChart padding line chart. This is the result of searching other open source chart libraries on the Internet, such as JFreeChart... and the source code. I don't know if it is correct, but it is basically okay to see the results. If you have any questions, please correct them!