Android Mp Chart饼图不显示单一颜色但显示多种颜色
Androidmpchart piechart not displaying single color but displaying colors when more than one
我正在使用 "MPAndroidChart",我要在饼图中显示三个值,它们是 "Marked For Review"、"Answered" 和 "Not Answered",它们各自的颜色是“ #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" />
库版本:编译'com.github.PhilJay:MPAndroidChart:v2.2.3'
将您的编译依赖项更改为 compile 'com.github.PhilJay:MPAndroidChart:v2.2.4'
我用那个版本的 MpAndroidChart 尝试了您的代码并且它运行良好,当我更改为您使用的版本时,同样它产生了您遇到的错误。
我正在使用 "MPAndroidChart",我要在饼图中显示三个值,它们是 "Marked For Review"、"Answered" 和 "Not Answered",它们各自的颜色是“ #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" />
库版本:编译'com.github.PhilJay:MPAndroidChart:v2.2.3'
将您的编译依赖项更改为 compile 'com.github.PhilJay:MPAndroidChart:v2.2.4'
我用那个版本的 MpAndroidChart 尝试了您的代码并且它运行良好,当我更改为您使用的版本时,同样它产生了您遇到的错误。