Adding a% to Y-Axis Labels in an AChartEngine Chart Diagram

I am working on an android application and am using AChartEngine for Charting. The bar chart is based on dynamic data coming from the server.

The Y-Axis icons are set to display from 0 to 100, and none of the labels is 11 with 0..10..20..30..40..60..70..80..90..100 in as Y-Axis tags. Is it possible to set custom Y-Axis labels in such a way that after the Y-Axis character it adds a β€œ%” sign so that it shows

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

How to do it?

+4
source share
3 answers

All you need to do: 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); 
+1
source

To set custom labels along the y axis, you just need to use the following method:

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

In addition, if you want to hide the default tags, do the following:

 mRenderer.setYLabels(0); 
+1
source

I haven’t done much with AChartEngine, but a quick look at the source code suggests that you can extend BarChart to accomplish what you need.

Look at the getLabel(double label) method located in AbstractChart ( BarChart extends XYChart , which extends AbstractChart on its own).

  /** * 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 understand how this works out; for example, just add "%" to the result above:

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

The only thing I'm not too sure is whether the same method is used to create labels for the X axis. If this happens, you probably need to do something a little smarter to enable it only for the axis you are interested in.

0
source

Source: https://habr.com/ru/post/1441735/


All Articles