问题
Related to: How can I share DomainAxis/RangeAxis across subplots without drawing them on each plot?
Note - I originally wrote this up as a question, and then realize my mistake, so I decided to just share the knowledge since I already had this all typed out.
In JFreeChart, there are two plot types that share an axis range and draw only one axis, while sharing that information with their subplots. These are CombinedDomainXYPlot
and CombinedRangeXYPlot
. I will focus on CombinedDomainXyPlot
, but the other should be identical for the purposes of this question. Looking at the draw code we see:
...
setFixedRangeAxisSpaceForSubplots(null);
AxisSpace space = calculateAxisSpace(g2, area);
Rectangle2D dataArea = space.shrink(area, null);
// set the width and height of non-shared axis of all sub-plots
setFixedRangeAxisSpaceForSubplots(space);
// draw the shared axis
ValueAxis axis = getDomainAxis();
RectangleEdge edge = getDomainAxisEdge();
double cursor = RectangleEdge.coordinate(dataArea, edge);
AxisState axisState = axis.draw(g2, cursor, area, dataArea, edge, info); // <- draw the share axis
if (parentState == null) {
parentState = new PlotState();
}
parentState.getSharedAxisStates().put(axis, axisState); // <- put the state of the shared axis in a shared object
// draw all the subplots
for (int i = 0; i < this.subplots.size(); i++) {
XYPlot plot = (XYPlot) this.subplots.get(i);
PlotRenderingInfo subplotInfo = null;
if (info != null) {
subplotInfo = new PlotRenderingInfo(info.getOwner());
info.addSubplotInfo(subplotInfo);
}
plot.draw(g2, this.subplotAreas[i], anchor, parentState, subplotInfo); // <- pass this shared object to the subplot draw function.
}
...
in the subplot (XYPlot) draw method, we see:
domainAxisState = (AxisState) parentState.getSharedAxisStates().get(getDomainAxis());
The getDomainAxis()
method is calls getDomainAxis(0)
, which is:
public ValueAxis getDomainAxis(int index) {
ValueAxis result = null;
if (index < this.domainAxes.size()) {
result = (ValueAxis) this.domainAxes.get(index); //<- try to find this domain axis in self
}
if (result == null) { //<- if not found in self ...
Plot parent = getParent();
if (parent instanceof XYPlot) {
XYPlot xy = (XYPlot) parent;
result = xy.getDomainAxis(index); //<- ...try to get from parent
}
}
return result;
}
Here, parent
is the CombinedDomainXYPlot
. This will return a reference to the domainAxis from CombinedDomainXYPlot
, to be used for retrieving the axisState
from the parentState
object. It seems the getDomainAxis()
method is not used for getting an axis to draw, only for getting a reference to it for other purposes.
I was looking into this because I was trying to use the same technique to pass multiple shared axes to multiple different groups of plots, but only draw them once. This technique seems to work, but there are glitches right now in that zooming the plots that only get state information does not update the main plot that has the axis... Working on it, but hopefully this information is useful to someone down the line.
Thanks,
Igor
回答1:
Answered in question post.
As state above, the getDomainAxis()
method in XYPlot
is apparently not used for getting an axis to draw, only for getting a reference to it for other purposes. So if you override this method to link to another axis and put it's state information into a shared object, this state info will be shared between plots.
来源:https://stackoverflow.com/questions/32849061/how-combineddomainxyplot-and-combinedrangexyplot-share-axis-information-with-sub