Estoy usando la biblioteca MPAndroid. implementación 'com.github.PhilJay: MPAndroidChart: v3.0.3' Necesito crear BarChart con etiquetas. Mi código está abajo pero no funciona: Esta es una matriz de barra para mostrar.
public ArrayList<BarEntry> getBarEntryArrayList() {
ArrayList<BarEntry> barEntries = new ArrayList<BarEntry>();
barEntries.add(new BarEntry(2f, 0));
barEntries.add(new BarEntry(4f, 1));
barEntries.add(new BarEntry(6f, 2));
barEntries.add(new BarEntry(8f, 3));
barEntries.add(new BarEntry(7f, 4));
barEntries.add(new BarEntry(3f, 5));
return barEntries;
}
Esta es una matriz de etiquetas para mostrar en la parte inferior.
public ArrayList<String> getBarEntryLabels() {
ArrayList<String> BarEntryLabels = new ArrayList<String>();
BarEntryLabels.add("J");
BarEntryLabels.add("F");
BarEntryLabels.add("M");
BarEntryLabels.add("A");
BarEntryLabels.add("M");
BarEntryLabels.add("J");
BarEntryLabels.add("J");
BarEntryLabels.add("A");
BarEntryLabels.add("S");
BarEntryLabels.add("O");
BarEntryLabels.add("N");
BarEntryLabels.add("D");
return BarEntryLabels;
}
Problema al establecer el conjunto de etiquetas y el conjunto de entrada
private void setBarChartData() {
BarDataSet barDataSet = new BarDataSet(getBarEntryArrayList(), getBarEntryLabels() );
barDataSet.setColors(ColorTemplate.COLORFUL_COLORS);
BarData barData = new BarData(barDataSet);
barChart.setData(barData);
barChart.animateY(3000);
}
Esta línea no funciona.
BarDataSet barDataSet = new BarDataSet(getBarEntryArrayList(), getBarEntryLabels() );
Si crea un código sin etiquetas, está funcionando.
BarDataSet barDataSet = new BarDataSet(getBarEntryArrayList(), "Dummy text" );
Tengo que usar una variedad de etiquetas. Gracias por adelantado.
Necesito crear un gráfico como este. Por favor, ayúdame.
3 respuestas
Cuando cree entradas de barra, agregue la etiqueta como tercer parámetro:
barEntries.add(new BarEntry(2f, 0, "J"));
barEntries.add(new BarEntry(4f, 1, "F"));
barEntries.add(new BarEntry(6f, 2, "M"));
barEntries.add(new BarEntry(8f, 3, "A"));
barEntries.add(new BarEntry(7f, 4, "M"));
barEntries.add(new BarEntry(3f, 5, "P"));
Después de barChart.setData(barData);
puede establecer etiquetas encima o debajo de las barras con setValueFormatter
:
barChart.getBarData().setValueFormatter(new IValueFormatter() {
@Override
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
return entry.getData().toString();
}
});
También aumente el tamaño de la fuente:
barChart.getData().setValueTextSize(15);
PD no necesitas el método getBarEntryLabels
.
Aquí está el resultado:
Puede configurarlo de la siguiente manera,
En Kotlin
val xAxis = chart.xAxis
xAxis.valueFormatter = IndexAxisValueFormatter(labels)
Si aún tiene problemas, asegúrese de no haber ocultado el eje x. Si oculta el eje x, no se mostrará la etiqueta. Si desea que el eje x esté oculto, debe hacer que el eje x sea visible y darle un color transparente.
Debes hacer lo siguiente:
barChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(BarEntryLabels));
Happy Coding :)
Nuevas preguntas
java
Java es un lenguaje de programación de alto nivel. Utilice esta etiqueta cuando tenga problemas para usar o comprender el idioma en sí. Esta etiqueta rara vez se usa sola y se usa con mayor frecuencia junto con [spring], [spring-boot], [jakarta-ee], [android], [javafx], [hadoop], [gradle] y [maven].