我正在使用mpandroidchart库来构建饼图。饼图的要求是它应该在每个条目中包含图标。
有没有办法将标签更改为图标?
我的片段类
public class MonitorOverallFragment extends Fragment {
private int[] CHART_COLORS = {Color.rgb(253,151,39), Color.rgb(103,63,180), Color.rgb(204,217,72), Color.rgb(44,152,240)};
private Context mContext;
private Activity mActivity;
@Override
public void onAttach(Context context) {
super.onAttach(context);
mContext = context;
icons1 = new Bitmap[]{BitmapFactory.decodeResource(mContext.getResources(),
R.drawable.ic_card_analitics_white),
BitmapFactory.decodeResource(mContext.getResources(),
R.drawable.ic_kredit_analitics_white),
BitmapFactory.decodeResource(mContext.getResources(),
R.drawable.ic_vklad_analitics_white),
BitmapFactory.decodeResource(mContext.getResources(),
R.drawable.ic_nps_analitics_white)};
}
public MonitorOverallFragment(){
}
int [] sampledata = {30, 40, 20, 10};
Bitmap[] icons1;
private String icons[] = {"a", "b", "c", "d"};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_monitor_overall, container, false);
setUpPieChart(rootView);
return rootView;
}
private void setUpPieChart(View v){
List<PieEntry> pieEntries = new ArrayList<>();
for(int i = 0; i<sampledata.length; i++){
pieEntries.add(new PieEntry(sampledata[i], icons1[i]));
}
PieDataSet dataSet = new PieDataSet(pieEntries, "");
dataSet.setColors(CHART_COLORS);
PieData data = new PieData(dataSet);
PieChart chart = (PieChart)v.findViewById(R.id.pie_monitor_overall);
chart.setData(data);
chart.setDrawEntryLabels(true);
Description description = new Description();
description.setText("");
chart.setDescription(description);
chart.invalidate();
}
}
答案 0 :(得分:2)
以下是我的代码中的示例:
Drawable user_icon = getDrawable(R.drawable.user);
myData = new ArrayList<>();
myData.add(new PieEntry(46, user_icon));
PieDataSet dataSet = new PieDataSet(myData, "");
PieData data = new PieData(dataSet);
pieChart.setData(data);
(我知道它没有得到很好的解释,但既然你问的是图标,那么你已经完成了图书馆的简单实现了)
请注意我使用的是3.0.2版本,以前的版本可能没有此功能。