Mpandroidchart: DataSet values not appearing on Horizontal Bar Chart

Created on 14 Dec 2015  路  6Comments  路  Source: PhilJay/MPAndroidChart

I would like to show the values at the end of each bar, but i can't get it work. Here is my code:

    /**
     * Set up horizontal barchart for polls stats
     * @param totals
     */
    private void setUpPollBarChart(int[] totals) {
        barChart.setDescription(""); 
        barChart.setMaxVisibleValueCount(maxValueCount);
        barChart.setPinchZoom(false);
        barChart.setDrawGridBackground(false); //Do not display grid background

        //Disable all interaction with the chart
        barChart.setDrawHighlightArrow(false);
        barChart.setHighlightPerTapEnabled(false);
        barChart.setHighlightPerDragEnabled(false);
        barChart.setDoubleTapToZoomEnabled(false);


        //Disable surroundings graphs (axis and legends)
        XAxis xAxis = barChart.getXAxis();
        xAxis.setEnabled(false);
        YAxis leftAxis = barChart.getAxisLeft();
        YAxis rightAxis = barChart.getAxisRight();
        leftAxis.setEnabled(false);
        rightAxis.setEnabled(false);
        Legend l = barChart.getLegend();
        l.setEnabled(false);

        //Set data
        ArrayList<BarEntry> yVals = new ArrayList<BarEntry>();
        ArrayList<String> xVals = new ArrayList<String>();

        for (int i=0; i < totals.length; i++) {
            xVals.add(String.valueOf(i));
            yVals.add(new BarEntry(StatisticsUtils.getNormalizedValue(totals, i),(totals.length-1) - i)); 
        }

        BarDataSet dataSet = new BarDataSet(yVals,"dataSet polls");
        dataSet.setDrawValues(true); // This doesn't seem to work
        dataSet.setValueTextColor(Color.BLACK);
        dataSet.setValueTextSize(12f);
        dataSet.setColors(new int[]{R.color.colorRedDark}, getContext());
        dataSet.setBarSpacePercent(StatisticsUtils.BAR_SPACE_PERCENT);

        BarData data = new BarData(xVals, dataSet);
        data.setDrawValues(true);  // just in case
        data.setValueTextSize(11f);
        data.setValueTextColor(Color.BLACK);

        horizontalBarChart.setData(data);
    }

device-2015-12-14-174155

(the red top one)

I'm using the 2.1.6, I already tried reinstalling the app.

What am I missing?

Most helpful comment

i am also facing same issue(DataSet values not appearing on Horizontal Bar Chart).
i resolved this issue by setAxisMinimum.

YAxis left = horizontalBarChart.getAxisLeft();
left.setAxisMinimum(0f);

All 6 comments

Thank you for answering quickly, I finally found the problem, the invalidate() didn't change anything there, it was the setMaxVisibleValueCount() that was causing trouble.

Regards,

Hi,

What was the issue, i am also facing same issue. How you resolved this issue.

facing the same issue here with HorizontalBar , what is the root cause? how could i get it fixed? already checked with invalidate() , but that doesn't work. Please, help.

Hello All,
I have been trying to add months to my barchart and plot rating against them. However, all the months don't display. Could you please help

package com.android_examples.barchart_android_examplescom;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.utils.ColorTemplate;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

BarChart chart ;
ArrayList<BarEntry> BARENTRY ;
ArrayList<String> BarEntryLabels ;
BarDataSet Bardataset ;
BarData BARDATA ;
int avg=0;
String[ ][ ] Array2D = new String[60][2];
Float[][] AvgArray = new Float[40][2];
Float[][] FinalAvg = new Float[40][2];
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    chart = (BarChart) findViewById(R.id.chart1);

    BARENTRY = new ArrayList<>();

    BarEntryLabels = new ArrayList<String>();

    // dummy data
    DummyData();

    AddValuesToBARENTRY();

    AddValuesToBarEntryLabels();

    Bardataset = new BarDataSet(BARENTRY, "Projects");

    BARDATA = new BarData(BarEntryLabels, Bardataset);

    Bardataset.setColors(ColorTemplate.COLORFUL_COLORS);
    chart.setData(BARDATA);
    chart.animateY(3000);

}

private void DummyData() {
    Array2D[0][0] = "20160101";
    Array2D[0][1] = "2";

    Array2D[1][0] = "20160102";
    Array2D[1][1] = "3.5";

    Array2D[2][0] = "20160103";
    Array2D[2][1] = "4";

    Array2D[3][0] = "20160104";
    Array2D[3][1] = "2.5";

    Array2D[4][0] = "20160105";
    Array2D[4][1] = "3";

    // for second month
    Array2D[5][0] = "20160201";
    Array2D[5][1] = "4";

    Array2D[6][0] = "20160202";
    Array2D[6][1] = "2.5";

    Array2D[7][0] = "20160204";
    Array2D[7][1] = "1.5";

    Array2D[8][0] = "20160209";
    Array2D[8][1] = "3.5";

    // segregate it w.r.t. month
    Float  rat_avg = 0.0f,final_avg, avg_temp;
    int month1 = 0,month=0;
    int avg_cnt =0;
    boolean month_flag = false;
    for (int i=0; i<=8; i++)
    {
            month = Integer.valueOf(Array2D[i][0].substring(4,6));

            if (month_flag == false)
            {
                month1 = month;
                month_flag = true;
            }
            if (month == month1)
            {
                avg_cnt++;
                avg_temp = Float.valueOf(Array2D[i][1]);
                rat_avg = rat_avg + avg_temp;
            }
            if(month != month1 || i==8)
            {
                final_avg = rat_avg/Float.valueOf(avg_cnt);
                month_flag = false;
                AvgArray[avg][0]= final_avg;
                AvgArray[avg][1]= Float.valueOf(month1);
                rat_avg = 0.0f;
                avg_cnt=0;
                avg++;
            }
    }

}

public void AddValuesToBARENTRY(){

  /*  // Sort the entries according to the month
    Arrays.sort(AvgArray, new Comparator<Float[]>() {
        @Override
        public int compare(final Float[] lhs, final Float[] rhs) {
            return rhs[1].compareTo(lhs[1]);
        }
    }); */
    int month=0;
    for (int i=0;i<=2;i++)
    {
        if (AvgArray[i][1] != null)
        {
            month = Math.round(AvgArray[i][1]);
            BARENTRY.add(new BarEntry(AvgArray[i][0],month));
        }
        else
        {
            break;
        }
    }



}

public void AddValuesToBarEntryLabels(){

    BarEntryLabels.add("January");
    BarEntryLabels.add("February");
    BarEntryLabels.add("March");
    BarEntryLabels.add("April");
    BarEntryLabels.add("May");
    BarEntryLabels.add("June");

}

}

screenshot_2016-11-22-10-38-53

i am also facing same issue(DataSet values not appearing on Horizontal Bar Chart).
i resolved this issue by setAxisMinimum.

YAxis left = horizontalBarChart.getAxisLeft();
left.setAxisMinimum(0f);

Was this page helpful?
0 / 5 - 0 ratings

Related issues

thanhcly920 picture thanhcly920  路  3Comments

manucheri picture manucheri  路  3Comments

JungYongWook picture JungYongWook  路  3Comments

andreyfel picture andreyfel  路  3Comments

blotfi picture blotfi  路  3Comments