【问题标题】:Subscript in Axis description轴描述中的下标
【发布时间】:2015-06-29 08:20:11
【问题描述】:

我想知道是否可以在轴描述中使用下标。我有以下代码

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

有没有办法给字符串“ax [m/s²]”中的 x 下标?下标将是例如X₉

【问题讨论】:

  • 你从哪里得到²? ...你不能用 x 做吗
  • 很遗憾这是不可能的。

标签: java jfreechart subscription chartpanel


【解决方案1】:

使用here 所示的方法,您可以为所需的轴标签指定AttributedString。给定一个名为domainNumberAxis,下面的示例使用TextAttribute 值来更改某些字符的SIZEWEIGHT,下标第二个字符并上标指数。

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

【讨论】:

  • 优秀;我更新了示例以说明TextAttribute 的一些可能变化。
【解决方案2】:

您可以尝试对下标/上标使用 unicode 值 - 这些值应该受到轴标签的 Graphics2D 渲染的尊重(通过 Graphics2D.drawString 方法)。例如 'X\u2089' 将呈现类似于 X₉。这当然取决于现有的 Unicode 下标值以及支持它的 java。

【讨论】:

    【解决方案3】:

    我不确定 jFreeChart,但纯 java 字符串可以承受,请尝试:

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

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

    【讨论】:

    • 感谢您的回答。我已经尝试过了,但我得到的只是一个空盒子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多