Aryan Gulati
Aryan Gulati

Reputation: 165

MPAndroidChart - Bar Chart not showing all X-axis labels

14-Aug-2020: Posted an Update Below

I have a BarChart in my activity, and I am using an IndexAxisValueFormatter to display custom labels. I want labels under all of the bars that are displayed. However, the graph only shows labels under certain bars, as in the screenshot below. This problem persists even when zooming.

When the graph is displayed, the positioning of the labels is such that they are unreadable (see screenshot below), but this gets fixed after any interaction with the chart (but all labels never show).

I have tried using the XAxis.setLabelCount(entries.size(), true) with the force boolean set to true, but there was no change in the rendering. Setting a limit on the bars shown with BarChart.setVisibleXRangeMaximum(3); also does not help.

The method that displays the data is:

private void plotDataBarGraph(int[] xCoordinates, int[] yCoordinates, String label, String[] labels){

    BarChart graph = findViewById(R.id.chart);
    graph.setScaleYEnabled(false);

    List<BarEntry> entries = new ArrayList<>();

    for (int i = 0; i < xCoordinates.length; i++){

        entries.add(new BarEntry(xCoordinates[i], yCoordinates[i])) ;

    }
    Collections.sort(entries, new EntryXComparator());

    BarDataSet dataSet = new BarDataSet(entries,label);
    dataSet.setColor(Color.BLUE);
    dataSet.setValueTextColor(Color.BLACK);

    BarData lineData = new BarData(dataSet);
    graph.setData(lineData);
    XAxis xAxis = graph.getXAxis();
    xAxis.setLabelCount(entries.size(), true);
    xAxis.setValueFormatter(new IndexAxisValueFormatter(labels));
    xAxis.setLabelRotationAngle(-45);
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    graph.invalidate(); // refresh

}

This is how the chart displays when the activity is first created:

Graph when first displayed

This is how the chart displays when the chart is interacted with (double tap or zoom):

Graph after interaction

I am not sure how to fix this issue, or why it is happening. Any assistance will be greatly appreciated!

Update: If I replace my BarChart with a CombinedChart, then the labels appear as expected. However, this is not suitable for what I need as I want to create a grouped bar chart (displaying two sets of data for the same month). Is there any way to achieve this using a CombinedChart? Assigning it two BarData makes it only draw the second one that was assigned.

Upvotes: 2

Views: 4888

Answers (3)

Aneeb
Aneeb

Reputation: 789

I also had the same problem. You should use force false to show all labels.
My issue was resolved with this:

barChart.getXAxis().setLabelCount(entries.size(), false);

force true not showing all labels. I am using this version v3.1.0

Upvotes: 2

Victor
Victor

Reputation: 461

For v3.1.0

I had the same problem. I solved it with this: xAxis.setLabelCount(entries.size, true)

If it doesn't work, check the library version.

Upvotes: 0

Lilya
Lilya

Reputation: 567

Try removing xAxis.setLabelCount(entries.size(), true) X axis shows all its values by default. There is no need to set the labels count by force unless you want a specified number.

From documentation:

/**
     * sets the number of label entries for the y-axis max = 25, min = 2, default: 6, be aware
     * that this number is not
     * fixed (if force == false) and can only be approximated.
     *
     * @param count the number of y-axis labels that should be displayed
     * @param force if enabled, the set label count will be forced, meaning that the exact
     *              specified count of labels will
     *              be drawn and evenly distributed alongside the axis - this might cause labels
     *              to have uneven values
     */
    public void setLabelCount(int count, boolean force) {

        setLabelCount(count);
        mForceLabels = force;
    }

Upvotes: 1

Related Questions