Subscript in Axis description

南笙酒味 提交于 2019-11-27 08:02:26

问题


I wanted to know if it is possible to use subscript in axis description. I have the following code

    XYItemRenderer lineYY = new StandardXYItemRenderer();
    lineYY.setSeriesPaint(0, Color.BLUE);
    lineYY.setSeriesVisibleInLegend(0,false);
    final NumberAxis yaxY = new NumberAxis("ax [m/s²]");
    yaxY.setRange(-11, 11);
    yaxY.setAutoRangeIncludesZero(false);
    XYPlot plotYY = new XYPlot(datasetY,null,yaxY, lineYY);
    plotYY.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);

Is there a way to subscript the x in the String "ax [m/s²]"? An subscript would be e.g. X₉


回答1:


Using the approach shown here, you can specify an AttributedString for the desired axis label. Given a NumberAxis named domain, the example below uses TextAttribute values to alter the SIZE and WEIGHT of some characters, subscripts the second character and superscripts the exponent.

String s = "ax [m/s2]";
AttributedString as = new AttributedString(s);
as.addAttribute(TextAttribute.SIZE, 24, 0, 2);
as.addAttribute(TextAttribute.SIZE, 16, 3, 9);
as.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD, 0, 2);
as.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUB, 1, 2);
as.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER, 7, 8);
domain.setAttributedLabel(as);




回答2:


You can try to use unicode values for the subscript/superscript - these should be respected by the Graphics2D rendering of the axis label (via Graphics2D.drawString method). For instance 'X\u2089' would be rendered similar to X₉. This is of course dependent on the Unicode subscript value existing as well as java supporting it.




回答3:


I am not sure about jFreeChart, but a pure java string could bear that, please try:

final NumberAxis yaxY = new NumberAxis("a\u2093 [m/s²]");

see: http://www.fileformat.info/info/unicode/char/2093/index.htm



来源:https://stackoverflow.com/questions/29784628/subscript-in-axis-description

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!