MPAndroidChart的具體屬性方法

android中經常使用的第三方圖表MPAndroidChart的一些具體屬性及方法說明 java

注意:在將折線圖轉爲曲線圖時,lineDataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);方法有的版本的jar包不能使用,android

要設置lineDataSet.setDrawCubic(true);(默認是折線)git

  

 2 import android.app.Activity;  
 3
import android.graphics.Color; 4 import android.os.Bundle; 5 6 import com.github.mikephil.charting.charts.LineChart; 7 import com.github.mikephil.charting.components.Legend; 8 import com.github.mikephil.charting.components.LimitLine; 9 import com.github.mikephil.charting.components.XAxis; 10 import com.github.mikephil.charting.components.YAxis; 11 import com.github.mikephil.charting.data.Entry; 12 import com.github.mikephil.charting.data.LineData; 13 import com.github.mikephil.charting.data.LineDataSet; 14 15 import java.util.ArrayList; 16 17 public class MainActivity extends Activity { 18 19 private LineChart mLineChart; 20 private XAxis xAxis; //X座標軸 21 private YAxis yAxis; //Y座標軸 22 23 @Override 24 protected void onCreate(Bundle savedInstanceState) { 25 super.onCreate(savedInstanceState); 26 setContentView(R.layout.activity_main); 27 28 mLineChart = (LineChart) findViewById(R.id.chart); 29 30 xAxis = mLineChart.getXAxis(); 31 yAxis = mLineChart.getAxisLeft(); 32 33 LineData mLineData = getLineData(); 34 showChart(mLineChart, mLineData); 35 36 } 37 38 private void showChart(LineChart lineChart, LineData lineData) { 39 40 //General Chart Styling 通用的圖表造型,還有些對於特定圖表有這特定方法的造型。 41 //請參考https://github.com/PhilJay/MPAndroidChart/wiki/Specific-chart-settings 42 lineChart.setBackgroundColor(Color.argb(200, 173, 215, 210));// 設置圖表背景 參數是個Color對象 43 44 lineChart.setDescription("setDescription我在這兒"); //圖表默認右下方的描述,參數是String對象 45 lineChart.setDescriptionColor(Color.rgb(227, 135, 0)); //上面字的顏色,參數是Color對象 46 // lineChart.setDescriptionPosition(400f,600f); //上面字的位置,參數是float類型,像素,從圖表左上角開始計算 47 // lineChart.setDescriptionTypeface(); //上面字的字體,參數是Typeface 對象 48 lineChart.setDescriptionTextSize(16); //上面字的大小,float類型[6,16] 49 50 lineChart.setNoDataTextDescription("沒有數據呢(⊙o⊙)"); //沒有數據時顯示在中央的字符串,參數是String對象 51 52 lineChart.setDrawGridBackground(false);//設置圖表內格子背景是否顯示,默認是false 53 lineChart.setGridBackgroundColor(Color.rgb(256, 0, 0));//設置格子背景色,參數是Color類型對象 54 55 lineChart.setDrawBorders(true); //設置圖表內格子外的邊框是否顯示 56 lineChart.setBorderColor(Color.rgb(236, 228, 126)); //上面的邊框顏色 57 lineChart.setBorderWidth(20); //上面邊框的寬度,float類型,dp單位 58 // lineChart.setMaxVisibleValueCount();設置圖表能顯示的最大值,僅當setDrawValues()屬性值爲true時有用 59 60 61 //Interaction with the Chart 圖表的交互 62 63 //Enabling / disabling interaction 64 lineChart.setTouchEnabled(true); // 設置是否能夠觸摸 65 lineChart.setDragEnabled(true);// 是否能夠拖拽 66 67 lineChart.setScaleEnabled(true);// 是否能夠縮放 x和y軸, 默認是true 68 lineChart.setScaleXEnabled(true); //是否能夠縮放 僅x軸 69 lineChart.setScaleYEnabled(true); //是否能夠縮放 僅y軸 70 71 lineChart.setPinchZoom(true); //設置x軸和y軸可否同時縮放。默認是否 72 lineChart.setDoubleTapToZoomEnabled(true);//設置是否能夠經過雙擊屏幕放大圖表。默認是true 73 74 lineChart.setHighlightEnabled(false); //If set to true, highlighting/selecting values via touch is possible for all underlying DataSets. 75 lineChart.setHighlightPerDragEnabled(true);//可否拖拽高亮線(數據點與座標的提示線),默認是true 76 77 lineChart.setAutoScaleMinMaxEnabled(false); 78 79 80 // Chart fling / deceleration 81 lineChart.setDragDecelerationEnabled(true);//拖拽滾動時,手放開是否會持續滾動,默認是true(false是拖到哪是哪,true拖拽以後還會有緩衝) 82 lineChart.setDragDecelerationFrictionCoef(0.99f);//與上面那個屬性配合,持續滾動時的速度快慢,[0,1) 0表明當即中止。 83 84 85 //Highlighting programmatically 86 87 // highlightValues(Highlight[] highs) 88 // Highlights the values at the given indices in the given DataSets. Provide null or an empty array to undo all highlighting. 89 // highlightValue(int xIndex, int dataSetIndex) 90 // Highlights the value at the given x-index in the given DataSet. Provide -1 as the x-index or dataSetIndex to undo all highlighting. 91 // getHighlighted() 92 // Returns an Highlight[] array that contains information about all highlighted entries, their x-index and dataset-index. 93 94 95 //其餘請參考https://github.com/PhilJay/MPAndroidChart/wiki/Interaction-with-the-Chart 96 //如手勢相關方法,選擇回調方法 97 98 99 // The Axis 座標軸相關的,XY軸通用 100 xAxis.setEnabled(true); //是否顯示X座標軸 及 對應的刻度豎線,默認是true 101 xAxis.setDrawAxisLine(true); //是否繪製座標軸的線,即含有座標的那條線,默認是true 102 xAxis.setDrawGridLines(true); //是否顯示X座標軸上的刻度豎線,默認是true 103 xAxis.setDrawLabels(true); //是否顯示X座標軸上的刻度,默認是true 104 105 xAxis.setTextColor(Color.rgb(145, 13, 64)); //X軸上的刻度的顏色 106 xAxis.setTextSize(5); //X軸上的刻度的字的大小 單位dp 107 // xAxis.setTypeface(Typeface tf); //X軸上的刻度的字體 108 xAxis.setGridColor(Color.rgb(145, 13, 64)); //X軸上的刻度豎線的顏色 109 xAxis.setGridLineWidth(1); //X軸上的刻度豎線的寬 float類型 110 xAxis.enableGridDashedLine(40, 3, 0); //虛線表示X軸上的刻度豎線(float lineLength, float spaceLength, float phase)三個參數,1.線長,2.虛線間距,3.虛線開始座標 111 112 113 //能夠設置一條警惕線,以下: 114 LimitLine ll = new LimitLine(10f, "警惕線"); 115 ll.setLineColor(Color.RED); 116 ll.setLineWidth(4f); 117 ll.setTextColor(Color.GRAY); 118 ll.setTextSize(12f); 119 // .. and more styling options 120 xAxis.addLimitLine(ll); 121 122 123 // X軸專用 124 xAxis.setLabelsToSkip(1); //設置座標相隔多少,參數是int類型 125 xAxis.resetLabelsToSkip(); //將自動計算座標相隔多少 126 xAxis.setAvoidFirstLastClipping(true); 127 xAxis.setSpaceBetweenLabels(4); 128 xAxis.setPosition(XAxis.XAxisPosition.BOTTOM_INSIDE);//把座標軸放在上下 參數有:TOP, BOTTOM, BOTH_SIDED, TOP_INSIDE or BOTTOM_INSIDE. 129 130 // Y軸專用 131 yAxis.setStartAtZero(false); //設置Y軸座標是否從0開始 132 yAxis.setAxisMaxValue(50); //設置Y軸座標最大爲多少 133 yAxis.resetAxisMaxValue(); //從新設置Y軸座標最大爲多少,自動調整 134 yAxis.setAxisMinValue(10); //設置Y軸座標最小爲多少 135 yAxis.resetAxisMinValue(); //從新設置Y軸座標,自動調整 136 yAxis.setInverted(false); //Y軸座標反轉,默認是false,即下小上大 137 yAxis.setSpaceTop(0); //Y軸座標距頂有多少距離,即留白 138 yAxis.setSpaceBottom(0); //Y軸座標距底有多少距離,即留白 139 yAxis.setShowOnlyMinMax(false); //參數若是爲true Y軸座標只顯示最大值和最小值 140 yAxis.setLabelCount(10, false); //第一個參數是Y軸座標的個數,第二個參數是 是否不均勻分佈,true是不均勻分佈 141 yAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART); //參數是INSIDE_CHART(Y軸座標在內部) 或 OUTSIDE_CHART(在外部(默認是這個)) 142 // yAxis.setValueFormatter(YAxisValueFormatterf); 143 // Sets a custom ValueFormatter for this axis. This interface allows to format/modify 144 // the original label text and instead return a customized text. 145 146 147 // add data 148 lineChart.setData(lineData); // 設置數據 149 150 // get the legend (only possible after setting data) 151 Legend mLegend = lineChart.getLegend(); // 設置比例圖標示,就是那個一組y的value的 152 153 // modify the legend ... 154 // mLegend.setPosition(LegendPosition.LEFT_OF_CHART); 155 mLegend.setForm(Legend.LegendForm.CIRCLE);// 樣式 156 mLegend.setFormSize(2f);// 字體 157 mLegend.setTextColor(Color.WHITE);// 顏色 158 // mLegend.setTypeface(mTf);// 字體 159 160 lineChart.animateX(1000); // 當即執行的動畫,x軸 161 } 162 163 private LineData getLineData() { 164 165 ArrayList<Entry> valsComp1 = new ArrayList<Entry>(); //座標點的集合 166 ArrayList<Entry> valsComp2 = new ArrayList<Entry>(); 167 168 Entry c1e1 = new Entry(100.000f, 1); //座標點的值,Entry(Y座標,X座標); 169 valsComp1.add(c1e1); 170 Entry c1e2 = new Entry(50.000f, 2); 171 valsComp1.add(c1e2); 172 173 Entry c2e1 = new Entry(30.000f, 1); //座標點的值,Entry(Y座標,X座標); 174 valsComp2.add(c2e1); 175 Entry c2e2 = new Entry(80.000f, 3); 176 valsComp2.add(c2e2); 177 178 LineDataSet setComp1 = new LineDataSet(valsComp1, "Company"); //座標線,LineDataSet(座標點的集合, 線的描述或名稱); 179 LineDataSet setComp2 = new LineDataSet(valsComp2, "Company"); 180 setComp1.setAxisDependency(YAxis.AxisDependency.LEFT); //以左邊座標軸爲準 仍是以右邊座標軸爲基準 181 setComp2.setAxisDependency(YAxis.AxisDependency.LEFT); 182 183 ArrayList<LineDataSet> dataSets = new ArrayList<LineDataSet>(); //座標線的集合。 184 dataSets.add(setComp1); 185 dataSets.add(setComp2); 186 187 ArrayList<String> xVals = new ArrayList<String>(); //X座標軸的值的集合 188 xVals.add("1.Q"); xVals.add("2.Q"); xVals.add("3.Q"); xVals.add("4.Q"); 189 xVals.add("1.Q"); xVals.add("2.Q"); xVals.add("3.Q"); xVals.add("4.Q"); 190 191 LineData data = new LineData(xVals, dataSets); //LineData(X座標軸的集合, 座標線的集合); 192 mLineChart.setData(data); //爲圖表添加 數據 193 mLineChart.invalidate(); // 從新更新顯示 194 195 return data; 196 } 197
198 } 轉自 : http://blog.csdn.net/ash_zheng/article/details/48712827
相關文章
相關標籤/搜索