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

标签 java swing charts jfreechart

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

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

我怎样才能做到这一点?有办法吗?

谢谢!

最佳答案

找到了。

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

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

希望这可以帮助其他人!

关于java - 在 JFreeChart 中,如何将域轴标签放置在图例旁边?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61735045/

相关文章:

Java JAXB : XML to Java object conversion

java - 使用 SAP JCo 3.x 的教程、手册和文档

Java 正则表达式空格匹配 $ 和 * ??

javascript - 将参数传递给 'document.getElementById'

java - 在内存数据库中使用 h2 的 JUnit 在 SQL 中具有 "ARRAY"函数时不起作用

java - 从查询更新 JTable

java - 如何将图像从一个面板拖动到另一个面板

java - KeyListener 第一次使用时没有响应?

php - Google Charts 日期时间问题,现在不显示 JSON 数据

ios - 向 UIView 添加多个 subview