这是引用文章:https://github.com/PhilJay/MPAndroidChart/wiki/Setting-Data在条形图标题下。在下面给定的代码中:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_graph_test, container, false);
BarChart chart = view.findViewById(R.id.bar_Chart_test);
List<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));
// gap of 2f
entries.add(new BarEntry(5f, 70f));
entries.add(new BarEntry(6f, 60f));
BarDataSet set = new BarDataSet(entries, "BarDataSet");
BarData data = new BarData(set);
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
return view;
}
输出是:
此处不显示 X 值。 Click here for the ScreenShot
如何将 X 轴值设置为文章中显示的月份(1 月 1 日、1 月 2 日、1 月 3 日......)。
最佳答案
你只需像这样制作一个简单的字符串列表:
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");
然后你这样做:
XAxis xAxis = chart.getXAxis();
xAxis.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
return xAxisLabel.get((int) value);
}
});
希望这对您有所帮助。
关于android - 如何在 MP Android 图表(条形图)中设置 X 轴标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47637653/