You know that getPreferredSize method exists for ChartPanel.I want a similar method for XYPlot.Because I have a background image, I will scale image for each plot size change in ChartPanel.XYPLot width is not important for me.I want to scale height of background. So I need to have size of XYPlot.
Changes can be Windows resize changes, also you know that XYPlot size is affected from domainAxis items, Legend items.
P.S: I know that I can read plot info in ChartEvent.I want to have coordinates without trigger mouse event.
EDIT:I am creating panel with following code.Another class calls this method, then adds JPanel with ChartPanel to JFrame
public void createPanel() {
XYPlot historyPlot = createHistoryPlot();
/** read forecast result job specific */
/** Creates future XYPlot */
XYPlot futurePlot = createFuturePlot();
/** range axis for CombinedRangeXYPlot */
final ValueAxis rangeAxis = new NumberAxis("");
CombinedRangeXYPlot plot = new CombinedRangeXYPlot(rangeAxis);
/** add subplot to plot */
plot.setGap(0);
plot.add(historyPlot, 1);
plot.add(futurePlot, 1);
/** Creates new plot includes combinedRange plot */
chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
panel = new ChartPanel(chart, true, true, true, false, true);
/** not enable zoom */
panel.setDomainZoomable(false);
panel.setRangeZoomable(false);
panel.validate();
panel.setVisible(true);
this.add(panel, BorderLayout.CENTER);
}
Then I am trying to customize graph with following code.It is invoked outer class after createPanel method is called.So ChartPAnel is created with subplots in it.
/**
* Customizes graph view.Changes view related settings.
* @param chart
* JFreeChart instance
*/
private void customizeGraphView(JFreeChart chart) {
CombinedRangeXYPlot combinedPlot = (CombinedRangeXYPlot) chart.getPlot();
@SuppressWarnings("unchecked")
/** read 2 subplot*/
List<XYPlot> subPlots = combinedPlot.getSubplots();
for (int plotIndex = 0; plotIndex < subPlots.size(); plotIndex++) {
/** get plot */
XYPlot plot = subPlots.get(plotIndex);
/** do not show domain grid lines */
plot.setDomainGridlinesVisible(false);
XYItemRenderer itemRenderer = plot.getRenderer();
/** if line and shape renderer */
if (itemRenderer instanceof StandardXYItemRenderer) {
StandardXYItemRenderer renderer = (StandardXYItemRenderer) itemRenderer;
/** show shapes in time series */
renderer.setBaseShapesVisible(true);
/** fill shapes in time series */
renderer.setBaseShapesFilled(true);
renderer.setBaseFillPaint(Color.BLACK);
// addItemLabels(renderer);
}
/** add severity bar for BackGround image for 2 subplot */
if (plotIndex == 0) {
addBackGroundImage(plot, Align.RIGHT);
} else {
/** returns java.awt.geom.Rectangle2D$Double[x=0.0,y=0.0,w=0.0,h=0.0] */
panel.getScreenDataArea()
addBackGroundImage(plot, Align.LEFT);
}
plot.setOutlineVisible(false);
}
}
It looks like
drawBackgroundImage()
does this for you when you usesetBackgroundImage()
. If not, you can overridedrawBackground()
to alter the existing behavior.Addendum: The
Rectangle2D
parameter sent todrawBackground()
should supply the geometry you want.Addendum: This example that overrides
drawBackground()
shows the plot'sRectangle2D
. Note thatChartPanel
inherits the default layout ofJPanel
, which isFlowLayout
.Console:
Code: