我正在使用JFreeChart使用CategoryPlot呈现LineChart。就像是:
JFreeChart chart = ChartFactory.createLineChart("Daily Revenue",
"Days", "Revenue", dataset);
CategoryPlot plot = chart.getCategoryPlot();
所以你可以理解我必须在XAxis上显示像23 Feb'18 11:00:00
这样的完整时间倾斜到45度,这是我能够实现的
CategoryAxis categoryAxis = chart.getCategoryPlot().getDomainAxis();
categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
但我想在XAxis上以2行显示我的文字,有点像:
23 Feb'18
11:00:00
倾斜到45度。我试过用过的
CategoryAxis categoryAxis = chart.getCategoryPlot().getDomainAxis();
categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
categoryAxis.setMaximumCategoryLabelLines(5);
没有成功,我怎么能实现那个?
经过一些自我研究后,我发现通过ChartUtilities.encodeAsPNG(chart.createBufferedImage(500, 300));
命令减小JFreeChart图像的宽度和高度值时,文本会自动调整为新行。
经过一些更多的研究,我得到了这个为我工作的命令。通过CategoryAxis
为X-Axis so getDomainAxis()
创建Y-Axis it is getRangeAxis()
的对象
CategoryAxis domainAxis = chart.getCategoryPlot().getDomainAxis();
domainAxis.setMaximumCategoryLabelWidthRatio(0.25f);
然后我们使用.setMaximumCategoryLabelWidthRatio(float ratio)
并根据您的要求调整它!