Reputation: 475
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);
}
}
Upvotes: 0
Views: 1225
Reputation: 205785
It looks like drawBackgroundImage()
does this for you when you use setBackgroundImage()
. If not, you can override drawBackground()
to alter the existing behavior.
Addendum: The Rectangle2D
parameter sent to drawBackground()
should supply the geometry you want.
Addendum: This example that overrides drawBackground()
shows the plot's Rectangle2D
. Note that ChartPanel
inherits the default layout of JPanel
, which is FlowLayout
.
Console:
java.awt.Rectangle[x=8,y=28,width=664,height=388]
Code:
public Test() {
Image image = null;
try {
image = ImageIO.read(new File("image.jpg"));
} catch (IOException ex) {
ex.printStackTrace(System.err);
}
XYPlot plot = new XYPlot() {
@Override
public void drawBackground(Graphics2D g2, Rectangle2D area) {
super.drawBackground(g2, area);
System.out.println(area);
}
};
plot.setBackgroundImage(image);
JFreeChart chart = new JFreeChart("Test", plot);
ChartPanel panel = new ChartPanel(chart);
this.add(panel);
}
Upvotes: 1