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

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

概述MPAndroidChart开源图表库之饼状图  为大家介绍一款图标开源库MPAndroidChart,它不仅可以在Android设备上绘制各种统计图表,而且可以对图表进行拖动和缩放 *** 作,用起来非常灵活。MPAndroidChart同样拥有常用的图表

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,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,223)); } // 设置显示的样式private voID showChart(lineChartlineChart,lineDatalineData,int color) { lineChart.setDrawborders(false); //是否在折线图上添加边框// no description text lineChart.setDescription("");// 数据描述// 如果没有数据的时候,会显示这个,类似ListvIEw的emtpyvIEwlineChart.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;@OverrIDeprotected 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的EmptyVIEwbarChart.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,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;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentVIEw(R.layout.activity_main);mbarChart = (barChart) findVIEwByID(R.ID.spread_bar_chart);mbarData = getbarData(4,barDataSets);returnbarData;}}

效果图如下:


以上所述是小编给大家介绍的MPAndroIDChart开源图表库的使用介绍之饼状图、折线图和柱状图的相关知识,希望对大家有所帮助。

您可能感兴趣的文章:手把手教你用Android自定义饼状图安卓(Android)开发之自定义饼状图Android动态绘制饼状图的示例代码 总结

以上是内存溢出为你收集整理的MPAndroidChart开源图表库的使用介绍之饼状图、折线图和柱状图全部内容,希望文章能够帮你解决MPAndroidChart开源图表库的使用介绍之饼状图、折线图和柱状图所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1149687.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存