本质上,我有一个看起来像这样的图表:
但我希望域轴标签显示在左侧,与图例在同一行,右侧:
这样我就可以更好地利用我得到的小空间。
我的图表源如下所示:
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());
这导致了我正在寻找的内容:
希望这可以帮助其他人!
关于java - 在 JFreeChart 中,如何将域轴标签放置在图例旁边?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61735045/