问题
I'm creating a BarChart that contains multiple frequency bands as the category axis. What I want to do is to show a visible grouping of these frequency bands:
For example:
Freq x1 ~ Freq x2 = Band y (so the domain axis has values for category x1, x1.1, x1.2 till x2) Freq x3 ~ Freq x4 = Band z (x3, x3.1 .....x4)
What I want to do is show markers for Band Y and Band Z in the graph. Note that based on the dataset that I may get, not all categories may be present. Say, for the 1st example, I've got values for x1 to x1.6 and so the band marker would be from x1 till x1.6
I hope I could explain my requirement. Is this possible in JFreeChart? If so, how may I go about achieving this?
Just to clarify a little more, here's a picture of something that I want to achieve:

回答1:
Do your Bands corresponded to Categories? If they do you can use a CategoryMarker
CategoryMarker marker = new CategoryMarker("Category 3");
marker.setLabel("Band Y");
marker.setPaint(Color.red);
marker.setOutlinePaint(Color.red);
marker.setAlpha(0.5f);
marker.setLabelAnchor(RectangleAnchor.TOP);
marker.setLabelTextAnchor(TextAnchor.TOP_CENTER);
marker.setLabelOffsetType(LengthAdjustmentType.CONTRACT);
plot.addDomainMarker(marker, Layer.BACKGROUND);

I can't work out how to create a Mutli-CategoryMarker but you can create something similer by
adjusting the ItemMargin
and CategoryMargin
and adding additional CategoryMarkers
{
CategoryMarker marker = new CategoryMarker("Category 2");
marker.setLabel("Band X");
marker.setLabelAnchor(RectangleAnchor.TOP);
marker.setLabelTextAnchor(TextAnchor.TOP_CENTER);
marker.setLabelOffsetType(LengthAdjustmentType.CONTRACT);
plot.addDomainMarker(marker, Layer.BACKGROUND);
}
{
CategoryMarker marker = new CategoryMarker("Category 3");
plot.addDomainMarker(marker, Layer.BACKGROUND);
}
renderer.setItemMargin(0.0);
CategoryAxis axis = plot.getDomainAxis();
axis.setCategoryMargin(0);
}

You could create a method to add multiple markers e.g
private void addMarkers(List<Comparable> keys){
...
A more correct solution may be to write your own implementation of the Renderer and accociated code
来源:https://stackoverflow.com/questions/11363393/jfreechart-barchart-category-markers