Search code examples
javachartsjfreechartgantt-chart

Gantt chart with nanoseconds


I need to create a Gantt chart for data from a microprocessor that has nanosecond precision and total duration of one second. Gantt chart in JFreeChart seems to fit my needs except that it uses java.util.Date which has only milliseconds precision (as mentioned here: JFree TimeSeries Chart Spanning Nanoseconds).

Date4j can be used instead but a lot of internal code needs to be changed so that is not really feasible Link.

A work-around is to create a ValueAxis Subclass to handle the nanoseconds. There are couple of examples that deal with custom labelling with ValueAxis here and here, unfortunately I could not find a way to create a nanoseconds timescale.

I ll be grateful if anyone can show me how to replace the existing Date Axis of the Gantt chart with nanosecond labelling.


Solution

  • I managed to solve my problem by overwriting the date axis with custom number format axis that is used to place the clock cycles in the x axis.

    CategoryPlot plot = (CategoryPlot) chart.getPlot();       
    CategoryItemRenderer renderer = plot.getRenderer(); 
    renderer.setSeriesPaint(0, Color.blue); 
    
    NumberAxis xAxis = new NumberAxis();
    DecimalFormat format = (DecimalFormat)DecimalFormat.getNumberInstance(Locale.ENGLISH);
    format.applyPattern("#");
    xAxis.setNumberFormatOverride(format);
    
    xAxis.setLabel("Cycles");
    plot.setRangeAxis(xAxis);
    
    renderer.setBaseToolTipGenerator(new IntervalCategoryToolTipGenerator("{3} - {4}", format));