import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.ColorTemplate;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity{
private static String TAG = "MainActivity";
private float[] yData = {10,15,140}; //numbers of each type
private String[] xData = {"Low","Medium","High"};
PieChart pieChart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pieChart = (PieChart) findViewById(R.id.idPieChart);
//pieChart.getDescription().setText("Numbers of Vul chart");
//pieChart.setBackgroundColor(Color.parseColor("#ffffffff"));
pieChart.setHighlightPerTapEnabled(true);
pieChart.setTouchEnabled(true);
pieChart.setRotationEnabled(true);
pieChart.setHoleRadius(40f);
pieChart.setTransparentCircleRadius(0);
pieChart.setUsePercentValues(true);
pieChart.setCenterText("Percentage of Types");
pieChart.setCenterTextSize(11);
pieChart.setDrawEntryLabels(true);
appDataSet();
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 select = e.toString().substring(pos1 + 3);
for(int i = 0; i < yData.length; i++){
if(yData[i] == Float.parseFloat(select)){
pos1 = i;
break;
}
}
String types = xData[pos1 + 1];
Toast.makeText(MainActivity.this, "Vulnerability type: " + types + "\n" + "Total:" + select, Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected() {
}
});
}
private void appDataSet() {
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]);
}
PieDataSet pieDataSet = new PieDataSet(yEntrys, "Types of Vulnerability");
pieDataSet.setSliceSpace(3);
pieDataSet.setValueTextSize(12);
ArrayList<Integer> colors = new ArrayList<>();
colors.add(Color.rgb(255,253,71)); //Yellow
colors.add(Color.rgb(255,191,71)); //Orange
colors.add(Color.rgb(255,99,99)); //red
pieDataSet.setColors(colors);
Legend legend = pieChart.getLegend();
legend.setForm(Legend.LegendForm.CIRCLE);
legend.setPosition(Legend.LegendPosition.LEFT_OF_CHART);
PieData pieData = new PieData(pieDataSet);
pieData.setValueTextSize(15f);
pieData.setValueFormatter(new PercentFormatter());
pieChart.setData(pieData);
pieChart.invalidate();
}
}
当我尝试触摸每个饼时,问题就出现了,我不知道代码中的问题是什么。此外,我从这个链接中学到了这段代码:https://www.youtube.com/watch?v=8BcTXbwDGbg
使用onclicklistener的另一种方式对我不起作用
@Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
if (e == null)
return;
Log.i("VAL SELECTED",
"Value: " + e.getVal() + ", xIndex: " + e.getXIndex()
+ ", DataSet index: " + dataSetIndex);
}
@Override
public void onNothingSelected() {
Log.i("PieChart", "nothing selected");
}
它不知道dataSetIndex