JFreeChart轴位置

时间:2012-03-12 12:00:17

标签: position jfreechart

假设左侧有一个垂直轴的公共​​条形图...我想知道如何控制图表左侧和垂直轴之间的空间?我们的想法是,只要轴上显示的刻度标签变大,轴就会向右推,以容纳刻度标签。我希望能够指定轴应始终显示在图表整个宽度的20%处,这可能吗?

2 个答案:

答案 0 :(得分:0)

条形图通常包含CategoryAxis个域名,因此您可以试用setLowerMargin()setUpperMargin()setCategoryMargin()

答案 1 :(得分:0)

您需要在范围轴上设置固定的轴空间: http://www.jfree.org/jfreechart/api/gjdoc/org/jfree/chart/plot/XYPlot.html#setFixedRangeAxisSpace:AxisSpace

假设您刚刚创建图表图像:

// Get your plot from the chart object..
XYPlot plot = (XYPlot)chart.getPlot();

// Create an instance of the image so we can do some calculations
BufferedImage image = new BufferedImage( width, height, BufferedImage.TYPE_INT_ARGB);

// Create an instance of the Graphics2D from your image
Graphics2D g2 = image.createGraphics();

// Get the reserve space that jfree chart sets aside for the axis
AxisSpace space = yAxis.reserveSpace(g2, plot, new Rectangle(width,height), plot.getRangeAxisEdge(), plot.getFixedRangeAxisSpace());

// Give that space a fixed width
space.setLeft(fixedAxisWidth);

// Set it in the plot
plot.setFixedRangeAxisSpace(space);

您可能只需定义一个AxisSpace而无需通过所有Graphics2D rigamarole,但这就是我过去的做法。