如何在MP Android Chart(条形图)中设置X轴标签?

时间:2017-12-04 16:21:10

标签: android android-layout mpandroidchart

这是参考文章:https://github.com/PhilJay/MPAndroidChart/wiki/Setting-Data在标题条形图下。在下面的给定代码中:

string_agg
输出是:

此处不显示X值。Click here for the ScreenShot

如何将X轴值设置为月(1月1日,1月2日,1月3日.....),如文章所示。

6 个答案:

答案 0 :(得分:6)

你只需要制作一个简单的字符串列表:

page.content()

然后你这样做:

final ArrayList<String> xAxisLabel = new ArrayList<>();
    xAxisLabel.add("Mon");
    xAxisLabel.add("Tue");
    xAxisLabel.add("Wed");
    xAxisLabel.add("Thu");
    xAxisLabel.add("Fri");
    xAxisLabel.add("Sat");
    xAxisLabel.add("Sun");

希望这有帮助。

答案 1 :(得分:0)

com.github.PhilJay:MPAndroidChart:v3.0.3

我使用的是标签清单:

final List list_x_axis_name = new ArrayList<>();
list_x_axis_name.add("label1");
list_x_axis_name.add("label2");
list_x_axis_name.add("label3");
list_x_axis_name.add("label4");
list_x_axis_name.add("label5");

并设置如下标签:

BarChart chartBar = (BarChart) findViewById(R.id.chartBar);
XAxis xAxis = chartBar.getXAxis();
xAxis.setGranularity(1f);
xAxis.setCenterAxisLabels(true);
xAxis.setLabelRotationAngle(-90);
xAxis.setValueFormatter(new IAxisValueFormatter() {
    &emsp;@override
    &emsp;public String getFormattedValue(float value, AxisBase axis) {
        &emsp;&emsp;if (value >= 0) {
           &emsp;&emsp;&emsp;if (value <= list_x_axis_name.size() - 1) {
              &emsp;&emsp;&emsp;&emsp;return list_x_axis_name.get((int) value);
               &emsp;&emsp;&emsp;
               }
               &emsp;&emsp;&emsp;
               return "";
               &emsp;&emsp;
            }
            &emsp;&emsp;
            return "";
            &emsp;
        }
});

答案 2 :(得分:0)

        HorizontalBarChart chart = findViewById(R.id.hbc_graph);   

        ArrayList<BarEntry> entries = new ArrayList<>();
        entries.add(new BarEntry(0, 3f));
        entries.add(new BarEntry(1, 8f));
        entries.add(new BarEntry(2, 6f));
        entries.add(new BarEntry(3, 11f));
        entries.add(new BarEntry(4, 5f));
        entries.add(new BarEntry(5, 14f));

        BarDataSet dataSet = new BarDataSet(entries,"Horizontal Bar");

        BarData data = new BarData(dataSet);
        chart.setData(data);
        chart.animateXY(2000, 2000);
        chart.invalidate();


        ArrayList<String> xLabels = new ArrayList<>();
        xLabels.add("January");
        xLabels.add("February");
        xLabels.add("March");
        xLabels.add("April");
        xLabels.add("May");
        xLabels.add("June");

        XAxis xAxis = chart.getXAxis();
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                Print.e(value);
                return xLabels.get((int) value);
            }

        });

答案 3 :(得分:0)

在动态设置的X轴标签中,

XAxis xAxis = chart.getXAxis();

xAxis.setValueFormatter(new IndexAxisValueFormatter(getDate));

public ArrayList<String> getDate() {

        ArrayList<String> label = new ArrayList<>();
        for (int i = 0; i < yourList.size(); i++)
            label.add(yourList.get(i).getDateValue());
        return label;
    }

答案 4 :(得分:0)

如果有人需要Kotlin版本示例,我会对其进行一些自定义:

val labels = arrayListOf(
    "Ene", "Feb", "Mar",
    "Abr", "May", "Jun",
    "Jul", "Ago", "Set",
    "Oct", "Nov", "Dic"
)

barChart.xAxis.valueFormatter = IndexAxisValueFormatter(labels)
barChart.xAxis.position = XAxis.XAxisPosition.BOTTOM

barChart.setDrawGridBackground(false)
barChart.axisLeft.isEnabled = false
barChart.axisRight.isEnabled = false
barChart.description.isEnabled = false

val entries = arrayListOf(
    BarEntry(0f, 10f),
    BarEntry(1f, 20f),
    BarEntry(2f, 30f),
    BarEntry(3f, 40f),
    BarEntry(4f, 50f),
    BarEntry(5f, 60f),
    BarEntry(6f, 70f),
    BarEntry(7f, 60f),
    BarEntry(8f, 50f),
    BarEntry(9f, 40f),
    BarEntry(10f, 30f),
    BarEntry(11f, 20f)
)    
val set = BarDataSet(entries, "BarDataSet")
set.valueTextSize = 12f

barChart.data = BarData(set)    
barChart.invalidate()
  • 默认情况下,xAxis标签显示在顶部。此示例在底部显示它们。
  • 我也禁用了文字说明和左右轴(是的,默认情况下,我们在两侧都有2个Y轴)。
  • 我决定禁用网格并增加文本大小(默认情况下很小)。

答案 5 :(得分:0)

科特琳:

val xAxisLabels = listOf("1", "2", "3", "4", "5", "6" ...)      
barChartView.xAxis.valueFormatter = IndexAxisValueFormatter(xAxisLabels)

Java:

ArrayList<String> xAxisLables = new ArrayList();
xAxisLables.add("1");
xAxisLables.add("2");
xAxisLables.add("3");
xAxisLables.add("4"); ...

OR

String[] xAxisLables = new String[]{"1","2", "3", "4" ...};

barChartView.getXAxis().setValueFormatter(new IndexAxisValueFormatter(xAxisLables));

您可以准备在xAxisLabels中显示的任何数据

相关问题