MPAndroidChart Bar Chart Values

柔情痞子 提交于 2020-08-08 05:40:09

问题


I've been reading through the documentation for MPAndroidChart (using v3.0.1) and I can't seem to find a way to remove the labels on the bars

I would like to hide the values that are shown on top of each bar, OR change the values of them to something I can set manually. Either option would work well for my implementation.

I'm not sure if it is even possible to do either. I'm very new to android development any help would be great.

my code is:

public class MainActivity extends AppCompatActivity {

protected BarChart chart;
@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(""); // this is the weirdest way to clear something!!
    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);


    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));
    entries.add(new BarEntry(1f, 80f));
    entries.add(new BarEntry(2f, 60f));
    entries.add(new BarEntry(3f, 50f));
    entries.add(new BarEntry(4f, 70f));
    entries.add(new BarEntry(5f, 60f));


    BarDataSet set = new BarDataSet(entries, "");
    set.setColor(Color.rgb(155, 155, 155));
    set.setValueTextColor(Color.rgb(155,155,155));

    return set;
}

}

Has anyone had any experience with this? Github link for the library is here: https://github.com/PhilJay/MPAndroidChart


回答1:


Simply create a new class and let it implement the IValueFormatter and return whatever you want to be displayed from the getFormattedValue(...) method like below.

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

BarDataSet set = new BarDataSet(entries, "");
set.setValueFormatter(new MyValueFormatter());
set.setColor(Color.rgb(155, 155, 155));
set.setValueTextColor(Color.rgb(155, 155, 155));

You can find out more here.




回答2:


For others coming through here looking for solutions, try setDrawValues on the data set instead.

BarDataSet dataSet = new BarDataSet(entries, "");
dataSet.setDrawValues(false);


来源:https://stackoverflow.com/questions/42320417/mpandroidchart-bar-chart-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!