我需要使用SharedPreferences中的数据创建一个饼图。这是我的代码的一部分,但我无法理解为什么这不起作用:
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;
这是图表代码:
// 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();
}
我根据这个代码示例做了饼图:https://www.youtube.com/watch?v=8BcTXbwDGbg&t=682s。
我没有使用SharedPreferences,而是使用了
private float[] yData = {25.3f, 10.6f, 66.76f, 44.32f, 46.01f, 16.89f, 23.9f};
它完美无缺!但是,当我尝试使用SharedPreferences时,它不起作用..应用程序停止。
答案 0 :(得分:0)
从
下载jarhttp://www.achartengine.org/content/download.html
将jar添加到项目lib文件夹中。开发人员也提供了一个示例。您可以检查并根据需要进行修改。
还有一个演示@
http://www.achartengine.org/content/demo.html
文档
http://www.achartengine.org/content/javadoc/org/achartengine/chart/PieChart.html
您可以将此外部库用于饼图