作者:民海育来仁湖 | 来源:互联网 | 2023-02-05 12:28
使用MPAndroidChart 3.0.2 绘制水平条形图.值显示在条形图的右侧.我可以使用setValueFormatter并使用IAxisValueFormatter接口在右侧显示标签.但是现在不显示这些值.
{
HorizontalBarChart barChart = (HorizontalBarChart) itemView.findViewById(R.id.barChart);
BarData data = new BarData();
ArrayList valueSet1 = new ArrayList<>();
ArrayList labels = new ArrayList<>();
labels.add("January");
labels.add("February");
labels.add("March");
labels.add("April");
labels.add("May");
labels.add("June");
ArrayList ylabels = new ArrayList<>();
int dataCount=0;
for (int i=0;i<6;++i) {
BarEntry entry = new BarEntry(dataCount,(i+1)*2);
valueSet1.add(entry);
ylabels.add(" "+i);
dataCount++;
}
List dataSets = new ArrayList<>();
BarDataSet bds = new BarDataSet(valueSet1, " ");
bds.setColors(ColorTemplate.MATERIAL_COLORS);
String[] xAxisLabels = labels.toArray(new String[0]);
String[] yAxisLabels = ylabels.toArray(new String[0]);
bds.setStackLabels(xAxisLabels);
dataSets.add(bds);
data.addDataSet(bds);
data.setDrawValues(true);
data.setBarWidth(0.4f);
XAxis xaxis = barChart.getXAxis();
xaxis.setDrawGridLines(false);
xaxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xaxis.setGranularityEnabled(true);
xaxis.setGranularity(1);
xaxis.setDrawLabels(true);
xaxis.setLabelCount(dataCount+1);
xaxis.setXOffset(10);
xaxis.setDrawAxisLine(false);
CategoryBarChartXaxisFormatter xaxisFormatter = new CategoryBarChartXaxisFormatter(xAxisLabels);
xaxis.setValueFormatter(xaxisFormatter);
YAxis yAxisLeft = barChart.getAxisLeft();
yAxisLeft.setEnabled(false);
YAxis yAxisRight = barChart.getAxisRight();
yAxisRight.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
yAxisRight.setDrawGridLines(false);
yAxisRight.setDrawAxisLine(false);
Legend legend = barChart.getLegend();
legend.setEnabled(false);
barChart.setFitBars(true);
barChart.setData(data);
barChart.setDescription(null);
}
public class CategoryBarChartXaxisFormatter implements IAxisValueFormatter {
private String[] mValues;
public CategoryBarChartXaxisFormatter(String[] values) {
this.mValues = values;
}
@Override
public String getFormattedValue(float value, AxisBase axis) {
int val = (int)value;
String label="";
if(val>=0 && val
要求是在右侧显示标签字符串,在左侧显示与每个条形对应的数字.我确实检查了一些堆栈溢出并谷歌它,但没有找到任何有用的功能到目前为止.
我明白了
但这是我要求的一个例子
要感谢一些帮助.谢谢你的时间
1> sauvik..:
几天前我实施了类似的东西.这是代码......
public class MainActivity extends AppCompatActivity {
protected HorizontalBarChart mChart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList labels = new ArrayList<>();
labels.add("January");
labels.add("February");
labels.add("March");
labels.add("April");
labels.add("May");
labels.add("June");
mChart = (HorizontalBarChart) findViewById(R.id.barChart);
mChart.setDrawBarShadow(false);
mChart.setDrawValueAboveBar(true);
mChart.getDescription().setEnabled(false);
mChart.setPinchZoom(false);
mChart.setDrawGridBackground(false);
XAxis xl = mChart.getXAxis();
xl.setPosition(XAxis.XAxisPosition.BOTTOM);
xl.setDrawAxisLine(true);
xl.setDrawGridLines(false);
CategoryBarChartXaxisFormatter xaxisFormatter = new CategoryBarChartXaxisFormatter(labels);
xl.setValueFormatter(xaxisFormatter);
xl.setGranularity(1);
YAxis yl = mChart.getAxisLeft();
yl.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
yl.setDrawGridLines(false);
yl.setEnabled(false);
yl.setAxisMinimum(0f);
YAxis yr = mChart.getAxisRight();
yr.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
yr.setDrawGridLines(false);
yr.setAxisMinimum(0f);
ArrayList yVals1 = new ArrayList();
for (int i = 0; i <6; i++) {
yVals1.add(new BarEntry(i, (i+1)*10));
}
BarDataSet set1;
set1 = new BarDataSet(yVals1, "DataSet 1");
ArrayList dataSets = new ArrayList();
dataSets.add(set1);
BarData data = new BarData(dataSets);
data.setValueTextSize(10f);
data.setBarWidth(.9f);
mChart.setData(data);
mChart.getLegend().setEnabled(false);
}
public class CategoryBarChartXaxisFormatter implements IAxisValueFormatter {
ArrayList mValues;
public CategoryBarChartXaxisFormatter(ArrayList values) {
this.mValues = values;
}
@Override
public String getFormattedValue(float value, AxisBase axis) {
int val = (int) value;
String label = "";
if (val >= 0 && val
结果
希望这可以帮助.