PedroMVU
PedroMVU

Reputation: 83

MPAndroidChart show specified range of XValues even when there is no corresponding YValue?

The Problem I am having is that I have charts which should always show a fixed range of XValues let's say all 24 hours in a day, but the Chart is only showing XValues up to and starting from the point of the first and last YValue, that is if I have two Y events, let's say 2am and 3pm, instead of showing the whole 24 hours, it will start at 2am and end at 3pm.

I think this has something to do with the new way to handle XValues via IAxisValueFormatter, since this used to work fine when we could add both X and Y values to a ChartData.

One hack to workaround this I found is to add empty YValues to first and last hour in my case 0 values, but this brings another issue that no matter what I set the minimum in LeftAxis it starts showing negative label values (starts in -15 instead of 0).

Is there an official way to do this? I have researched quite a bit about this and I am thinking on going back to a previous version of the library where I could just use the old way.

Edit: Just to clarify the problem doesn't have anything to do with the labels, but with the start and end of the Chart in X, it is discarding the range of X which doesn't have any YValues which I understand makes sense but it makes the results look ugly in certain cases.

Upvotes: 0

Views: 857

Answers (1)

Ayush Khare
Ayush Khare

Reputation: 1842

You can use the setLabelCount() method to force the axis to draw labels according to the count you specify

According to the documentation:

setLabelCount(int count, boolean force)

@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

And the setLabelCount method is defined below

public void setLabelCount(int count) {

    if (count > 25)
        count = 25;
    if (count < 2)
        count = 2;

    mLabelCount = count;
    mForceLabels = false;
}

You can use setLabelCount() with setAxisMaximum() and setAxisMinimum()

For example:

axis.setLabelCount(24, true);
axis.setAxisMaximum(24f);
axis.setAxisMinimum(0f);

This will draw values from 0 - 24 regardless of Y-Axis values

Upvotes: 1

Related Questions