I am using Vaadin charts from vaadin 7.7.6. When I create a chart where I have several values obtained in different dates, the chart is created correctly. Here I put an image of the obtained result where each value is a pair to be represented.
The issue comes, when I want to convert this chart to a PDF file or create a web service that returns this chart as an image to be included elsewhere.
For creating an image from the chart, I use the SVGGenerator as recommended in the documentation. In general, it works really fine and I am able to create PDFs with charts inside. But with this chart I have an issue if the X axis is using dates. Here I copy the chart after converted to SVG:
(I have removed some chart controls as the Navigation and the Range selector because are useless in an image or PDF).
At the image we can observe that now in the X axis the dates are not showed correctly. I have checked for several hours the issue, and I am pretty sure that the issue is related to the SVGConversor.
Reading the SVGConversor code, I found a withLang(String lang)
method. My thought was that maybe, if I set the language, the date is showed correctly (something similar to localization). I am not able to found any documentation of the value needed to this function and therefore, I have tried some values such as en
, en_EN
, en-EN
, English
, .... But does not matter the value I put there, always this error is launched:
java.lang.RuntimeException: SVG generation failed: ReferenceError: Can't find variable: en
Still, I am not sure that the withLang(String lang)
method is the solution to my issue.
The question is: How can I show the correct dates in the X axis of my chart when converted to SVG?
Seems that the solution at the end was really easy. The issue is that I have not set the xAxis type. It is not a problem when using the chart in a Vaadin view, but it is mandatory for the SVGConverter to know the axis. Then adding this line of code:
chart.getConfiguration().getxAxis().setType(AxisType.DATETIME);
Fix the issue with the SVGConverter. Now the chart is represented correctly in my PDF documents.