MpAndroidChart HorizontalBarChart自定义标签 [英] MpAndroidChart HorizontalBarChart Customize label
问题描述
我想将我的栏值添加到栏左侧,并将栏标签添加到栏右侧。
以下是启动HorizontalBarChart的代码
HorizontalBarChart mChart =(HorizontalBarChart)findViewById(R.id.chart1);
mChart.setOnChartValueSelectedListener(this);
mChart.setDrawBarShadow(false);
mChart.setDrawValueAboveBar(true);
mChart.setDescription(strHeading);
mChart.setMaxVisibleValueCount(60);
mChart.setPinchZoom(false);
mChart.setDrawGridBackground(false);
XAxis xl = mChart.getXAxis();
xl.setDrawLabels(false);
YAxis yl = mChart.getAxisLeft();
yl.setDrawLabels(false);
YAxis yr = mChart.getAxisRight();
yr.setTypeface(mTfLight);
yr.setDrawAxisLine(true);
yr.setDrawGridLines(false);
setData(12,50);
mChart.setFitBars(true);
mChart.animateY(2500);
mChart.getXAxis()。setPosition(XAxis.XAxisPosition.BOTTOM_INSIDE);
Legend l = mChart.getLegend();
l.setPosition(Legend.LegendPosition.ABOVE_CHART_LEFT);
l.setFormSize(8f);
.setXEntrySpace(4f);
下面是绑定数据的方法,下面的代码
float barWidth = 9f;
float spaceForBar = 10f;
ArrayList< BarEntry> yVals1 = new ArrayList< BarEntry>();
for(int i = 0; i< arrayChart.size(); i ++){
ModelChart modelChart = arrayChart.get(i);
String aString = modelChart.getHeader();
float space = i * spaceForBar;
BarEntry eachEntry = new BarEntry(i * spaceForBar,modelChart.getValue(),modelChart.getHeader());
yVals1.add(eachEntry);
if(mChart.getData()!= null& $ amp;
mChart.getData()。getDataSetCount()> 0){
BarDataSet set1 =(BarDataSet)mChart.getData()。getDataSetByIndex(0);
set1.setValues(yVals1);
mChart.getData()。notifyDataChanged();
mChart.notifyDataSetChanged();
} else {
set1 = new BarDataSet(yVals1,A,B,C);
ArrayList< IBarDataSet> dataSets = new ArrayList< IBarDataSet>();
dataSets.add(set1);
BarData data = new BarData(dataSets);
data.setValueTextSize(10f);
data.setValueTypeface(mTfLight);
data.setBarWidth(barWidth);
mChart.setData(data);
使用 ValueFormatter
和 AxisValueFormatter
接口。
-
创建一个将标签添加到条形右侧的格式化程序:
public class LabelFormatter implements ValueFormatter {
@Override
public String getFormattedValue(float value,Entry entry,int dataSetIndex,ViewPortHandler viewPortHandler){
//假设getHeader()返回标签
return(String )entry.getData();
code $ <$ $ p $ <$ li> 创建一个轴格式化程序,在你的酒吧的左边:
pre $public class BarValueFormatter implements AxisValueFormatter {
private final DataSet mData;
public BarValueFormatter(DataSet data){
mData = data;
$ b @Override
public String getFormattedValue(float value,AxisBase axis){
return String.valueOf((int)mData.getEntryForXPos(value,DataSet。 Rounding.CLOSEST).getY());
$将您的格式化程序设置为数据集和轴:
set1.setValueFormatter(new LabelFormatter());
mChart.getXAxis()。setValueFormatter(new BarValueFormatter(set1));
请注意,您需要将呼叫 xl.setDrawLabels(false);
为x轴,所以这些值被正确呈现。
I want to add my bar values to the left of the bar and the bar labels to the right of the bar.
Below is the code that initilizes HorizontalBarChart
HorizontalBarChart mChart = (HorizontalBarChart) findViewById(R.id.chart1);
mChart.setOnChartValueSelectedListener(this);
mChart.setDrawBarShadow(false);
mChart.setDrawValueAboveBar(true);
mChart.setDescription(strHeading);
mChart.setMaxVisibleValueCount(60);
mChart.setPinchZoom(false);
mChart.setDrawGridBackground(false);
XAxis xl = mChart.getXAxis();
xl.setDrawLabels(false);
YAxis yl = mChart.getAxisLeft();
yl.setDrawLabels(false);
YAxis yr = mChart.getAxisRight();
yr.setTypeface(mTfLight);
yr.setDrawAxisLine(true);
yr.setDrawGridLines(false);
setData(12, 50);
mChart.setFitBars(true);
mChart.animateY(2500);
mChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM_INSIDE);
Legend l = mChart.getLegend();
l.setPosition(Legend.LegendPosition.ABOVE_CHART_LEFT);
l.setFormSize(8f);
l.setXEntrySpace(4f);
Below is the method that binds the data, The following code
float barWidth = 9f;
float spaceForBar = 10f;
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
for (int i=0 ; i< arrayChart.size(); i++){
ModelChart modelChart = arrayChart.get(i);
String aString = modelChart.getHeader();
float space = i * spaceForBar;
BarEntry eachEntry = new BarEntry(i * spaceForBar, modelChart.getValue() , modelChart.getHeader());
yVals1.add(eachEntry);
}
if (mChart.getData() != null &&
mChart.getData().getDataSetCount() > 0) {
BarDataSet set1 = (BarDataSet)mChart.getData().getDataSetByIndex(0);
set1.setValues(yVals1);
mChart.getData().notifyDataChanged();
mChart.notifyDataSetChanged();
} else {
set1 = new BarDataSet(yVals1, "A , B , C");
ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
dataSets.add(set1);
BarData data = new BarData(dataSets);
data.setValueTextSize(10f);
data.setValueTypeface(mTfLight);
data.setBarWidth(barWidth);
mChart.setData(data);
}
Yes, this is definitely possible using the ValueFormatter
and AxisValueFormatter
interfaces.
Create a formatter that adds the labels to the right of your bars:
public class LabelFormatter implements ValueFormatter { @Override public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) { // assuming that getHeader() returned the label return (String) entry.getData(); } }
Create an axis formatter that adds the values to the left of your bars:
public class BarValueFormatter implements AxisValueFormatter { private final DataSet mData; public BarValueFormatter(DataSet data) { mData = data; } @Override public String getFormattedValue(float value, AxisBase axis) { return String.valueOf((int) mData.getEntryForXPos(value, DataSet.Rounding.CLOSEST).getY()); } }
Set your formatters to the dataset and axis:
set1.setValueFormatter(new LabelFormatter()); mChart.getXAxis().setValueFormatter(new BarValueFormatter(set1));
Note that you need to remove the call to xl.setDrawLabels(false);
for the x-axis, so the values are properly rendered.
这篇关于MpAndroidChart HorizontalBarChart自定义标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!