Android Statistical Chart MPAndroidChart., mpandroidchart
Android Statistical Chart MPAndroidChart
MPAndroidChart is an open-source third-party Statistical Chart library on the Android platform. It can draw a variety of complex and rich statistical charts, such as common line charts, pie charts, bar charts, scatter charts, candle charts used in financial stocks, bubble statistical charts, and radar statistical pie charts. In short, AndroidMPChart can basically meet the daily Statistical Chart development needs on the Android platform. AndroidMPChart on github project home page: https://github.com/PhilJay/MPAndroidChart in your own project libs, import its released jar package to use MPAndroidChart. MPAndroidChart released jar package page in: https://github.com/PhilJay/MPAndroidChart/releases AndroidMPChart usage: In the releases page to download the latest jar package, copy to your own project libs can be used.
The Code is as follows:
1 package com. lixu. zhexian; 2 3 import java. util. arrayList; 4 import com. github. mikephil. charting. charts. lineChart; 5 import com. github. mikephil. charting. components. legend; 6 import com. github. mikephil. charting. components. legend. legendForm; 7 import com. github. mikephil. charting. components. legend. legendPosition; 8 import com. github. mikephil. charting. components. XAxis. XAxisPosition; 9 import com. githu B. mikephil. charting. components. XAxis; 10 import com. github. mikephil. charting. data. entry; 11 import com. github. mikephil. charting. data. lineData; 12 import com. github. mikephil. charting. data. lineDataSet; 13 import com. github. mikephil. charting. formatter. valueFormatter; 14 import com. github. mikephil. charting. utils. viewPortHandler; 15 import android. app. activity; 16 import android. graphics. color; 17 impo Rt android. OS. bundle; 18 import android. view. window; 19 20 public class MainActivity extends Activity {21 22 @ Override 23 protected void onCreate (Bundle savedInstanceState) {24 super. onCreate (savedInstanceState); 25 requestWindowFeature (Window. FEATURE_NO_TITLE); 26 setContentView (R. layout. activity_main); 27 28 LineChart mlinechart = (LineChart) findViewById (R. id. linechart); 29 30 LineData mLineD Ata = LineData (30); 31 // place the X axis to the bottom. The default value is 32 x axis mXAxis = mlinechart. getXAxis (); 33 mXAxis. setPosition (XAxisPosition. BOTTOM); 34 35 setChartStyle (mlinechart, mLineData); 36} 37 38 // set the display style 39 private void setChartStyle (LineChart mlinechart, LineData mLineData) {40 // whether to add the border 41 mlinechart to the line. setDrawBorders (false); 42 // data description 43 mlinechart. setDescription ("temperature record data"); 44 // This is displayed if no data exists, similar to listvi Emtpyview 45 mlinechart. setNoDataTextDescription ("if the data passed to MPAndroidChart is empty, you will see this text. "); 46 // specifies whether to draw the background color. 47 // If mLineChart. setDrawGridBackground (false), 48 // mLineChart. setGridBackgroundColor () is invalid; 49 mlinechart. setDrawGridBackground (true); 50 // the background of the line chart 51 mlinechart. setGridBackgroundColor (Color. CYAN); 52 // set to Touch 53 mlinechart. setTouchEnabled (true); 54 // you can drag 55 mlinechart. setDragEnabled (true); 56 // you can specify a scale of 57 mlinechart. setScaleEnabled (true); 58 mlinechart. setPinchZoom (false); 59 // x y axis background 60 m Linechart. setBackgroundColor (Color. YELLOW); 61 // set data 62 mlinechart of x Y axis. setData (mLineData); 63 // sets the ratio icon, which is the 64 Legend mLegend = mlinechart of the values of the y group. getLegend (); 65 mLegend. setPosition (LegendPosition. BELOW_CHART_CENTER); 66 // style 67 mLegend. setForm (LegendForm. LINE); 68 // font 69 mLegend. setFormSize (20366f); 70 // The font color below 71 mLegend. setTextColor (Color. BLUE); 72 // set the animation 73 mlinechart for the X axis. animateX (150 00); 74 75} 76 77 private LineData (int count) {78 ArrayList <String> x = new ArrayList <String> (); 79 // x axis data 80 for (int I = 0; I <count; I ++) {81 x. add ("day" + I + "day"); 82} 83 // 84 ArrayList of y axis data <Entry> y = new ArrayList <Entry> (); 85 for (int I = 0; I <count; I ++) {86 float f = (float) (Math. random () * 100); 87 Entry entry = new Entry (f, I); 88 y. add (entry); 89} 90 // dataset 91 on the Y axis LineDataSet set = new LineDataSet (y, "dataset -- wood Sub"); 92 // use the set of y axes to set the parameter 93 // set the width of 94. setLineWidth (3.0f); 95 // display the circular size 96 set. setCircleSize (5.0f); 97 // the color of the Line 98 set. setColor (Color. BLACK); 99 // the color of the sphere is 100 set. setCircleColor (Color. RED); 101 // set mLineDataSet. after setDrawHighlightIndicators (false), The crosstab lines of 102 // Highlight will not be displayed, 103 // At the same time, mLineDataSet. setHighLightColor () is invalid. 104 105 set. setDrawHighlightIndicators (true); 106 // after clicking it, the color of the cross line is 107 set. setHighLightColor (Color. BLUE); 108 // set the font size of the data point to 109 set. setValueTextSize (10.0f); 110 // mLineDataSet. setDrawCircleHole (true); 111 112 // use a curve to change the line style. 113 set. setDrawCubic (true); 114 // by default, the smoothness of the straight line is 115 //. The larger the value, the smoother the curve. 116 set. setCubicIntensity (0.2f); 117 118 // fill the area below the curve, Red, translucent. 119 set. setDrawFilled (true); 120 // The smaller the value, the larger the transparency is. 121 set. setFillAlpha (150); 122 set. setFillColor (Color. RED); 123 124 // fill in the color of the data point on the line and the blank space in the center of the circle. 125 set. setCircleColorHole (Color. YELLOW); 126 // set the format of data displayed on the line. If this parameter is not set, the float data format is displayed by default. 127 set. setValueFormatter (new ValueFormatter () {128 129 @ Override130 public String getFormattedValue (float value, Entry entry, int dataSetIndex, 131 ViewPortHandler viewPortHandler) {132 int n = (int) value; 133 String str = n + "℃"; 134 return str; 135} 136}); 137 ArrayList <LineDataSet> mLineDataSets = new ArrayList <LineDataSet> (); 138 mLineDataSets. add (set); 139 140 LineData mLineData = new LineData (x, mLineDataSets); 141 return mLineData; 142 143} 144}
Xml file:
1 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 2 xmlns: tools = "http://schemas.android.com/tools" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent" 5 android: orientation = "vertical"> 6 7 <TextView 8 android: layout_width = "match_parent" 9 android: layout_height = "wrap_content" 10 android: gravity = "center" 11 android: textSize = "30sp" 12 android: text = "Daily temperature record curve" 13 android: textColor = "# ff0000"/> 14 15 <com. github. mikephil. charting. charts. lineChart16 android: id = "@ + id/linechart" 17 android: layout_width = "match_parent" 18 android: layout_height = "match_parent"/> 19 20 </LinearLayout>
Run: