0

Is there any way to set no label for specifics values ?

I explain : I have a GraphView for which Y values cannot be higher than 3. And I only want verticals labels for the values 1, 2 and 3.

I tried to use the following method :

myGraph.getGraphViewStyle().setNumVerticalLabels(3);

But this one puts my labels for the Y values 0,1.5 and 3. This is not what I want.

Then I tried to set a custom label formatter. But the thing is I did not succeed in putting no values for the other labels.

Here is my current code :

myGraph.setCustomLabelFormatter(new CustomLabelFormatter() {
    @Override
    public String formatLabel(double value, boolean isValueX) {
        if(!isValueX) {

            String res = null;

            if(value == 1.0) {
                res = getString(R.string.my_label_1);
            } else if(value == 2.0) {
                res = getString(R.string.my_label_2);
            } else if(value == 3.0) {
                res = getString(R.string.my_label_3);
            }

            return res;
        }

        return null;
    }
}

So I return null when I don't want to display the value but the library makes that when we return null, it puts the double value of the label. I tried to return an empty string or a string with blank spaces but it makes my app crashes.

Have you any idea about a way to do what I want ?

It would be great !

Thanks in advance ;)

Alexandre D.
  • 711
  • 1
  • 9
  • 28

1 Answers1

0

If you don't mind a decimal point you could simply:

    myGraph.getGraphViewStyle().setNumVerticalLabels(4);

Alternatively, to remove the decimal point you could use a staticLabelFormatter eg.

   // Version 4.0.0  syntax

    StaticLabelsFormatter staticLabelsFormatter = new StaticLabelsFormatter(graphView);
    staticLabelsFormatter.setVerticalLabels(new String[]{"0","1","2","3"});

    graphView.getViewport().setYAxisBoundsManual(true);
    graphView.getViewport().setMinY(0d);
    graphView.getViewport().setMaxY(3d);
    graphView.getGridLabelRenderer().setNumVerticalLabels(4);
    graphView.getGridLabelRenderer().setLabelFormatter(staticLabelsFormatter);

Or:

    // Version 4.0.0  syntax

    NumberFormat nf = NumberFormat.getInstance();
    nf.setMinimumFractionDigits(0);

    StaticLabelsFormatter staticLabelsFormatter = new StaticLabelsFormatter(graphView);
    staticLabelsFormatter.setDynamicLabelFormatter(new DefaultLabelFormatter(nf, nf));

    graphView.getViewport().setYAxisBoundsManual(true);
    graphView.getViewport().setMinY(0d);
    graphView.getViewport().setMaxY(3d);
    graphView.getGridLabelRenderer().setNumVerticalLabels(4); 
    graphView.getGridLabelRenderer().setLabelFormatter(staticLabelsFormatter);
arober11
  • 1,969
  • 18
  • 31