MPAndroidChart的具体属性方法

[java]  view plain  copy
 print ?
  1. package com.ashzheng.mpandroidchart;  
  2.   
  3. import android.app.Activity;  
  4. import android.graphics.Color;  
  5. import android.os.Bundle;  
  6.   
  7. import com.github.mikephil.charting.charts.LineChart;  
  8. import com.github.mikephil.charting.components.Legend;  
  9. import com.github.mikephil.charting.components.LimitLine;  
  10. import com.github.mikephil.charting.components.XAxis;  
  11. import com.github.mikephil.charting.components.YAxis;  
  12. import com.github.mikephil.charting.data.Entry;  
  13. import com.github.mikephil.charting.data.LineData;  
  14. import com.github.mikephil.charting.data.LineDataSet;  
  15.   
  16. import java.util.ArrayList;  
  17.   
  18. public class MainActivity extends Activity {  
  19.   
  20.     private LineChart mLineChart;  
  21.     private XAxis xAxis;         //X坐标轴  
  22.     private YAxis yAxis;         //Y坐标轴  
  23.   
  24.     @Override  
  25.     protected void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.activity_main);  
  28.   
  29.         mLineChart = (LineChart) findViewById(R.id.chart);  
  30.   
  31.         xAxis = mLineChart.getXAxis();  
  32.         yAxis = mLineChart.getAxisLeft();  
  33.   
  34.         LineData mLineData = getLineData();  
  35.         showChart(mLineChart, mLineData);  
  36.   
  37.     }  
  38.   
  39.     private void showChart(LineChart lineChart, LineData lineData) {  
  40.   
  41.         //General Chart Styling 通用的图表造型,还有些对于特定图表有这特定方法的造型。  
  42.         //请参考https://github.com/PhilJay/MPAndroidChart/wiki/Specific-chart-settings  
  43.         lineChart.setBackgroundColor(Color.argb(200173215210));// 设置图表背景 参数是个Color对象  
  44.   
  45.         lineChart.setDescription("setDescription我在这儿"); //图表默认右下方的描述,参数是String对象  
  46.         lineChart.setDescriptionColor(Color.rgb(2271350));  //上面字的颜色,参数是Color对象  
  47. //      lineChart.setDescriptionPosition(400f,600f);    //上面字的位置,参数是float类型,像素,从图表左上角开始计算  
  48. //      lineChart.setDescriptionTypeface();     //上面字的字体,参数是Typeface 对象  
  49.         lineChart.setDescriptionTextSize(16);    //上面字的大小,float类型[6,16]  
  50.   
  51.         lineChart.setNoDataTextDescription("没有数据呢(⊙o⊙)");   //没有数据时显示在中央的字符串,参数是String对象  
  52.   
  53.         lineChart.setDrawGridBackground(false);//设置图表内格子背景是否显示,默认是false  
  54.         lineChart.setGridBackgroundColor(Color.rgb(25600));//设置格子背景色,参数是Color类型对象  
  55.   
  56.         lineChart.setDrawBorders(true);     //设置图表内格子外的边框是否显示  
  57.         lineChart.setBorderColor(Color.rgb(236228126));   //上面的边框颜色  
  58.         lineChart.setBorderWidth(20);       //上面边框的宽度,float类型,dp单位  
  59. //      lineChart.setMaxVisibleValueCount();设置图表能显示的最大值,仅当setDrawValues()属性值为true时有用  
  60.   
  61.   
  62.         //Interaction with the Chart 图表的交互  
  63.   
  64.         //Enabling / disabling interaction  
  65.         lineChart.setTouchEnabled(true); // 设置是否可以触摸  
  66.         lineChart.setDragEnabled(true);// 是否可以拖拽  
  67.   
  68.         lineChart.setScaleEnabled(true);// 是否可以缩放 x和y轴, 默认是true  
  69.         lineChart.setScaleXEnabled(true); //是否可以缩放 仅x轴  
  70.         lineChart.setScaleYEnabled(true); //是否可以缩放 仅y轴  
  71.   
  72.         lineChart.setPinchZoom(true);  //设置x轴和y轴能否同时缩放。默认是否  
  73.         lineChart.setDoubleTapToZoomEnabled(true);//设置是否可以通过双击屏幕放大图表。默认是true  
  74.   
  75.         lineChart.setHighlightEnabled(false);  //If set to true, highlighting/selecting values via touch is possible for all underlying DataSets.  
  76.         lineChart.setHighlightPerDragEnabled(true);//能否拖拽高亮线(数据点与坐标的提示线),默认是true  
  77.   
  78.         lineChart.setAutoScaleMinMaxEnabled(false);  
  79.   
  80.   
  81.         // Chart fling / deceleration  
  82.         lineChart.setDragDecelerationEnabled(true);//拖拽滚动时,手放开是否会持续滚动,默认是true(false是拖到哪是哪,true拖拽之后还会有缓冲)  
  83.         lineChart.setDragDecelerationFrictionCoef(0.99f);//与上面那个属性配合,持续滚动时的速度快慢,[0,1) 0代表立即停止。  
  84.   
  85.   
  86.         //Highlighting programmatically  
  87.   
  88. //        highlightValues(Highlight[] highs)  
  89. //               Highlights the values at the given indices in the given DataSets. Provide null or an empty array to undo all highlighting.  
  90. //        highlightValue(int xIndex, int dataSetIndex)  
  91. //               Highlights the value at the given x-index in the given DataSet. Provide -1 as the x-index or dataSetIndex to undo all highlighting.  
  92. //        getHighlighted()  
  93. //               Returns an Highlight[] array that contains information about all highlighted entries, their x-index and dataset-index.  
  94.   
  95.   
  96.         //其他请参考https://github.com/PhilJay/MPAndroidChart/wiki/Interaction-with-the-Chart  
  97.         //如手势相关方法,选择回调方法  
  98.   
  99.   
  100. //        The Axis 坐标轴相关的,XY轴通用  
  101.         xAxis.setEnabled(true);     //是否显示X坐标轴 及 对应的刻度竖线,默认是true  
  102.         xAxis.setDrawAxisLine(true); //是否绘制坐标轴的线,即含有坐标的那条线,默认是true  
  103.         xAxis.setDrawGridLines(true); //是否显示X坐标轴上的刻度竖线,默认是true  
  104.         xAxis.setDrawLabels(true); //是否显示X坐标轴上的刻度,默认是true  
  105.   
  106.         xAxis.setTextColor(Color.rgb(1451364)); //X轴上的刻度的颜色  
  107.         xAxis.setTextSize(5); //X轴上的刻度的字的大小 单位dp  
  108. //      xAxis.setTypeface(Typeface tf); //X轴上的刻度的字体  
  109.         xAxis.setGridColor(Color.rgb(1451364)); //X轴上的刻度竖线的颜色  
  110.         xAxis.setGridLineWidth(1); //X轴上的刻度竖线的宽 float类型  
  111.         xAxis.enableGridDashedLine(4030); //虚线表示X轴上的刻度竖线(float lineLength, float spaceLength, float phase)三个参数,1.线长,2.虚线间距,3.虚线开始坐标  
  112.   
  113.   
  114.         //可以设置一条警戒线,如下:  
  115.         LimitLine ll = new LimitLine(10f, "警戒线");  
  116.         ll.setLineColor(Color.RED);  
  117.         ll.setLineWidth(4f);  
  118.         ll.setTextColor(Color.GRAY);  
  119.         ll.setTextSize(12f);  
  120.         // .. and more styling options  
  121.         xAxis.addLimitLine(ll);  
  122.   
  123.   
  124. //      X轴专用  
  125.         xAxis.setLabelsToSkip(1);    //设置坐标相隔多少,参数是int类型  
  126.         xAxis.resetLabelsToSkip();   //将自动计算坐标相隔多少  
  127.         xAxis.setAvoidFirstLastClipping(true);  
  128.         xAxis.setSpaceBetweenLabels(4);  
  129.         xAxis.setPosition(XAxis.XAxisPosition.BOTTOM_INSIDE);//把坐标轴放在上下 参数有:TOP, BOTTOM, BOTH_SIDED, TOP_INSIDE or BOTTOM_INSIDE.  
  130.   
  131. //      Y轴专用  
  132.         yAxis.setStartAtZero(false);    //设置Y轴坐标是否从0开始  
  133.         yAxis.setAxisMaxValue(50);    //设置Y轴坐标最大为多少  
  134.         yAxis.resetAxisMaxValue();    //重新设置Y轴坐标最大为多少,自动调整  
  135.         yAxis.setAxisMinValue(10);    //设置Y轴坐标最小为多少  
  136.         yAxis.resetAxisMinValue();    //重新设置Y轴坐标,自动调整  
  137.         yAxis.setInverted(false);    //Y轴坐标反转,默认是false,即下小上大  
  138.         yAxis.setSpaceTop(0);    //Y轴坐标距顶有多少距离,即留白  
  139.         yAxis.setSpaceBottom(0);    //Y轴坐标距底有多少距离,即留白  
  140.         yAxis.setShowOnlyMinMax(false);    //参数如果为true Y轴坐标只显示最大值和最小值  
  141.         yAxis.setLabelCount(10false);    //第一个参数是Y轴坐标的个数,第二个参数是 是否不均匀分布,true是不均匀分布  
  142.         yAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);  //参数是INSIDE_CHART(Y轴坐标在内部) 或 OUTSIDE_CHART(在外部(默认是这个))  
  143. //      yAxis.setValueFormatter(YAxisValueFormatterf);  
  144. //              Sets a custom ValueFormatter for this axis. This interface allows to format/modify  
  145. //              the original label text and instead return a customized text.  
  146.   
  147.   
  148.         // add data  
  149.         lineChart.setData(lineData); // 设置数据  
  150.   
  151.         // get the legend (only possible after setting data)  
  152.         Legend mLegend = lineChart.getLegend(); // 设置比例图标示,就是那个一组y的value的  
  153.   
  154.         // modify the legend ...  
  155.         // mLegend.setPosition(LegendPosition.LEFT_OF_CHART);  
  156.         mLegend.setForm(Legend.LegendForm.CIRCLE);// 样式  
  157.         mLegend.setFormSize(2f);// 字体  
  158.         mLegend.setTextColor(Color.WHITE);// 颜色  
  159. //      mLegend.setTypeface(mTf);// 字体  
  160.   
  161.         lineChart.animateX(1000); // 立即执行的动画,x轴  
  162.     }  
  163.   
  164.     private LineData getLineData() {  
  165.   
  166.         ArrayList<Entry> valsComp1 = new ArrayList<Entry>();     //坐标点的集合  
  167.         ArrayList<Entry> valsComp2 = new ArrayList<Entry>();  
  168.   
  169.         Entry c1e1 = new Entry(100.000f, 1); //坐标点的值,Entry(Y坐标,X坐标);  
  170.         valsComp1.add(c1e1);  
  171.         Entry c1e2 = new Entry(50.000f, 2);  
  172.         valsComp1.add(c1e2);  
  173.   
  174.         Entry c2e1 = new Entry(30.000f, 1); //坐标点的值,Entry(Y坐标,X坐标);  
  175.         valsComp2.add(c2e1);  
  176.         Entry c2e2 = new Entry(80.000f, 3);  
  177.         valsComp2.add(c2e2);  
  178.   
  179.         LineDataSet setComp1 = new LineDataSet(valsComp1, "Company");    //坐标线,LineDataSet(坐标点的集合, 线的描述或名称);  
  180.         LineDataSet setComp2 = new LineDataSet(valsComp2, "Company");  
  181.         setComp1.setAxisDependency(YAxis.AxisDependency.LEFT);     //以左边坐标轴为准 还是以右边坐标轴为基准  
  182.         setComp2.setAxisDependency(YAxis.AxisDependency.LEFT);  
  183.   
  184.         ArrayList<LineDataSet> dataSets = new ArrayList<LineDataSet>(); //坐标线的集合。  
  185.         dataSets.add(setComp1);  
  186.         dataSets.add(setComp2);  
  187.   
  188.         ArrayList<String> xVals = new ArrayList<String>();      //X坐标轴的值的集合  
  189.         xVals.add("1.Q"); xVals.add("2.Q"); xVals.add("3.Q"); xVals.add("4.Q");  
  190.         xVals.add("1.Q"); xVals.add("2.Q"); xVals.add("3.Q"); xVals.add("4.Q");  
  191.   
  192.         LineData data = new LineData(xVals, dataSets);  //LineData(X坐标轴的集合, 坐标线的集合);  
  193.         mLineChart.setData(data);   //为图表添加 数据  
  194.         mLineChart.invalidate(); // 重新更新显示  
  195.   
  196.         return data;  
  197.     }  
  198.   
  199. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值