MpAndroidChart Horizo​​ntalBarChart自定义标签 [英] MpAndroidChart HorizontalBarChart Customize label

查看:325
本文介绍了MpAndroidChart Horizo​​ntalBarChart自定义标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的栏值添加到栏左侧,并将栏标签添加到栏右侧。





以下是启动Horizo​​ntalBarChart的代码

  Horizo​​ntalBarChart mChart =(Horizo​​ntalBarChart)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 接口。


  1. 创建一个将标签添加到条形右侧的格式化程序:

      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());




    $
  2. 将您的格式化程序设置为数据集和轴:

      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.

  1. 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();
        }
    }
    

  2. 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());
        }
    }
    

  3. 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 Horizo​​ntalBarChart自定义标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆