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. How to preview html file in our browser at sublime text?

    sublime preview html.md open In Browser what should we do if we want to preview html file in our bro ...

  2. [LeetCode] Find Pivot Index 寻找中枢点

    Given an array of integers nums, write a method that returns the "pivot" index of this arr ...

  3. [LeetCode] Non-negative Integers without Consecutive Ones 非负整数不包括连续的1

    Given a positive integer n, find the number of non-negative integers less than or equal to n, whose ...

  4. CSS滚动条样式定制

    效果图如下 <!DOCTYPE html> <!-- saved from url=(0061)http://www.xuanfengge.com/demo/201311/scrol ...

  5. [BZOJ 3332]旧试题

    Description 圣诞节将至.一年一度的难题又摆在wyx面前——如何给妹纸送礼物. wyx的后宫有n人,这n人之间有着复杂的关系网,相互认识的人有m对.wyx想要量化后宫之间的亲密度,于是准备给 ...

  6. [USACO17FEB]Why Did the Cow Cross the Road II S

    题目描述 The long road through Farmer John's farm has  crosswalks across it, conveniently numbered  (). ...

  7. 笔记10 在XML中声明切面(1)

    1.无注解的Audience package XMLconcert; public class Audience { public void silenceCellPhones() { System. ...

  8. Maven之自定义archetype生成项目骨架

    Maven之自定义archetype生成项目骨架(一) http://blog.csdn.net/sxdtzhaoxinguo/article/details/46895013

  9. web.xml is missing and <failOnMissingWebXml> is set to true

    这时候需要右击项目-->Java EE Tools-->Generate Deployment Descriptor Stub .然后系统会在src/main/webapp/WEB_INF ...

  10. 消息中间件--ActiveMQ&JMS消息服务

    ### 消息中间件 ### ---------- **消息中间件** 1. 消息中间件的概述 2. 消息中间件的应用场景 * 异步处理 * 应用解耦 * 流量削峰 * 消息通信   --------- ...