我正在使用" MPAndroidChart"我在饼图中显示了三个值,这些值是"标记为评论","已回答"和#34;未回答"各自的颜色是"#ff9600","#68e09b","#ececf0"。 如果我将多个值传递给图表,则颜色会正确显示,但是当我传递单个值时则不会。
当我传递单个值 - http://imgur.com/qIpJt2q时(问题!!)。
当我传递两个或更多值时 - http://imgur.com/4OGu0lZ
这是我的代码:
int bookmarked_questions_size = 0;
int answered_questions_size = 0;
int skipped_question_count = 30;
public void addChart() {
pieChart = (PieChart) findViewById(R.id.pausePieChart);
String testTitle = tagTestsModel.getTitle();
pieChart.setUsePercentValues(false);
pieChart.setDescription("");
pieChart.getLegend().setEnabled(false);
pieChart.setDrawHoleEnabled(true);
pieChart.setHoleRadius(20);
pieChart.setTransparentCircleRadius(20f);
pieChart.setCenterText("" + tagTestsModel.getQuestionCount());
ArrayList<String> xVals = new ArrayList<String>();
ArrayList<Entry> yVals = new ArrayList<Entry>();
ArrayList<Integer> colors = new ArrayList<Integer>();
if (bookmarked_questions_size > 0) {
xVals.add("Marked For Review");
yVals.add(new Entry(bookmarked_questions_size, 0));
colors.add(Color.parseColor("#ff9600"));
}
if (answered_questions_size > 0) {
xVals.add("Answered");
yVals.add(new Entry(answered_questions_size, 1));
colors.add(Color.parseColor("#68e09b"));
}
if (skipped_question_count > 0) {
xVals.add("Not Answered");
yVals.add(new Entry(skipped_question_count, 2));
colors.add(Color.parseColor("#ececf0"));
}
PieDataSet dataSet = new PieDataSet(yVals, "Topics Covered");
dataSet.setSliceSpace(3);
dataSet.setSelectionShift(5);
dataSet.setColors(colors);
PieData data = new PieData(xVals, dataSet);
data.calcMinMax(0, (int) tagTestsModel.getQuestionCount());
data.setValueTextSize(11f);
data.setValueTextColor(Color.GRAY);
data.setValueFormatter(new MyValueFormatter());
pieChart.setData(data);
pieChart.invalidate();
}
如果执行单个if条件,则会出现问题,否则它会按预期工作。
Xml代码:
<com.github.mikephil.charting.charts.PieChart
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
android:id="@+id/pausePieChart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/linearLayout3"
android:layout_below="@+id/questionTimeRelativeLayout" />
图书馆版:编译&#39; com.github.PhilJay:MPAndroidChart:v2.2.3&#39;
答案 0 :(得分:0)
将编译依赖项更改为compile 'com.github.PhilJay:MPAndroidChart:v2.2.4'
我使用该版本的MpAndroidChart尝试了您的代码并且它完美运行,当我更改为您使用的版本时,它产生了您的错误。