我制作的饼图如下图。
但是我希望在饼图周围显示标签(在饼图内部),并且当数据为零时,标签将不会显示,因此不会重叠。有人可以教我怎么做吗?预先谢谢你:)
这是我用Java编写的代码
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.utils.ColorTemplate;
public class MyRecyclerViewPer extends RecyclerView.Adapter<MyRecyclerViewPer.MyViewHolder> {
private Context context;
ArrayList <Integer> tlist;
TextView swipe;
String[] disorder = {"a", "b", "c","d","e"};
int[] one = new int[5];
int[] two = new int[5];
int[] three = new int[5];
int[] four = new int[5];
int[] five = new int[5];
private AdapterView.OnItemClickListener listener;
public MyRecyclerViewPer(Context c, ArrayList <Integer> t, int[] o, int[] tw, int[] th, int[] fo,int[] fi){
context=c;
tlist=t;
one=o;
two=tw;
three=th;
four=fo;
five=fi;
}
class MyViewHolder extends ViewHolder{
TextView tname,sc,o,t,th,fo,fi;
RelativeLayout parentLayout;
PieChart pieChart;
PieData pieData;
PieDataSet pieDataSet;
ArrayList pieEntries;
ArrayList PieEntryLabels;
public MyViewHolder(View view){
super(view);
sc=itemView.findViewById(R.id.score);
tname=itemView.findViewById(R.id.title);
o=itemView.findViewById(R.id.one);
t=itemView.findViewById(R.id.two);
th=itemView.findViewById(R.id.three);
fo=itemView.findViewById(R.id.four);
fi=itemView.findViewById(R.id.five);
parentLayout = itemView.findViewById(R.id.parent_layout);
swipe = itemView.findViewById(R.id.swipe);
swipe.setText("<<< Swipe left or right to refresh >>>");
pieChart = itemView.findViewById(R.id.pieChart);
pieEntries = new ArrayList<>();
pieDataSet = new PieDataSet(pieEntries, "");
pieData = new PieData(pieDataSet);
pieDataSet.setColors(ColorTemplate.JOYFUL_COLORS);
pieDataSet.setValueTextColor(Color.WHITE);
pieDataSet.setValueTextSize(10f);
pieDataSet.setSliceSpace(5f);
pieDataSet.setValueLinePart1OffsetPercentage(90.f);
pieDataSet.setValueLinePart1Length(.10f);
pieDataSet.setValueLinePart2Length(.50f);
}
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.chart,parent,false));
}
@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
holder.tname.setText(disorder[position]);
holder.sc.setText(String.valueOf(tlist.get(position)));
holder.o.setText(String.valueOf(one[position]));
holder.t.setText(String.valueOf(two[position]));
holder.th.setText(String.valueOf(three[position]));
holder.fo.setText(String.valueOf(four[position]));
holder.fi.setText(String.valueOf(five[position]));
holder.pieEntries = new ArrayList<>();
holder.pieEntries.add(new PieEntry(one[position], "Normal"));
holder.pieEntries.add(new PieEntry(two[position], "Mild"));
holder.pieEntries.add(new PieEntry(three[position], "Moderate"));
holder.pieEntries.add(new PieEntry(four[position], "Severe"));
holder.pieEntries.add(new PieEntry(five[position], "Very severe"));
holder.pieDataSet = new PieDataSet(holder.pieEntries, "");
holder.pieData = new PieData(holder.pieDataSet);
holder.pieChart.setData(holder.pieData);
holder.pieDataSet.setColors(ColorTemplate.JOYFUL_COLORS);
holder.pieDataSet.setSliceSpace(2f);
holder.pieDataSet.setValueTextColor(Color.WHITE);
holder.pieDataSet.setValueTextSize(10f);
holder.pieDataSet.setSliceSpace(5f);
}
@Override
public int getItemCount() {
return tlist.size();
}
}