原地址:http://blog.csdn.net/androidxiaogang/article/details/53719631
MPAndroidChartMPAndroidChart是一个开源的图标库,能够实现各种各样的效果,自定义比较容易,在项目中有简单使用,做一个记录。
1、柱状图的简单使用
效果图:
1、导包,在这用的是两个jar包
2、xml文件中配置
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.github.mikephil.charting.charts.BarChart
android:id="@+id/barChart"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
</com.github.mikephil.charting.charts.BarChart>
</LinearLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
3、代码中设置如下:
package com.example.mpchart;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.utils.ValueFormatter;
import java.util.ArrayList;
/**
* Created by Administrator on 2016/12/18.
*/
public class BarActivity extends Activity {
private BarChart barChart;
private XAxis xAxis;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bar);
barChart= (BarChart) findViewById(R.id.barChart);
//1、基本设置
xAxis=barChart.getXAxis();
xAxis.setDrawAxisLine(true);
xAxis.setDrawGridLines(false);
barChart.setDrawGridBackground(false); // 是否显示表格颜色
barChart.getAxisLeft().setDrawAxisLine(false);
barChart.setTouchEnabled(false); // 设置是否可以触摸
barChart.setDragEnabled(true);// 是否可以拖拽
barChart.setScaleEnabled(true);// 是否可以缩放
//2、y轴和比例尺
barChart.setDescription("四个季度");// 数据描述
barChart.getAxisLeft().setEnabled(false);
barChart.getAxisRight().setEnabled(false);
Legend legend = barChart.getLegend();//隐藏比例尺
legend.setEnabled(false);
//3、x轴数据,和显示位置
ArrayList<String> xValues = new ArrayList<String>();
xValues.add("一季度");
xValues.add("二季度");
xValues.add("三季度");
xValues.add("四季度");
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);//数据位于底部
//4、y轴数据
ArrayList<BarEntry> yValues = new ArrayList<BarEntry>();
//new BarEntry(20, 0)前面代表数据,后面代码柱状图的位置;
yValues.add(new BarEntry(20, 0));
yValues.add(new BarEntry(18, 1));
yValues.add(new BarEntry(4, 2));
yValues.add(new BarEntry(45, 3));
//5、设置显示的数字为整形
BarDataSet barDataSet=new BarDataSet(yValues,"");
barDataSet.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float v) {
int n = (int) v;
return n + "";
}
});
//6、设置柱状图的颜色
barDataSet.setColors(new int[]{Color.rgb(104, 202, 37), Color.rgb(192, 32, 32),
Color.rgb(34, 129, 197), Color.rgb(175, 175, 175)});
//7、显示,柱状图的宽度和动画效果
BarData barData = new BarData(xValues, barDataSet);
barDataSet.setBarSpacePercent(40f);//值越大,柱状图就越宽度越小;
barChart.animateY(1000);
barChart.setData(barData); //
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
2、饼状图
package com.example.mpchart;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.utils.PercentFormatter;
import java.util.ArrayList;
/**
* Created by Administrator on 2016/12/18.
*/
public class PieActivity extends Activity {
private PieChart pieChart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pie);
pieChart = (PieChart) findViewById(R.id.pie_chart);
//1、基本设置
pieChart.setDrawCenterText(false); //饼状图中间文字不显示
pieChart.setDescription("");
pieChart.setDrawHoleEnabled(false); //设置实心
pieChart.setRotationAngle(90); // 初始旋转角度
//2、添加数据
ArrayList<String> xValues = new ArrayList<String>(); //xVals用来表示每个饼块上的内容
xValues.add("this is one");
xValues.add("this is two");
xValues.add("this is three");
xValues.add("this is four");
ArrayList<Entry> yValues = new ArrayList<Entry>();
yValues.add(new Entry(10, 0));
yValues.add(new Entry(3, 1));
yValues.add(new Entry(23, 2));
yValues.add(new Entry(56, 3));
//3、y轴数据
PieDataSet pieDataSet = new PieDataSet(yValues, ""/*显示在比例图上*/);
pieDataSet.setSliceSpace(0f); //设置个饼状图之间的距离
//4、设置颜色
ArrayList<Integer> colors = new ArrayList<Integer>();
colors.add(Color.rgb(205, 205, 205));
colors.add(Color.rgb(114, 188, 223));
colors.add(Color.rgb(255, 123, 124));
colors.add(Color.rgb(57, 135, 200));
pieDataSet.setColors(colors);
//5、 设置数据
PieData pieData = new PieData(xValues, pieDataSet);
DisplayMetrics metrics = getResources().getDisplayMetrics();
float px = 5 * (metrics.densityDpi / 160f);
pieDataSet.setSelectionShift(px); // 选中态多出的长度
pieData.setValueFormatter(new PercentFormatter());//显示百分比
//6、去掉比例尺和说明
Legend legend = pieChart.getLegend();//下标说明,false
legend.setEnabled(false);
pieChart.setDescription("");
//7、显示百分比
pieData.setValueFormatter(new PercentFormatter());
//8、显示
pieChart.setData(pieData);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
demo下载(as)地址
http://download.csdn.net/detail/androidxiaogang/9714452