Pie Chart with SharedPreferences

217 Views Asked by At

I need to create a pie chart with data from SharedPreferences. This is part of my code, but I can't understand why this isn't working:

SharedPreferences sharedPref = getSharedPreferences("janeiroInfo", Context.MODE_PRIVATE);
int ord = sharedPref.getInt("ordenado", 0);
int div = sharedPref.getInt("dividas", 0);
int alim = sharedPref.getInt("alimentacao", 0);
int lux = sharedPref.getInt("luxos", 0);
int poup = sharedPref.getInt("poupanca", 0);
int desp = sharedPref.getInt("despesas", 0);

int saldo = ord - div - alim - lux - poup - desp;

private static String TAG = "Janeiro";


float a = (div*100)/(float) ord;
float b = (alim*100)/(float) ord;
float c = (lux*100)/(float) ord;
float d = (poup*100)/(float) ord;
float e = (desp*100)/(float) ord;
float f = (float) saldo;
private float[] yData = {a, b, c, d, e, f};
private String[] xData = {"Mitch", "Jessica", "Mohammad", "Kelsey", "Sam", "Robert"};
PieChart pieChart;

This is the chart code:

// Graph
    Log.d(TAG, "onCreate: starting to create chart");

    pieChart = (PieChart) findViewById(R.id.idPieChart);

    pieChart.setDescription("   ");
    pieChart.setRotationEnabled(true);
    pieChart.setHoleRadius(25f);
    pieChart.setTransparentCircleAlpha(0);
    pieChart.setCenterText("Super Cool Chart");
    pieChart.setCenterTextSize(10);

    addDataSet();

    pieChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
        @Override
        public void onValueSelected(Entry e, Highlight h) {
            Log.d(TAG, "onValueSelected: Value select from chart.");
            Log.d(TAG, "onValueSelected: " + e.toString());
            Log.d(TAG, "onValueSelected: " + h.toString());

            int pos1 = e.toString().indexOf("(sum): ");
            String sales = e.toString().substring(pos1 + 7);

            for(int i = 0; i < yData.length; i++){
                if(yData[i] == Float.parseFloat(sales)){
                    pos1 = i;
                    break;
                }
            }

            String employee = xData[pos1];
            Toast.makeText(Janeiro.this, "Employee " + employee + "\n" + "Sales: $" + sales + "K", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onNothingSelected() {

        }
    });


    }

private void addDataSet() {
    Log.d(TAG, "addDataSet started");
    ArrayList<PieEntry>  yEntrys = new ArrayList<>();
    ArrayList<String> xEntrys = new ArrayList<>();

    for(int i = 0; i < yData.length; i++){
        yEntrys.add(new PieEntry(yData[i], i));
    }

    for(int i = 1; i < xData.length; i++){
        xEntrys.add((xData[i]));
    }

    // create data set
    PieDataSet pieDataSet = new PieDataSet(yEntrys, "  ");
    pieDataSet.setSliceSpace(2);
    pieDataSet.setValueTextSize(14);

    //add colors
    ArrayList<Integer> colors = new ArrayList<>();
    colors.add(Color.argb(200, 41, 106, 171)); 
    colors.add(Color.argb(229, 121, 68, 182)); 
    colors.add(Color.argb(200, 178, 68, 182)); 
    colors.add(Color.argb(200, 229, 83, 175)); 
    colors.add(Color.argb(200, 182, 68, 106)); 
    colors.add(Color.argb(200, 179, 9, 66)); 
    colors.add(Color.argb(200, 68, 159, 182)); 

    pieDataSet.setColors(colors);

    // add legend
    Legend legend = pieChart.getLegend();
    legend.setForm(Legend.LegendForm.CIRCLE);
    legend.setPosition(Legend.LegendPosition.LEFT_OF_CHART);

    // create pie data object
    PieData pieData = new PieData(pieDataSet);
    pieChart.setData(pieData);
    pieChart.invalidate();

}

I did Pie Chart based on this code example: https://www.youtube.com/watch?v=8BcTXbwDGbg&t=682s.

I did it without SharedPreferences, instead i used

private float[] yData = {25.3f, 10.6f, 66.76f, 44.32f, 46.01f, 16.89f, 23.9f}; 

and it worked perfectly! But when i try to used SharedPreferences it doesn't work.. The app stops.

1

There are 1 best solutions below

1
On

Download the jar from

http://www.achartengine.org/content/download.html

Add the jar to the projects lib folder. There is a sample also provided by the developers. You can check that and modify the same as you wish.

There is also a demo @

http://www.achartengine.org/content/demo.html

The docs

http://www.achartengine.org/content/javadoc/org/achartengine/chart/PieChart.html

you can use this external library for pie charts