MPAndroidChart 条形图值 [英] MPAndroidChart Bar Chart Values

查看:74
本文介绍了MPAndroidChart 条形图值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读 MPAndroidChart 的文档(使用 v3.0.1),但似乎找不到删除条形上标签的方法

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.

我不确定是否可以这样做.我对 android 开发很陌生,任何帮助都会很棒.

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

我的代码是:

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;
}

}

有没有人有这方面的经验?该库的 Github 链接在这里:https://github.com/PhilJay/MPAndroidChart

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

推荐答案

只需创建一个新类并让它实现 IValueFormatter 并从 getFormattedValue<返回您想要显示的任何内容/code>(...) 方法如下.

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.

这篇关于MPAndroidChart 条形图值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆