点击每个栏时显示MPandroidChart信息

时间:2017-04-09 18:18:21

标签: android user-interface mpandroidchart

我正在使用MPAndroidChart(V3.0.1)并且我正在尝试找到一种方法来在用户使用BarChart时点击每列时显示更多信息。

每列最终都会有一些额外的统计信息,只有在点击后才能显示。

我已经阅读了文档,但如果它在那里,我要么错过了,要么根本不理解我读过的内容(不是专业编码员)。

到目前为止,这是我为我的计划所做的事情,

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(目前不在我的代码中)显示有关列的其他信息,例如数据列的平均值和方差。 / p>

1 个答案:

答案 0 :(得分: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() { }