仅使用一个值不生成MPAndroidChart饼图?

时间:2016-03-09 16:59:43

标签: android android-studio mpandroidchart

我正在使用MPAndroidChart库的PieChart。

我正在尝试使用SQLite数据库中的数据生成图表,但我遇到了问题。如果我的数据只有一个值,则不会生成图表,如图中所示: Chart not generated

看到颜色没有出现。唯一出现的是ValueText和红色圆圈中的Legend。

但是,如果我的数据有多个值,则会成功生成图表,如图所示: Chart generated

使用两个数据值生成图表。

这是我的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();
}
}

这是我的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;
}
}

任何人都可以帮助我?

3 个答案:

答案 0 :(得分:2)

我找到了解决方法。在我的情况下,我有3个条目,其中只有一个是非零值。我通过将切片空间设置为0f

来解决这个问题
dataSet.setSliceSpace(0f);

当然由您来设置设置此0的条件;)

答案 1 :(得分:1)

此问题已知,并且在最新提交中已经已修复

所有版本v2.2.3+都将包含此修复程序。下一个版本将在几天后发布。

答案 2 :(得分:0)

在我的情况下,我的PieEntry值是&lt; 1.只需将值大于1并使用dataSet.setSliceSpace(0f);做到了。