Search code examples
jfreechart

How to use GridArrangement and BlockContainer


I would like to create an MxN grid of charts - similar to

for i in M*N:
   ax = fig.add_subplot(M, N, i + 1)

for matplotlib

There appears to be supporting classes - within the org.jfree.chart.block package. However I have been unable to locate documentation, examples, testcases for using that arrangement/layout with a set of charts.

Pointers appreciated.


Solution

  • This part of the API is rather low-level, and mostly used internally by JFreechart. For example, GridArrangement may be used to create a particular legend layout, within a chart.

    In my opinion, the easiest way to create a grid of charts, is to use a Swing JPanel and a GridLayout, and fill that grid with your charts.

    JPanel grid = new JPanel( new GridLayout(m,n) );
    for(int i=0; i<m*n; i++)
       grid.add(new ChartPanel(createChart(i)));
    

    You can also use a CombinedPlot. This allows to add as many plots as you want, either layed out side-by-side, or stacked vertically (but not on a grid, as far as I know). The good thing with this approach is that your plots will directly share a common axis, and will be aligned nicely. (But that depends on your problem: do your charts share one common axis ? Perhaps two ?)