如何在 MP Android 图表(条形图)中设置 X 轴标签?
How to set X axis labels in MP Android Chart (Bar Graph)?
这是参考文章: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-axis 值设置为文章中显示的月份(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);
}
});
希望对您有所帮助。
对于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() {
@override
public String getFormattedValue(float value, AxisBase axis) {
if (value >= 0) {
if (value <= list_x_axis_name.size() - 1) {
return list_x_axis_name.get((int) value);
}
return "";
}
return "";
}
});
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);
}
});
动态设置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;
}
如果有人需要 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 轴)。
- 我决定禁用网格并增加文本大小(默认情况下很小)。
科特林:
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
中准备任何您想要显示的数据
这是参考文章: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-axis 值设置为文章中显示的月份(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);
}
});
希望对您有所帮助。
对于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() {
@override
public String getFormattedValue(float value, AxisBase axis) {
if (value >= 0) {
if (value <= list_x_axis_name.size() - 1) {
return list_x_axis_name.get((int) value);
}
return "";
}
return "";
}
});
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);
}
});
动态设置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;
}
如果有人需要 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 轴)。
- 我决定禁用网格并增加文本大小(默认情况下很小)。
科特林:
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
中准备任何您想要显示的数据