Java 在JFreeChart中,如何更改水平条形图中类别标签的文本对齐

Java 在JFreeChart中,如何更改水平条形图中类别标签的文本对齐,java,jfreechart,Java,Jfreechart,我在一个图表中工作,该图表具有以下演示: ?如何使类别标签左对齐或对齐?实际上,它们看起来居中,但这不是我所需要的 我使用的代码如下所示: JFreeChart chart = getChart(); CategoryPlot plot = (CategoryPlot) chart.getPlot(); BarRenderer renderer = (BarRenderer) plot.getRenderer(); renderer.setDrawBarOutline(true); Cat

我在一个图表中工作,该图表具有以下演示:

?如何使类别标签左对齐或对齐?实际上,它们看起来居中,但这不是我所需要的

我使用的代码如下所示:

JFreeChart chart = getChart();
CategoryPlot plot = (CategoryPlot) chart.getPlot();

BarRenderer renderer = (BarRenderer) plot.getRenderer();
renderer.setDrawBarOutline(true);

CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setMaximumCategoryLabelLines(5);

CategoryLabelPositions p = domainAxis.getCategoryLabelPositions();

CategoryLabelPosition left = new CategoryLabelPosition(
    RectangleAnchor.LEFT, TextBlockAnchor.CENTER_LEFT, 
    TextAnchor.CENTER_LEFT, 0.0,
    CategoryLabelWidthType.RANGE, 0.70f //Assign 70% of space for category labels
);

domainAxis.setCategoryLabelPositions(CategoryLabelPositions
        .replaceLeftPosition(p, left));

NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

BasicStroke stroke = new BasicStroke(1);
plot.setDomainGridlinePaint(Color.black);
plot.setDomainGridlineStroke(stroke);
plot.setRangeGridlinePaint(Color.black);
plot.setRangeGridlineStroke(stroke);

CategoryDataset cd = plot.getDataset();

setBarColors(renderer, plot, cd);

谢谢。

类别标签是
org.jfree.text.TextBlock
的实例,它确实有一个
setLineAlignment(HorizontalAlignment)
方法,但我没有看到通过CategoryPlot或CategoryAxis API获得它们的方法(有很多方法和强制类型转换,所以我不能肯定地告诉你这样的方法不存在,只是我没有找到一个)。不过,重写
CategoryAxis
中的
createLabel
方法来设置对齐是可行的

(在获取域轴的代码之前):


您可能希望将请求发布到或,因为David Gilbert将是最好的确定回答此问题的人。

标签文本向左对齐,这是正确的。现在我必须解决左侧对齐问题。非常感谢Zebby。左侧对齐是通过CategoryLabelPositions完成的,这已在上述代码中使用。
plot.setDomainAxis(new CategoryAxis() {
    @Override
    protected TextBlock createLabel(Comparable category, float width, RectangleEdge edge, Graphics2D g2) {
        TextBlock label = TextUtilities.createTextBlock(category.toString(), getTickLabelFont(category), getTickLabelPaint(category), width, this.getMaximumCategoryLabelLines(), new G2TextMeasurer(g2));
        label.setLineAlignment(HorizontalAlignment.LEFT);
        return label;
    }
});