近段时间要再项目上加统计图(饼状图),觉得MPAndroidChart这个统计图控件很好用,但是并没有明确它的点击事件,而是点击之后放大部分图区,再次点击回归正常,而我们的要求是需要点击展示相对应的图的数据,没有我们想要的点击事件我们就找,用另一种方法来实现
添加MPAndroidChart依赖
//MPAndroidChart
implementation 'com.github.PhilJay:MPAndroidChart:v3.0.2'
在布局设置控件
<com.dhy.health.mpandroidchartdemo.mpandroidchart.MyPieChart
android:id="@+id/pie_chart"
android:layout_margin="30dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="50dp" />
实现饼状图
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.dhy.health.mpandroidchartdemo.mpandroidchart.MyPieChart;
import com.github.mikephil.charting.animation.Easing;
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.listener.OnChartValueSelectedListener;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
MyPieChart pie_chart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pie_chart=findViewById(R.id.pie_chart);
setDgcChart();
}
public void setDgcChart() {
//TODO 设置饼图还是环形图
pie_chart.setDrawHoleEnabled(false);
//TODO 设置显示的数值是占百分比还是直接显示数值
pie_chart.setUsePercentValues(true);
//TODO 如果设置为true 组件内容不可删除
pie_chart.getDescription().setEnabled(false);
//TODO 设置附加到自动计算的偏移的额外偏移(在图表视图周围)。
pie_chart.setExtraOffsets(0.f, 50.f, 0.f, 80.f);
//TODO 减速摩擦系数在[0;1]区间内,数值越大表示速度会缓慢下降,例如,如果设定为0,则会立即停止。1是无效值,将自动转换为0.999f。
pie_chart.setDragDecelerationFrictionCoef(0.5f);
//TODO 设置环形图中间孔的空白的背景颜色
pie_chart.setHoleColor(Color.WHITE); //中间圆颜色
//TODO 设置环形图中间的空白和环形图之间的浅色区域
pie_chart.setTransparentCircleColor(Color.WHITE);
//TODO 设置中间隔开区域的透明度
pie_chart.setTransparentCircleAlpha(110);
//TODO 设置环形图中间空白区域的大小(半径)
pie_chart.setHoleRadius(30f); //圆半径
//TODO 设置中间浅色区域的大小
pie_chart.setTransparentCircleRadius(44f);
pie_chart.setDrawCenterText(true);
pie_chart.setRotationAngle(-90);
//TODO 设置是否可以旋转
pie_chart.setRotationEnabled(false);
//TODO 是否可以点击
pie_chart.setHighlightPerTapEnabled(true);
//TODO 触摸监听
pie_chart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
@Override
public void onValueSelected(Entry entry, Highlight highlight) {
Log.e("*************", "开启");
if (highlight.getX() == 0) {
Toast.makeText(MainActivity.this,""+entries.get(0).getLabel(),Toast.LENGTH_SHORT).show();
} else if (highlight.getX() == 1.0) {
Toast.makeText(MainActivity.this,""+entries.get(1).getValue(),Toast.LENGTH_SHORT).show();
} else if (highlight.getX() == 2.0) {
Toast.makeText(MainActivity.this,""+entries.get(2).toString(),Toast.LENGTH_SHORT).show();
}
}
@Override
public void onNothingSelected() {
Log.e("*************", "关闭");
}
});
//((PieHolder) holder).pie_chart.setEntryLabelColor(mContext.getResources().getColor(R.color.red_main)); //设置饼图标签颜色
//TODO 添加数据
setPieData(pie_chart);
//TODO 设置加载数据的动画
pie_chart.animateY(1200, Easing.EasingOption.EaseInOutQuad); //设置饼图动画
pie_chart.setDrawEntryLabels(!pie_chart.isDrawEntryLabelsEnabled());
Legend l = pie_chart.getLegend();
l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
l.setOrientation(Legend.LegendOrientation.VERTICAL);
l.setDrawInside(false);
l.setTextColor(Color.BLUE);
l.setEnabled(false);
}
//TODO 添加选项的文字
ArrayList<PieEntry> entries = new ArrayList<PieEntry>();
//TODO 添加选项的颜色
ArrayList<Integer> colors = new ArrayList<Integer>();
/**
* 设置饼图数据
*/
private void setPieData(MyPieChart chart) {
entries.add(new PieEntry(50f, "偏低"));
entries.add(new PieEntry(20f, "正常"));
entries.add(new PieEntry(30f, "偏高"));
colors.add(Color.parseColor("#78B2F3"));
colors.add(Color.parseColor("#3AD29F"));
colors.add(Color.parseColor("#FDBE00"));
PieDataSet dataSet = new PieDataSet(entries, "Election Results");
//TODO 不同块之间的间距
dataSet.setSliceSpace(0f);
//TODO 选中时候突出的间距
dataSet.setSelectionShift(0f);
//TODO 设置选项的颜色
dataSet.setColors(colors);
//TODO 设置饼图内的指示线的位置(是靠近内环还是靠近外环)
dataSet.setValueLinePart1OffsetPercentage(100f);
//TODO 设置指向饼图的指示线的长度1
dataSet.setValueLinePart1Length(0.7f);
//TODO 设置指向文字的指示线的长度2
dataSet.setValueLinePart2Length(0.0f);
dataSet.setHighlightEnabled(true);
//TODO 设置指示线的粗细
dataSet.setValueLineWidth(2f);
//TODO 设置提示文字在图外还是图内
dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
//TODO 设置指示线条颜色,必须设置成这样,才能和饼图区域颜色一致
dataSet.setValueLineColor(0xff000000);
//TODO 设置提示文字的颜色
dataSet.setValueTextColor(Color.BLACK);
PieData data = new PieData(dataSet);
data.setValueFormatter(new PercentFormatter());
data.setValueTextSize(12f);
data.setHighlightEnabled(true);
chart.setData(data);
chart.highlightValues(null);
chart.invalidate();
}
}
相关方法在注释里面写的很清楚了