这是我的XML:
<net.kenyang.piechart.PieChart
android:id="@+id/pieChart"
android:layout_below="@id/id_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
这是我用于初始化饼图的代码:
ArrayList<PieChartData> alPercentage = new ArrayList<PieChartData>();
alPercentage.add(new PieChartData(55.0f, "1"));
alPercentage.add(new PieChartData(45.0f, "2"));
try {
// setting data
pieChart.setAdapter(alPercentage);
// setting a listener
pieChart.setOnSelectedListener(new PieChart.OnSelectedLisenter() {
@Override
public void onSelected(int iSelectedIndex) {
Toast.makeText(PSTravelStatisticsActivity.this, "Select index:" + iSelectedIndex, Toast.LENGTH_SHORT).show();
}
});
pieChart.setVisibility(View.VISIBLE);
pieChart.invalidate();
} catch (Exception e) {
Log.i("","error is : " + e.getMessage());
if (e.getMessage().equals(PieChart.ERROR_NOT_EQUAL_TO_100)){
Log.e("kenyang", "percentage is not equal to 100");
}
}
现在它没有给我一个错误 如果我尝试以百分比小于100的方式制作它,那么它会引发异常。所以这意味着使用这个逻辑,它应该工作,它确实发送了2个日志,但就是这样:
10-08 16:10:11.556 12591-12591/nl.hgrams.passenger I/net.kenyang.piechart.PieChart﹕ PieChart init
10-08 16:10:11.626 12591-12591/nl.hgrams.passenger I/net.kenyang.piechart.PieChart﹕ onDraw
答案 0 :(得分:0)
它是否喜欢这样,然后才有效:
final PieRenderer pieRenderer = pieChart.getRenderer(PieRenderer.class);
pieRenderer.setDonutSize((float) 75 / 100, PieRenderer.DonutMode.PERCENT);
selectDataType();
pieChart.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(pieRenderer.getContainingSegment(new PointF(event.getX(), event.getY())) == bikeS){
segment = 0;
selectSegment(segment, true);
}
if(pieRenderer.getContainingSegment(new PointF(event.getX(), event.getY())) == carS){
segment = 1;
selectSegment(segment, true);
}
if(pieRenderer.getContainingSegment(new PointF(event.getX(), event.getY())) == transitS){
segment = 2;
selectSegment(segment, true);
}
if(pieRenderer.getContainingSegment(new PointF(event.getX(), event.getY())) == walkS){
segment = 3;
selectSegment(segment, true);
}
return false;
}
});
pieChart.removeSegment(carS);
pieChart.removeSegment(walkS);
pieChart.removeSegment(transitS);
pieChart.removeSegment(bikeS);
bikeS = new Segment("", tm.getBicycling());
carS = new Segment("", tm.getDriving());
transitS = new Segment("", tm.getTransit());
walkS = new Segment("", tm.getWalking());
pieChart.addSeries(bikeS, new SegmentFormatter(bikeCP, bikeCP, bikeCP, bikeCP));
pieChart.addSeries(carS, new SegmentFormatter(carC, carC, carC, carC));
pieChart.addSeries(transitS, new SegmentFormatter(transitC, transitC, transitC, transitC));
pieChart.addSeries(walkS, new SegmentFormatter(walkC, walkC, walkC, walkC));
typeText.setText(getString(R.string.bike));
typePercent.setTextColor(getResources().getColor(R.color.bike_color));
typeImage.setImageResource(R.drawable.profile_travel_mode_pie_bicycling);
pieChart.invalidate();