//图表库
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
X-axis: XAxis
Y-axis: YAxis
Legend: Legend
Description: Description
Limit line: LimitLine
Displayed view: MarkerView (it is displayed when selected)
The English scientific name of pie chart is Sector Graph, also known as Pie Graph. Commonly used in statistics modules. The 2D pie chart is circular, and when drawing by hand, a compass is often used for drawing. Data arranged in only one column or row of a worksheet can be plotted in a pie chart . Pie chart showing a data series
package com.xxmassdeveloper.mpchartexample.notimportant;
import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Typeface;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.view.View;
import android.widget.Toast;
import com.github.mikephil.charting.charts.Chart;
import com.sbas.xueliapplication.R;
/**
* Base class of all Activities of the Demo Application.
*
* @author Philipp Jahoda
*/
public abstract class DemoBase extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback {
protected final String[] months = new String[] {
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"
};
protected final String[] parties = new String[] {
"Party A", "Party B", "Party C", "Party D", "Party E", "Party F", "Party G", "Party H",
"Party I", "Party J", "Party K", "Party L", "Party M", "Party N", "Party O", "Party P",
"Party Q", "Party R", "Party S", "Party T", "Party U", "Party V", "Party W", "Party X",
"Party Y", "Party Z"
};
private static final int PERMISSION_STORAGE = 0;
protected Typeface tfRegular;
protected Typeface tfLight;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tfRegular = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");
tfLight = Typeface.createFromAsset(getAssets(), "OpenSans-Light.ttf");
}
protected float getRandom(float range, float start) {
return (float) (Math.random() * range) + start;
}
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.move_left_in_activity, R.anim.move_right_out_activity);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == PERMISSION_STORAGE) {
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
saveToGallery();
} else {
Toast.makeText(getApplicationContext(), "Saving FAILED!", Toast.LENGTH_SHORT)
.show();
}
}
}
protected void requestStoragePermission(View view) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
Snackbar.make(view, "Write permission is required to save image to gallery", Snackbar.LENGTH_INDEFINITE)
.setAction(android.R.string.ok, new View.OnClickListener() {
@Override
public void onClick(View v) {
ActivityCompat.requestPermissions(DemoBase.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_STORAGE);
}
}).show();
} else {
Toast.makeText(getApplicationContext(), "Permission Required!", Toast.LENGTH_SHORT)
.show();
ActivityCompat.requestPermissions(DemoBase.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_STORAGE);
}
}
protected void saveToGallery(Chart chart, String name) {
if (chart.saveToGallery(name + "_" + System.currentTimeMillis(), 70))
Toast.makeText(getApplicationContext(), "Saving SUCCESSFUL!",
Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(), "Saving FAILED!", Toast.LENGTH_SHORT)
.show();
}
protected abstract void saveToGallery();
}
<?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">
<RelativeLayout
android:layout_width="222px"
android:layout_height="400px">
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/chart1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/seekBar1" />
<SeekBar
android:id="@+id/seekBar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_margin="8dp"
android:layout_marginRight="5dp"
android:layout_toLeftOf="@+id/tvYMax"
android:max="200"
android:paddingBottom="12dp"
android:visibility="gone" />
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/seekBar2"
android:layout_margin="8dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="35dp"
android:layout_toLeftOf="@+id/tvXMax"
android:max="25"
android:paddingBottom="12dp"
android:visibility="gone" />
<TextView
android:id="@+id/tvXMax"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/seekBar1"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_marginBottom="15dp"
android:gravity="right"
android:text="@string/dash"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="gone" />
<TextView
android:id="@+id/tvYMax"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/seekBar2"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_marginBottom="15dp"
android:gravity="right"
android:text="@string/dash"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="gone" />
</RelativeLayout>
<LinearLayout
android:layout_marginTop="500px"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/actionToggleYValues"
android:text="是否显示Y轴文字"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/actionToggleXValues"
android:text="是否显示X轴文字"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/actionToggleIcons"
android:text="是否显示Icons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/actionTogglePercent"
android:text="是否显示百分比"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/actionToggleMinAngles"
android:text="是否显示最小角"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/actionToggleHole"
android:text="是否显示中间洞"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="590px"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/actionToggleCurvedSlices"
android:text="是否显示弯曲"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/actionDrawCenter"
android:text="是否显示中间文字"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/actionToggleSpin"
android:text="旋转动画"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/animateX"
android:text="X轴动画"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/animateY"
android:text="Y轴动画"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/animateXY"
android:text="XY轴动画"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/actionSave"
android:text="保存"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</RelativeLayout>
package com.xxmassdeveloper.mpchartexample;
import com.sbas.xueliapplication.R;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import androidx.core.content.ContextCompat;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
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.Entry;
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.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.IDataSet;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.github.mikephil.charting.utils.MPPointF;
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
import java.util.ArrayList;
public class PieChartActivity extends DemoBase implements OnSeekBarChangeListener,
OnChartValueSelectedListener,View.OnClickListener {
private PieChart pieChart;
private SeekBar seekBarX, seekBarY;
private TextView tvX, tvY;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_piechart);
setTitle("PieChartActivity");
tvX = findViewById(R.id.tvXMax);
tvY = findViewById(R.id.tvYMax);
seekBarX = findViewById(R.id.seekBar1);
seekBarY = findViewById(R.id.seekBar2);
seekBarX.setOnSeekBarChangeListener(this);
seekBarY.setOnSeekBarChangeListener(this);
pieChart = findViewById(R.id.chart1);
pieChart.setUsePercentValues(true);
pieChart.getDescription().setEnabled(false);
pieChart.setExtraOffsets(5, 10, 5, 5);
pieChart.setDragDecelerationFrictionCoef(0.95f);
pieChart.setCenterTextTypeface(tfLight);
pieChart.setCenterText(generateCenterSpannableText());
pieChart.setDrawHoleEnabled(true);
pieChart.setHoleColor(Color.WHITE);
pieChart.setTransparentCircleColor(Color.WHITE);
pieChart.setTransparentCircleAlpha(110);
pieChart.setHoleRadius(58f);
pieChart.setTransparentCircleRadius(61f);
pieChart.setDrawCenterText(true);
pieChart.setRotationAngle(0);
// enable rotation of the chart by touch
pieChart.setRotationEnabled(true);
pieChart.setHighlightPerTapEnabled(true);
// chart.setUnit(" €");
// chart.setDrawUnitsInChart(true);
// add a selection listener
pieChart.setOnChartValueSelectedListener(this);
seekBarX.setProgress(5);
seekBarY.setProgress(10);
pieChart.animateY(1400, Easing.EaseInOutQuad);
// chart.spin(2000, 0, 360);
//设置顶部 什么颜色代表什么东西。
Legend l = pieChart.getLegend();
l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
l.setDrawInside(true);
l.setXEntrySpace(7f);
l.setYEntrySpace(0f);
l.setYOffset(0f);
l.setWordWrapEnabled(true);
// entry label styling
pieChart.setEntryLabelColor(Color.WHITE);
pieChart.setEntryLabelTypeface(tfRegular);
pieChart.setEntryLabelTextSize(12f);
initButton();
}
private void initButton() {
Button actionToggleYValues = findViewById(R.id.actionToggleYValues);
actionToggleYValues.setOnClickListener(this::onClick);
Button actionToggleXValues = findViewById(R.id.actionToggleXValues);
actionToggleXValues.setOnClickListener(this::onClick);
Button actionToggleIcons = findViewById(R.id.actionToggleIcons);
actionToggleIcons.setOnClickListener(this::onClick);
Button actionTogglePercent = findViewById(R.id.actionTogglePercent);
actionTogglePercent.setOnClickListener(this::onClick);
Button actionToggleMinAngles = findViewById(R.id.actionToggleMinAngles);
actionToggleMinAngles.setOnClickListener(this::onClick);
Button actionToggleHole = findViewById(R.id.actionToggleHole);
actionToggleHole.setOnClickListener(this::onClick);
Button actionToggleCurvedSlices = findViewById(R.id.actionToggleCurvedSlices);
actionToggleCurvedSlices.setOnClickListener(this::onClick);
Button actionDrawCenter = findViewById(R.id.actionDrawCenter);
actionDrawCenter.setOnClickListener(this::onClick);
Button actionToggleSpin = findViewById(R.id.actionToggleSpin);
actionToggleSpin.setOnClickListener(this::onClick);
Button animateX = findViewById(R.id.animateX);
animateX.setOnClickListener(this::onClick);
Button animateY = findViewById(R.id.animateY);
animateY.setOnClickListener(this::onClick);
Button animateXY = findViewById(R.id.animateXY);
animateXY.setOnClickListener(this::onClick);
Button actionSave = findViewById(R.id.actionSave);
actionSave.setOnClickListener(this::onClick);
}
private void setData_count_range(int count, float range) {
ArrayList<PieEntry> entries = new ArrayList<>();
// NOTE: The order of the entries when being added to the entries array determines their position around the center of
// the chart.
for (int i = 0; i < count ; i++) {
float v =(float) ((Math.random() * range) + range / 5);
Log.e("tag",v+" setData_count_range");
entries.add(new PieEntry(v,
parties[i % parties.length],
getResources().getDrawable(R.drawable.star)));
}
PieDataSet dataSet = new PieDataSet(entries, "Election Results");
dataSet.setDrawIcons(false);
dataSet.setSliceSpace(3f);
dataSet.setIconsOffset(new MPPointF(0, 40));
dataSet.setSelectionShift(5f);
// add a lot of colors
ArrayList<Integer> colors = new ArrayList<>();
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 data = new PieData(dataSet);
data.setValueFormatter(new PercentFormatter());
data.setValueTextSize(11f);
data.setValueTextColor(Color.WHITE);
data.setValueTypeface(tfLight);
pieChart.setData(data);
// undo all highlights
pieChart.highlightValues(null);
pieChart.invalidate();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.viewGithub: {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PieChartActivity.java"));
startActivity(i);
break;
}
case R.id.actionToggleValues: {
for (IDataSet<?> set : pieChart.getData().getDataSets())
set.setDrawValues(!set.isDrawValuesEnabled());
pieChart.invalidate();
break;
}
case R.id.actionToggleIcons: {
for (IDataSet<?> set : pieChart.getData().getDataSets())
set.setDrawIcons(!set.isDrawIconsEnabled());
pieChart.invalidate();
break;
}
case R.id.actionToggleHole: {
if (pieChart.isDrawHoleEnabled())
pieChart.setDrawHoleEnabled(false);
else
pieChart.setDrawHoleEnabled(true);
pieChart.invalidate();
break;
}
case R.id.actionToggleMinAngles: {
if (pieChart.getMinAngleForSlices() == 0f)
pieChart.setMinAngleForSlices(36f);
else
pieChart.setMinAngleForSlices(0f);
pieChart.notifyDataSetChanged();
pieChart.invalidate();
break;
}
case R.id.actionToggleCurvedSlices: {
boolean toSet = !pieChart.isDrawRoundedSlicesEnabled() || !pieChart.isDrawHoleEnabled();
pieChart.setDrawRoundedSlices(toSet);
if (toSet && !pieChart.isDrawHoleEnabled()) {
pieChart.setDrawHoleEnabled(true);
}
if (toSet && pieChart.isDrawSlicesUnderHoleEnabled()) {
pieChart.setDrawSlicesUnderHole(false);
}
pieChart.invalidate();
break;
}
case R.id.actionDrawCenter: {
if (pieChart.isDrawCenterTextEnabled())
pieChart.setDrawCenterText(false);
else
pieChart.setDrawCenterText(true);
pieChart.invalidate();
break;
}
case R.id.actionToggleXValues: {
pieChart.setDrawEntryLabels(!pieChart.isDrawEntryLabelsEnabled());
pieChart.invalidate();
break;
}
case R.id.actionTogglePercent:
pieChart.setUsePercentValues(!pieChart.isUsePercentValuesEnabled());
pieChart.invalidate();
break;
case R.id.animateX: {
pieChart.animateX(1400);
break;
}
case R.id.animateY: {
pieChart.animateY(1400);
break;
}
case R.id.animateXY: {
pieChart.animateXY(1400, 1400);
break;
}
case R.id.actionToggleSpin: {
pieChart.spin(1000, pieChart.getRotationAngle(), pieChart.getRotationAngle() + 360, Easing.EaseInOutCubic);
break;
}
case R.id.actionSave: {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
saveToGallery();
} else {
requestStoragePermission(pieChart);
}
break;
}
}
return true;
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvX.setText(String.valueOf(seekBarX.getProgress()));
tvY.setText(String.valueOf(seekBarY.getProgress()));
setData_count_range(seekBarX.getProgress(), seekBarY.getProgress());
}
@Override
protected void saveToGallery() {
saveToGallery(pieChart, "PieChartActivity");
}
private SpannableString generateCenterSpannableText() {
SpannableString s = new SpannableString("MPAndroidChart\ndeveloped by Philipp Jahoda");
s.setSpan(new RelativeSizeSpan(1.7f), 0, 14, 0);
s.setSpan(new StyleSpan(Typeface.NORMAL), 14, s.length() - 15, 0);
s.setSpan(new ForegroundColorSpan(Color.GRAY), 14, s.length() - 15, 0);
s.setSpan(new RelativeSizeSpan(.8f), 14, s.length() - 15, 0);
s.setSpan(new StyleSpan(Typeface.ITALIC), s.length() - 14, s.length(), 0);
s.setSpan(new ForegroundColorSpan(ColorTemplate.getHoloBlue()), s.length() - 14, s.length(), 0);
return s;
}
@Override
public void onValueSelected(Entry e, Highlight h) {
if (e == null)
return;
Log.i("VAL SELECTED",
"Value: " + e.getY() + ", index: " + h.getX()
+ ", DataSet index: " + h.getDataSetIndex());
}
@Override
public void onNothingSelected() {
Log.i("PieChart", "nothing selected");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.viewGithub: {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PieChartActivity.java"));
startActivity(i);
break;
}
case R.id.actionToggleYValues: {
for (IDataSet<?> set : pieChart.getData().getDataSets())
set.setDrawValues(!set.isDrawValuesEnabled());
pieChart.invalidate();
break;
}
case R.id.actionToggleIcons: {
for (IDataSet<?> set : pieChart.getData().getDataSets())
set.setDrawIcons(!set.isDrawIconsEnabled());
pieChart.invalidate();
break;
}
case R.id.actionToggleHole: {
if (pieChart.isDrawHoleEnabled())
pieChart.setDrawHoleEnabled(false);
else
pieChart.setDrawHoleEnabled(true);
pieChart.invalidate();
break;
}
case R.id.actionToggleMinAngles: {
if (pieChart.getMinAngleForSlices() == 0f)
pieChart.setMinAngleForSlices(36f);
else
pieChart.setMinAngleForSlices(0f);
pieChart.notifyDataSetChanged();
pieChart.invalidate();
break;
}
case R.id.actionToggleCurvedSlices: {
boolean toSet = !pieChart.isDrawRoundedSlicesEnabled() || !pieChart.isDrawHoleEnabled();
pieChart.setDrawRoundedSlices(toSet);
if (toSet && !pieChart.isDrawHoleEnabled()) {
pieChart.setDrawHoleEnabled(true);
}
if (toSet && pieChart.isDrawSlicesUnderHoleEnabled()) {
pieChart.setDrawSlicesUnderHole(false);
}
pieChart.invalidate();
break;
}
case R.id.actionDrawCenter: {
if (pieChart.isDrawCenterTextEnabled())
pieChart.setDrawCenterText(false);
else
pieChart.setDrawCenterText(true);
pieChart.invalidate();
break;
}
case R.id.actionToggleXValues: {
pieChart.setDrawEntryLabels(!pieChart.isDrawEntryLabelsEnabled());
pieChart.invalidate();
break;
}
case R.id.actionTogglePercent:
pieChart.setUsePercentValues(!pieChart.isUsePercentValuesEnabled());
pieChart.invalidate();
break;
case R.id.animateX: {
pieChart.animateX(1400);
break;
}
case R.id.animateY: {
pieChart.animateY(1400);
break;
}
case R.id.animateXY: {
pieChart.animateXY(1400, 1400);
break;
}
case R.id.actionToggleSpin: {
pieChart.spin(1000, pieChart.getRotationAngle(), pieChart.getRotationAngle() + 360, Easing.EaseInOutCubic);
break;
}
case R.id.actionSave: {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
saveToGallery();
} else {
requestStoragePermission(pieChart);
}
break;
}
}
}
}