2017-04-09 66 views
0

我正在使用MPAndroidChart(V3.0.1),我試圖找到一種方式來顯示更多的信息,當用戶點擊每個列時,使用BarChart。隨着MPandroidChart顯示信息,當在每個酒吧點擊

每列最終都會有一些額外的統計數據,只有在他們被點擊後纔會顯示。

我已經閱讀了文檔,但如果它在那裏,我要麼錯過了它,要麼根本不明白我讀過的東西(不是專業的編碼器)。

這裏是我有我的計劃,到目前爲止,

public class MainActivity extends AppCompatActivity { 

protected BarChart chart; 

String[] NameArray = {"a1","a2","a3","a4","a5","a6","a7","a8","a9","a10","a11"}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_main); 
    chart = (BarChart) findViewById(R.id.chart1); 

    Description desc ; 
    Legend L; 

    L = chart.getLegend(); 
    desc = chart.getDescription(); 
    desc.setText(""); 
    L.setEnabled(false); 

    YAxis leftAxis = chart.getAxisLeft(); 
    YAxis rightAxis = chart.getAxisRight(); 
    XAxis xAxis = chart.getXAxis(); 

    xAxis.setPosition(XAxisPosition.BOTTOM); 
    xAxis.setTextSize(10f); 
    xAxis.setDrawAxisLine(true); 
    xAxis.setDrawGridLines(false); 
    xAxis.setDrawLabels(true); 
    xAxis.setValueFormatter(new MyXAxisValueFormatter(NameArray)); 

    leftAxis.setTextSize(10f); 
    leftAxis.setDrawLabels(false); 
    leftAxis.setDrawAxisLine(true); 
    leftAxis.setDrawGridLines(false); 

    rightAxis.setDrawAxisLine(false); 
    rightAxis.setDrawGridLines(false); 
    rightAxis.setDrawLabels(false); 


    BarData data = new BarData( setData()); 


    data.setBarWidth(0.9f); // set custom bar width 
    chart.setData(data); 

    chart.setFitBars(true); // make the x-axis fit exactly all bars 
    chart.invalidate(); // refresh 
    chart.setScaleEnabled(false); 

    chart.setDoubleTapToZoomEnabled(false); 
    chart.setBackgroundColor(Color.rgb(255, 255, 255)); 
    chart.animateXY(2000, 2000); 
    chart.setDrawBorders(false); 
    chart.setDescription(desc); 
    chart.setDrawValueAboveBar(true);// 



} 


private BarDataSet setData() { 

    ArrayList<BarEntry> entries = new ArrayList<>(); 

    entries.add(new BarEntry(0f, 30f, "VB")); 
    entries.add(new BarEntry(1f, 80f, "V0")); 
    entries.add(new BarEntry(2f, 60f, "V1")); 
    entries.add(new BarEntry(3f, 50f, "V2")); 
    entries.add(new BarEntry(4f, 70f, "V3")); 
    entries.add(new BarEntry(5f, 60f, "V4")); 

    BarDataSet set = new BarDataSet(entries,""); 

    set.setValueTextColor(Color.rgb(155, 155, 155)); 
    set.setValueFormatter(new MyValueFormatter()); 
    set.setColor(Color.rgb(155, 155, 155)); 
     return set; 
} 

public class MyValueFormatter implements IValueFormatter{ 
    @Override 
    public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) 
    { 
     return "V" + (int)value; 
    } 
} 


public class MyXAxisValueFormatter implements IAxisValueFormatter { 

    private String[] mValues; 

    private MyXAxisValueFormatter(String[] values) { 

     this.mValues = values; 
    } 

    @Override 
    public String getFormattedValue(float value, AxisBase axis) {   
     return mValues[(int) value]; 
    } 


} 

簡化問題

當用戶點擊圖表中的數據列的一個,我想我的TextViews(目前不在我的代碼中)顯示有關列的其他信息,例如數據列的平均值和方差。

回答

1

在你onCreate()方法添加此行,

chart.setOnChartValueSelectedListener(this); 

所以,你可以點擊列,並顯示其他數據。添加此功能,

protected RectF mOnValueSelectedRectF = new RectF(); 

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

     if (e == null) 
      return; 

     RectF bounds = mOnValueSelectedRectF; 
     chart.getBarBounds((BarEntry) e, bounds); 
     MPPointF position = mChart.getPosition(e, AxisDependency.LEFT); 

     Log.i("bounds", bounds.toString()); 
     Log.i("position", position.toString()); 

     Log.i("x-index", 
       "low: " + chart.getLowestVisibleX() + ", high: " 
         + chart.getHighestVisibleX()); 

     MPPointF.recycleInstance(position); 
    } 

    @Override 
public void onNothingSelected() { } 
+0

我試圖實現這一點,但我的onCreate()方法是給我的錯誤:錯誤:(63,47)錯誤:不兼容的類型:MainActivity不能轉換到OnChartValueSelectedListener – Diesel

+0

此外,還需要將「implements OnChartValueSelectedListener」添加到我的MainActivity中,如下所示:「public class MainActivity extends AppCompatActivity implements OnChartValueSelectedListener { 」。 – Diesel