Android MPChart具体方法介绍

来源:互联网 发布:股票交易软件制作 编辑:程序博客网 时间:2024/06/06 22:04
import android.app.Activity;import android.graphics.Color;import android.graphics.PointF;import android.os.Bundle;import android.view.View;import com.github.mikephil.charting.charts.LineChart;import com.github.mikephil.charting.components.Legend;import com.github.mikephil.charting.components.LimitLine;import com.github.mikephil.charting.components.XAxis;import com.github.mikephil.charting.components.YAxis;import com.github.mikephil.charting.data.LineData;import com.github.mikephil.charting.data.LineDataSet;import com.up72.football.R;import java.util.ArrayList;public class MainActivity extends Activity {    private LineChart mLineChart;    private XAxis xAxis;         //X坐标轴      private YAxis yAxis;         //Y坐标轴      @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mLineChart = (LineChart) findViewById(R.id.chart);        xAxis = mLineChart.getXAxis();        yAxis = mLineChart.getAxisLeft();        LineData mLineData = getLineData();        modifyingTheViewport();        animations();        markerView();        miscellaneous();        //showChart(mLineChart, mLineData);      }    private void miscellaneous() {        mLineChart.getData();   //返回这个图表中的data类型的值,          mLineChart.getViewPortHandler();    //返回这个图表中的各种状态,返回类型为ViewPortHandler          mLineChart.getRenderer();   //返回绘制图表的数据,类型是DataRenderer          PointF pointF = new PointF();        pointF = mLineChart.getCenter();   //返回中心点,返回类型是PointF,即(x,y)          mLineChart.getCenterOffsets();  //返回图表绘制区域的中心点          mLineChart.getYMin();  //返回 数据Y的最小值 返回值是float类型          mLineChart.getYMax();  //返回 数据Y的最大值 返回值是float类型          System.out.println(mLineChart.getHighestVisibleXIndex());        mLineChart.getLowestVisibleXIndex();  //返回 能见到的X的最小值下标 返回值是int类型          mLineChart.getHighestVisibleXIndex();  //返回 能见到的X的最大值下标 返回值是int类型          mLineChart.getChartBitmap();  //返回 最后状态的图表的Bitmap          //。。。。。。      }    private void markerView() {    }    private void animations() {        //动画效果在API版本11(Android 3.0.X)以上才能起作用,低版本不会执行,但不会崩溃          //执行动画并不会刷新表格 即执行invalidate();方法  ////        mLineChart.animateX(1000); //从左到右绘制图形,参数是int类型的 持续时间  ////        mLineChart.animateY(3000); //从下到上绘制图形,参数是int类型的 持续时间          mLineChart.animateXY(3000, 2000); //x,y同时绘制,参数是(int xDuration, int yDuration)      }    private void modifyingTheViewport() {        mLineChart.setVisibleXRangeMaximum(5);  //一个界面显示多少个点,其他点可以通过滑动看到          mLineChart.setVisibleXRangeMinimum(3);  //一个界面最少显示多少个点,放大后最多 放大到 剩多少 个点  //      mLineChart.setViewPortOffsets(0,0,0, 0);  //设置图表显示的偏移量,不建议使用,因为会影响图表的自动计算          mLineChart.resetViewPortOffsets();  //重新设置图表显示的偏移量,自动计算,可取消上面的那个操作  //        mLineChart.setExtraOffsets(20, 30, 50, 10);  //设置图表外,布局内显示的偏移量          mLineChart.fitScreen();  //重置图表的缩放,拖动并使图表符合它的界限          mLineChart.moveViewToX(2);  //将左边的边放到指定的位置,参数是(float xindex)          mLineChart.zoomIn();  //默认视图放大1.4倍,          mLineChart.zoomOut();  //默认视图缩小到0.7倍,          mLineChart.zoom(1.0f,1.0f,0f,0f);  //自定义缩放(float scaleX, float scaleY, float x, float y)X缩放倍数,Y缩放倍数,x坐标,y坐标。1f是原大小      }    private void showChart(LineChart lineChart, LineData lineData) {        //General Chart Styling 通用的图表造型,还有些对于特定图表有这特定方法的造型。          //请参考https://github.com/PhilJay/MPAndroidChart/wiki/Specific-chart-settings          lineChart.setBackgroundColor(Color.argb(0, 173, 215, 210));// 设置图表背景 参数是个Color对象          lineChart.setDescription("setDescription我在这儿"); //图表默认右下方的描述,参数是String对象          lineChart.setDescriptionColor(Color.rgb(227, 135, 0));  //上面字的颜色,参数是Color对象  //      lineChart.setDescriptionPosition(400f,600f);    //上面字的位置,参数是float类型,像素,从图表左上角开始计算  //      lineChart.setDescriptionTypeface();     //上面字的字体,参数是Typeface 对象          lineChart.setDescriptionTextSize(16);    //上面字的大小,float类型[6,16]          lineChart.setNoDataTextDescription("没有数据呢(⊙o⊙)");   //没有数据时显示在中央的字符串,参数是String对象          lineChart.setDrawGridBackground(false);//设置图表内格子背景是否显示,默认是false          lineChart.setGridBackgroundColor(Color.rgb(255, 0, 0));//设置格子背景色,参数是Color类型对象          lineChart.setDrawBorders(true);     //设置图表内格子外的边框是否显示          lineChart.setBorderColor(Color.rgb(236, 228, 126));   //上面的边框颜色          lineChart.setBorderWidth(20);       //上面边框的宽度,float类型,dp单位  //      lineChart.setMaxVisibleValueCount();设置图表能显示的最大值,仅当setDrawValues()属性值为true时有用          //Interaction with the Chart 图表的交互          //Enabling / disabling interaction          lineChart.setTouchEnabled(true); // 设置是否可以触摸          lineChart.setDragEnabled(true);// 是否可以拖拽          lineChart.setScaleEnabled(true);// 是否可以缩放 x和y轴, 默认是true          lineChart.setScaleXEnabled(true); //是否可以缩放 仅x轴          lineChart.setScaleYEnabled(true); //是否可以缩放 仅y轴          lineChart.setPinchZoom(true);  //设置x轴和y轴能否同时缩放。默认是否          lineChart.setDoubleTapToZoomEnabled(true);//设置是否可以通过双击屏幕放大图表。默认是true          lineChart.setHighlightEnabled(false);  //If set to true, highlighting/selecting values via touch is possible for all underlying DataSets.          lineChart.setHighlightPerDragEnabled(true);//能否拖拽高亮线(数据点与坐标的提示线),默认是true          lineChart.setAutoScaleMinMaxEnabled(false);        // Chart fling / deceleration          lineChart.setDragDecelerationEnabled(true);//拖拽滚动时,手放开是否会持续滚动,默认是true(false是拖到哪是哪,true拖拽之后还会有缓冲)          lineChart.setDragDecelerationFrictionCoef(0.99f);//与上面那个属性配合,持续滚动时的速度快慢,[0,1) 0代表立即停止。          //Highlighting programmatically  //        highlightValues(Highlight[] highs)  //               Highlights the values at the given indices in the given DataSets. Provide null or an empty array to undo all highlighting.  //        highlightValue(int xIndex, int dataSetIndex)  //               Highlights the value at the given x-index in the given DataSet. Provide -1 as the x-index or dataSetIndex to undo all highlighting.  //        getHighlighted()  //               Returns an Highlight[] array that contains information about all highlighted entries, their x-index and dataset-index.          //其他请参考https://github.com/PhilJay/MPAndroidChart/wiki/Interaction-with-the-Chart          //如手势相关方法,选择回调方法  //        The Axis 坐标轴相关的,XY轴通用          xAxis.setEnabled(true);     //是否显示X坐标轴 及 对应的刻度竖线,默认是true          xAxis.setDrawAxisLine(true); //是否绘制坐标轴的线,即含有坐标的那条线,默认是true          xAxis.setDrawGridLines(true); //是否显示X坐标轴上的刻度竖线,默认是true          xAxis.setDrawLabels(true); //是否显示X坐标轴上的刻度,默认是true          xAxis.setTextColor(Color.rgb(145, 13, 64)); //X轴上的刻度的颜色          xAxis.setTextSize(5); //X轴上的刻度的字的大小 单位dp  //      xAxis.setTypeface(Typeface tf); //X轴上的刻度的字体          xAxis.setGridColor(Color.rgb(145, 13, 64)); //X轴上的刻度竖线的颜色          xAxis.setGridLineWidth(1); //X轴上的刻度竖线的宽 float类型          xAxis.enableGridDashedLine(40, 3, 0); //虚线表示X轴上的刻度竖线(float lineLength, float spaceLength, float phase)三个参数,1.线长,2.虚线间距,3.虚线开始坐标          //可以设置一条警戒线,如下:          LimitLine ll = new LimitLine(40f, "警戒线");        ll.setLineColor(Color.RED);        ll.setLineWidth(4f);        ll.setTextColor(Color.GRAY);        ll.setTextSize(12f);        // .. and more styling options          xAxis.addLimitLine(ll);//      X轴专用          xAxis.setLabelsToSkip(1);    //设置坐标相隔多少,参数是int类型          xAxis.resetLabelsToSkip();   //将自动计算坐标相隔多少          xAxis.setAvoidFirstLastClipping(true);        xAxis.setSpaceBetweenLabels(4);        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM_INSIDE);//把坐标轴放在上下 参数有:TOP, BOTTOM, BOTH_SIDED, TOP_INSIDE or BOTTOM_INSIDE.  //      Y轴专用          yAxis.setStartAtZero(true);    //设置Y轴坐标是否从0开始          yAxis.setAxisMaxValue(50);    //设置Y轴坐标最大为多少          yAxis.resetAxisMaxValue();    //重新设置Y轴坐标最大为多少,自动调整          yAxis.setAxisMinValue(10);    //设置Y轴坐标最小为多少          yAxis.resetAxisMinValue();    //重新设置Y轴坐标,自动调整          yAxis.setInverted(false);    //Y轴坐标反转,默认是false,即下小上大          yAxis.setSpaceTop(0);    //Y轴坐标距顶有多少距离,即留白          yAxis.setSpaceBottom(0);    //Y轴坐标距底有多少距离,即留白          yAxis.setShowOnlyMinMax(false);    //参数如果为true Y轴坐标只显示最大值和最小值          yAxis.setLabelCount(10, false);    //第一个参数是Y轴坐标的个数,第二个参数是 是否不均匀分布,true是不均匀分布          yAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);  //参数是INSIDE_CHART(Y轴坐标在内部) 或 OUTSIDE_CHART(在外部(默认是这个))  //      yAxis.setValueFormatter(YAxisValueFormatterf);  //              Sets a custom ValueFormatter for this axis. This interface allows to format/modify  //              the original label text and instead return a customized text.          //lineChart.setData(lineData); //添加数据          Legend mLegend = lineChart.getLegend(); // 设置坐标线描述?? 的样式          mLegend.setEnabled(true);        mLegend.setTextColor(Color.rgb(255, 255, 255));// 坐标线描述文字的颜色          mLegend.setTextSize(15);// 坐标线描述文字的大小,单位dp  //      mLegend.setTypeface(mTf);// 坐标线描述文字的字体          mLegend.setWordWrapEnabled(true); //坐标线描述 是否 不允许出边界          mLegend.setMaxSizePercent(0.95f);   //坐标线描述 占据的大小x%  默认0.95 即95%          mLegend.setPosition(Legend.LegendPosition.BELOW_CHART_CENTER);//位置 RIGHT_OF_CHART, RIGHT_OF_CHART_CENTER,          // RIGHT_OF_CHART_INSIDE, BELOW_CHART_LEFT, BELOW_CHART_RIGHT, BELOW_CHART_CENTER or PIECHART_CENTER (PieChart only), ... and more          mLegend.setForm(Legend.LegendForm.CIRCLE);// 样式SQUARE, CIRCLE or LINE.          mLegend.setFormSize(10f);// 大小 单位dp          mLegend.setXEntrySpace(20f);// 条目的水平间距          mLegend.setYEntrySpace(20f);// 条目的垂直间距          mLegend.setFormToTextSpace(20f);//Sets the space between the legend-label and the corresponding legend-form.      }    public void clickMe(View view){     //添加坐标线          LineData data = mLineChart.getData();        if(data != null) {            int count = (data.getDataSetCount() + 1);            // create 10 y-vals              ArrayList<Entry> yVals = new ArrayList<Entry>();            if(data.getXValCount() == 0) {                // add 10 x-entries                  for (int i = 0; i < 10; i++) {                    data.addXValue("" + (i+1));                }            }            for (int i = 0; i < data.getXValCount(); i++) {                yVals.add(new Entry((float) (Math.random() * 50f) + 50f * count, i));            }            LineDataSet set = new LineDataSet(yVals, "DataSet " + count);            set.setLineWidth(2.5f);            set.setCircleSize(4.5f);            int color = Color.rgb(255,0,0);            set.setColor(color);            set.setCircleColor(color);      //设置结点圆圈的颜色              set.setHighLightColor(Color.rgb(0,255,0));  //设置 坐标提示线的颜色              set.setValueTextSize(30f);      //设置坐标提示文字的大小              set.setValueTextColor(Color.rgb(0,0,255));  //设置坐标提示文字的颜色              data.addDataSet(set);            mLineChart.notifyDataSetChanged();            mLineChart.invalidate();        }    }    public void clickMe2(View view) {   //删除坐标线          LineData data = mLineChart.getData();        if(data != null) {            data.removeDataSet(data.getDataSetByIndex(data.getDataSetCount() - 1));            mLineChart.notifyDataSetChanged();            mLineChart.invalidate();        }    }    private LineData getLineData() {        ArrayList<Entry> valsComp1 = new ArrayList<Entry>();     //坐标点的集合          ArrayList<Entry> valsComp2 = new ArrayList<Entry>();        Entry c1e1 = new Entry(100.000f, 1); //坐标点的值,Entry(Y坐标,X坐标);          valsComp1.add(c1e1);        Entry c1e2 = new Entry(50.000f, 2);        valsComp1.add(c1e2);        Entry c1e3 = new Entry(60.000f, 3);        valsComp1.add(c1e3);        Entry c1e4 = new Entry(70.000f, 4);        valsComp1.add(c1e4);        Entry c2e1 = new Entry(30.000f, 1); //坐标点的值,Entry(Y坐标,X坐标);          valsComp2.add(c2e1);        Entry c2e2 = new Entry(80.000f, 2);        valsComp2.add(c2e2);        Entry c2e3 = new Entry(30.000f, 3); //坐标点的值,Entry(Y坐标,X坐标);          valsComp2.add(c2e3);        Entry c2e4 = new Entry(90.000f, 4);        valsComp2.add(c2e4);        LineDataSet setComp1 = new LineDataSet(valsComp1, "Company");    //坐标线,LineDataSet(坐标点的集合, 线的描述或名称);          LineDataSet setComp2 = new LineDataSet(valsComp2, "Company");        setComp1.setAxisDependency(YAxis.AxisDependency.LEFT);     //以左边坐标轴为准 还是以右边坐标轴为基准          setComp2.setAxisDependency(YAxis.AxisDependency.LEFT);        setComp1.setColors(new int[]{R.color.red1, R.color.red2, R.color.red3, R.color.red4}, this);        setComp2.setColors(new int[]{R.color.green1, R.color.green2, R.color.green3, R.color.green4}, this);        ArrayList<LineDataSet> dataSets = new ArrayList<LineDataSet>(); //坐标线的集合。          dataSets.add(setComp1);        dataSets.add(setComp2);        ArrayList<String> xVals = new ArrayList<String>();      //X坐标轴的值的集合          xVals.add("1.Q"); xVals.add("2.Q"); xVals.add("3.Q"); xVals.add("4.Q");        xVals.add("1.Q"); xVals.add("2.Q"); xVals.add("3.Q"); xVals.add("4.Q");        LineData data = new LineData(xVals, dataSets);  //LineData(X坐标轴的集合, 坐标线的集合);          mLineChart.setData(data);   //为图表添加 数据          mLineChart.invalidate(); // 重新更新显示          return data;    }}  
1 0