问题
I'm working with the PieChart of the MPAndroidChart library.
I'm trying to generate the chart with data from my SQLite database but I have a problem. If my data have only one value, the chart is not generated, like in the image:
See that the colors not appear. The only thing that appear is the ValueText and the Legend in red circles.
However, if my data have more than one value, the chart is generated successfully, like in the image:
With two data values the chart is generated.
This is my GraficoActivity:
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import java.util.ArrayList;
import java.util.Map;
public class GraficoActivity extends AppCompatActivity {
private PieChart mChart;
private Map<String, Float> dadosGrafico = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grafico);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mChart = (PieChart) findViewById(R.id.pieChart);
final Grupo grupo = (Grupo) getIntent().getSerializableExtra("grupoGrafico");
GraficoDAO dao = new GraficoDAO(this);
dadosGrafico = dao.carregaDadosGrafico(grupo);
mChart.setTransparentCircleRadius(0);
mChart.setDescription("");
mChart.setUsePercentValues(true);
mChart.setMinimumWidth(500);
mChart.setMinimumHeight(500);
mChart.setDrawHoleEnabled(true);
mChart.setHoleRadius(0);
mChart.setRotationAngle(0);
mChart.setRotationEnabled(true);
mChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
@Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
if (e == null)
return;
}
@Override
public void onNothingSelected() {}
});
addData(dadosGrafico);
Legend l = mChart.getLegend();
l.setPosition(Legend.LegendPosition.BELOW_CHART_CENTER);
l.setXEntrySpace(7);
l.setYEntrySpace(8);
l.setTextColor(Color.BLACK);
l.setTextSize(12f);
}
private void addData(Map<String, Float> dadosGrafico) {
int i = 0;
ArrayList<Entry> valGrafico = new ArrayList<Entry>();
ArrayList<String> legendaGrafico = new ArrayList<String>();
for (Map.Entry<String, Float> entry : dadosGrafico.entrySet()) {
valGrafico.add(new Entry(entry.getValue(), i++));
legendaGrafico.add(entry.getKey());
}
PieDataSet dataSet = new PieDataSet(valGrafico, null);
dataSet.setSliceSpace(3);
dataSet.setSelectionShift(5);
ArrayList<Integer> cores = new ArrayList<Integer>();
for (int c : CoresGrafico.CORES_GRAFICO)
cores.add(c);
dataSet.setColors(cores);
PieData data = new PieData(legendaGrafico, dataSet);
data.setValueFormatter(new PercentFormatter());
data.setValueTextSize(11f);
data.setValueTextColor(Color.WHITE);
mChart.setData(data);
mChart.highlightValues(null);
mChart.invalidate();
}
}
And this is my GraficoDAO:
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.HashMap;
import java.util.Map;
public class GraficoDAO extends SQLiteOpenHelper {
private static final String DATABASE = "BancoConsultores";
private static final int VERSAO = 1;
private static final String TABLEAPONTA = "Apontamentos";
private static final String TABLEGRUPO = "Grupos";
private static final String TABLECONSULT = "Consultores";
private float[] valores;
private String[] consultores;
public GraficoDAO(Context ctx) {
super(ctx, DATABASE, null, VERSAO);
}
@Override
public void onCreate(SQLiteDatabase db) {}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
public Map<String,Float> carregaDadosGrafico(Grupo grupo) {
Map<String,Float> dados = new HashMap<String,Float>();
String sql = " SELECT SUM(horas_ap) AS total_horas, nome_con FROM " + TABLEAPONTA
+ " INNER JOIN " + TABLEGRUPO
+ " ON " + TABLEGRUPO + ".id = " + TABLEAPONTA + ".id_grupo "
+ " AND " + TABLEGRUPO + ".ano = " + grupo.getAno()
+ " INNER JOIN " + TABLECONSULT
+ " ON " +TABLECONSULT + ".id_con = " + TABLEAPONTA + ".id_consultor"
+ " WHERE " + TABLEAPONTA + ".id_grupo = " + grupo.getId()
+ " GROUP BY nome_con;";
Cursor c = getReadableDatabase().rawQuery(sql,null);
c.moveToFirst();
if (c.getCount() > 0){
do {
dados.put(c.getString(c.getColumnIndex("nome_con")),c.getFloat(c.getColumnIndex("total_horas")));
} while (c.moveToNext());
}
return dados;
}
}
Anyone can help me?
回答1:
I found solution for this. In my case I have 3 Entries, from which only one is non-zero value. I resolved this through setting slice-space to 0f
dataSet.setSliceSpace(0f);
Of course is up to you to set conditions for setting this 0 ;)
回答2:
This issue is already known and has already been fixed in the latest commits.
All releases v2.2.3+ will contain the fix. The next release will be out in a couple of days.
回答3:
In my case my PieEntry values were < 1. Just putting values bigger than 1 and using dataSet.setSliceSpace(0f); made it.
来源:https://stackoverflow.com/questions/35897894/mpandroidchart-pie-chart-is-not-generated-with-only-one-value