当前位置:   article > 正文

MPAndroidChart使用之HalfPieChart_com.github.mikephil.charting.charts.piechart实现半圆

com.github.mikephil.charting.charts.piechart实现半圆

一、引入MPAndroidChart库:

项目下build.gradle:

  1. allprojects {
  2. repositories {
  3. maven { url "https://jitpack.io" }
  4. }
  5. }
your app下build.gradle:

  1. dependencies {
  2. compile 'com.github.PhilJay:MPAndroidChart:v3.0.2'
  3. }
Sync Now即可。

二、
效果:


布局:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent" >
  5. <com.github.mikephil.charting.charts.PieChart
  6. android:id="@+id/chart1"
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent" />
  9. </RelativeLayout>

HalfPieChartActivity.java

  1. package com.james.mpandroidchart.ui.activity;
  2. import android.graphics.Color;
  3. import android.graphics.Typeface;
  4. import android.os.Bundle;
  5. import android.support.annotation.Nullable;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.text.SpannableString;
  8. import android.text.style.ForegroundColorSpan;
  9. import android.text.style.RelativeSizeSpan;
  10. import android.text.style.StyleSpan;
  11. import android.view.Display;
  12. import android.widget.RelativeLayout;
  13. import com.github.mikephil.charting.animation.Easing;
  14. import com.github.mikephil.charting.charts.PieChart;
  15. import com.github.mikephil.charting.components.Legend;
  16. import com.github.mikephil.charting.data.PieData;
  17. import com.github.mikephil.charting.data.PieDataSet;
  18. import com.github.mikephil.charting.data.PieEntry;
  19. import com.github.mikephil.charting.formatter.PercentFormatter;
  20. import com.github.mikephil.charting.utils.ColorTemplate;
  21. import com.james.mpandroidchart.R;
  22. import java.util.ArrayList;
  23. /**
  24. * Created by 1 on 2017/3/28.
  25. */
  26. public class HalfPieChartActivity extends AppCompatActivity {
  27. private PieChart mChart;
  28. private Typeface mTfLight;
  29. private Typeface mTfRegular;
  30. private String[] mParties = new String[] {
  31. "Party A", "Party B", "Party C", "Party D", "Party E", "Party F", "Party G", "Party H",
  32. "Party I", "Party J", "Party K", "Party L", "Party M", "Party N", "Party O", "Party P",
  33. "Party Q", "Party R", "Party S", "Party T", "Party U", "Party V", "Party W", "Party X",
  34. "Party Y", "Party Z"
  35. };
  36. @Override
  37. protected void onCreate(@Nullable Bundle savedInstanceState) {
  38. super.onCreate(savedInstanceState);
  39. mTfLight = Typeface.createFromAsset(getAssets(), "OpenSans-Light.ttf");
  40. mTfRegular = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");
  41. setContentView(R.layout.activity_piechart_half);
  42. mChart = (PieChart) findViewById(R.id.chart1);
  43. moveOffScreen();
  44. mChart.setUsePercentValues(true);
  45. mChart.getDescription().setEnabled(false);
  46. mChart.setCenterTextTypeface(mTfLight);//设置字体
  47. mChart.setCenterText(generateCenterSpannableText());//中间显示文字
  48. mChart.setDrawHoleEnabled(true);//true设置绘图孔启用
  49. mChart.setHoleColor(Color.WHITE);//设置绘图孔颜色
  50. mChart.setTransparentCircleColor(Color.WHITE);//
  51. mChart.setTransparentCircleAlpha(110);//透明度
  52. mChart.setHoleRadius(58f);//中间圆的半径
  53. mChart.setTransparentCircleRadius(61f);//
  54. mChart.setDrawCenterText(true);//中心是否允许画文字
  55. mChart.setRotationEnabled(false);//整个视图是否旋转
  56. mChart.setHighlightPerTapEnabled(true);//true点击各个板块会向上突起一点
  57. mChart.setMaxAngle(180f); // 显示一半
  58. mChart.setRotationAngle(180f);
  59. mChart.setCenterTextOffset(0, -20);//中间文字向上偏移20
  60. setData(4, 100);//数据
  61. mChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);//设置进来动画。1400是动画执行的时间
  62. Legend l = mChart.getLegend();
  63. l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);//竖直方向
  64. l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);//水平方向设置
  65. l.setOrientation(Legend.LegendOrientation.VERTICAL);//PartyA,PartyB,PartyC,PartyD竖直排放
  66. l.setDrawInside(false);
  67. l.setXEntrySpace(7f);//PartyA,PartyB,PartyC,PartyD的x轴间距
  68. l.setYEntrySpace(0f);//PartyA,PartyB,PartyC,PartyD的y轴间距
  69. l.setYOffset(0f);//y轴偏移量
  70. // entry label styling
  71. mChart.setEntryLabelColor(Color.WHITE);
  72. mChart.setEntryLabelTypeface(mTfRegular);
  73. mChart.setEntryLabelTextSize(12f);
  74. }
  75. private void setData(int count, float range) {
  76. ArrayList<PieEntry> values = new ArrayList<PieEntry>();
  77. for (int i = 0; i < count; i++) {
  78. values.add(new PieEntry((float) ((Math.random() * range) + range / 5), mParties[i % mParties.length]));
  79. }
  80. PieDataSet dataSet = new PieDataSet(values, "Election Results");
  81. dataSet.setSliceSpace(3f);
  82. dataSet.setSelectionShift(5f);
  83. dataSet.setColors(ColorTemplate.MATERIAL_COLORS);
  84. //dataSet.setSelectionShift(0f);
  85. PieData data = new PieData(dataSet);
  86. data.setValueFormatter(new PercentFormatter());
  87. data.setValueTextSize(11f);
  88. data.setValueTextColor(Color.WHITE);
  89. data.setValueTypeface(mTfLight);//设置字体
  90. mChart.setData(data);
  91. mChart.invalidate();
  92. }
  93. private void moveOffScreen() {
  94. Display display = getWindowManager().getDefaultDisplay();
  95. int height = display.getHeight(); // deprecated
  96. int offset = (int)(height * 0.65); /* percent to move */
  97. RelativeLayout.LayoutParams rlParams =
  98. (RelativeLayout.LayoutParams)mChart.getLayoutParams();
  99. rlParams.setMargins(0, 0, 0, -offset);
  100. mChart.setLayoutParams(rlParams);
  101. }
  102. private SpannableString generateCenterSpannableText() {
  103. SpannableString s = new SpannableString("MPAndroidChart\ndeveloped by Philipp Jahoda");
  104. s.setSpan(new RelativeSizeSpan(1.7f), 0, 14, 0);
  105. s.setSpan(new StyleSpan(Typeface.NORMAL), 14, s.length() - 15, 0);
  106. s.setSpan(new ForegroundColorSpan(Color.GRAY), 14, s.length() - 15, 0);
  107. s.setSpan(new RelativeSizeSpan(.8f), 14, s.length() - 15, 0);
  108. s.setSpan(new StyleSpan(Typeface.ITALIC), s.length() - 14, s.length(), 0);
  109. s.setSpan(new ForegroundColorSpan(ColorTemplate.getHoloBlue()), s.length() - 14, s.length(), 0);
  110. return s;
  111. }
  112. }



 mChart.setTransparentCircleRadius(61f);//
指的是:

 mChart.setEntryLabelColor(Color.RED);

设置的是

  1. Legend l = mChart.getLegend();
  2. l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
  3. l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
  4. l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
  5. l.setDrawInside(false);
  6. l.setXEntrySpace(7f);
  7. l.setYEntrySpace(0f);
  8. l.setYOffset(0f);

设置的是:





声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/254822
推荐阅读
相关标签
  

闽ICP备14008679号