Insert horizontal scrolling in line chart in Mpchart Android

Asked

Viewed 139 times

0

I’m using the library Android Mpchart (v3.0.1) to draw graphics on my Android app, however I wanted to make my chart scrollable on X-axis, this is my code:

private void loadDadosGraficoLinha() throws Exception{

    ArrayList<Peso> listaPesos = new DALPeso(ActivityHistoricoPeso.this).getListaPesos(ActivityHistoricoPeso.this);
    ArrayList<Entry> listaEntradasPesos = new ArrayList<>();
    //ArrayList<String> listaRotulosX = new ArrayList<>();
    //SimpleDateFormat objFormatoData = new SimpleDateFormat("dd/MM/yyyy");

    for(Peso peso: listaPesos){
        listaEntradasPesos.add(new Entry(peso.getCodigo(), peso.getPeso()));
        //listaRotulosX.add(objFormatoData.format(peso.getDataRegistro()));
    }

    LineDataSet objLineDataSet = new LineDataSet(listaEntradasPesos, "Peso (Kg)");//Iniciando dataset
    objLineDataSet.setColor(Color.rgb(255, 101, 0));//Seta a cor laranja para a linha
    objLineDataSet.setDrawFilled(true);//Preenche com uma cor o espaço abaixo das linhas
    objLineDataSet.setFillColor(Color.rgb(255, 101, 0));//Seta a cor do espaço abaixo das linhas laranja
    objLineDataSet.setCircleColor(Color.rgb(255, 101, 0));//Seta a cor dos pontos laranja

    LineData objLineData = new LineData(objLineDataSet);//Inicia um linha de valores no gráfico

    Description objDescription = new Description();//Cria um objeto de descrição
    objDescription.setText("Registros do último mês");//seta uma descrição para o gráfico

    YAxis objYAxis = graficoLinha.getAxisRight();
    objYAxis.setEnabled(false);

    graficoLinha.setDescription(objDescription);
    graficoLinha.animateY(2000);//Define uma animação de 2 segundos para o eixo Y
    graficoLinha.setHardwareAccelerationEnabled(true);//Habilita a aceleração de hardware
    graficoLinha.setScrollContainer(true);//Deveria habilitar a barra de rolagem no container mais não faz nada
    graficoLinha.setData(objLineData);//seta os dados que serão mostrados no gráfico
    graficoLinha.invalidate();//Atualiza o gráfico
}

The result of this code is this: Gráfico em linha

However each time I insert a new weight it keeps squeezing the values in this graph space when in fact my intention is that it loads all the records but leaves these rollable data horizontal without decreasing the zoom factor and without squeezing the points in this small layout space

  • Sorry I do not know if I understood well or I think I did not express well, but the intention is that it loads all the records even, however, I want all this data to be rollable horizontally without decreasing the zoom factor and without squeezing the points in this small layout space

  • You said, "I wanted to fixed display only 5 points of the X-axis". Are they all or only 5? Not very clear.

  • 1

    Corrected question, I need all the data to be loaded into the graph according to the spinner filter, that is, if there are a thousand points it will bring me the thousand records the problem is that the Mpchart adds these thousand records without creating a scrolling and I want to create a horizontal scrolling so that I can view the rest

  • After trying continuously since last night I managed to find a way to solve my problem, just insert the following line of code: graficoLinha.setVisibleXRangeMaximum(4f);

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.