Doubt with the Androidplot

Asked

Viewed 78 times

0

Personal someone could tell me why this code is not generating the desired graph?

//screen code

import java.text.DecimalFormat;
import java.text.FieldPosition;
import java.text.Format;
import java.text.ParsePosition;
import java.util.Arrays;
import java.util.Observable;
import java.util.Observer;

import com.androidplot.Plot;
import com.androidplot.xy.BoundaryMode;
import com.androidplot.xy.LineAndPointFormatter;
import com.androidplot.xy.SimpleXYSeries;
import com.androidplot.xy.XYPlot;
import com.androidplot.xy.XYSeries;
import com.androidplot.xy.XYStepMode;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.RadioGroup;
import android.widget.Spinner;


public class TelaB extends Activity {

        //Vetor de culturas da comboBox;
        private String[] culturas = new String[] {"Milho"};
        //Cria um objeto do XYPlot grafico  
        private static XYPlot grafico;

        //Vetor de dias da semana que sera colocado no grafico
        final static String[] dias = new String[]{"Dom","Seg", "Ter", "Qua", "Qui", "Sex", "Sab"};

        @Override
        protected void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.activity_telab);
            //pega o comboBox pelo id
            final Spinner combo = (Spinner) findViewById(R.id.comboCultura);
            //Cria um arrayAdapter
            ArrayAdapter adaptador = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, culturas);
            adaptador.setDropDownViewResource(android.R.layout.simple_spinner_item);
            //Set o ArrayAdapter no Spinner
            combo.setAdapter(adaptador);
            //Listener do Spinner
            combo.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                //Se selecionado algo
                @Override
                public void onItemSelected(AdapterView<?> parent, View view,
                        int position, long id) {
                    //Fazer algo com isso;

                }
                //Se não
                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                    // TODO Auto-generated method stub

                }

            });
        // pego o RadioGroup pelo id
        final RadioGroup dados = (RadioGroup) findViewById(R.id.dados);
        //pega o Grafico pelo id
        grafico = (XYPlot)findViewById(R.id.grafico);
        //Listener do RadioGroup
        dados.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            //Cria booleans para testar os Radios se estão setados
            boolean temp = R.id.temp == checkedId;
            boolean umid = R.id.umid == checkedId;
            boolean pres = R.id.pres == checkedId;
            boolean alt = R.id.alt == checkedId;
            boolean umids = R.id.alt == checkedId;
            boolean vento = R.id.vento == checkedId;
            boolean lum = R.id.lum == checkedId;
            boolean temps = R.id.temps == checkedId;
            //Testes dos Boolean
            if(temp){
                //Seta os vetores com os dados
                Number[] dadoA = {27,29,24,26,30,29,28};
                Number[] dadoC = {24,30,24,30,24,30,24};
                //Nome que vai na lateral indicando o que é o grafico
                String nome = "Temperatura em ºC";
                //Chama o método que cria o gráfico
                TelaB.grafico(dadoA, dadoC, nome);
            }else if(umid){
                Number[] dadoA = {};
                Number[] dadoC = {};
                String nome = "Umidade";
                TelaB.grafico(dadoA, dadoC, nome);
            }else if(pres){
                Number[] dadoA = {};
                Number[] dadoC = {};
                String nome = "Pressão";
                TelaB.grafico(dadoA, dadoC, nome);
            }else if(alt){
                Number[] dadoA = {};
                Number[] dadoC = {};
                String nome = "Altitude";
                TelaB.grafico(dadoA, dadoC, nome);
            }else if(umids){
                Number[] dadoA = {0.2,0.4,4,6,6,7,4};
                Number[] dadoC = {0.3,0.2,3,7,3,7,3};
                String nome = "Umidade do solo /mm";
                TelaB.grafico(dadoA, dadoC, nome);
            }else if(vento){
                Number[] dadoA = {};
                Number[] dadoC = {};
                String nome = "Velocidade do vento";
                TelaB.grafico(dadoA, dadoC, nome);
            }else if(lum){
                Number[] dadoA = {};
                Number[] dadoC = {};
                String nome = "Luminosidade";
                TelaB.grafico(dadoA, dadoC, nome);
            }else if(temps){
                Number[] dadoA = {};
                Number[] dadoC = {};
                String nome = "Temperatura do solo";
                TelaB.grafico(dadoA, dadoC, nome);
            }

        }
    });


    }

    //Metodo que gera o gráfico
    public static void grafico(Number[] dadoA, Number[] dadoC, String nome){

        //Converte os arrays em uma série XY
        XYSeries Agri_Weather = new SimpleXYSeries(Arrays.asList(dadoA), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY,"DadoAgri_Weather");
        XYSeries Cultura = new SimpleXYSeries(Arrays.asList(dadoC), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY,"DadoCultura");

        //Cria um formato de linha e ponto
        LineAndPointFormatter Agri_Weather_Formato = new LineAndPointFormatter(Color.rgb(0,0,255),Color.rgb(200,200,200), null, null);
        LineAndPointFormatter Cultura_Formato = new LineAndPointFormatter(Color.rgb(0,0,255),Color.rgb(200,200,200), null, null);

        //Adiciona Agri_Weather e Cultura ao XYplot
        grafico.addSeries(Agri_Weather, Agri_Weather_Formato);
        grafico.addSeries(Cultura, Cultura_Formato);

        //Formata o DomainValues X-Axis
        grafico.setDomainValueFormat(new Format(){
            @Override
            public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos){
                return new StringBuffer(dias[((Number)obj).intValue()]);
            }
            @Override
            public Object parseObject(String source, ParsePosition pos){
                return null;
            }

        });

        grafico.setDomainLabel("");
        grafico.setRangeLabel(nome);

        //Incrementa o X-Axis em 1
        grafico.setDomainStep(XYStepMode.INCREMENT_BY_VAL, 1);
        grafico.getGraphWidget().setRangeLabelWidth(25);

        //Reduz o numero de range labels
        grafico.setTicksPerRangeLabel(2);

        //Reduz o numero de domain labels
        grafico.setTicksPerDomainLabel(2);

    }
}

//Screen layout

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <Spinner
            android:id="@+id/comboCultura"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:drawSelectorOnTop="true" />

        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/dados"
            >

        <RadioButton
            android:id="@+id/temp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Temperatura" />
        <RadioButton
            android:id="@+id/umid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Umidade" />
        <RadioButton
            android:id="@+id/pres"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Pressão" />
        <RadioButton
            android:id="@+id/alt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Altitude" />
        <RadioButton
            android:id="@+id/umids"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Umidade do solo" />
        <RadioButton
            android:id="@+id/vento"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Velocidade do vento" />
        <RadioButton
            android:id="@+id/lum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Luminosidade" />
        <RadioButton
            android:id="@+id/temps"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Temperatura do solo" />
        </RadioGroup>

    </LinearLayout>

</ScrollView>

<com.androidplot.xy.XYPlot
    android:id="@+id/grafico"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    title="Gráfico meteorologico"/>

The type of graph desired is the line one, and on the days of the week (which was placed in the code) of segment X in the graph one must place points to stand out in the lines.

The part that is problematic is in the Androidplot, more specifically in the graphic method of the code, because the values are being passed correctly, but when I run in the emulator it does not generate any graph when clicking on the Spinners.

Thank you

  • 1

    Hi Lucas, welcome to Stackoverflow in English! Please specify your problem better: what is the "desired chart", and what part of the code is not working as it should? I suggest [Dit] your question to make it more focused.

  • Hello. Being more specific, the desired chart is a chart of lines, with points referencing the days of the week (placed in the code). The part that is not working is the androidPlot, the rest is ok, and I know that in the graphic function the values are being passed in the vectors correctly, the problem is to generate, since when I run in the emulator it does not appear when I click on the radio button. Thanks for the help

No answers

Browser other questions tagged

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