Android MPAndroidChart开源图表库之饼状图的代码

所属分类: 软件编程 / Android 阅读数: 49
收藏 0 赞 0 分享

MPAndroidChart是一款基于Android的开源图表库,MPAndroidChart不仅可以在Android设备上绘制各种统计图表,而且可以对图表进行拖动和缩放操作,应用起来非常灵活。MPAndroidChart同样拥有常用的图表类型:线型图、饼图、柱状图和散点图。

GitHub地址

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

1.从上面的地址中下载最新mpandroidchartlibrary-2-0-8.jar包, 然后copy到项目的libs中;

2.定义xml文件;

3.主要Java逻辑代码如下,注释已经都添加上了。

package com.jackie.mpandroidpiechart; 
import java.util.ArrayList; 
import com.github.mikephil.charting.charts.PieChart; 
import com.github.mikephil.charting.components.Legend; 
import com.github.mikephil.charting.components.Legend.LegendPosition; 
import com.github.mikephil.charting.data.Entry; 
import com.github.mikephil.charting.data.PieData; 
import com.github.mikephil.charting.data.PieDataSet; 
import android.support.v7.app.ActionBarActivity; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.util.DisplayMetrics; 
public class MainActivity extends ActionBarActivity { 
 private PieChart mChart; 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.activity_main); 
 mChart = (PieChart) findViewById(R.id.spread_pie_chart); 
 PieData mPieData = getPieData(4, 100); 
 showChart(mChart, mPieData); 
 } 
 private void showChart(PieChart pieChart, PieData pieData) { 
 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 
 */ 
 private PieData getPieData(int count, float range) { 
 ArrayList<String> xValues = new ArrayList<String>(); //xVals用来表示每个饼块上的内容 
 for (int i = 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轴的集合 
 PieDataSet pieDataSet = 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(); 
 float px = 5 * (metrics.densityDpi / 160f); 
 pieDataSet.setSelectionShift(px); // 选中态多出的长度 
 PieData pieData = new PieData(xValues, pieDataSet); 
 return pieData; 
 } 
} 

 效果图如下:

主要是一些基本属性和API的调用,具体每个API都有什么样的效果和作用,只能靠自己去尝试。后面还会陆陆续续为大家介绍MPAndroidChart其他类型的图表。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

使用ViewPager实现android软件使用向导功能实现步骤

现在的大部分android软件,都是使用说明,就是第一次使用该软件时,会出现向导,可以左右滑动,然后就进入应用的主界面了,下面我们就实现这个功能
收藏 0 赞 0 分享

android在异步任务中关闭Cursor的代码方法

android在异步任务中如何关闭Cursor?在我们开发应用的时候,很多时候会遇到这种问题,下面我们就看看代码如何实现
收藏 0 赞 0 分享

Android自定义桌面功能代码实现

android自定义桌面其实很简单,看一个例子就明白了
收藏 0 赞 0 分享

android将图片转换存到数据库再从数据库读取转换成图片实现代码

有时候我们想把图片存入到数据库中,尽管这不是一种明智的选择,但有时候还是不得以会用到,下面说说将图片转换成byte[]数组存入到数据库中去,并从数据库中取出来转换成图像显示出来
收藏 0 赞 0 分享

TextView显示系统时间(时钟功能带秒针变化

用System.currentTimeMillis()可以获取系统当前的时间,我们可以开启一个线程,然后通过handler发消息,来实时的更新TextView上显示的系统时间,可以做一个时钟的功能
收藏 0 赞 0 分享

Android用ListView显示SDCard文件列表的小例子

本文简单实现了用ListView显示SDCard文件列表,目录的回退等功能暂不讨论,获取文件列表,files即为所选择目录下的所有文件列表
收藏 0 赞 0 分享

Android拦截外拨电话程序示例

这篇文章主要介绍了Android拦截外拨电话的示例,大家参考使用吧
收藏 0 赞 0 分享

通过Html网页调用本地安卓(android)app程序代码

如何使用html网页和本地app进行传递数据呢?经过研究,发现还是有方法的,总结了一下,大致有一下几种方式
收藏 0 赞 0 分享

android Textview文字监控(Textview使用方法)

以手机号充值为例,当用户输入最后一位数时候,进行汇率的变换,本文就实现类似这样的功能
收藏 0 赞 0 分享

Android ListView长按弹出菜单二种实现方式示例

这篇文章主要介绍了Android ListView长按弹出菜单的方法,大家参考实现
收藏 0 赞 0 分享
查看更多