Edy Bourne
Edy Bourne

Reputation: 6187

In JFreeChart, how to position the Domain Axis Label next to the Legend?

Essentially, I have a chart that looks like this:

How it is

but I want the Domain axis label to show on the left, on the same line as the legend, on the right:

How I want it

so I can better utilize the small space I got.

My chart source looks like this:

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

How can I accomplish this? Is there a way?

Thank you!

Upvotes: 1

Views: 860

Answers (1)

Edy Bourne
Edy Bourne

Reputation: 6187

Found it.

To move the axis to the desired position, I used:

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

Then to move the legend up in the same line, I had to adjust it's padding to use a negative value, which essentially moves it up, like so:

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

This results in what I was looking for:

enter image description here

Hope this helps someone else out there!

Upvotes: 2

Related Questions