MainActivity类代码:
private float[] yData = {25, 35, 40};
private String[] xData = {"x", "y", "z"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainLayout = (RelativeLayout) findViewById(R.id.layot);
mChart = (PieChart) findViewById(R.id.view);
mainLayout.setBackgroundColor(Color.parseColor("#ffffff"));
mChart.setUsePercentValues(true);
mChart.setMaxAngle(360);
mChart.setDrawHoleEnabled(true);
mChart.setHoleRadius(2);
mChart.setRotationAngle(360);
mChart.setRotationEnabled(false);
mChart.setHighlightPerTapEnabled(false);
mChart.setTransparentCircleRadius(0);
ImageView imageView=new ImageView(this);
imageView.setImageResource(R.mipmap.ic_launcher);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100, 100);
imageView.setLayoutParams(layoutParams);
mChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
@Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
// display msg when value selected
if (e == null)
return;
Toast.makeText(MainActivity.this,
xData[e.getXIndex()] + " = " + e.getVal() + "%", Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected() {
}
});
addData();
MyMarkerView mv = new MyMarkerView(getApplicationContext(), R.layout.marker_layout);
mChart.setMarkerView(mv);
}
private void addData() {
ArrayList<Entry> entries = new ArrayList<>();
for (int i=0;i<yData.length;i++){
entries.add(new Entry(yData[i],i));
}
PieDataSet dataset = new PieDataSet(entries, "# of Calls");
dataset.setSliceSpace(0);
dataset.setColors(ColorTemplate.COLORFUL_COLORS);
ArrayList<String> labels = new ArrayList<String>();
for (int i=0;i<xData.length;i++){
labels.add(xData[i]);
}
PieData data = new PieData(labels, dataset); // initialize Piedata
mChart.setData(data);
mChart.setDescription("Description");
mChart.animateY(2000);
}
和MyMarkerView类
public MyMarkerView(Context context, int layoutResource) {
super(context, layoutResource);
tvContent = (TextView) findViewById(R.id.tvContent);
tvContent.setText("xyz");
}
// callbacks everytime the MarkerView is redrawn, can be used to update the
// content (user-interface)
@Override
public void refreshContent(Entry e, Highlight highlight) {
if (e instanceof CandleEntry) {
CandleEntry ce = (CandleEntry) e;
tvContent.setText("" + Utils.formatNumber(ce.getHigh(), 0, true));
} else {
tvContent.setText("" + Utils.formatNumber(e.getVal(), 0, true));
}
}
@Override
public int getXOffset(float xpos) {
// this will center the marker-view horizontally
return (getWidth() / 2);
}
@Override
public int getYOffset(float ypos) {
// this will cause the marker-view to be above the selected value
return getHeight();
}
我在饼图上使用此代码进行标记,但未在图表上显示任何内容。我的代码中有错误吗?请帮忙。
答案 0 :(得分:0)
您的代码一见钟情。 也许只需添加:
mchart.setDrawMarkerViews(true);
将customMarkerView附加到图表后。