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. div中的内容超过容器宽度的问题

    问题描述: <div class="category">    <div class="column-a">排名</div> ...

  2. [LeetCode] Split Linked List in Parts 拆分链表成部分

    Given a (singly) linked list with head node root, write a function to split the linked list into k c ...

  3. 【tyvj】刷题记录(1001~1099)(64/99)

    1001:排序完按照题意做即可. #include<cstdio> #include<iostream> #include<cmath> #include<a ...

  4. 【Uva 10269 马里奥与公主的归途】

    ·马里奥n次解救了公主,现在需要从魔王的宫殿返回. ·英文题,述大意:      给定一个点数不超过100的无向图,其中的点分为两类:乡村和城堡. 输入A个乡村,B个城堡(乡村编号1~A,城堡编号A+ ...

  5. 【分解爪UVA11396-二分图染色模板】

    ·Rujia:"稍加推理即可解决该题--" ·英文题,述大意:      一张无向连通图,每个点连有三条边.询问是否可以将这个图分成若干个爪子,并满足条件:①每条边只能属于一个爪子 ...

  6. bzoj3262陌上花开 cdq分治

    3262: 陌上花开 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 2794  Solved: 1250[Submit][Status][Discus ...

  7. mysql 及 posgresql之优劣势大比拼

    特性 MySQL PostgreSQL 实例 通过执行 MySQL 命令(mysqld)启动实例.一个实例可以管理一个或多个数据库.一台服务器可以运行多个 mysqld 实例.一个实例管理器可以监视 ...

  8. Chinese-Text-Classification,用卷积神经网络基于 Tensorflow 实现的中文文本分类。

    用卷积神经网络基于 Tensorflow 实现的中文文本分类 项目地址: https://github.com/fendouai/Chinese-Text-Classification 欢迎提问:ht ...

  9. Linux学习之CentOS(七)---常用基本操命令1

    cd   pwd  mkdir  rmdir  ls  cp  rm  mv  cat  tac  nl  more   less   head   tail   touch ①目录管理:ls.cd. ...

  10. 学习sharding-jdbc 分库分表扩展框架

    先丢代码地址 https://gitee.com/a247292980/sharding-jdbc 再丢pom.xml的dependency <properties> <projec ...