Java 在JFreeChart中,如何将域轴标签定位在图例旁边?
基本上,我有一个图表如下所示:Java 在JFreeChart中,如何将域轴标签定位在图例旁边?,java,swing,charts,jfreechart,Java,Swing,Charts,Jfreechart,基本上,我有一个图表如下所示: 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(Col
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));
但我希望域轴标签显示在左侧,与图例在同一行,右侧:
所以我可以更好地利用我的小空间
我的图表源如下所示:
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());
这就是我想要的结果:
希望这对其他人有帮助 为了更好的帮助,请尽快发布一个正确的,包括您正在使用的JFreeChart版本,我在JFreeChart 1.5上。虽然我没有机会做一个最小的重做案例,但我确实找到了解决方案,所以我将在这里发布。