Skip to content
Advertisement

Create Java Apache POI Line Chart where Dates appear on the horizontal X-axis

I’m trying to create a simple line chart using the Java Apache POI which is essentially a time series, consisting of a Date and a numeric value:

JavaScript

I would like for the dates to appear on the x-axis. However, this seems to be difficult to achieve. I can do it manually in Excel but not via the API.

Advertisement

Answer

This is actually very easy using the current apache poi 4.1.0 ;-).

There is an example for creating line charts in https://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/. This one can take as basis.

The only things you need to know additional is that dates are stored as numeric values in double precision. So the category axis values come fromNumericCellRange and not fromStringCellRange. And if really a date axis is needed instead of a category axis, XDDFDateAxis must be created instead of XDDFCategoryAxis.

But.

Additional problems result from Microsofts weird decisions to make things default in Excel versions later than 2007. In Excel versions later than 2007 category axes are no more text axes type by default. Even date axes are no more date axes type by default. Instead they are “auto” type dependent on the data. So we need explicitly set that we do not want that auto type. Also axes number formats are no more linked to the source. So we need set even that explicitly.

Complete example:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement