is there a better way for reusing the MPAndroid graphview multiple time?

26 views Asked by At

i have been trying to use a single MPAndroid graphview with few function to show the data in the graph either monthly, weekly or yearly by the response of button click. The functions works but on the view sometime it shows and sometime it just cramble all the data and sometimes shows nothing, so i need help if anyone can help me with this.Thanks in advance.

here is the code

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_general_trend_access);
        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.blue)));
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("General Overview");
        actionBar.setElevation(0);
        barChart = findViewById(R.id.barChart);
        sdsDatabaseHelper = new SdsDatabaseHelper(GeneralTrendAccess.this);
        databaseReference = FirebaseDatabase.getInstance().getReference("SalesData_"+sdsDatabaseHelper.databaseName());
        salesDataArrayList = new ArrayList<>();
        arrayList = new ArrayList<>();
        weekly = findViewById(R.id.weekly);
        monthly = findViewById(R.id.monthly);
        yearly = findViewById(R.id.yearly);

        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {

                for (DataSnapshot dataSnapshot: snapshot.getChildren()) {
                    salesData = new SalesData();
                    salesData = dataSnapshot.getValue(SalesData.class);
                    generalTrendData = new GeneralTrendData();
                    generalTrendData.setDate(salesData.getUpload_and_sales_date());
                    generalTrendData.setTotalExpense(salesData.getExpenses());
                    generalTrendData.setTotalSales(salesData.getTotal_sales());
                    generalTrendData.setTotalProfLoss(salesData.getProf_loss());
                    arrayList.add(generalTrendData);
                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

                Toast.makeText(GeneralTrendAccess.this,"data fetch error",Toast.LENGTH_SHORT).show();

            }
        });

and one of button response function

weekly.setOnClickListener(e->{
            XAxis xAxis = barChart.getXAxis();
            xAxis.setCenterAxisLabels(true);
            xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
            xAxis.setGranularity(1);
            xAxis.setGranularityEnabled(true);
            xAxis.setValueFormatter(new IndexAxisValueFormatter(weeks));
            barChart.setVisibleXRangeMaximum(4);
            final Dialog mDialog = new Dialog(GeneralTrendAccess.this);
            mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            mDialog.setContentView(R.layout.weekly_monthly_requirement);
            _month = mDialog.findViewById(R.id.field_month);
            _year = mDialog.findViewById(R.id.field_year);
            cancelled = mDialog.findViewById(R.id.btn_cancelled);
            allowed = mDialog.findViewById(R.id.btn_allowed);
            mDialog.show();
            mDialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

            allowed.setOnClickListener(event->{
                if ((_month.getText().toString()!=null) && (_year.getText().toString()!=null)){
                    makingBars((TreeMap) getWeeklyTrend(Integer.parseInt(_month.getText().toString()),
                            Integer.parseInt(_year.getText().toString())));
                }else {
                    Toast.makeText(this, "some fields are empty", Toast.LENGTH_SHORT).show();
                }
                mDialog.dismiss();
            });

            cancelled.setOnClickListener(event->{
                mDialog.dismiss();
            });
        });

and this one of method for setting data to the graph

@SuppressLint("SuspiciousIndentation")
    public void makingBars(TreeMap treeMap){
        ArrayList<BarEntry> barEntries = new ArrayList<>();
        ArrayList<BarEntry> barEntries1 = new ArrayList<>();
        ArrayList<BarEntry> barEntries2 = new ArrayList<>();
        if (!treeMap.isEmpty()){
            Set<Map.Entry<Integer,GraphData>> super_size = treeMap.entrySet();
            for (Map.Entry<Integer,GraphData> entry : super_size){
                if (entry != null) {
                    barEntries.add(new BarEntry(entry.getKey(), (float) entry.getValue().getTs()));
                    barEntries1.add(new BarEntry(entry.getKey(), (float) entry.getValue().getTe()));
                    barEntries2.add(new BarEntry(entry.getKey(), (float) entry.getValue().getTpl()));
                }else {
                    Toast.makeText(this, "No data added", Toast.LENGTH_SHORT).show();
                }
            }

            if (!barEntries.isEmpty()){
                BarDataSet profLossSet = new BarDataSet(barEntries2,"Profit/Loss");
                profLossSet.setColor(Color.GREEN);
                BarDataSet totalSalesSet = new BarDataSet(barEntries,"Total Sales");
                totalSalesSet.setColor(Color.BLUE);
                BarDataSet totalExpenseSet = new BarDataSet(barEntries1,"Expenses");
                totalExpenseSet.setColor(Color.RED);


                BarData trendData = new BarData(totalSalesSet,profLossSet,totalExpenseSet);
                trendData.setBarWidth(0.15f);
                barChart.setDragEnabled(true);
                barChart.zoomOut();
                barChart.setData(trendData);
                barChart.groupBars(0,0.5f,0.1f);
                barChart.invalidate();
            }else {
                barChart.setNoDataText("Nothing to show here yet.");
                barChart.setNoDataTextColor(Color.BLACK);
                barChart.setNoDataTextTypeface(Typeface.DEFAULT);
            }
        }else {
            barChart.setNoDataText("The month or year entered has no data stored.");
            barChart.setNoDataTextColor(Color.BLACK);
            barChart.setNoDataTextTypeface(Typeface.DEFAULT);
        }

    }

i tried reviewing the code and even looking up for tutorials and even trying to to check the MPAndroid project examples and doc but i got nothing to help

0

There are 0 answers