在JFreeChart中,如何将域轴标签放置在图例旁边?

问题描述 投票:1回答:1

基本上,我有一个看起来像这样的图表:

How it is

但是我希望域轴标签显示在左侧,与图例在同一行,在右侧:

How I want it

所以我可以更好地利用我得到的小空间。

我的图表来源看起来像这样:

Color lightGray = new Color(200, 200, 200);
Color gray = new Color(150, 150, 150);
JFreeChart chart = createChart(createDataset(), yAxisTitle, xAxisTitle);
chart.setBackgroundPaint(Color.BLACK);

chart.getPlot().setBackgroundPaint(Color.BLACK);
chart.getPlot().setInsets(new RectangleInsets(0,2,2,2));
chart.getXYPlot().setBackgroundPaint(Color.BLACK);

chart.getXYPlot().getDomainAxis().setAxisLinePaint(lightGray);
chart.getXYPlot().getDomainAxis().setTickLabelFont(new Font("Arial", Font.PLAIN, 10));
chart.getXYPlot().getDomainAxis().setTickLabelPaint(lightGray);
chart.getXYPlot().getDomainAxis().setLabelFont(new Font("Arial", Font.PLAIN, 10));
chart.getXYPlot().getDomainAxis().setLabelPaint(lightGray);

chart.getXYPlot().getRangeAxis().setAxisLinePaint(lightGray);
chart.getXYPlot().getRangeAxis().setTickLabelFont(new Font("Arial", Font.PLAIN, 10));
chart.getXYPlot().getRangeAxis().setTickLabelPaint(lightGray);
chart.getXYPlot().getRangeAxis().setLabelFont(new Font("Arial", Font.PLAIN, 10));
chart.getXYPlot().getRangeAxis().setLabelPaint(lightGray);

chart.getTitle().setFont(new Font("Arial", Font.PLAIN, 12));
chart.getTitle().setBackgroundPaint(Color.BLACK);
chart.getTitle().setPaint(lightGray);

chart.getLegend().setItemFont(new Font("Arial", Font.PLAIN, 10));
chart.getLegend().setBackgroundPaint(Color.BLACK);
chart.getLegend().setItemPaint(gray);

chart.setTextAntiAlias(true);
chart.setAntiAlias(true);
chart.getLegend().setHorizontalAlignment(HorizontalAlignment.RIGHT);

panel.add(new ChartPanel(chart, false));

我该如何完成?有办法吗?

谢谢!

java swing charts jfreechart
1个回答
1
投票

找到了。

为了将轴移动到所需位置,我使用了:

chart.getXYPlot().getDomainAxis().setLabelLocation(AxisLabelLocation.LOW_END);

然后将图例在同一行中向上移动,我不得不调整其填充以使用负值,这实际上将其向上移动,如下所示:

RectangleInsets padding = chart.getLegend().getPadding();
        chart.getLegend().setPadding(new RectangleInsets(padding.getTop() - 20, padding.getRight(), padding.getBottom(), padding.getLeft()));
        chart.getLegend().setBounds(chart.getLegend().getBounds());

这导致我一直在寻找:

enter image description here

希望这可以帮助其他人!

© www.soinside.com 2019 - 2024. All rights reserved.