android中常用的第三方图表MPAndroidChart的一些具体属性及方法说明 

注意:在将折线图转为曲线图时,lineDataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);方法有的版本的jar包不能使用,

要设置lineDataSet.setDrawCubic(true);(默认是折线)

  

 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

 

相关文章:

  • 2022-01-04
  • 2022-03-01
  • 2021-10-27
  • 2022-12-23
  • 2021-12-25
  • 2022-02-03
  • 2022-12-23
猜你喜欢
  • 2021-09-24
  • 2021-05-28
  • 2022-03-03
  • 2022-12-23
  • 2021-12-29
  • 2021-12-28
  • 2022-12-23
相关资源
相似解决方案