gpt4 book ai didi

android - 如何在水平条形图中显示右侧的标签和左侧的值?

转载 作者:太空狗 更新时间:2023-10-29 15:50:36 26 4
gpt4 key购买 nike

使用 MPAndroidChart 绘制水平条形图3.0.2。这些值显示在条形图的右侧。我可以使用 setValueFormatter 并使用 IAxisValueFormatter 接口(interface)在右侧显示标签。但是现在不显示这些值。

{
HorizontalBarChart barChart = (HorizontalBarChart) itemView.findViewById(R.id.barChart);

BarData data = new BarData();

ArrayList<BarEntry> valueSet1 = new ArrayList<>();

ArrayList<String> labels = new ArrayList<>();
labels.add("January");
labels.add("February");
labels.add("March");
labels.add("April");
labels.add("May");
labels.add("June");
ArrayList<String> 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<IBarDataSet> 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<mValues.length) {
label= mValues[val];
}
else {
label= "";
}
return label;
}
}

要求是在右侧显示标签字符串,在左侧显示对应于每个条的数字。我确实检查了一些堆栈溢出并用谷歌搜索了它,但到目前为止没有找到任何有用的东西。

我明白了 enter image description here

但这是我要求的一个例子enter image description here请感谢一些帮助。感谢您的宝贵时间

最佳答案

几天前我实现了类似的东西。这是代码...

public class MainActivity extends AppCompatActivity {

protected HorizontalBarChart mChart;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ArrayList<String> 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<BarEntry> yVals1 = new ArrayList<BarEntry>();
for (int i = 0; i < 6; i++) {
yVals1.add(new BarEntry(i, (i+1)*10));
}

BarDataSet set1;
set1 = new BarDataSet(yVals1, "DataSet 1");
ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
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<String> mValues;

public CategoryBarChartXaxisFormatter(ArrayList<String> values) {
this.mValues = values;
}

@Override
public String getFormattedValue(float value, AxisBase axis) {

int val = (int) value;
String label = "";
if (val >= 0 && val < mValues.size()) {
label = mValues.get(val);
} else {
label = "";
}
return label;
}
}

}

结果

enter image description here

希望这对您有所帮助。

关于android - 如何在水平条形图中显示右侧的标签和左侧的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43687572/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com