Android 的自定义 MPAndroid 图表
Custom MPAndroidChart for Android
我正在使用这个库 https://github.com/PhilJay/MPAndroidChart 在我的项目中制作条形图,我已经尝试并遵循 wiki 但仍然无法解决我遗留的一些问题。我正在尝试删除右侧的值 (4,3,2,1,0) 和下面的标签 "BarDataSet" 以及左侧的色块。最后,我尝试在条形图上设置的颜色不是我定义的颜色。
我做了什么:
List<BarEntry> entries = new ArrayList<>();
entries.add(new BarEntry(0f, 55f));
entries.add(new BarEntry(1f, 80f));
entries.add(new BarEntry(2f, 60f));
entries.add(new BarEntry(3f, 50f));
entries.add(new BarEntry(4f, 40f));
BarDataSet set = new BarDataSet(entries, "BarDataSet");
BarData data = new BarData(set);
data.setBarWidth(0.9f); // set custom bar width
barChart.setData(data);
barChart.setFitBars(true); // make the x-axis fit exactly all bars
barChart.invalidate(); // refresh
barChart.getAxisLeft().setEnabled(false);
barChart.getAxisRight().setEnabled(false);
barChart.getXAxis().setDrawGridLines(false);
barChart.getDescription().setEnabled(false);
set.setColors(R.color.star_bar);
而我现在拥有的是:
我想删除右边的标签(4 到 0)和 "BarDataSet"。
我为 R.color.star_bar 定义的颜色是黄色,但不知何故它显示出紫色。
删除行:
set.setColors(R.color.star_bar);
然后,您必须添加以下行:
barChart.getLegend().setEnabled(false); // hide the legend
set.setColor(ContextCompat.getColor(this, R.color.colorAccent)); // set yellow color
// hide the labels
barChart.getXAxis().setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
return "";
}
});
如果右侧的 0 到 4 是条值,要删除它们,请设置:
data.setDrawValues(false);
否则,如果它们是标签,则尝试:
barChart.getXAxis().setDrawLabels(false);
而 "BarDataSet" 是图例,您可以使用以下方法隐藏它:
barChart.getLegend().setEnabled(false);
你的代码中还有一件事是你先使图表无效然后设置一些属性,但我认为最后使图表无效总是更好,所以把下面的行放在最后并尝试:
barChart.invalidate();
我正在使用这个库 https://github.com/PhilJay/MPAndroidChart 在我的项目中制作条形图,我已经尝试并遵循 wiki 但仍然无法解决我遗留的一些问题。我正在尝试删除右侧的值 (4,3,2,1,0) 和下面的标签 "BarDataSet" 以及左侧的色块。最后,我尝试在条形图上设置的颜色不是我定义的颜色。
我做了什么:
List<BarEntry> entries = new ArrayList<>();
entries.add(new BarEntry(0f, 55f));
entries.add(new BarEntry(1f, 80f));
entries.add(new BarEntry(2f, 60f));
entries.add(new BarEntry(3f, 50f));
entries.add(new BarEntry(4f, 40f));
BarDataSet set = new BarDataSet(entries, "BarDataSet");
BarData data = new BarData(set);
data.setBarWidth(0.9f); // set custom bar width
barChart.setData(data);
barChart.setFitBars(true); // make the x-axis fit exactly all bars
barChart.invalidate(); // refresh
barChart.getAxisLeft().setEnabled(false);
barChart.getAxisRight().setEnabled(false);
barChart.getXAxis().setDrawGridLines(false);
barChart.getDescription().setEnabled(false);
set.setColors(R.color.star_bar);
而我现在拥有的是:
我想删除右边的标签(4 到 0)和 "BarDataSet"。 我为 R.color.star_bar 定义的颜色是黄色,但不知何故它显示出紫色。
删除行:
set.setColors(R.color.star_bar);
然后,您必须添加以下行:
barChart.getLegend().setEnabled(false); // hide the legend
set.setColor(ContextCompat.getColor(this, R.color.colorAccent)); // set yellow color
// hide the labels
barChart.getXAxis().setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
return "";
}
});
如果右侧的 0 到 4 是条值,要删除它们,请设置:
data.setDrawValues(false);
否则,如果它们是标签,则尝试:
barChart.getXAxis().setDrawLabels(false);
而 "BarDataSet" 是图例,您可以使用以下方法隐藏它:
barChart.getLegend().setEnabled(false);
你的代码中还有一件事是你先使图表无效然后设置一些属性,但我认为最后使图表无效总是更好,所以把下面的行放在最后并尝试:
barChart.invalidate();