List of usage examples for org.jfree.chart ChartPanel getWidth
@BeanProperty(bound = false) public int getWidth()
From source file:edu.jhuapl.graphs.jfreechart.utils.HighResChartUtil.java
/** * Returns a high resolution BufferedImage of the chart. Uses the default DPI_FILE_RESOLUTION. * * @param resolution The resolution, in dots per inch, of the image to generate. * @return the buffered image.//from ww w. j a v a 2 s . c o m */ public static BufferedImage getHighResChartImage(ChartPanel chartPanel, int resolution) { int screenResolution = Toolkit.getDefaultToolkit().getScreenResolution(); double scaleRatio = resolution / screenResolution; int width = chartPanel.getWidth(); int height = chartPanel.getHeight(); int rasterWidth = (int) (width * scaleRatio); int rasterHeight = (int) (height * scaleRatio); BufferedImage image = new BufferedImage(rasterWidth, rasterHeight, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = image.createGraphics(); g2.transform(AffineTransform.getScaleInstance(scaleRatio, scaleRatio)); chartPanel.getChart().draw(g2, new Rectangle2D.Double(0, 0, width, height), null); g2.dispose(); return image; }
From source file:ec.util.chart.swing.Charts.java
public static void copyChart(@Nonnull ChartPanel chartPanel) { Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); Insets insets = chartPanel.getInsets(); int w = chartPanel.getWidth() - insets.left - insets.right; int h = chartPanel.getHeight() - insets.top - insets.bottom; Transferable selection = new ChartTransferable2(chartPanel.getChart(), w, h, chartPanel.getMinimumDrawWidth(), chartPanel.getMinimumDrawHeight(), chartPanel.getMaximumDrawWidth(), chartPanel.getMaximumDrawHeight(), true); clipboard.setContents(selection, null); }
From source file:GUI.ResponseStatistics.java
/** * Creates a sample dataset */ private void InitPieChart(JFreeChart chart) { ChartPanel chartPanel = new ChartPanel(chart); // default size chartPanel.setSize(560, 800);/*from w w w . j a va 2 s. com*/ Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize(); int x = (int) ((dimension.getWidth() - chartPanel.getWidth()) / 2); int y = (int) ((dimension.getHeight() - chartPanel.getHeight()) / 2); chartPanel.setLocation(WIDTH, WIDTH); // add it to our application setContentPane(chartPanel); chartPanel.addChartMouseListener(new ChartMouseListener() { public void chartMouseClicked(ChartMouseEvent e) { wait = false; } public void chartMouseMoved(ChartMouseEvent e) { } }); }
From source file:net.bioclipse.model.ScatterPlotMouseHandler.java
@Override public void mouseDragged(MouseEvent e) { super.mouseDragged(e); ChartPanel chartPanel = getChartPanel(e); JFreeChart selectedChart = chartPanel.getChart(); ChartDescriptor cd = ChartUtils.getChartDescriptor(selectedChart); int[] indices = cd.getSourceIndices(); XYPlot plot = (XYPlot) chartPanel.getChart().getPlot(); //Create double buffer Image buffer = chartPanel.createImage(chartPanel.getWidth(), chartPanel.getHeight()); Graphics bufferGraphics = buffer.getGraphics(); chartPanel.paint(bufferGraphics);/* ww w .jav a2 s . c om*/ if (lastX == 0 && lastY == 0) { lastX = e.getX(); lastY = e.getY(); } drawRect = new Rectangle(); int x1 = Math.min(Math.min(e.getX(), lastX), startX); int y1 = Math.min(Math.min(e.getY(), lastY), startY); int x2 = Math.max(Math.max(e.getX(), lastX), startX); int y2 = Math.max(Math.max(e.getY(), lastY), startY); drawRect.x = x1; drawRect.y = y1; drawRect.width = x2 - drawRect.x; drawRect.height = y2 - drawRect.y; //Create a clipping rectangle Rectangle clipRect = new Rectangle(drawRect.x - 100, drawRect.y - 100, drawRect.width + 200, drawRect.height + 200); //Check for selected points for (int j = 0; j < plot.getDataset().getItemCount(plot.getDataset().getSeriesCount() - 1); j++) { for (int i = 0; i < plot.getDataset().getSeriesCount(); i++) { Number xK = plot.getDataset().getX(i, j); Number yK = plot.getDataset().getY(i, j); Point2D datasetPoint2D = new Point2D.Double(domainValueTo2D(chartPanel, plot, xK.doubleValue()), rangeValueTo2D(chartPanel, plot, yK.doubleValue())); if (drawRect.contains(datasetPoint2D)) { PlotPointData cp = new PlotPointData(indices[j], cd.getXLabel(), cd.getYLabel()); boolean pointAdded = mouseDragSelection.addPoint(cp); if (pointAdded) { ((ScatterPlotRenderer) plot.getRenderer()).addMarkedPoint(j, i); selectedChart.plotChanged(new PlotChangeEvent(plot)); } } else if (!mouseDragSelection.isEmpty()) { PlotPointData cp = new PlotPointData(indices[j], cd.getXLabel(), cd.getYLabel()); boolean pointRemoved = mouseDragSelection.removePoint(cp); if (pointRemoved) { ((ScatterPlotRenderer) plot.getRenderer()).removeMarkedPoint(new Point(j, i)); selectedChart.plotChanged(new PlotChangeEvent(plot)); } } } } Iterator<PlotPointData> iterator = currentSelection.iterator(); while (iterator.hasNext()) { PlotPointData next = iterator.next(); Point dataPoint = next.getDataPoint(); ((ScatterPlotRenderer) plot.getRenderer()).addMarkedPoint(dataPoint); } lastX = e.getX(); lastY = e.getY(); Graphics graphics = chartPanel.getGraphics(); graphics.setClip(clipRect); //Draw selection rectangle bufferGraphics.drawRect(drawRect.x, drawRect.y, drawRect.width, drawRect.height); graphics.drawImage(buffer, 0, 0, chartPanel.getWidth(), chartPanel.getHeight(), null); }
From source file:ec.util.chart.swing.Charts.java
public static void saveChart(@Nonnull ChartPanel chartPanel) throws IOException { JFileChooser fileChooser = new JFileChooser(); FileFilter defaultFilter = new FileNameExtensionFilter("PNG (.png)", "png"); fileChooser.addChoosableFileFilter(defaultFilter); fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("JPG (.jpg) (.jpeg)", "jpg", "jpeg")); if (Charts.canWriteChartAsSVG()) { fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("SVG (.svg)", "svg")); fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("Compressed SVG (.svgz)", "svgz")); }//from www .j a v a 2 s. c om fileChooser.setFileFilter(defaultFilter); File currentDir = chartPanel.getDefaultDirectoryForSaveAs(); if (currentDir != null) { fileChooser.setCurrentDirectory(currentDir); } if (fileChooser.showSaveDialog(chartPanel) == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); try (OutputStream stream = Files.newOutputStream(file.toPath())) { writeChart(getMediaType(file), stream, chartPanel.getChart(), chartPanel.getWidth(), chartPanel.getHeight()); } chartPanel.setDefaultDirectoryForSaveAs(fileChooser.getCurrentDirectory()); } }
From source file:cish.CISH.java
@Override public String getHTMLReport(String HTMLFolderName) { String output = "<html>"; String lb = "\n"; if (!tss.isEmpty()) { // Save the parameters output += "<table><tr>" + lb + " <td colspan=2><i><u>CISH Parameters</u></i></td>" + lb + " </tr>" + lb + " <tr>" + lb + " <td><b>Point Radius:</b></td>" + lb + " <td>" + getParam_PointSignalRadius() + "</td>" + lb + " </tr>" + lb + " <tr>" + lb + " <td><b>Number of Random Points for Local Ratio:</b></td>" + lb + " <td>" + getParam_nPts() + "</td>" + lb + " </tr>" + lb + " <tr>" + lb + " <td><b>Polarity:</b></td>" + lb + " <td>" + (getParam_darkpoints() ? "Detect Dark Points" : "Detect Light Points") + "</td>" + lb + " </tr>" + lb; output += "</table><br>" + lb; // Save the CISH Table output += "<table>" + lb + " <tr>" + lb; for (int i = 0; i < jXTable1.getColumnCount(); i++) { output += " <th>" + jXTable1.getColumnName(i) + "</th>" + lb; }/*from ww w . j ava 2 s . c om*/ output += " </tr>" + lb; for (int i = 0; i < jXTable1.getRowCount(); i++) { output += " <tr>" + lb; for (int j = 0; j < jXTable1.getColumnCount(); j++) { output += " <td>" + jXTable1.getStringAt(i, j) + "</td>" + lb; } output += " </tr>" + lb; } output += "</table>" + lb; // Save the CISH plot image ChartPanel chartpanel = (ChartPanel) jPanel1.getComponent(0); if (chartpanel != null) { File file_tmp = new File(HTMLFolderName + "CISHPlot.png"); try { ChartUtilities.saveChartAsPNG(file_tmp, chartpanel.getChart(), chartpanel.getWidth(), chartpanel.getHeight()); } catch (IOException ex) { Logger.getLogger(CISH.class.getName()).log(Level.SEVERE, null, ex); } output += "<br><br>" + lb; output += "<img src=\"" + HTMLFolderName + "CISHPlot.png\" alt=\"Global vs. Local Ratio Plot\"><br><br>" + lb; } // Save the TMA spot images with CISH marks for (TMAspot ts : tss) { try { File file_tmp = new File(HTMLFolderName + ts.getName() + "_CISH.jpg"); BufferedImage bi = ts.getBufferedImage(); drawInformationPreNuclei(ts, bi.createGraphics(), 1, 0, 0, bi.getWidth(), bi.getHeight()); ImageIO.write(bi, "jpg", file_tmp); output += "<a href=\"" + HTMLFolderName + ts.getName() + "_CISH.jpg\">" + "<img src=\"" + HTMLFolderName + ts.getName() + "_CISH.jpg\" alt=\"" + ts.getName() + " CISH Points\" width=\"100\">" + "</a> " + lb; } catch (IOException ex) { Logger.getLogger(CISH.class.getName()).log(Level.SEVERE, null, ex); } } output += "<br>" + "<font color=\"" + Misc.Color2HTML(COLORS[0]) + "\"><b>centromer</b></font> " + "<font color=\"" + Misc.Color2HTML(COLORS[1]) + "\"><b>gene</b></font> " + "<font color=\"" + Misc.Color2HTML(COLORS[4]) + "\"><b>both</b></font> " + "<br><br>" + lb; } output += "</html>"; return output; }
From source file:org.jfree.chart.demo.SuperDemo.java
private void exportToPDF() { java.awt.Component component = chartContainer.getComponent(0); if (component instanceof ChartPanel) { JFileChooser jfilechooser = new JFileChooser(); jfilechooser.setName("untitled.pdf"); jfilechooser.setFileFilter(new FileFilter() { public boolean accept(File file) { return file.isDirectory() || file.getName().endsWith(".pdf"); }//from ww w . jav a 2s .c o m public String getDescription() { return "Portable Document Format (PDF)"; } }); int i = jfilechooser.showSaveDialog(this); if (i == 0) { ChartPanel chartpanel = (ChartPanel) component; try { JFreeChart jfreechart = (JFreeChart) chartpanel.getChart().clone(); PDFExportTask pdfexporttask = new PDFExportTask(jfreechart, chartpanel.getWidth(), chartpanel.getHeight(), jfilechooser.getSelectedFile()); Thread thread = new Thread(pdfexporttask); thread.start(); } catch (CloneNotSupportedException clonenotsupportedexception) { clonenotsupportedexception.printStackTrace(); } } } else { String s = "Unable to export the selected item. There is "; s = s + "either no chart selected,\nor else the chart is not "; s = s + "at the expected location in the component hierarchy\n"; s = s + "(future versions of the demo may include code to "; s = s + "handle these special cases)."; JOptionPane.showMessageDialog(this, s, "PDF Export", 1); } }
From source file:de.fhbingen.wbs.wpOverview.tabs.APCalendarPanel.java
/** * Initialize the work package calendar panel inclusive the listeners. *//*from w w w .j a va2s . co m*/ private void init() { List<Workpackage> userWp = new ArrayList<Workpackage>(WpManager.getUserWp(WPOverview.getUser())); Collections.sort(userWp, new APLevelComparator()); dataset = createDataset(userWp); chart = createChart(dataset); final ChartPanel chartPanel = new ChartPanel(chart); final JPopupMenu popup = new JPopupMenu(); JMenuItem miSave = new JMenuItem(LocalizedStrings.getButton().save(LocalizedStrings.getWbs().timeLine())); miSave.addActionListener(new ActionListener() { @Override public void actionPerformed(final ActionEvent arg0) { JFileChooser chooser = new JFileChooser(); chooser.setFileFilter(new ExtensionAndFolderFilter("jpg", "jpeg")); //NON-NLS chooser.setSelectedFile(new File("chart-" //NON-NLS + System.currentTimeMillis() + ".jpg")); int returnVal = chooser.showSaveDialog(reference); if (returnVal == JFileChooser.APPROVE_OPTION) { try { File outfile = chooser.getSelectedFile(); ChartUtilities.saveChartAsJPEG(outfile, chart, chartPanel.getWidth(), chartPanel.getWidth()); Controller.showMessage( LocalizedStrings.getMessages().timeLineSaved(outfile.getCanonicalPath())); } catch (IOException e) { Controller.showError(LocalizedStrings.getErrorMessages().timeLineExportError()); } } } }); popup.add(miSave); chartPanel.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(final MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON3) { popup.show(e.getComponent(), e.getX(), e.getY()); } } }); chartPanel.setMinimumDrawHeight(50 + 15 * userWp.size()); chartPanel.setMaximumDrawHeight(50 + 15 * userWp.size()); chartPanel.setMaximumDrawWidth(9999); chartPanel.setPreferredSize( new Dimension((int) chartPanel.getPreferredSize().getWidth(), 50 + 15 * userWp.size())); chartPanel.setPopupMenu(null); this.setLayout(new BorderLayout()); this.removeAll(); JPanel panel = new JPanel(); panel.setLayout(new GridBagLayout()); GridBagConstraints constraints = new GridBagConstraints(); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.weightx = 1; constraints.weighty = 1; constraints.anchor = GridBagConstraints.NORTHWEST; panel.add(chartPanel, constraints); panel.setBackground(Color.white); this.add(panel, BorderLayout.CENTER); GanttRenderer.setDefaultShadowsVisible(false); GanttRenderer.setDefaultBarPainter(new BarPainter() { @Override public void paintBar(final Graphics2D g, final BarRenderer arg1, final int row, final int col, final RectangularShape rect, final RectangleEdge arg5) { String wpName = (String) dataset.getColumnKey(col); int i = 0; int spaceCount = 0; while (wpName.charAt(i++) == ' ' && spaceCount < 17) { spaceCount++; } g.setColor(new Color(spaceCount * 15, spaceCount * 15, spaceCount * 15)); g.fill(rect); g.setColor(Color.black); g.setStroke(new BasicStroke()); g.draw(rect); } @Override public void paintBarShadow(final Graphics2D arg0, final BarRenderer arg1, final int arg2, final int arg3, final RectangularShape arg4, final RectangleEdge arg5, final boolean arg6) { } }); ((CategoryPlot) chart.getPlot()).setRenderer(new GanttRenderer() { private static final long serialVersionUID = -6078915091070733812L; public void drawItem(final Graphics2D g2, final CategoryItemRendererState state, final Rectangle2D dataArea, final CategoryPlot plot, final CategoryAxis domainAxis, final ValueAxis rangeAxis, final CategoryDataset dataset, final int row, final int column, final int pass) { super.drawItem(g2, state, dataArea, plot, domainAxis, rangeAxis, dataset, row, column, pass); } }); }
From source file:jamel.gui.JamelWindow.java
/** * Exports a rapport./*from w ww .j a va2 s . c om*/ */ public void exportRapport() { final int max = tabbedPane.getTabCount(); final ProgressMonitor progressMonitor = new ProgressMonitor(this, "Exporting", "", 0, max); progressMonitor.setMillisToDecideToPopup(0); final String rc = System.getProperty("line.separator"); final File outputDirectory = new File("exports/" + this.getTitle() + "-" + (new Date()).getTime()); outputDirectory.mkdir(); try { final FileWriter writer = new FileWriter(new File(outputDirectory, "Rapport.html")); writer.write("<HTML>" + rc); writer.write("<HEAD>"); writer.write("<TITLE>" + this.getTitle() + "</TITLE>" + rc); writer.write("</HEAD>" + rc); writer.write("<BODY>" + rc); writer.write("<H1>" + this.getTitle() + "</H1>" + rc); writer.write("<HR>" + rc); final Date start = viewManager.getStart().getDate(); final Date end = viewManager.getEnd().getDate(); for (int tabIndex = 0; tabIndex < max; tabIndex++) { try { final JPanel currentTab = (JPanel) tabbedPane.getComponentAt(tabIndex); final String tabTitle = tabbedPane.getTitleAt(tabIndex); writer.write("<H2>" + tabTitle + "</H2>" + rc); writer.write("<TABLE>" + rc); final int chartPanelCount = currentTab.getComponentCount(); for (int chartIndex = 0; chartIndex < chartPanelCount; chartIndex++) { if ((chartIndex == 3) | (chartIndex == 6)) writer.write("<TR>" + rc); final ChartPanel aChartPanel = (ChartPanel) currentTab.getComponent(chartIndex); final JamelChart chart = (JamelChart) aChartPanel.getChart(); if (chart != null) { final String chartTitle = chart.getTitle().getText(); if (!chartTitle.equals("Empty")) { try { chart.setTitle(""); chart.setTimeRange(start, end); String imageName = (tabTitle + "-" + chartIndex + "-" + chartTitle + ".png") .replace(" ", "_").replace("&", "and"); ChartUtilities.saveChartAsPNG(new File(outputDirectory, imageName), chart, aChartPanel.getWidth(), aChartPanel.getHeight()); writer.write("<TD><IMG src=\"" + imageName + "\" title=\"" + chartTitle + "\">" + rc); chart.setTitle(chartTitle); } catch (IOException e) { e.printStackTrace(); } } } } writer.write("</TABLE>" + rc); writer.write("<HR>" + rc); } catch (ClassCastException e) { } progressMonitor.setProgress(tabIndex); } writer.write("<H2>Scenario</H2>" + rc); writer.write(this.consoleText.toString()); writer.write("</BODY>" + rc); writer.write("</HTML>" + rc); writer.close(); } catch (IOException e1) { e1.printStackTrace(); } progressMonitor.close(); }
From source file:com.appnativa.rare.ui.chart.jfreechart.ChartHandler.java
@Override public UIImage getChartImage(iPlatformComponent chartPanel, ChartDefinition cd) { if (chartPanel == null) { return null; }// w w w . j a v a 2s .co m ChartPanel cp = (ChartPanel) chartPanel.getView(); return new UIImage(cp.getChart().createBufferedImage(cp.getWidth(), cp.getHeight())); }