MPAndroidChart——饼图
生活随笔
收集整理的這篇文章主要介紹了
MPAndroidChart——饼图
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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;@Overrideprotected 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 touchmPieChart.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 highlightsmPieChart.highlightValues(null);mPieChart.invalidate();} }轉載于:https://www.cnblogs.com/sesexxoo/p/6190497.html
總結
以上是生活随笔為你收集整理的MPAndroidChart——饼图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Matlab 图像的邻域和块操作
- 下一篇: 树莓派2代B model 上手初体验,不