Search code examples
javajfreechart

Change Plot label position in a Combined Plot JFreeChart


Is there a way to change the position of the Category Plot Labels (title) (censored with black bars in the image) to be on top of the chart and / or aligned vertically to each other?

JFreeChart combined plot

Here's the classes I use to construct it.

var renderer = new LineAndShapeRenderer();
...
var yAxis = new NumberAxis();
...
var xAxis = new CategoryAxis(title);
...
CategoryPlot plot = new CategoryPlot(dataset, xAxis, yAxis, renderer);
...
var combinedPlot = new CombinedRangeCategoryPlot();
combinedPlot.add(plot, weight);
...

I'm using version 1.0.19 of JFreeChart since newer versions produce some visual artifacts.


Solution

  • There has been a misunderstanding: I meant the labels, "Plot 0" and "Plot1", in your example; and I'm asking for the ability to move them on top of the chart…

    Invoke setDomainAxisLocation() on each subplot domain axis to move the axis and its label. Alternatively, obtain the required axis references from the List<CategoryPlot> shown below.

    plot0.setDomainAxisLocation(AxisLocation.TOP_OR_LEFT);
    plot1.setDomainAxisLocation(AxisLocation.TOP_OR_LEFT);
    

    enter image description here

    I only need those "Plot 0" and "Plot1" labels on top, not the tick labels…

    You can hide the tick labels as desired using setTickLabelsVisible(false), and you can have multiple axes, as shown here.

    imge

    To adjust the tick label orientation, invoke setCategoryLabelPositions() on each sublot comprising the combined plot. Do this either

    • When the plot is created.

        CategoryAxis axis0 = new CategoryAxis("Plot 0");
        axis0.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
        CategoryPlot plot0 = new CategoryPlot(…, axis0, …);
        …
        CategoryAxis axis1 = new CategoryAxis("Plot 1");
        axis1.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
        CategoryPlot plot1 = new CategoryPlot(…, axis1, …);
      
    • After creating the combined plot.

        CombinedRangeCategoryPlot combinedplot = new CombinedRangeCategoryPlot(…);
        …
        List<CategoryPlot> list = combinedplot.getSubplots();
        list.get(0).getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.UP_90);
        list.get(1).getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.UP_90);
      

    Vertical labels are illustrated below.

    image