您的位置:首页 > 移动开发 > Android开发

MPAndroidChart的具体属性方法

2015-09-24 16:21 417 查看
package com.ashzheng.mpandroidchart;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;

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.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;

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();
showChart(mLineChart, mLineData);

}

private void showChart(LineChart lineChart, LineData lineData) {

//General Chart Styling 通用的图表造型,还有些对于特定图表有这特定方法的造型。
//请参考https://github.com/PhilJay/MPAndroidChart/wiki/Specific-chart-settings
lineChart.setBackgroundColor(Color.argb(200, 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(256, 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(10f, "警戒线");
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(false);    //设置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.

// add data
lineChart.setData(lineData); // 设置数据

// get the legend (only possible after setting data)
Legend mLegend = lineChart.getLegend(); // 设置比例图标示,就是那个一组y的value的

// modify the legend ...
// mLegend.setPosition(LegendPosition.LEFT_OF_CHART);
mLegend.setForm(Legend.LegendForm.CIRCLE);// 样式
mLegend.setFormSize(2f);// 字体
mLegend.setTextColor(Color.WHITE);// 颜色
//      mLegend.setTypeface(mTf);// 字体

lineChart.animateX(1000); // 立即执行的动画,x轴
}

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 c2e1 = new Entry(30.000f, 1); //坐标点的值,Entry(Y坐标,X坐标);
valsComp2.add(c2e1);
Entry c2e2 = new Entry(80.000f, 3);
valsComp2.add(c2e2);

LineDataSet setComp1 = new LineDataSet(valsComp1, "Company");    //坐标线,LineDataSet(坐标点的集合, 线的描述或名称);
LineDataSet setComp2 = new LineDataSet(valsComp2, "Company");
setComp1.setAxisDependency(YAxis.AxisDependency.LEFT);     //以左边坐标轴为准 还是以右边坐标轴为基准
setComp2.setAxisDependency(YAxis.AxisDependency.LEFT);

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;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: