4

I am working on an android app and using AChartEngine for Charting. The Bar Chart is drawn on the basis of the dynamic data coming from a server.

Th Y-Axis labels are set to be shown from 0 to 100 and no of labels are 11 s it shows 0..10..20..30..40..60..70..80..90..100 as Y-Axis Labels. Is it possible to set custom Y-Axis labels such that it adds '%' sign after the Y-Axis title value so that it shows,

0%..10%..20%..30%..40%..60%..70%..80%..90%..100% as Y-Axis label values.

How to do it??

Fahad Abid Janjua
  • 1,024
  • 3
  • 14
  • 35

3 Answers3

1

In order to set custom labels on the Y axis, you just need to use the following method:

mRenderer.addYTextLabel(10, "10%");
mRenderer.addYTextLabel(20, "20%");
...

Also, if you want to hide the default labels, do this:

mRenderer.setYLabels(0);
Dan D.
  • 32,246
  • 5
  • 63
  • 79
1

All what you need to do is: Enjoy ;)

renderer.addXTextLabel(0, "0");
    renderer.addYTextLabel(10, "10%");
    renderer.addYTextLabel(20, "20%");
    renderer.addYTextLabel(30, "30%");
    renderer.addYTextLabel(40, "40%");
    renderer.addYTextLabel(50, "50%");
    renderer.addYTextLabel(60, "60%");
    renderer.addYTextLabel(70, "70%");
    renderer.addYTextLabel(80, "80%");
    renderer.addYTextLabel(90, "90%");
    renderer.addYTextLabel(100, "100%");

    renderer.setYLabels(0);
0

I haven't actually done a lot with AChartEngine, but a quick glance at the source code suggests you could extend BarChart to accomplish what you're after.

Have a look at the getLabel(double label) method located in AbstractChart (BarChart extends XYChart, which on its turn extends AbstractChart).

 /**
   * Makes sure the fraction digit is not displayed, if not needed.
   * 
   * @param label the input label value
   * @return the label without the useless fraction digit
   */
  protected String getLabel(double label) {
    String text = "";
    if (label == Math.round(label)) {
      text = Math.round(label) + "";
    } else {
      text = label + "";
    }
    return text;
  }

I would start with something naive to see how that works out; e.g. simply append "%" on the result of above:

@Override protected String getLabel(double label) {
    return super.getLabel(label) + "%";
}

Only thing I'm not too sure about is whether the same method is used to generate labels for the X-axis. If that's the case, you'll probably need to do something slightly smarter to enable it just for the axis you're interested in.

MH.
  • 45,303
  • 10
  • 103
  • 116