MPAndroidChart——饼图

MPAndroidChart是安卓下的一个开源图形库,很多效果,简单看几个效果图

Github地址:https://github.com/PhilJay/MPAndroidChart

今天简单用一下饼图

饼图

效果图

1. 导入Library

Github上有MPChartLib库,用Eclipse开发,可以直接在工程里添加这个Library就可以了,使用Android Studio也可以直接添加库,也可以通过gradle依赖

在build.gradle里添加:

repositories {
maven { url "https://jitpack.io" }
} dependencies {
compile 'com.github.PhilJay:MPAndroidChart:v2.1.6'
}

2. 布局

在XML里添加饼图的控件

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#88888888"
android:gravity="center"
android:text="数据显示区1" /> <com.github.mikephil.charting.charts.PieChart
android:id="@+id/pie_chart"
android:layout_width="match_parent"
android:layout_height="300dp" /> <TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#88888888"
android:gravity="center"
android:text="数据显示区2" />
</LinearLayout>
</ScrollView>

3. 使用

package com.example.kongqw.piedemo;

import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.Toast; import com.github.mikephil.charting.animation.Easing;
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.formatter.PercentFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.ColorTemplate; import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap; public class MainActivity extends AppCompatActivity { private PieChart mPieChart; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPieChart = (PieChart) findViewById(R.id.pie_chart); // 显示百分比
mPieChart.setUsePercentValues(true);
// 描述信息
mPieChart.setDescription("测试饼图");
// 设置偏移量
mPieChart.setExtraOffsets(5, 10, 5, 5);
// 设置滑动减速摩擦系数
mPieChart.setDragDecelerationFrictionCoef(0.95f); mPieChart.setCenterText("测试饼图,中间文字");
/*
设置饼图中心是否是空心的
true 中间是空心的,环形图
false 中间是实心的 饼图
*/
mPieChart.setDrawHoleEnabled(true);
/*
设置中间空心圆孔的颜色是否透明
true 透明的
false 非透明的
*/
mPieChart.setHoleColorTransparent(true);
// 设置环形图和中间空心圆之间的圆环的颜色
mPieChart.setTransparentCircleColor(Color.WHITE);
// 设置环形图和中间空心圆之间的圆环的透明度
mPieChart.setTransparentCircleAlpha(110); // 设置圆孔半径
mPieChart.setHoleRadius(58f);
// 设置空心圆的半径
mPieChart.setTransparentCircleRadius(61f);
// 设置是否显示中间的文字
mPieChart.setDrawCenterText(true); // 设置旋转角度 ??
mPieChart.setRotationAngle(0);
// enable rotation of the chart by touch
mPieChart.setRotationEnabled(true);
mPieChart.setHighlightPerTapEnabled(false); // add a selection listener
// mPieChart.setOnChartValueSelectedListener(this); TreeMap<String, Float> data = new TreeMap<>();
data.put("data1", 0.5f);
data.put("data2", 0.3f);
data.put("data3", 0.1f);
data.put("data4", 0.1f);
setData(data); // 设置动画
mPieChart.animateY(1400, Easing.EasingOption.EaseInOutQuad); // 设置显示的比例
Legend l = mPieChart.getLegend();
l.setPosition(Legend.LegendPosition.RIGHT_OF_CHART);
l.setXEntrySpace(7f);
l.setYEntrySpace(0f);
l.setYOffset(0f);
} public void setData(TreeMap<String, Float> data) {
ArrayList<String> xVals = new ArrayList<String>();
ArrayList<Entry> yVals1 = new ArrayList<Entry>(); int i = 0;
Iterator it = data.entrySet().iterator();
while (it.hasNext()) {
// entry的输出结果如key0=value0等
Map.Entry entry = (Map.Entry) it.next();
String key = (String) entry.getKey();
float value = (float) entry.getValue();
xVals.add(key);
yVals1.add(new Entry(value, i++));
} PieDataSet dataSet = new PieDataSet(yVals1, "Election Results");
// 设置饼图区块之间的距离
dataSet.setSliceSpace(2f);
dataSet.setSelectionShift(5f); // 添加颜色
ArrayList<Integer> colors = new ArrayList<Integer>();
for (int c : ColorTemplate.VORDIPLOM_COLORS)
colors.add(c);
for (int c : ColorTemplate.JOYFUL_COLORS)
colors.add(c);
for (int c : ColorTemplate.COLORFUL_COLORS)
colors.add(c);
for (int c : ColorTemplate.LIBERTY_COLORS)
colors.add(c);
for (int c : ColorTemplate.PASTEL_COLORS)
colors.add(c);
colors.add(ColorTemplate.getHoloBlue());
dataSet.setColors(colors);
// dataSet.setSelectionShift(0f); PieData data1 = new PieData(xVals, dataSet);
data1.setValueFormatter(new PercentFormatter());
data1.setValueTextSize(10f);
data1.setValueTextColor(Color.BLACK);
mPieChart.setData(data1); // undo all highlights
mPieChart.highlightValues(null); mPieChart.invalidate();
}
}

MPAndroidChart——饼图的更多相关文章

  1. MPAndroidChart饼图属性及相关设置

    公司最近在做统计功能,所以用到了饼图,在网上查了一些资料最终决定使用MPAndroidChart,使用起来非常方便,还有一些问题通过各种查找,终于解决...废话不多说,先看下效果图: 布局文件: &l ...

  2. android MPAndroidChart饼图实现图例后加数字或文本(定制图例)

    转载请注明:http://blog.csdn.net/ly20116/article/details/50905789 MPAndroidChart是一个非常优秀的开源图表库,MPAndroidCha ...

  3. 笑谈Android图表-MPAndroidChart

    MPAndroidChart是一款基于Android的开源图表库,MPAndroidChart不仅可以在Android设备上绘制各种统计图表,而且可以对图表进行拖动和缩放操作,应用起来非常灵活.MPA ...

  4. Android开发学习之路--图表实现(achartengine/MPAndroidChart)之初体验

      已经有一段时间没有更新博客了,在上周离开工作了4年的公司,从此不再安安稳稳地工作了,更多的是接受挑战和实现自身价值的提高.离开了嵌入式linux,从此拥抱移动互联网,也许有点为时已晚,但是相信通过 ...

  5. <Android 应用 之路> MPAndroidChart~PieChart

    简介 MPAndroidChart是PhilJay大神给Android开发者带来的福利.MPAndroidChart是一个功能强大并且使用灵活的图表开源库,支持Android和IOS两种,这里我们暂时 ...

  6. MPAndroidChart Wiki(译文)~Part 2

    7. 填充数据 这一章节将讲解给各式各样的图表设置数据的方法. 7.1 LineChart(线形图) 想给图表添加数据,使用如下方法: public void setData(ChartData da ...

  7. android开源图表库MPAndroidChart(曲线图、直方图、饼状图)

    github地址:https://github.com/PhilJay/MPAndroidChart 添加依赖: Add the following to your project level bui ...

  8. 情人节闷在家里做画( 安卓统计图MPAndroidChart开发 )

    有些时候觉得一个人挺好的,可以更自由安排自己的时间: 有些时候觉得有个人挺好的,很多事情一个人做起来太没意思了,纵使心中澎湃,倾听的独有自己. 废话少说,直接上图 MPAndroidChart是啥 一 ...

  9. 读取数据库数据,并将数据整合成3D饼图在jsp中显示

    首先我将生成饼图的方法独立写成一个PieChar.java类,详细代码如下:(数据库需要自己建,如有需要的话) import java.io.IOException; import java.sql. ...

随机推荐

  1. [C#]设计模式-建造者模式-创建型模式

    介绍完工厂模式,现在来看一下建造者模式.建造者模式就是将一系列对象组装为一个完整对象并且返回给用户,例如汽车,就是需要由各个部件来由工人建造成一个复杂的组合实体,这个复杂实体的构造过程就被外部化到一个 ...

  2. [LeetCode] Beautiful Arrangement II 优美排列之二

    Given two integers n and k, you need to construct a list which contains n different positive integer ...

  3. python打造一个Mysql数字类型注入脚本(1)

    前言: 总是想写一个sql注入脚本,但是之前的那些都不行. 这次做好了准备,然后嘿嘿嘿. 准备: sql注入的基础知识 熟悉怎么判断 正文: 思路概念图: 这里我没有限制用户输入,不限制的话可能会 @ ...

  4. “百度杯”CTF比赛 九月场_SQLi

    题目在i春秋ctf大本营 看网页源码提示: 这边是个大坑,访问login.php发现根本不存在注入点,看了wp才知道注入点在l0gin.php 尝试order by语句,发现3的时候页面发生变化,说明 ...

  5. [Apio2012]dispatching 左偏树

    题目描述 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Master.除了 Master以外,每名忍者都有且仅有一个上级.为保密,同时增 ...

  6. bzoj1926[Sdoi2010]粟粟的书架 二分 主席树

    1926: [Sdoi2010]粟粟的书架 Time Limit: 30 Sec  Memory Limit: 552 MBSubmit: 1064  Solved: 421[Submit][Stat ...

  7. bzoj1132[POI2008]Tro 计算几何

    1132: [POI2008]Tro Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 1722  Solved: 575[Submit][Status] ...

  8. 两个文件比较之comm命令

    comm命令可用于两个文件之间的比较.它有很多不错的选项可用来调整输出,以便我们执行交集.求差(difference)以及差集操作. 交集:打印出两个文件所共有的行. 求差:打印出指定文件所包含的 ...

  9. 计科1702冯亚杰C语言程序设计预备作业

    阅读邹欣老师的博客--师生关系,针对文中的几种师生关系谈谈你的看法,你期望的师生关系是什么样的? 答:首先老师和学生之间要互相尊重,我认为这是必要的.在第一点的基础上师生要互相帮助,互相配合,共同进步 ...

  10. python3全栈开发-并发编程的多进程理论

    一. 什么是进程 进程:正在进行的一个过程或者说一个任务.而负责执行任务则是cpu. 举例(单核+多道,实现多个进程的并发执行): duoduo在一个时间段内有很多任务要做:python备课的任务,写 ...