MPAndroidChart開(kāi)源圖表庫(kù)的使用介紹之餅狀圖、折線圖和柱狀圖
MPAndroidChart開(kāi)源圖表庫(kù)之餅狀圖
為大家介紹一款圖標(biāo)開(kāi)源庫(kù)MPAndroidChart,它不僅可以在Android設(shè)備上繪制各種統(tǒng)計(jì)圖表,而且可以對(duì)圖表進(jìn)行拖動(dòng)和縮放操作,用起來(lái)非常靈活。MPAndroidChart同樣擁有常用的圖表類型:線型圖、餅圖、柱狀圖和散點(diǎn)圖。
mpandroidchartlibrary.jar包下載地址:
https://github.com/PhilJay/MPAndroidChart/releases
下面主要實(shí)現(xiàn)以下餅狀圖:
1.從上面的地址中下載最新mpandroidchartlibrary-2-0-8.jar包, 然后copy到項(xiàng)目的libs中
2. 定義xml文件

3. 主要Java邏輯代碼如下。
importjava.util.ArrayList;
importcom.github.mikephil.charting.charts.PieChart;
importcom.github.mikephil.charting.components.Legend;
importcom.github.mikephil.charting.components.Legend.LegendPosition;
importcom.github.mikephil.charting.data.Entry;
importcom.github.mikephil.charting.data.PieData;
importcom.github.mikephil.charting.data.PieDataSet;
import android.support.v7.app.ActionBarActivity;
importandroid.graphics.Color;
importandroid.os.Bundle;
importandroid.util.DisplayMetrics;
public class MainActivity extends ActionBarActivity {
privatePieChartmChart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mChart = (PieChart) findViewById(R.id.spread_pie_chart);
PieDatamPieData = getPieData(4, 100);
showChart(mChart, mPieData);
}
private void showChart(PieChartpieChart, PieDatapieData) {
pieChart.setHoleColorTransparent(true);
pieChart.setHoleRadius(60f); //半徑
pieChart.setTransparentCircleRadius(64f); // 半透明圈
//pieChart.setHoleRadius(0) //實(shí)心圓
pieChart.setDescription("測(cè)試餅狀圖");
// mChart.setDrawYValues(true);
pieChart.setDrawCenterText(true); //餅狀圖中間可以添加文字
pieChart.setDrawHoleEnabled(true);
pieChart.setRotationAngle(90); // 初始旋轉(zhuǎn)角度
// draws the corresponding description value into the slice
// mChart.setDrawXValues(true);
// enable rotation of the chart by touch
pieChart.setRotationEnabled(true); // 可以手動(dòng)旋轉(zhuǎn)
// display percentage values
pieChart.setUsePercentValues(true); //顯示成百分比
// mChart.setUnit(" €");
// mChart.setDrawUnitsInChart(true);
// add a selection listener
// mChart.setOnChartValueSelectedListener(this);
// mChart.setTouchEnabled(false);
// mChart.setOnAnimationListener(this);
pieChart.setCenterText("Quarterly Revenue"); //餅狀圖中間的文字
//設(shè)置數(shù)據(jù)
pieChart.setData(pieData);
// undo all highlights
// pieChart.highlightValues(null);
// pieChart.invalidate();
Legend mLegend = pieChart.getLegend(); //設(shè)置比例圖
mLegend.setPosition(LegendPosition.RIGHT_OF_CHART); //最右邊顯示
// mLegend.setForm(LegendForm.LINE); //設(shè)置比例圖的形狀,默認(rèn)是方形
mLegend.setXEntrySpace(7f);
mLegend.setYEntrySpace(5f);
pieChart.animateXY(1000, 1000); //設(shè)置動(dòng)畫
// mChart.spin(2000, 0, 360);
}
/**
*
* @param count 分成幾部分
* @param range
*/
privatePieDatagetPieData(int count, float range) {
ArrayList<String>xValues = new ArrayList<String>(); //xVals用來(lái)表示每個(gè)餅塊上的內(nèi)容
for (inti = 0; i< count; i++) {
xValues.add("Quarterly" + (i + 1)); //餅塊上顯示成Quarterly1, Quarterly2, Quarterly3, Quarterly4
}
ArrayList<Entry>yValues = new ArrayList<Entry>(); //yVals用來(lái)表示封裝每個(gè)餅塊的實(shí)際數(shù)據(jù)
// 餅圖數(shù)據(jù)
/**
* 將一個(gè)餅形圖分成四部分,四部分的數(shù)值比例為14:14:34:38
* 所以 14代表的百分比就是14%
*/
float quarterly1 = 14;
float quarterly2 = 14;
float quarterly3 = 34;
float quarterly4 = 38;
yValues.add(new Entry(quarterly1, 0));
yValues.add(new Entry(quarterly2, 1));
yValues.add(new Entry(quarterly3, 2));
yValues.add(new Entry(quarterly4, 3));
//y軸的集合
PieDataSetpieDataSet = new PieDataSet(yValues, "Quarterly Revenue 2014"/*顯示在比例圖上*/);
pieDataSet.setSliceSpace(0f); //設(shè)置個(gè)餅狀圖之間的距離
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);
DisplayMetrics metrics = getResources().getDisplayMetrics();
floatpx = 5 * (metrics.densityDpi / 160f);
pieDataSet.setSelectionShift(px); // 選中態(tài)多出的長(zhǎng)度
PieDatapieData = new PieData(xValues, pieDataSet);
returnpieData;
}
}
效果圖如下:

MPAndroidChart開(kāi)源圖表庫(kù)之折線圖
1. 將mpandroidchartlibrary-2-0-8.jar包c(diǎn)opy到項(xiàng)目的libs中
2. 定義xml文件

3. 主要Java邏輯代碼如下。
packagecom.example.mpandroidlinechart;
importjava.util.ArrayList;
importcom.github.mikephil.charting.charts.LineChart;
importcom.github.mikephil.charting.components.Legend;
importcom.github.mikephil.charting.components.Legend.LegendForm;
importcom.github.mikephil.charting.data.Entry;
importcom.github.mikephil.charting.data.LineData;
importcom.github.mikephil.charting.data.LineDataSet;
import android.support.v7.app.ActionBarActivity;
importandroid.graphics.Color;
importandroid.os.Bundle;
public class MainActivity extends ActionBarActivity {
privateLineChartmLineChart;
// private Typeface mTf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLineChart = (LineChart) findViewById(R.id.spread_line_chart);
// mTf = Typeface.createFromAsset(getAssets(), "OpenSans-Bold.ttf");
LineDatamLineData = getLineData(36, 100);
showChart(mLineChart, mLineData, Color.rgb(114, 188, 223));
}
// 設(shè)置顯示的樣式
private void showChart(LineChartlineChart, LineDatalineData, int color) {
lineChart.setDrawBorders(false); //是否在折線圖上添加邊框
// no description text
lineChart.setDescription("");// 數(shù)據(jù)描述
// 如果沒(méi)有數(shù)據(jù)的時(shí)候,會(huì)顯示這個(gè),類似listview的emtpyview
lineChart.setNoDataTextDescription("You need to provide data for the chart.");
// enable / disable grid background
lineChart.setDrawGridBackground(false); // 是否顯示表格顏色
lineChart.setGridBackgroundColor(Color.WHITE& 0x70FFFFFF); // 表格的的顏色,在這里是是給顏色設(shè)置一個(gè)透明度
// enable touch gestures
lineChart.setTouchEnabled(true); // 設(shè)置是否可以觸摸
// enable scaling and dragging
lineChart.setDragEnabled(true);// 是否可以拖拽
lineChart.setScaleEnabled(true);// 是否可以縮放
// if disabled, scaling can be done on x- and y-axis separately
lineChart.setPinchZoom(false);//
lineChart.setBackgroundColor(color);// 設(shè)置背景
// add data
lineChart.setData(lineData); // 設(shè)置數(shù)據(jù)
// get the legend (only possible after setting data)
Legend mLegend = lineChart.getLegend(); // 設(shè)置比例圖標(biāo)示,就是那個(gè)一組y的value的
// modify the legend ...
// mLegend.setPosition(LegendPosition.LEFT_OF_CHART);
mLegend.setForm(LegendForm.CIRCLE);// 樣式
mLegend.setFormSize(6f);// 字體
mLegend.setTextColor(Color.WHITE);// 顏色
// mLegend.setTypeface(mTf);// 字體
lineChart.animateX(2500); // 立即執(zhí)行的動(dòng)畫,x軸
}
/**
* 生成一個(gè)數(shù)據(jù)
* @param count 表示圖表中有多少個(gè)坐標(biāo)點(diǎn)
* @param range 用來(lái)生成range以內(nèi)的隨機(jī)數(shù)
* @return
*/
privateLineDatagetLineData(int count, float range) {
ArrayList<String>xValues = new ArrayList<String>();
for (inti = 0; i< count; i++) {
// x軸顯示的數(shù)據(jù),這里默認(rèn)使用數(shù)字下標(biāo)顯示
xValues.add("" + i);
}
// y軸的數(shù)據(jù)
ArrayList<Entry>yValues = new ArrayList<Entry>();
for (inti = 0; i< count; i++) {
float value = (float) (Math.random() * range) + 3;
yValues.add(new Entry(value, i));
}
// create a dataset and give it a type
// y軸的數(shù)據(jù)集合
LineDataSetlineDataSet = new LineDataSet(yValues, "測(cè)試折線圖" /*顯示在比例圖上*/);
// mLineDataSet.setFillAlpha(110);
// mLineDataSet.setFillColor(Color.RED);
//用y軸的集合來(lái)設(shè)置參數(shù)
lineDataSet.setLineWidth(1.75f); // 線寬
lineDataSet.setCircleSize(3f);// 顯示的圓形大小
lineDataSet.setColor(Color.WHITE);// 顯示顏色
lineDataSet.setCircleColor(Color.WHITE);// 圓形的顏色
lineDataSet.setHighLightColor(Color.WHITE); // 高亮的線的顏色
ArrayList<LineDataSet>lineDataSets = new ArrayList<LineDataSet>();
lineDataSets.add(lineDataSet); // add the datasets
// create a data object with the datasets
LineDatalineData = new LineData(xValues, lineDataSets);
returnlineData;
}
}
效果圖如下:

MPAndroidChart開(kāi)源圖表庫(kù)之柱狀圖
1. 將mpandroidchartlibrary-2-0-8.jar包c(diǎn)opy到項(xiàng)目的libs中
2. 定義xml文件

3. 主要Java邏輯代碼如下。
packagecom.jackie.mpandoidbarchart;
importjava.util.ArrayList;
importcom.github.mikephil.charting.charts.BarChart;
importcom.github.mikephil.charting.charts.LineChart;
importcom.github.mikephil.charting.components.Legend;
importcom.github.mikephil.charting.components.Legend.LegendForm;
importcom.github.mikephil.charting.components.XAxis;
importcom.github.mikephil.charting.components.XAxis.XAxisPosition;
importcom.github.mikephil.charting.data.BarData;
importcom.github.mikephil.charting.data.BarDataSet;
importcom.github.mikephil.charting.data.BarEntry;
import android.support.v7.app.ActionBarActivity;
importandroid.graphics.Color;
importandroid.os.Bundle;
public class MainActivity extends ActionBarActivity {
privateBarChartmBarChart;
privateBarDatamBarData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBarChart = (BarChart) findViewById(R.id.spread_bar_chart);
mBarData = getBarData(4, 100);
showBarChart(mBarChart, mBarData);
}
private void showBarChart(BarChartbarChart, BarDatabarData) {
barChart.setDrawBorders(false); ////是否在折線圖上添加邊框
barChart.setDescription("");// 數(shù)據(jù)描述
// 如果沒(méi)有數(shù)據(jù)的時(shí)候,會(huì)顯示這個(gè),類似ListView的EmptyView
barChart.setNoDataTextDescription("You need to provide data for the chart.");
barChart.setDrawGridBackground(false); // 是否顯示表格顏色
barChart.setGridBackgroundColor(Color.WHITE& 0x70FFFFFF); // 表格的的顏色,在這里是是給顏色設(shè)置一個(gè)透明度
barChart.setTouchEnabled(true); // 設(shè)置是否可以觸摸
barChart.setDragEnabled(true);// 是否可以拖拽
barChart.setScaleEnabled(true);// 是否可以縮放
barChart.setPinchZoom(false);//
// barChart.setBackgroundColor();// 設(shè)置背景
barChart.setDrawBarShadow(true);
barChart.setData(barData); // 設(shè)置數(shù)據(jù)
Legend mLegend = barChart.getLegend(); // 設(shè)置比例圖標(biāo)示
mLegend.setForm(LegendForm.CIRCLE);// 樣式
mLegend.setFormSize(6f);// 字體
mLegend.setTextColor(Color.BLACK);// 顏色
// X軸設(shè)定
// XAxisxAxis = barChart.getXAxis();
// xAxis.setPosition(XAxisPosition.BOTTOM);
barChart.animateX(2500); // 立即執(zhí)行的動(dòng)畫,x軸
}
privateBarDatagetBarData(int count, float range) {
ArrayList<String>xValues = new ArrayList<String>();
for (inti = 0; i< count; i++) {
xValues.add("第" + (i + 1) + "季度");
}
ArrayList<BarEntry>yValues = new ArrayList<BarEntry>();
for (inti = 0; i< count; i++) {
float value = (float) (Math.random() * range/*100以內(nèi)的隨機(jī)數(shù)*/) + 3;
yValues.add(new BarEntry(value, i));
}
// y軸的數(shù)據(jù)集合
BarDataSetbarDataSet = new BarDataSet(yValues, "測(cè)試餅狀圖");
barDataSet.setColor(Color.rgb(114, 188, 223));
ArrayList<BarDataSet>barDataSets = new ArrayList<BarDataSet>();
barDataSets.add(barDataSet); // add the datasets
BarDatabarData = new BarData(xValues, barDataSets);
returnbarData;
}
}
packagecom.jackie.mpandoidbarchart;
importjava.util.ArrayList;
importcom.github.mikephil.charting.charts.BarChart;
importcom.github.mikephil.charting.charts.LineChart;
importcom.github.mikephil.charting.components.Legend;
importcom.github.mikephil.charting.components.Legend.LegendForm;
importcom.github.mikephil.charting.components.XAxis;
importcom.github.mikephil.charting.components.XAxis.XAxisPosition;
importcom.github.mikephil.charting.data.BarData;
importcom.github.mikephil.charting.data.BarDataSet;
importcom.github.mikephil.charting.data.BarEntry;
import android.support.v7.app.ActionBarActivity;
importandroid.graphics.Color;
importandroid.os.Bundle;
public class MainActivity extends ActionBarActivity {
privateBarChartmBarChart;
privateBarDatamBarData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBarChart = (BarChart) findViewById(R.id.spread_bar_chart);
mBarData = getBarData(4, 100);
showBarChart(mBarChart, mBarData);
}
private void showBarChart(BarChartbarChart, BarDatabarData) {
barChart.setDrawBorders(false); ////是否在折線圖上添加邊框
barChart.setDescription("");// 數(shù)據(jù)描述
// 如果沒(méi)有數(shù)據(jù)的時(shí)候,會(huì)顯示這個(gè),類似ListView的EmptyView
barChart.setNoDataTextDescription("You need to provide data for the chart.");
barChart.setDrawGridBackground(false); // 是否顯示表格顏色
barChart.setGridBackgroundColor(Color.WHITE& 0x70FFFFFF); // 表格的的顏色,在這里是是給顏色設(shè)置一個(gè)透明度
barChart.setTouchEnabled(true); // 設(shè)置是否可以觸摸
barChart.setDragEnabled(true);// 是否可以拖拽
barChart.setScaleEnabled(true);// 是否可以縮放
barChart.setPinchZoom(false);//
// barChart.setBackgroundColor();// 設(shè)置背景
barChart.setDrawBarShadow(true);
barChart.setData(barData); // 設(shè)置數(shù)據(jù)
Legend mLegend = barChart.getLegend(); // 設(shè)置比例圖標(biāo)示
mLegend.setForm(LegendForm.CIRCLE);// 樣式
mLegend.setFormSize(6f);// 字體
mLegend.setTextColor(Color.BLACK);// 顏色
// X軸設(shè)定
// XAxisxAxis = barChart.getXAxis();
// xAxis.setPosition(XAxisPosition.BOTTOM);
barChart.animateX(2500); // 立即執(zhí)行的動(dòng)畫,x軸
}
privateBarDatagetBarData(int count, float range) {
ArrayList<String>xValues = new ArrayList<String>();
for (inti = 0; i< count; i++) {
xValues.add("第" + (i + 1) + "季度");
}
ArrayList<BarEntry>yValues = new ArrayList<BarEntry>();
for (inti = 0; i< count; i++) {
float value = (float) (Math.random() * range/*100以內(nèi)的隨機(jī)數(shù)*/) + 3;
yValues.add(new BarEntry(value, i));
}
// y軸的數(shù)據(jù)集合
BarDataSetbarDataSet = new BarDataSet(yValues, "測(cè)試餅狀圖");
barDataSet.setColor(Color.rgb(114, 188, 223));
ArrayList<BarDataSet>barDataSets = new ArrayList<BarDataSet>();
barDataSets.add(barDataSet); // add the datasets
BarDatabarData = new BarData(xValues, barDataSets);
returnbarData;
}
}
效果圖如下:

以上所述是小編給大家介紹的MPAndroidChart開(kāi)源圖表庫(kù)的使用介紹之餅狀圖、折線圖和柱狀圖的相關(guān)知識(shí),希望對(duì)大家有所幫助。
- 詳解Android圖表 MPAndroidChart折線圖
- Android MPAndroidChart開(kāi)源庫(kù)圖表之折線圖的實(shí)例代碼
- Android自定義View實(shí)現(xiàn)折線圖效果
- Android繪制動(dòng)態(tài)折線圖
- Android HelloChart開(kāi)源庫(kù)圖表之折線圖的實(shí)例代碼
- Android開(kāi)發(fā)之天氣趨勢(shì)折線圖
- Android自定義控件實(shí)現(xiàn)折線圖
- Android自定義可左右滑動(dòng)和點(diǎn)擊的折線圖
- Android自定義View簡(jiǎn)易折線圖控件(二)
- Android開(kāi)發(fā)RecyclerView實(shí)現(xiàn)折線圖效果
相關(guān)文章
Java類型通配符應(yīng)用實(shí)戰(zhàn)分析
這篇文章主要介紹了Java類型通配符應(yīng)用實(shí)戰(zhàn),簡(jiǎn)單分析了Java類型通配符概念、原理并結(jié)合實(shí)例形式給出了Java類型通配符相關(guān)使用技巧,需要的朋友可以參考下2019-07-07
Java計(jì)算數(shù)學(xué)表達(dá)式代碼詳解
這篇文章主要介紹了Java計(jì)算數(shù)學(xué)表達(dá)式代碼詳解,具有一定借鑒價(jià)值,需要的朋友可以了解下。2017-12-12
Java xml出現(xiàn)錯(cuò)誤 javax.xml.transform.TransformerException: java.
這篇文章主要介紹了Java xml出現(xiàn)錯(cuò)誤 javax.xml.transform.TransformerException: java.lang.NullPointerException的相關(guān)資料,需要的朋友可以參考下2016-11-11
MYSQL批量插入數(shù)據(jù)的實(shí)現(xiàn)代碼
非常的實(shí)現(xiàn)原理,代碼較多,建議大家仔細(xì)看看。2008-10-10
Mybatis-Plus實(shí)現(xiàn)自動(dòng)生成代碼的操作步驟
AutoGenerator 是 MyBatis-Plus 的代碼生成器,通過(guò) AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各個(gè)模塊的代碼,極大的提升了開(kāi)發(fā)效率,本文將給大家介紹Mybatis-Plus實(shí)現(xiàn)自動(dòng)生成代碼的操作步驟2023-10-10
Spring常用注解 使用注解來(lái)構(gòu)造IoC容器的方法
下面小編就為大家分享一篇Spring常用注解 使用注解來(lái)構(gòu)造IoC容器的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
spring?boot如何配置靜態(tài)路徑詳解(404出現(xiàn)的坑)
這篇文章主要給大家介紹了關(guān)于spring?boot如何配置靜態(tài)路徑的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02
SpringBoot集成支付寶沙箱支付的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot集成支付寶沙箱支付的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12

