quisiera mostrar un gráfico en android studio utilizando la librería MPAndroidChart que se conecte a MySQL para mostrar los datos.
Tengo el gráfico solo me falta la conexión a la base de datos pero no tengo idea de como hacerla.
/**
* Crea el grafico
* */
public void setDataChart()
{
//obtiene registro de la base de datos
sqlite.openConnection();
ArrayList<Indicadores> candidatos = sqlite.getCandidatos();
sqlite.closeConnection();
//propiedades de grafico
pieChart.setRotationEnabled(true);
pieChart.animateXY(1000, 1000);
pieChart.setHoleRadius(36f);
pieChart.setDescription("");
//valores
ArrayList<Entry> yVals = new ArrayList<Entry>();
ArrayList<String> xVals = new ArrayList<String>();
//colores de la torta
ArrayList<Integer> colors = new ArrayList<Integer>();
colors.add(Color.parseColor("#FF0000"));
colors.add(Color.parseColor("#958A39FF"));
colors.add(Color.parseColor("#95FF4B32"));
colors.add(Color.parseColor("#9530A400"));
colors.add(Color.parseColor("#95258CFF"));
sqlite.openConnection();
float total_votos= sqlite.getTotalVotos();
sqlite.closeConnection();
//ingresa datos a grafico
for(int index=0;index<candidatos.size();index++)
{
float votos = (candidatos.get(index).getVotos()/total_votos)*100f;
yVals.add(new Entry(votos,index));
xVals.add( candidatos.get(index).getSigla());
}
PieDataSet dataSet = new PieDataSet(yVals, "");
dataSet.setSliceSpace(3f);
dataSet.setSelectionShift(5f);
dataSet.setColors(colors);
PieData data = new PieData(xVals, dataSet);
data.setValueFormatter( new PercentFormatter() );
data.setValueTextSize(12f);
data.setValueTextColor(Color.WHITE);
pieChart.setData(data);
pieChart.highlightValues(null);
pieChart.invalidate();
}
/**
* Crea una tabla con los registros de votacion
* */
private void createDataTable()
{
sqlite.openConnection();
ArrayList<Indicadores> candidatos = sqlite.getCandidatos();
sqlite.closeConnection();
//
TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams();
tableRowParams.setMargins(5, 3, 5, 3);
tableRowParams.weight = 1;
//encabezado
TableRow rowHead = new TableRow(this);
rowHead.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
//col 1
TextView col1 = new TextView(this);
col1.setBackgroundResource(R.drawable.tv_bg);
col1.setGravity(Gravity.CENTER);
col1.setTypeface(null, Typeface.BOLD);
col1.setTextSize(12);
col1.setTextColor((Color.parseColor("#FFFFFF")));
col1.setPadding(6, 10, 6, 10);
col1.setText( "SIGLA" );
//col 2
TextView col2 = new TextView(this);
col2.setBackgroundResource(R.drawable.tv_bg);
col2.setGravity(Gravity.CENTER);
col2.setTypeface(null, Typeface.BOLD);
col2.setTextSize(12);
col2.setTextColor((Color.parseColor("#FFFFFF")));
col2.setPadding(6, 10, 6, 10);
col2.setText( "INDICADORES" );
//col 3
TextView col3 = new TextView(this);
col3.setBackgroundResource(R.drawable.tv_bg);
col3.setGravity(Gravity.CENTER);
col3.setTypeface(null, Typeface.BOLD);
col3.setTextSize(12);
col3.setTextColor((Color.parseColor("#FFFFFF")));
col3.setPadding(6, 10, 6, 10);
col3.setText( "PORCENTAJE" );
//col 4
TextView col4 = new TextView(this);
col4.setBackgroundResource(R.drawable.tv_bg);
col4.setGravity(Gravity.CENTER);
col4.setTypeface(null, Typeface.BOLD);
col4.setTextSize(12);
col4.setTextColor((Color.parseColor("#FFFFFF")));
col4.setPadding(6, 10, 6, 10);
col4.setText( "FECHA" );
//a�ade columnas
rowHead.addView(col1,tableRowParams);
rowHead.addView(col2,tableRowParams);
rowHead.addView(col3,tableRowParams);
rowHead.addView(col4,tableRowParams);
//a�ade fila
table_layout.addView(rowHead);
//filas de datos
for(Indicadores c: candidatos)
{
TableRow row = new TableRow(this);
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
//col 1
TextView tv1 = new TextView(this);
tv1.setBackgroundResource(R.drawable.tv_bg);
tv1.setGravity(Gravity.CENTER);
tv1.setTypeface(null, Typeface.BOLD);
tv1.setTextSize(12);
tv1.setTextColor((Color.parseColor("#FFFFFF")));
tv1.setPadding(6, 10, 6, 10);
tv1.setText( c.getSigla() );
//col 2
TextView tv2 = new TextView(this);
tv2.setBackgroundResource(R.drawable.tv_bg);
tv2.setGravity(Gravity.CENTER);
tv2.setTextSize(12);
tv2.setTextColor((Color.parseColor("#FFFFFF")));
tv2.setPadding(6, 10, 6, 10);
tv2.setText( c.getNombre() );
//col 4
TextView tv4 = new TextView(this);
tv4.setBackgroundResource(R.drawable.tv_bg);
tv4.setGravity(Gravity.CENTER);
tv4.setTextSize(12);
tv4.setTextColor((Color.parseColor("#FFFFFF")));
tv4.setPadding(6, 10, 6, 10);
tv4.setText( c.getFecha() );
//col 3
TextView tv3 = new TextView(this);
tv3.setBackgroundResource(R.drawable.tv_bg);
tv3.setGravity(Gravity.CENTER);
tv3.setTextSize(12);
tv3.setTextColor((Color.parseColor("#FFFFFF")));
tv3.setPadding(6, 10, 6, 10);
tv3.setText( String.valueOf(c.getVotos()) );
//a�ade columnas
row.addView(tv1,tableRowParams);
row.addView(tv2,tableRowParams);
row.addView(tv3,tableRowParams);
row.addView(tv4,tableRowParams);
//a�ade fila
table_layout.addView(row);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.chart, menu);
return true;
}