Skip to content

howto date time axis

Erich Seifert edited this page Mar 14, 2016 · 1 revision

How can I display date or time labels on my axis?

Any class derived from java.text.Format can be used to format axis labels. So if you want to display labels with date and/or time information, it's best to use the responsible classes from the Java API:

Example which uses ​java.text.DateFormat to show only minutes and seconds:

DataTable data = new DataTable(Long.class, Double.class);
data.add(System.currentTimeMillis(), 1.23);
data.add(System.currentTimeMillis(), 1.24);
data.add(System.currentTimeMillis(), 1.25);

XYPlot plot = new XYPlot(data);
// Format all number as time
AxisRenderer rendererX = plot.getAxisRenderer(XYPlot.AXIS_X);
DateFormat dateFormat = DateFormat.getTimeInstance(DateFormat.SIMPLE);
rendererX.setTickLabelFormat(dateFormat);

Example which uses ​java.text.SimpleDateFormat to show date and time in an ​ISO 8601-like format:

DataTable data = new DataTable(Long.class, Double.class);
data.add(System.currentTimeMillis(), 1.23);
data.add(System.currentTimeMillis(), 1.24);
data.add(System.currentTimeMillis(), 1.25);

XYPlot plot = new XYPlot(data);
// Format all number as time
AxisRenderer rendererX = plot.getAxisRenderer(XYPlot.AXIS_X);
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
rendererX.setTickLabelFormat(dateFormat);