简体   繁体   English

在条形图AChartEngine中的Y轴标签上添加%符号

[英]Adding % sign to Y-Axis Labels in Bar Chart AChartEngine

I am working on an android app and using AChartEngine for Charting. 我正在开发一个Android应用程序,并使用AChartEngine进行图表绘制。 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. Y轴标签设置为从0到100显示,标签个数都不是11,它显示0..10..20..30..40..60..70..80..90 .. 100作为Y轴标签。 Is it possible to set custom Y-Axis labels such that it adds '%' sign after the Y-Axis title value so that it shows, 是否可以设置自定义Y轴标签,以便在Y轴标题值之后添加'%'符号,以便显示,

0%..10%..20%..30%..40%..60%..70%..80%..90%..100% as Y-Axis label values. 0%.. 10%.. 20%.. 30%.. 40%.. 60%.. 70%.. 80%.. 90%.. 100%作为Y轴标签值。

How to do it?? 怎么做??

In order to set custom labels on the Y axis, you just need to use the following method: 为了在Y轴上设置自定义标签,您只需要使用以下方法:

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

Also, if you want to hide the default labels, do this: 另外,如果要隐藏默认标签,请执行以下操作:

mRenderer.setYLabels(0);

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);

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. 我实际上对AChartEngine并没有做很多事情,但是快速浏览一下源代码就可以建议您扩展BarChart来完成您要完成的工作。

Have a look at the getLabel(double label) method located in AbstractChart ( BarChart extends XYChart , which on its turn extends AbstractChart ). 看一下AbstractChart中的getLabel(double label)方法( BarChart扩展了XYChart ,而XYChart又扩展了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; 我将从一些幼稚的事情开始,看看它是如何工作的。 eg 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. 我不太确定的是,是否使用相同的方法为X轴生成标签。 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. 如果是这种情况,您可能需要做一些更聪明的操作才能针对您感兴趣的轴启用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM