当前位置: 移动技术网 > IT编程>开发语言>Java > MPAndroidChart开源图表库的使用介绍之饼状图、折线图和柱状图

MPAndroidChart开源图表库的使用介绍之饼状图、折线图和柱状图

2019年07月22日  | 移动技术网IT编程  | 我要评论
mpandroidchart开源图表库之饼状图   为大家介绍一款图标开源库mpandroidchart,它不仅可以在android设备上绘制各种统计图表,而且可以对图表

mpandroidchart开源图表库之饼状图

  为大家介绍一款图标开源库mpandroidchart,它不仅可以在android设备上绘制各种统计图表,而且可以对图表进行拖动和缩放操作,用起来非常灵活。mpandroidchart同样拥有常用的图表类型:线型图、饼图、柱状图和散点图。

mpandroidchartlibrary.jar包下载地址:

https://github.com/philjay/mpandroidchart/releases

  下面主要实现以下饼状图:

  1.从上面的地址中下载最新mpandroidchartlibrary-2-0-8.jar包, 然后copy到项目的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) //实心圆
piechart.setdescription("测试饼状图"); 
// mchart.setdrawyvalues(true); 
piechart.setdrawcentertext(true); //饼状图中间可以添加文字
piechart.setdrawholeenabled(true); 
piechart.setrotationangle(90); // 初始旋转角度
// draws the corresponding description value into the slice 
// mchart.setdrawxvalues(true); 
// enable rotation of the chart by touch 
piechart.setrotationenabled(true); // 可以手动旋转
// 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"); //饼状图中间的文字
//设置数据
piechart.setdata(piedata); 
// undo all highlights 
// piechart.highlightvalues(null); 
// piechart.invalidate(); 
legend mlegend = piechart.getlegend(); //设置比例图
mlegend.setposition(legendposition.right_of_chart); //最右边显示
// mlegend.setform(legendform.line); //设置比例图的形状,默认是方形
mlegend.setxentryspace(7f); 
mlegend.setyentryspace(5f); 
piechart.animatexy(1000, 1000); //设置动画
// mchart.spin(2000, 0, 360); 
} 
/** 
* 
* @param count 分成几部分
* @param range 
*/ 
privatepiedatagetpiedata(int count, float range) { 
arraylist<string>xvalues = new arraylist<string>(); //xvals用来表示每个饼块上的内容
for (inti = 0; i< count; i++) { 
xvalues.add("quarterly" + (i + 1)); //饼块上显示成quarterly1, quarterly2, quarterly3, quarterly4 
} 
arraylist<entry>yvalues = new arraylist<entry>(); //yvals用来表示封装每个饼块的实际数据
// 饼图数据
/** 
* 将一个饼形图分成四部分,四部分的数值比例为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); //设置个饼状图之间的距离
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); // 选中态多出的长度
piedatapiedata = new piedata(xvalues, piedataset); 
returnpiedata; 
} 
}

效果图如下:


mpandroidchart开源图表库之折线图

1. 将mpandroidchartlibrary-2-0-8.jar包copy到项目的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)); 
} 
// 设置显示的样式
private void showchart(linechartlinechart, linedatalinedata, int color) { 
linechart.setdrawborders(false); //是否在折线图上添加边框
// no description text 
linechart.setdescription("");// 数据描述
// 如果没有数据的时候,会显示这个,类似listview的emtpyview
linechart.setnodatatextdescription("you need to provide data for the chart."); 
// enable / disable grid background 
linechart.setdrawgridbackground(false); // 是否显示表格颜色
linechart.setgridbackgroundcolor(color.white& 0x70ffffff); // 表格的的颜色,在这里是是给颜色设置一个透明度
// enable touch gestures 
linechart.settouchenabled(true); // 设置是否可以触摸
// 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);// 设置背景
// 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(legendform.circle);// 样式
mlegend.setformsize(6f);// 字体
mlegend.settextcolor(color.white);// 颜色
// mlegend.settypeface(mtf);// 字体
linechart.animatex(2500); // 立即执行的动画,x轴
} 
/** 
* 生成一个数据
* @param count 表示图表中有多少个坐标点
* @param range 用来生成range以内的随机数
* @return 
*/ 
privatelinedatagetlinedata(int count, float range) { 
arraylist<string>xvalues = new arraylist<string>(); 
for (inti = 0; i< count; i++) { 
// x轴显示的数据,这里默认使用数字下标显示
xvalues.add("" + i); 
} 
// y轴的数据
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轴的数据集合
linedatasetlinedataset = new linedataset(yvalues, "测试折线图" /*显示在比例图上*/); 
// mlinedataset.setfillalpha(110); 
// mlinedataset.setfillcolor(color.red); 
//用y轴的集合来设置参数
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开源图表库之柱状图

1. 将mpandroidchartlibrary-2-0-8.jar包copy到项目的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("");// 数据描述
// 如果没有数据的时候,会显示这个,类似listview的emptyview
barchart.setnodatatextdescription("you need to provide data for the chart."); 
barchart.setdrawgridbackground(false); // 是否显示表格颜色
barchart.setgridbackgroundcolor(color.white& 0x70ffffff); // 表格的的颜色,在这里是是给颜色设置一个透明度
barchart.settouchenabled(true); // 设置是否可以触摸
barchart.setdragenabled(true);// 是否可以拖拽
barchart.setscaleenabled(true);// 是否可以缩放
barchart.setpinchzoom(false);// 
// barchart.setbackgroundcolor();// 设置背景
barchart.setdrawbarshadow(true);
barchart.setdata(bardata); // 设置数据
legend mlegend = barchart.getlegend(); // 设置比例图标示
mlegend.setform(legendform.circle);// 样式
mlegend.setformsize(6f);// 字体
mlegend.settextcolor(color.black);// 颜色
// x轴设定
// xaxisxaxis = barchart.getxaxis();
// xaxis.setposition(xaxisposition.bottom);
barchart.animatex(2500); // 立即执行的动画,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以内的随机数*/) + 3;
yvalues.add(new barentry(value, i)); 
}
// y轴的数据集合
bardatasetbardataset = new bardataset(yvalues, "测试饼状图"); 
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("");// 数据描述
// 如果没有数据的时候,会显示这个,类似listview的emptyview
barchart.setnodatatextdescription("you need to provide data for the chart."); 
barchart.setdrawgridbackground(false); // 是否显示表格颜色
barchart.setgridbackgroundcolor(color.white& 0x70ffffff); // 表格的的颜色,在这里是是给颜色设置一个透明度
barchart.settouchenabled(true); // 设置是否可以触摸
barchart.setdragenabled(true);// 是否可以拖拽
barchart.setscaleenabled(true);// 是否可以缩放
barchart.setpinchzoom(false);// 
// barchart.setbackgroundcolor();// 设置背景
barchart.setdrawbarshadow(true);
barchart.setdata(bardata); // 设置数据
legend mlegend = barchart.getlegend(); // 设置比例图标示
mlegend.setform(legendform.circle);// 样式
mlegend.setformsize(6f);// 字体
mlegend.settextcolor(color.black);// 颜色
// x轴设定
// xaxisxaxis = barchart.getxaxis();
// xaxis.setposition(xaxisposition.bottom);
barchart.animatex(2500); // 立即执行的动画,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以内的随机数*/) + 3;
yvalues.add(new barentry(value, i)); 
}
// y轴的数据集合
bardatasetbardataset = new bardataset(yvalues, "测试饼状图"); 
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开源图表库的使用介绍之饼状图、折线图和柱状图的相关知识,希望对大家有所帮助。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网