2017-03-18 71 views
1
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=8BcTXbwDGbgMPandroid Onclicklistener無法正常工作

使用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

回答

0

,我看到其他的方式,我猜你應該在你的代碼中更改條目到PieEnty

@Override 
     public void onValueSelected(PieEntry e, Highlight h) { 
} 

你的想法是什麼?

+0

如果我使用這樣 「pieChart.setOnChartValueSelectedListener(newOnChartValueSelectedListener()」 我怎樣才能改變這條線,它不工作 –

+0

您能不能告訴錯誤日誌? –

+0

班「從OnChartValueSelectedListener派生的匿名類」必須被宣佈如果OnChartValueSelectedListener是抽象類,你必須創建它的一個實例,我的意思是在OnChar之前刪除新的抽象方法'onValueSelected(Entry,Highlight)'onChartValueSelectedListener' –