Android 实现会旋转的饼状统计图实例代码

Android 实现会旋转的饼状统计图实例代码,第1张

概述Android实现会旋转的饼状统计图实例代码最近在做一个项目,由于有需要统计的需要,于是就做成了下面饼状统计图。

AndroID 实现会旋转的饼状统计图实例代码

最近在做一个项目,由于有需要统计的需要,于是就做成了下面饼状统计图。

下图是效果图:

大致思路是:

关于的介绍这里不做详细介绍,如果想深入请点击开源项目MPAndroIDChart

下面是其实现:

首先是添加MPAndroIDChart依赖:

maven { url "https://jitpack.io" } 


compile 'com.github.PhilJay:MPAndroIDChart:v3.0.1'


Mainactivity

package com.example.geekp.myapplication;import androID.graphics.color;import androID.graphics.Typeface;import androID.support.annotation.Nullable;import androID.support.design.Widget.TabLayout;import androID.support.design.Widget.floatingActionbutton;import androID.support.design.Widget.Snackbar;import androID.support.v7.app.AppCompatActivity;import androID.support.v7.Widget.Toolbar;import androID.support.v4.app.Fragment;import androID.support.v4.app.FragmentManager;import androID.support.v4.app.FragmentPagerAdapter;import androID.support.v4.vIEw.VIEwPager;import androID.os.Bundle;import androID.text.SpannableString;import androID.text.style.relativeSizeSpan;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.vIEw.Window;import androID.vIEw.WindowManager;import androID.Widget.TextVIEw;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.PIEData;import com.github.mikephil.charting.data.PIEDataSet;import com.github.mikephil.charting.data.PIEEntry;import com.github.mikephil.charting.formatter.PercentFormatter;import com.github.mikephil.charting.utils.colorTemplate;import java.util.ArrayList;import butterknife.BindVIEw;import butterknife.ButterKnife;public class MainActivity extends AppCompatActivity {  private SectionsPagerAdapter mSectionsPagerAdapter;  private VIEwPager mVIEwPager;  @OverrIDe  protected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    //设置全屏    getwindow().setFlags(WindowManager.LayoutParams.FLAG_FulLSCREEN,WindowManager.LayoutParams.FLAG_FulLSCREEN);    setContentVIEw(R.layout.activity_main);    Toolbar toolbar = (Toolbar) findVIEwByID(R.ID.toolbar);    setSupportActionbar(toolbar);    // Create the adapter that will return a fragment for each of the three    // primary sections of the activity.    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());    // Set up the VIEwPager with the sections adapter.    mVIEwPager = (VIEwPager) findVIEwByID(R.ID.container);    mVIEwPager.setAdapter(mSectionsPagerAdapter);    getSupportActionbar().setTitle("饼状统计图");    TabLayout tabLayout = (TabLayout) findVIEwByID(R.ID.tabs);    tabLayout.setupWithVIEwPager(mVIEwPager);  }  //fragment  public static class PlaceholderFragment extends Fragment {    @BindVIEw(R.ID.chart1)    PIEChart mChart;    @BindVIEw(R.ID.tvXMax)    TextVIEw tvXMax;    @BindVIEw(R.ID.tvYMax)    TextVIEw tvYMax;    protected String[] mPartIEs = new String[]{        "已完成","未完成"    };    protected Typeface mTfRegular;    protected Typeface mTflight;    private static final String ARG_SECTION_NUMBER = "section_number";    public PlaceholderFragment() {    }    public static PlaceholderFragment newInstance(int sectionNumber) {      PlaceholderFragment fragment = new PlaceholderFragment();      Bundle args = new Bundle();      args.putInt(ARG_SECTION_NUMBER,sectionNumber);      fragment.setArguments(args);      return fragment;    }    @OverrIDe    public VIEw onCreateVIEw(LayoutInflater inflater,VIEwGroup container,Bundle savedInstanceState) {      VIEw rootVIEw = inflater.inflate(R.layout.fragment_main,container,false);      ButterKnife.bind(this,rootVIEw);      int index = getArguments().getInt(ARG_SECTION_NUMBER);      mTfRegular = Typeface.createFromAsset(getContext().getAssets(),"OpenSans-Regular.ttf");      mTflight = Typeface.createFromAsset(getContext().getAssets(),"OpenSans-light.ttf");      mChart.setUsePercentValues(true);      mChart.getDescription().setEnabled(false);      mChart.setExtraOffsets(5,10,5,5);      mChart.setDragDecelerationFrictionCoef(0.95f);      mChart.setCenterTextTypeface(mTflight);      mChart.setCenterText(generateCenterSpannableText(index));      mChart.setDrawHoleEnabled(true);      mChart.setHolecolor(color.WHITE);      mChart.settransparentCirclecolor(color.WHITE);      mChart.settransparentCircleAlpha(110);      mChart.setHoleRadius(58f);      mChart.settransparentCircleRadius(61f);      mChart.setDrawCenterText(true);      mChart.setRotationAngle(0);      // enable rotation of the chart by touch      mChart.setRotationEnabled(true);      mChart.setHighlightPerTapEnabled(true);      setData(index);      mChart.animateY(1400,Easing.EasingOption.EaseInOutQuad);      // mChart.spin(2000,360);      Legend l = mChart.getLegend();      l.setVerticalAlignment(Legend.LegendVerticalAlignment.top);      l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);      l.setorIEntation(Legend.LegendOrIEntation.VERTICAL);      l.setDrawInsIDe(false);      l.setXEntrySpace(7f);      l.setYEntrySpace(0f);      l.setYOffset(0f);      // entry label styling      mChart.setEntryLabelcolor(color.WHITE);      mChart.setEntryLabelTypeface(mTfRegular);      mChart.setEntryLabelTextSize(12f);      return rootVIEw;    }    //饼状图中间要显示的内容    private SpannableString generateCenterSpannableText(int index) {      String sectionname = "";      switch (index) {        case 1:          sectionname = "科目一";          break;        case 2:          sectionname = "科目二";          break;        case 3:          sectionname = "科目三";          break;        case 4:          sectionname = "科目四";          break;      }      SpannableString s = new SpannableString(sectionname);      s.setSpan(new relativeSizeSpan(1.7f),sectionname.length(),0);      return s;    }    private voID setData(int fragmentIndex) {      ArrayList<PIEEntry> entrIEs = new ArrayList<PIEEntry>();      PIEDataSet dataSet = new PIEDataSet(entrIEs,"正确率:" + 25 + "%");      dataSet.setSliceSpace(3f);      dataSet.setSelectionShift(5f);      ArrayList<Integer> colors = new ArrayList<Integer>();      if (fragmentIndex == 1) {        //这里写的是饼状图的组成部分,像我这样写就是第一部分是占百分之七十五,第二部分是占了百分之二十五        entrIEs.add(new PIEEntry(75,mPartIEs[0]));        entrIEs.add(new PIEEntry(25,mPartIEs[1]));        for (int c : colorTemplate.VORDIPLOM_colorS)          colors.add(c);      } else if (fragmentIndex == 2) {        entrIEs.add(new PIEEntry(50,mPartIEs[0]));        entrIEs.add(new PIEEntry(50,mPartIEs[1]));        colors.add(getResources().getcolor(R.color.pIEcolor8));        colors.add(getResources().getcolor(R.color.pIEcolor2));      } else if (fragmentIndex == 3) {        entrIEs.add(new PIEEntry(45,mPartIEs[0]));        entrIEs.add(new PIEEntry(55,mPartIEs[1]));        colors.add(getResources().getcolor(R.color.pIEcolor3));        colors.add(getResources().getcolor(R.color.pIEcolor4));      } else {        entrIEs.add(new PIEEntry(60,mPartIEs[0]));        entrIEs.add(new PIEEntry(40,mPartIEs[1]));        colors.add(getResources().getcolor(R.color.pIEcolor5));        colors.add(getResources().getcolor(R.color.pIEcolor6));      }      colors.add(colorTemplate.getHoloBlue());      dataSet.setcolors(colors);      //dataSet.setSelectionShift(0f);      PIEData data = new PIEData(dataSet);      data.setValueFormatter(new PercentFormatter());      data.setValueTextSize(11f);      data.setValueTextcolor(color.BLACK);      data.setValueTypeface(mTflight);      mChart.setData(data);      // undo all highlights      mChart.highlightValues(null);      mChart.invalIDate();    }  }  //适配器  public class SectionsPagerAdapter extends FragmentPagerAdapter {    public SectionsPagerAdapter(FragmentManager fm) {      super(fm);    }    @OverrIDe    public Fragment getItem(int position) {      return PlaceholderFragment.newInstance(position + 1);    }    @OverrIDe    public int getCount() {      return 4;    }    //这个方法用于显示标题    @OverrIDe    public CharSequence getPageTitle(int position) {      switch (position) {        case 0:          return "科目一";        case 1:          return "科目二";        case 2:          return "科目三";        case 3:          return "科目四";      }      return null;    }  }}

activity_main.xml

<?xml version="1.0" enCoding="utf-8"?><androID.support.design.Widget.CoordinatorLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"  xmlns:app="http://schemas.androID.com/apk/res-auto"  xmlns:tools="http://schemas.androID.com/tools"  androID:ID="@+ID/main_content"  androID:layout_wIDth="match_parent"  androID:layout_height="match_parent"  androID:fitsSystemwindows="true"  tools:context="com.example.geekp.myapplication.MainActivity">  <androID.support.design.Widget.AppbarLayout    androID:ID="@+ID/appbar"    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:paddingtop="@dimen/appbar_padding_top"    androID:theme="@style/Apptheme.AppbarOverlay">    <androID.support.v7.Widget.Toolbar      androID:ID="@+ID/toolbar"      androID:layout_wIDth="match_parent"      androID:layout_height="?attr/actionbarSize"      androID:background="?attr/colorPrimary"      app:layout_scrollFlags="scroll|enteralways"      app:popuptheme="@style/Apptheme.PopupOverlay">    </androID.support.v7.Widget.Toolbar>    <androID.support.design.Widget.TabLayout      androID:ID="@+ID/tabs"      androID:layout_wIDth="match_parent"      androID:layout_height="wrap_content" />  </androID.support.design.Widget.AppbarLayout>  <androID.support.v4.vIEw.VIEwPager    androID:ID="@+ID/container"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    app:layout_behavior="@string/appbar_scrolling_vIEw_behavior" /></androID.support.design.Widget.CoordinatorLayout>

fragment.xml

<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"  androID:layout_wIDth="match_parent"  androID:layout_height="match_parent">  <com.github.mikephil.charting.charts.PIEChart    androID:ID="@+ID/chart1"    androID:layout_margintop="100dp"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent" />  <TextVIEw    androID:ID="@+ID/tvXMax"    androID:layout_wIDth="50dp"    androID:layout_height="wrap_content"    androID:layout_alignParentRight="true"    androID:layout_marginBottom="15dp"    androID:layout_marginRight="10dp"    androID:gravity="right"    androID:textAppearance="?androID:attr/textAppearanceMedium" />  <TextVIEw    androID:ID="@+ID/tvYMax"    androID:layout_wIDth="50dp"    androID:layout_height="wrap_content"    androID:layout_alignParentRight="true"    androID:layout_marginBottom="15dp"    androID:layout_marginRight="10dp"    androID:gravity="right"    androID:textAppearance="?androID:attr/textAppearanceMedium" /></relativeLayout>

源码传送门:http://xiazai.jb51.net/201612/yuanma/piechart-master(jb51.net).rar

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

总结

以上是内存溢出为你收集整理的Android 实现会旋转的饼状统计图实例代码全部内容,希望文章能够帮你解决Android 实现会旋转的饼状统计图实例代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1147485.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存