MPAndroidChart icon indicating copy to clipboard operation
MPAndroidChart copied to clipboard

Why does AxisBase.setLabelCount has a maximum value ? And why 25 ?

Open philippeauriach opened this issue 9 years ago • 11 comments

AxisBase.setLabelCount put a maximum of 25 labels. Why this limit ? How has it been decided ?

public void setLabelCount(int count) {

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

     mLabelCount = count;
     mForceLabels = false;
 }

I'm trying to have only specific labels displayed on xAxis (one every two points, starting at the second data point), so I was going to tell the axis I have 52 labels (the number of points in my case, from 0 to 51), and then in my custom axisFormatter, return empty string for labels that should not be shown. Is there an easier way ?

In other word, I want to specifically tell the graph to show a label for one (or several) x points, by deciding which x point (not having it automatically chosen by the lib).

thanks

philippeauriach avatar Dec 30 '16 14:12 philippeauriach

I had need for this and ended up leaving it off to implement later (to do more important things). Later on I ended up needing limit lines to denote sections of the graph. So I modified the library to allow you to place a label on the center bottom of a limit line.

While this change may not be the best solution to your problem, it would work. Just add limit lines at every point you need to show a label for. You can check out my changes at #2631 and a very rough quick example on my fork here.

Just make sure you really want to show that many labels, because the more labels you add at specific points the higher chance they will overlap, especially on smaller screens.

Again, it may not be the best solution to your use case but if you need something quick this will work.

JStav avatar Dec 30 '16 15:12 JStav

the solution with limit lines actually looks perfect ! testing right now. thanks for your quick answer. I do not want to actually display 53 labels, I juste want to pick precisely which one to be displayed (not having the lib to decide that label at position 5.67 or whatever to be displayed, I need 5 and 6, always, the graph is non scrollable).

philippeauriach avatar Dec 30 '16 15:12 philippeauriach

No problem, happy to help

JStav avatar Dec 30 '16 15:12 JStav

Just one thing though, how could I point to your fork instead of the lib ?

philippeauriach avatar Dec 30 '16 15:12 philippeauriach

The easiest way is probably replacing your gradle dependency with

compile 'com.github.jstav:MPAndroidChart:limit-line-label-bottom-SNAPSHOT'

You can also fork it yourself and make the changes, and then point gradle to your fork. This makes is safer because you have control of the changes.

JStav avatar Dec 30 '16 15:12 JStav

did not know this was possible, thanks a lot !

philippeauriach avatar Dec 30 '16 15:12 philippeauriach

As long as you have maven { url "https://jitpack.io" } in your repositories in build.gradle it will work. JitPack is an excellent service.

JStav avatar Dec 30 '16 15:12 JStav

ok your solution works ! thanks again. There is just a problem with labels been "cut" by the graph view, but I should be able to handle this by giving the graph a bigger y range.

philippeauriach avatar Dec 30 '16 16:12 philippeauriach

@JStav thanks for your solution, but unfortunately it did not exactly fit my needs : the labels are indeed well positioned, but they are above the graph (not above or under the graph like labels). So I tweaked the lib and added the ability to specify exactly where labels should be drawn : https://github.com/philippeauriach/MPAndroidChart/tree/custom-labels With this solution you can ask the graph to draw labels at positions for example 10, 20 and 60, and those three label will always be drawn, the others wont. Because of this, it is only suitable for fixed graph (without horizontal scaling/dragging). For now it is only made for the x axis, it should not be hard to implement the same thing for y. It is working right now but need further testing and maybe some code styling / adjustment, fine tuning etc... before I'll make a PR. In the meantime I'm open to suggestions ! Thanks again @JStav for your help.

philippeauriach avatar Jan 02 '17 11:01 philippeauriach

@philippeauriach Great Job. I used your version and it works great! Thanks! I think that this feature is very useful and i hope that it will be merged in the main release.

fabriziogueli avatar Jan 09 '17 09:01 fabriziogueli

Just created a PR here : https://github.com/PhilJay/MPAndroidChart/pull/2692

philippeauriach avatar Jan 09 '17 11:01 philippeauriach