Search code examples
javajfreechart

JFreeChart: Candlestick chart with different x-axis?


Is it possible to draw a candlestick chart with the x-axis being of the type number/integer, instead of time? Currently i'm using ChartFactory#createCandlestickChart which takes an OHLCDataset, which uses a timestamps for the x-axis.


Solution

  • ChartFactory::createCandlestickChart uses a DateAxis internally, which sees each data item's Date as the number of milliseconds since the epoch. As a result, you can

    • Specify the desired format in setDateFormatOverride() as shown here, or

    • Use a NumberAxis in your own factory, perhaps modeled on the one cited, or simply substitute a NumberAxis:

        XYPlot xyplot = chart.getXYPlot();
        NumberAxis domain = new NumberAxis();
        domain.setAutoRangeIncludesZero(false);
        domain.setNumberFormatOverride(NumberFormat.getInstance());
        xyplot.setDomainAxis(domain);
      

    Axis image