Cómo añadir títulos de Axis a mpandroidchart
Estoy intentando agregar títulos del eje al gráfico de barras creado usando la biblioteca de la carta de mpandroid. Estoy utilizando más de un fragmento en mi actividad y el gráfico de barras es un fragmento. Estoy agregando datos dinámicamente y todo está funcionando bien. Quiero añadir títulos a los ejes x e y y cuando busqué encontré que no es compatible con la biblioteca. Sugieren que añada la vista de texto en el gráfico o edite la biblioteca.
Sé que este problema es el mismo que este post, pero no tienen la respuesta. Cómo agregar String Label a MPAndroidCharts
- Eliminar "No hay datos de gráficos disponibles" de MPAndroidChart y añadir mi propio mensaje
- MPAndroidChart ¿hay una manera de establecer diferentes colores para diferentes bares?
- ¿Cómo obtener bar clic evento de gráficos horizontalbar de MPcharts en android?
- En MPAndroidChart, ¿cómo agregar el evento de clic para cada barra en Barchart?
- MPAndroidChart con valores nulos
Intenté agregar un textview para ver y se agregó a la tapa del gráfico. He intentado dar layout_gravity pero no está funcionando. Cómo agregar la vista de texto a la parte inferior de la vista. También necesito agregar el título del eje de y también. Intenté agregar al contenedor y la visión también.
public class BarChartFragment extends Fragment { ScreenUtility screenUtility; BarChart bar; String nameOfChart,xAxisDesciption,yAxisDescription; TextView xAxisName; public BarChartFragment() { // Required empty public constructor } @Override public void onCreate(Bundle savedInstanceState) { //this.setRetainInstance(true); super.onCreate(savedInstanceState); if (getArguments() != null) { } } public View getView(){ return bar.getRootView(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { screenUtility = new ScreenUtility(getActivity()); ArrayList<BarEntry> entries = new ArrayList<>(); BarDataSet dataSet = new BarDataSet(entries,nameOfChart); dataSet.setColors(getColors()); dataSet.setStackLabels(new String[]{"CU1", "CU2"}); dataSet.setValueFormatter(new MyValueFormatter()); ArrayList<String> labels = new ArrayList<String>(); ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>(); dataSets.add(dataSet); bar = new BarChart(getActivity()); BarData data = new BarData(labels,dataSets); bar.setData(data); bar.setDrawValuesForWholeStack(true); bar.setDrawBarShadow(false); bar.setDrawValueAboveBar(false); bar.setHighlightEnabled(false); bar.setDrawGridBackground(false); bar.setDescription(""); XAxis x = bar.getXAxis(); x.setDrawGridLines(false); x.setPosition(XAxis.XAxisPosition.BOTTOM); YAxis yLabels = bar.getAxisLeft(); yLabels.setDrawGridLines(false); YAxis yLabels1 = bar.getAxisRight(); yLabels1.setEnabled(false); Legend legend = bar.getLegend(); legend.setPosition(Legend.LegendPosition.BELOW_CHART_RIGHT); bar.animateY(2000); int layoutHeight = screenUtility.getWidth() > screenUtility.getHeight() ? (int) screenUtility.getHeight() : (int) screenUtility.getWidth(); if(screenUtility.getHeight()>screenUtility.getWidth()) { ViewGroup.LayoutParams params = new ViewGroup.LayoutParams((int) screenUtility.getWidth() - 20, layoutHeight); bar.getRootView().setLayoutParams(new ViewGroup.LayoutParams(params)); }else{ ViewGroup.LayoutParams params = new ViewGroup.LayoutParams((int) (screenUtility.getWidth()/2) - 20, layoutHeight); bar.getRootView().setLayoutParams(new ViewGroup.LayoutParams(params)); } bar.setNoDataText("No Data is Available"); //Tried adding Textview Here xAxisName = new TextView(getActivity()); xAxisName.setText("Date"); xAxisName.setGravity(Gravity.BOTTOM); container.addView(xAxisName); return bar.getRootView(); }
No puedo agregar el textview a la actividad porque todo se agrega dinámicamente y así que tengo que agregar el textview a cada gráfico. Cómo resolver esto y llevarlo al final del gráfico.
- MPAndroidChart - Añadir etiquetas al gráfico de barras
- Mostrar valores de eje dentro de las barras BarChart de la biblioteca MPAndroidChart
- MPAndroidChart BarChart horizontal / vertical
- ¿Cómo quitar la descripción del gráfico en MPAndroidChart?
- MPAndroidChart StackedBarChart mostrando valores pero sin barras
- ¿Cómo hacer que un CombinedChart tenga dos escalas diferentes en MPAndroidChart?
- MarkerView sale del gráfico para el último punto del gráfico
- Forzar BarChart eje Y etiquetas a ser enteros?
He encontrado la solución y la publicaré para futuras referencias para otras personas. Encontré el texto vertical desde el siguiente enlace.
Etiqueta vertical (girada) en Android
xAxisName = new TextView(getActivity()); xAxisName.setText("Date"); FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT); params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM; params.setMargins(0, 0, 0, 20); VerticalTextView yAxisName = new VerticalTextView(getActivity(),null); yAxisName.setText("Yaxis Label"); FrameLayout.LayoutParams params2 = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT); params2.gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL; container.addView(xAxisName, params); container.addView(yAxisName,params2);
Sugiero que usted (use additionalTextViews fuera del gráfico) o modifique la biblioteca para su propósito.
Actualmente no se admite de forma predeterminada.
- PlaceAutoCompleteFragment en android no muestra sugerencias y se cierra después de ingresar la primera letra
- Android Studio 2.2 Constraint Layout Botones no visibles