简体   繁体   English

MPAndroidChart - 如何设置独立于图表数据的 x 轴标签

[英]MPAndroidChart - How to set x axis labels independent of chart data

Using MPAndroidCharts , imagine I have the following entries:使用MPAndroidCharts ,假设我有以下条目:

{x: 2, y: 3}, {x: 4, y: 4} and {x: 6, y: 10}

And I wanna show the following labels on the x axis:我想在 x 轴上显示以下标签:

{x: 1, label: "A"}, {x: 4, label: "B"}, {x: 7, label: "C"} and {x: 10, label: "D"}

How can I achieve this?我怎样才能做到这一点?

You could use a valueFormatter to change the Axis labels to what you want您可以使用valueFormatter将轴标签更改为您想要的

XAxis xl = chart.getXAxis();
        // Make sure we get the values in the right places
        xl.setAxisMaximum(10f);
        xl.setAxisMinimum(0f);
        xl.setGranularity(1f);
        xl.setGranularityEnabled(true);
        // Create a formatter that returns the right string for each value
        xl.setValueFormatter(new ValueFormatter()
        {
            @Override
            public String getFormattedValue(float value) {
                String str="";
                if(value==1f)
                    str="A";
                else if(value==4f)
                    str="B";
                else if(value==7f)
                    str="C";
                else if(value==10f)
                    str="D";
                else
                    str="";
                return str;
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM