List of usage examples for org.jfree.chart ChartPanel getBounds
public Rectangle getBounds()
From source file:net.sf.mzmine.chartbasics.ChartLogics.java
/** * /*from w ww. jav a2 s. c o m*/ * Domain and Range axes need to share the same unit (e.g. mm) * * @param myChart * @return */ public static double calcWidthToHeight(ChartPanel myChart, double chartHeight) { makeChartResizable(myChart); // paint on a ghost panel JPanel parent = (JPanel) myChart.getParent(); JPanel p = new JPanel(); p.removeAll(); p.add(myChart, BorderLayout.CENTER); p.setBounds(myChart.getBounds()); myChart.paintImmediately(myChart.getBounds()); p.removeAll(); parent.add(myChart); XYPlot plot = (XYPlot) myChart.getChart().getPlot(); ChartRenderingInfo info = myChart.getChartRenderingInfo(); Rectangle2D dataArea = info.getPlotInfo().getDataArea(); Rectangle2D chartArea = info.getChartArea(); // calc title space: will be added later to the right plot size double titleWidth = chartArea.getWidth() - dataArea.getWidth(); double titleHeight = chartArea.getHeight() - dataArea.getHeight(); // calc right plot size with axis dim. // real plot width is given by factor; double realPH = chartHeight - titleHeight; // ranges ValueAxis domainAxis = plot.getDomainAxis(); org.jfree.data.Range x = domainAxis.getRange(); ValueAxis rangeAxis = plot.getRangeAxis(); org.jfree.data.Range y = rangeAxis.getRange(); // real plot height can be calculated by double realPW = realPH / y.getLength() * x.getLength(); double width = realPW + titleWidth; return width; }
From source file:net.sf.mzmine.chartbasics.ChartLogics.java
/** * Returns dimensions for limiting factor width or height * //from www . j a v a 2 s . co m * @param myChart * @return */ public static Dimension calcMaxSize(ChartPanel myChart, double chartWidth, double chartHeight) { makeChartResizable(myChart); // paint on a ghost panel JPanel parent = (JPanel) myChart.getParent(); JPanel p = new JPanel(); p.removeAll(); p.add(myChart, BorderLayout.CENTER); p.setBounds(myChart.getBounds()); myChart.paintImmediately(myChart.getBounds()); p.removeAll(); parent.add(myChart); XYPlot plot = (XYPlot) myChart.getChart().getPlot(); ChartRenderingInfo info = myChart.getChartRenderingInfo(); Rectangle2D dataArea = info.getPlotInfo().getDataArea(); Rectangle2D chartArea = info.getChartArea(); // calc title space: will be added later to the right plot size double titleWidth = chartArea.getWidth() - dataArea.getWidth(); double titleHeight = chartArea.getHeight() - dataArea.getHeight(); // calculatig width for max height // calc right plot size with axis dim. // real plot width is given by factor; double realPH = chartHeight - titleHeight; // ranges ValueAxis domainAxis = plot.getDomainAxis(); org.jfree.data.Range x = domainAxis.getRange(); ValueAxis rangeAxis = plot.getRangeAxis(); org.jfree.data.Range y = rangeAxis.getRange(); // real plot height can be calculated by double realPW = realPH / y.getLength() * x.getLength(); double width = realPW + titleWidth; // if width is higher than given chartWidth then calc height for chartWidth if (width > chartWidth) { // calc right plot size with axis dim. // real plot width is given by factor; realPW = chartWidth - titleWidth; // real plot height can be calculated by realPH = realPW / x.getLength() * y.getLength(); double height = realPH + titleHeight; // Return size return new Dimension((int) chartWidth, (int) height); } else { // Return size return new Dimension((int) width, (int) chartHeight); } }
From source file:net.sf.mzmine.chartbasics.ChartLogics.java
/** * Calculates the size of a chart for a given fixed plot width and height * //from ww w. j ava 2 s.com * @param chart * @param plotWidth * @return */ public static Dimension calcSizeForPlotSize(ChartPanel myChart, double plotWidth, double plotHeight, int iterations) { makeChartResizable(myChart); // estimate plotwidth / height double estimatedChartWidth = plotWidth + 200; double estimatedChartHeight = plotHeight + 200; double lastW = estimatedChartWidth; double lastH = estimatedChartHeight; // paint and get closer try { for (int i = 0; i < iterations; i++) { // paint on ghost panel with estimated height (if copy panel==true) myChart.setSize((int) estimatedChartWidth, (int) estimatedChartHeight); myChart.paintImmediately(myChart.getBounds()); // rendering info ChartRenderingInfo info = myChart.getChartRenderingInfo(); Rectangle2D dataArea = info.getPlotInfo().getDataArea(); Rectangle2D chartArea = info.getChartArea(); // // calc title space: will be added later to the right plot size // double titleWidth = chartArea.getWidth()-dataArea.getWidth(); // double titleHeight = chartArea.getHeight()-dataArea.getHeight(); // calc width and height estimatedChartWidth = estimatedChartWidth - dataArea.getWidth() + plotWidth; estimatedChartHeight = estimatedChartHeight - dataArea.getHeight() + plotHeight; if ((int) lastW == (int) estimatedChartWidth && (int) lastH == (int) estimatedChartHeight) break; else { lastW = estimatedChartWidth; lastH = estimatedChartHeight; } } } catch (Exception ex) { ex.printStackTrace(); } return new Dimension((int) estimatedChartWidth, (int) estimatedChartHeight); }
From source file:net.sf.mzmine.chartbasics.ChartLogics.java
/** * calculates the correct height with multiple iterations Domain and Range axes need to share the * same unit (e.g. mm)/*from w ww . j ava 2 s. c o m*/ * * @param myChart * @param copyToNewPanel * @param dataWidth width of data * @param axis for width calculation * @return */ public static double calcHeightToWidth(ChartPanel myChart, double chartWidth, double estimatedHeight, int iterations, boolean copyToNewPanel) { // if(myChart.getChartRenderingInfo()==null || // myChart.getChartRenderingInfo().getChartArea()==null || // myChart.getChartRenderingInfo().getChartArea().getWidth()==0) // result double height = estimatedHeight; double lastH = height; makeChartResizable(myChart); // paint on a ghost panel JPanel parent = (JPanel) myChart.getParent(); JPanel p = copyToNewPanel ? new JPanel() : parent; if (copyToNewPanel) p.add(myChart, BorderLayout.CENTER); try { for (int i = 0; i < iterations; i++) { // paint on ghost panel with estimated height (if copy panel==true) myChart.setSize((int) chartWidth, (int) estimatedHeight); myChart.paintImmediately(myChart.getBounds()); XYPlot plot = (XYPlot) myChart.getChart().getPlot(); ChartRenderingInfo info = myChart.getChartRenderingInfo(); Rectangle2D dataArea = info.getPlotInfo().getDataArea(); Rectangle2D chartArea = info.getChartArea(); // calc title space: will be added later to the right plot size double titleWidth = chartArea.getWidth() - dataArea.getWidth(); double titleHeight = chartArea.getHeight() - dataArea.getHeight(); // calc right plot size with axis dim. // real plot width is given by factor; double realPW = chartWidth - titleWidth; // ranges ValueAxis domainAxis = plot.getDomainAxis(); org.jfree.data.Range x = domainAxis.getRange(); ValueAxis rangeAxis = plot.getRangeAxis(); org.jfree.data.Range y = rangeAxis.getRange(); // real plot height can be calculated by double realPH = realPW / x.getLength() * y.getLength(); // the real height height = realPH + titleHeight; // for next iteration estimatedHeight = height; if ((int) lastH == (int) height) break; else lastH = height; } } catch (Exception ex) { ex.printStackTrace(); } if (copyToNewPanel) { // reset to frame p.removeAll(); parent.add(myChart); } return height; }
From source file:trendgraph.XYLineChart_AWT.java
public XYLineChart_AWT(int yearStart, int yearEnd, String[] creditUnionName, String columnName) throws SQLException { super("Graph"); super.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.yearStart = yearStart; this.yearEnd = yearEnd; this.creditUnionName = creditUnionName; this.columnName = columnName; saveGraphButton = new JButton("Save Graph"); saveGraphButton.setBorderPainted(false); saveGraphButton.setFocusPainted(false); JFreeChart xylineChart = ChartFactory.createXYLineChart("CU Report", "Year (YYYY)", //X-axis columnName, //Y-axis (replace with columnName createDataset(), PlotOrientation.VERTICAL, true, true, false); ChartPanel chartPanel = new ChartPanel(xylineChart); chartPanel.setPreferredSize(new java.awt.Dimension(1000, 800)); //(x, y) final XYPlot plot = xylineChart.getXYPlot(); XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(); renderer.setSeriesPaint(0, Color.RED); //can be GREEN, YELLOW, ETC. renderer.setSeriesStroke(0, new BasicStroke(3.0f)); //Font size renderer.setSeriesPaint(1, Color.BLUE); //can be GREEN, YELLOW, ETC. renderer.setSeriesStroke(1, new BasicStroke(3.0f)); //Font size renderer.setSeriesPaint(2, Color.GREEN); //can be GREEN, YELLOW, ETC. renderer.setSeriesStroke(2, new BasicStroke(3.0f)); //Font size renderer.setSeriesPaint(3, Color.yellow); //can be GREEN, YELLOW, ETC. renderer.setSeriesStroke(3, new BasicStroke(3.0f)); //Font size plot.setRenderer(renderer);// w w w .j av a 2 s . co m chartPanel.setLayout(new FlowLayout(FlowLayout.TRAILING)); chartPanel.add(saveGraphButton); setContentPane(chartPanel); pack(); RefineryUtilities.centerFrameOnScreen(this); setVisible(true); saveGraphButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Rectangle rect = chartPanel.getBounds(); FileChooser chooser = new FileChooser(); //get chosen path and save the variable String path = chooser.getPath(); path = path.replace("\\", "/"); String format = "png"; String fileName = path + "." + format; BufferedImage captureImage = new BufferedImage(rect.width, rect.height, BufferedImage.TYPE_INT_ARGB); chartPanel.paint(captureImage.getGraphics()); File file = new File(fileName); try { ImageIO.write(captureImage, format, file); //write data to file } catch (IOException ex) { Logger.getLogger(XYLineChart_AWT.class.getName()).log(Level.SEVERE, null, ex); } } }); }