package com.fantasychong.piecharttest1028;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Description;
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.ValueFormatter;
import java.math.BigDecimal;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private PieChart piechart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
piechart = findViewById(R.id.main_piechart);
initPieChart(); //配置控件
initData(); //配置数据
}
/**
* 配置数据
*/
private void initData() {
ArrayList<PieEntry> pieEntries = new ArrayList<>();
String a = null;
String b = null;
String c = null;
String d = null;
a = "40%";
b = "30%";
c = "20%";
d = "10%";
a = a.substring(0, a.length() - 1);
b = b.substring(0, b.length() - 1);
c = c.substring(0, c.length() - 1);
d = d.substring(0, d.length() - 1);
pieEntries.add(new PieEntry(Float.parseFloat(a), "暗夜猎手"+ a));
pieEntries.add(new PieEntry(Float.parseFloat(b), "暴走萝莉"+ b));
pieEntries.add(new PieEntry(Float.parseFloat(c), "寒冰射手"+ c));
pieEntries.add(new PieEntry(Float.parseFloat(d), "诺克萨斯之手"+ d));
PieDataSet pieDataSet = new PieDataSet(pieEntries, null);
pieDataSet.setSliceSpace(0f);//设置每块饼之间的空隙
pieDataSet.setSelectionShift(0f);//点击某个饼时拉长的宽度
pieDataSet.setColors(getResources().getColor(R.color.colorAccent), getResources().getColor(R.color.color_348f79),
getResources().getColor(R.color.color_4e738d), getResources().getColor(R.color.color_fd794b));
PieData pieData = new PieData(pieDataSet);
pieData.setDrawValues(true); //设置是否显示数据实体(百分比,true:以下属性才有意义)
pieData.setValueTextColor(Color.WHITE); //设置所有DataSet内数据实体(百分比)的文本颜色
pieData.setValueTextSize(12f); //设置所有DataSet内数据实体(百分比)的文本字体大小
pieData.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value) {
Log.d("fantasychong_value", value + "");
if (value == 0) {
return "";
} else {
BigDecimal b = new BigDecimal(String.valueOf(value));
float num = b.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
return num + "%";
}
}
});
piechart.setData(pieData);
piechart.highlightValues(null);
piechart.invalidate(); //将图表重绘以显示设置的属性和数据
}
/**
* 配置控件
*/
private void initPieChart() {
piechart.setUsePercentValues(true);
Description description = piechart.getDescription();
description.setText(""); //设置描述的文字
piechart.setHighlightPerTapEnabled(true); //设置piecahrt图表点击Item高亮是否可用
piechart.animateX(0);
piechart.setDrawEntryLabels(false); // 设置entry中的描述label是否画进饼状图中
piechart.setEntryLabelColor(Color.WHITE);//设置该文字是的颜色
piechart.setEntryLabelTextSize(10f);//设置该文字的字体大小
piechart.setDrawHoleEnabled(true);//设置圆孔的显隐,也就是内圆
piechart.setHoleRadius(42f);//设置内圆的半径。外圆的半径好像是不能设置的,改变控件的宽度和高度,半径会自适应。
piechart.setHoleColor(Color.WHITE);//设置内圆的颜色
piechart.setDrawCenterText(false);//设置是否显示文字
piechart.setCenterText("");//设置饼状图中心的文字
piechart.setCenterTextSize(10f);//设置文字的消息
piechart.setCenterTextColor(Color.RED);//设置文字的颜色
piechart.setTransparentCircleRadius(31f);//设置内圆和外圆的一个交叉园的半径,这样会凸显内外部的空间
piechart.setTransparentCircleColor(Color.BLACK);//设置透明圆的颜色
piechart.setTransparentCircleAlpha(50);//设置透明圆你的透明度
piechart.setRotationAngle(20);
piechart.setRotationEnabled(false);
piechart.setExtraOffsets(10, 10, 10, 15);
Legend legend = piechart.getLegend();//图例
legend.setEnabled(true);//是否显示
legend.setVerticalAlignment(Legend.LegendVerticalAlignment.CENTER);//对齐
legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);//对齐
legend.setYOffset(-30f);
legend.setXOffset(15f);
legend.setYEntrySpace(10);
legend.setOrientation(Legend.LegendOrientation.VERTICAL);//设置图例的排列走向:vertacal相当于分行
legend.setForm(Legend.LegendForm.SQUARE);//设置图例的图形样式,默认为圆形
legend.setFormSize(15f);//设置图例的大小
legend.setTextSize(15f);//设置图注的字体大小
legend.setTextColor(getResources().getColor(R.color.color_000000));
legend.setFormToTextSpace(5f);//设置图例到图注的距离
legend.setDrawInside(false);
legend.setWordWrapEnabled(false);//设置图列换行(注意使用影响性能,仅适用legend位于图表下面),我也不知道怎么用的
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
PieChartTest1028.zip (99个子文件)
PieChartTest1028
gradlew 5KB
settings.gradle 15B
.DS_Store 6KB
.idea
libraries
Gradle__androidx_versionedparcelable_versionedparcelable_1_1_0_aar.xml 686B
Gradle__androidx_lifecycle_lifecycle_common_2_1_0_jar.xml 594B
Gradle__androidx_core_core_1_1_0_aar.xml 781B
Gradle__androidx_interpolator_interpolator_1_0_0_aar.xml 651B
Gradle__com_google_code_findbugs_jsr305_2_0_1_jar.xml 364B
Gradle__org_hamcrest_hamcrest_core_1_3_jar.xml 551B
Gradle__net_sf_kxml_kxml2_2_3_0_jar.xml 518B
Gradle__androidx_activity_activity_1_0_0_aar.xml 631B
Gradle__androidx_arch_core_core_runtime_2_0_0_aar.xml 645B
Gradle__androidx_appcompat_appcompat_resources_1_1_0_aar.xml 666B
Gradle__androidx_test_espresso_espresso_idling_resource_3_2_0_aar.xml 689B
Gradle__androidx_lifecycle_lifecycle_livedata_2_0_0_aar.xml 663B
Gradle__androidx_constraintlayout_constraintlayout_1_1_3_aar.xml 452B
Gradle__androidx_savedstate_savedstate_1_0_0_aar.xml 641B
Gradle__androidx_loader_loader_1_0_0_aar.xml 621B
Gradle__androidx_annotation_annotation_1_1_0_jar.xml 567B
Gradle__androidx_appcompat_appcompat_1_1_0_aar.xml 806B
Gradle__androidx_customview_customview_1_0_0_aar.xml 641B
Gradle__androidx_lifecycle_lifecycle_viewmodel_2_1_0_aar.xml 666B
Gradle__com_github_PhilJay_MPAndroidChart_v3_1_0_aar.xml 654B
Gradle__androidx_drawerlayout_drawerlayout_1_0_0_aar.xml 821B
Gradle__androidx_viewpager_viewpager_1_0_0_aar.xml 636B
Gradle__org_hamcrest_hamcrest_integration_1_3_jar.xml 586B
Gradle__androidx_collection_collection_1_1_0_jar.xml 567B
Gradle__javax_inject_javax_inject_1_jar.xml 536B
Gradle__junit_junit_4_12_jar.xml 495B
Gradle__androidx_arch_core_core_common_2_1_0_jar.xml 569B
Gradle__org_hamcrest_hamcrest_library_1_3_jar.xml 565B
Gradle__androidx_vectordrawable_vectordrawable_animated_1_1_0_aar.xml 688B
Gradle__com_squareup_javawriter_2_1_1_jar.xml 546B
Gradle__androidx_test_monitor_1_2_0_aar.xml 620B
Gradle__androidx_vectordrawable_vectordrawable_1_1_0_aar.xml 661B
Gradle__androidx_test_runner_1_2_0_aar.xml 617B
Gradle__androidx_test_espresso_espresso_core_3_2_0_aar.xml 656B
Gradle__androidx_fragment_fragment_1_1_0_aar.xml 801B
Gradle__androidx_lifecycle_lifecycle_runtime_2_1_0_aar.xml 660B
Gradle__androidx_cursoradapter_cursoradapter_1_0_0_aar.xml 656B
Gradle__androidx_lifecycle_lifecycle_livedata_core_2_0_0_aar.xml 678B
Gradle__androidx_constraintlayout_constraintlayout_solver_1_1_3_jar.xml 417B
runConfigurations.xml 564B
misc.xml 357B
workspace.xml 13KB
caches
build_file_checksums.ser 542B
gradle_models.ser 109KB
gradle.xml 580B
encodings.xml 135B
modules.xml 369B
gradle
wrapper
gradle-wrapper.properties 232B
gradle-wrapper.jar 53KB
.gradle
buildOutputCleanup
outputFiles.bin 51KB
cache.properties 51B
buildOutputCleanup.lock 17B
vcs-1
gc.properties 0B
5.1.1
executionHistory
executionHistory.bin 1.06MB
executionHistory.lock 17B
fileChanges
last-build.bin 1B
javaCompile
javaCompile.lock 17B
classAnalysis.bin 174KB
taskHistory.bin 22KB
fileContent
fileContent.lock 17B
vcsMetadata-1
fileHashes
fileHashes.bin 126KB
resourceHashesCache.bin 18KB
fileHashes.lock 17B
gc.properties 0B
local.properties 397B
gradlew.bat 2KB
gradle.properties 1KB
.gitignore 203B
app
src
androidTest
java
com
fantasychong
piecharttest1028
ExampleInstrumentedTest.java 737B
test
java
com
fantasychong
piecharttest1028
ExampleUnitTest.java 394B
main
AndroidManifest.xml 727B
res
mipmap-hdpi
ic_launcher.png 3KB
ic_launcher_round.png 5KB
mipmap-xxxhdpi
ic_launcher.png 9KB
ic_launcher_round.png 15KB
mipmap-xhdpi
ic_launcher.png 4KB
ic_launcher_round.png 7KB
mipmap-anydpi-v26
ic_launcher.xml 272B
ic_launcher_round.xml 272B
mipmap-mdpi
ic_launcher.png 2KB
ic_launcher_round.png 3KB
drawable
ic_launcher_background.xml 5KB
layout
activity_main.xml 533B
drawable-v24
ic_launcher_foreground.xml 2KB
values
colors.xml 444B
strings.xml 79B
styles.xml 383B
mipmap-xxhdpi
ic_launcher.png 6KB
ic_launcher_round.png 10KB
java
com
fantasychong
piecharttest1028
MainActivity.java 6KB
libs
app.iml 14KB
.gitignore 7B
build.gradle 996B
proguard-rules.pro 751B
PieChartTest1028.iml 870B
build.gradle 592B
共 99 条
- 1
资源评论
Fantasychong丶
- 粉丝: 70
- 资源: 37
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功