I am using JFreeChart to render a LineChart using CategoryPlot. Something like:
JFreeChart chart = ChartFactory.createLineChart("Daily Revenue",
"Days", "Revenue", dataset);
CategoryPlot plot = chart.getCategoryPlot();
So as you can understand I must have to display full Time like 23 Feb'18 11:00:00
on the XAxis tilted to 45degrees which I am able to achieve using
CategoryAxis categoryAxis = chart.getCategoryPlot().getDomainAxis();
categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
But I want to display my text on XAxis in 2 lines somewhat like:
23 Feb'18
11:00:00
tilted to 45 degrees. For which I had tried using
CategoryAxis categoryAxis = chart.getCategoryPlot().getDomainAxis();
categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
categoryAxis.setMaximumCategoryLabelLines(5);
with no success so how can I achieve that??
After some self-research I found out that while reducing the width and height values of the JFreeChart Image through ChartUtilities.encodeAsPNG(chart.createBufferedImage(500, 300));
command that the text is getting auto-adjusted to new line.
So after some more research, I got this command that does the work for me. By Creating a Object of CategoryAxis
along X-Axis so getDomainAxis()
for Y-Axis it is getRangeAxis()
CategoryAxis domainAxis = chart.getCategoryPlot().getDomainAxis();
domainAxis.setMaximumCategoryLabelWidthRatio(0.25f);
then we use .setMaximumCategoryLabelWidthRatio(float ratio)
and adjust it as per your requirement!!