如何使用JFreeChart在2行中的折线图的X轴上渲染日期值?

问题描述 投票:0回答:1

我正在使用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);

没有成功,我怎么能实现那个?

jfreechart axis linechart text-styling
1个回答
0
投票

经过一些自我研究后,我发现通过ChartUtilities.encodeAsPNG(chart.createBufferedImage(500, 300));命令减小JFreeChart图像的宽度和高度值时,文本会自动调整为新行。

经过一些更多的研究,我得到了这个为我工作的命令。通过CategoryAxisX-Axis so getDomainAxis()创建Y-Axis it is getRangeAxis()的对象

CategoryAxis domainAxis = chart.getCategoryPlot().getDomainAxis();
domainAxis.setMaximumCategoryLabelWidthRatio(0.25f);

然后我们使用.setMaximumCategoryLabelWidthRatio(float ratio)并根据您的要求调整它!

© www.soinside.com 2019 - 2024. All rights reserved.