List of usage examples for org.jfree.chart JFreeChart draw
public void draw(Graphics2D g2, Rectangle2D chartArea, Point2D anchor, ChartRenderingInfo info)
From source file:org.gbif.portal.web.util.ChartUtils.java
/** * Writes out the image using the supplied file name. * //from w w w .j a va2 s . co m * @param legend * @param fileName * @return */ public static String writePieChartImageToTempFile(Map<String, Double> legend, String fileName) { String filePath = System.getProperty(tmpDirSystemProperty) + File.separator + fileName + defaultExtension; File fileToCheck = new File(filePath); if (fileToCheck.exists()) { return fileName + defaultExtension; } final DefaultPieDataset data = new DefaultPieDataset(); Set<String> keys = legend.keySet(); for (String key : keys) { logger.info("Adding key : " + key); data.setValue(key, legend.get(key)); } // create a pie chart... final boolean withLegend = true; final JFreeChart chart = ChartFactory.createPieChart(null, data, withLegend, false, false); PiePlot piePlot = (PiePlot) chart.getPlot(); piePlot.setLabelFont(new Font("Arial", Font.PLAIN, 10)); piePlot.setLabelBackgroundPaint(Color.WHITE); LegendTitle lt = chart.getLegend(); lt.setBackgroundPaint(Color.WHITE); lt.setWidth(300); lt.setBorder(0, 0, 0, 0); lt.setItemFont(new Font("Arial", Font.PLAIN, 11)); chart.setPadding(new RectangleInsets(0, 0, 0, 0)); chart.setBorderVisible(false); chart.setBackgroundImageAlpha(0); chart.setBackgroundPaint(Color.WHITE); chart.setBorderPaint(Color.LIGHT_GRAY); final BufferedImage image = new BufferedImage(300, 250, BufferedImage.TYPE_INT_RGB); KeypointPNGEncoderAdapter adapter = new KeypointPNGEncoderAdapter(); adapter.setQuality(1); try { adapter.encode(image); } catch (IOException e) { logger.error(e.getMessage(), e); } final Graphics2D g2 = image.createGraphics(); g2.setFont(new Font("Arial", Font.PLAIN, 11)); final Rectangle2D chartArea = new Rectangle2D.Double(0, 0, 300, 250); // draw chart.draw(g2, chartArea, null, null); try { FileOutputStream fOut = new FileOutputStream(fileToCheck); ChartUtilities.writeChartAsPNG(fOut, chart, 300, 250); return fileToCheck.getName(); } catch (IOException e) { logger.error(e.getMessage(), e); return null; } }
From source file:org.jfree.chart.demo.ChartTiming1.java
/** * Runs the timing./* w w w. j a v a 2s . c o m*/ */ public void run() { this.finished = false; // create a dataset... final DefaultPieDataset data = new DefaultPieDataset(); data.setValue("One", new Double(10.3)); data.setValue("Two", new Double(8.5)); data.setValue("Three", new Double(3.9)); data.setValue("Four", new Double(3.9)); data.setValue("Five", new Double(3.9)); data.setValue("Six", new Double(3.9)); // create a pie chart... final boolean withLegend = true; final JFreeChart chart = ChartFactory.createPieChart("Testing", data, withLegend, true, false); final BufferedImage image = new BufferedImage(400, 300, BufferedImage.TYPE_INT_RGB); final Graphics2D g2 = image.createGraphics(); final Rectangle2D chartArea = new Rectangle2D.Double(0, 0, 400, 300); // set up the timer... final Timer timer = new Timer(10000, this); timer.setRepeats(false); int count = 0; timer.start(); while (!this.finished) { chart.draw(g2, chartArea, null, null); System.out.println("Charts drawn..." + count); if (!this.finished) { count++; } } System.out.println("DONE"); }
From source file:org.jfree.chart.demo.ChartTiming4.java
/** * Runs the test./* w w w .j a v a2 s . c o m*/ */ public void run() { this.finished = false; // create a dataset... populateData(); // create a fast scatter chart... final Plot plot = new FastScatterPlot(this.data, new NumberAxis("X"), new NumberAxis("Y")); final JFreeChart chart = new JFreeChart("Fast Scatter Plot Timing", JFreeChart.DEFAULT_TITLE_FONT, plot, true); final BufferedImage image = new BufferedImage(400, 300, BufferedImage.TYPE_INT_RGB); final Graphics2D g2 = image.createGraphics(); final Rectangle2D chartArea = new Rectangle2D.Double(0, 0, 400, 300); // set up the timer... final Timer timer = new Timer(10000, this); timer.setRepeats(false); int count = 0; timer.start(); while (!this.finished) { chart.draw(g2, chartArea, null, null); System.out.println("Charts drawn..." + count); if (!this.finished) { count++; } } System.out.println("DONE"); }
From source file:org.jfree.chart.demo.ChartTiming2.java
/** * Runs the test./*from w w w. j a v a 2 s .c om*/ */ public void run() { this.finished = false; // create a dataset... final XYDataset data = new SampleXYDataset2(1, 1440); // create a scatter chart... final boolean withLegend = true; final JFreeChart chart = ChartFactory.createScatterPlot("Scatter plot timing", "X", "Y", data, PlotOrientation.VERTICAL, withLegend, false, false); final XYPlot plot = chart.getXYPlot(); plot.setRenderer(new XYDotRenderer()); final BufferedImage image = new BufferedImage(400, 300, BufferedImage.TYPE_INT_RGB); final Graphics2D g2 = image.createGraphics(); final Rectangle2D chartArea = new Rectangle2D.Double(0, 0, 400, 300); // set up the timer... final Timer timer = new Timer(10000, this); timer.setRepeats(false); int count = 0; timer.start(); while (!this.finished) { chart.draw(g2, chartArea, null, null); System.out.println("Charts drawn..." + count); if (!this.finished) { count++; } } System.out.println("DONE"); }
From source file:org.jfree.chart.demo.ChartTiming3.java
/** * Runs the test./*from w ww . jav a 2s. co m*/ */ public void run() { this.finished = false; // create a dataset... final XYSeries series = new XYSeries("Random Data"); for (int i = 0; i < 1440; i++) { final double x = Math.random(); final double y = Math.random(); series.add(x, y); } final XYDataset data = new XYSeriesCollection(series); // create a scatter chart... final boolean withLegend = true; final JFreeChart chart = ChartFactory.createScatterPlot("Scatter plot timing", "X", "Y", data, PlotOrientation.VERTICAL, withLegend, false, false); final XYPlot plot = chart.getXYPlot(); plot.setRenderer(new XYDotRenderer()); final BufferedImage image = new BufferedImage(400, 300, BufferedImage.TYPE_INT_RGB); final Graphics2D g2 = image.createGraphics(); final Rectangle2D chartArea = new Rectangle2D.Double(0, 0, 400, 300); // set up the timer... final Timer timer = new Timer(10000, this); timer.setRepeats(false); int count = 0; timer.start(); while (!this.finished) { chart.draw(g2, chartArea, null, null); System.out.println("Charts drawn..." + count); if (!this.finished) { count++; } } System.out.println("DONE"); }
From source file:ubic.gemma.web.controller.expression.experiment.ExpressionExperimentQCController.java
private void addChartToGraphics(JFreeChart chart, Graphics2D g2, double x, double y, double width, double height) { chart.draw(g2, new Rectangle2D.Double(x, y, width, height), null, null); }
From source file:org.gbif.portal.web.controller.dataset.IndexingHistoryController.java
/** * Create a time series graphic to display indexing processes. * //from w ww. j a v a 2 s .c o m * @param dataProvider * @param dataResource * @param activities * @param fileNamePrefix * @return */ public String timeSeriesTest(DataProviderDTO dataProvider, DataResourceDTO dataResource, List<LoggedActivityDTO> loggedActivities, String fileNamePrefix, int minProcessingTimeToRender) { List<LoggedActivityDTO> activities = new ArrayList<LoggedActivityDTO>(); for (LoggedActivityDTO la : loggedActivities) { if (la.getDataResourceKey() != null && la.getDataResourceName() != null && la.getEventName() != null) activities.add(la); } //if no activities to render, return if (activities.isEmpty()) return null; Map<String, Integer> drActualCount = new HashMap<String, Integer>(); Map<String, Integer> drCount = new HashMap<String, Integer>(); //record the actual counts for (LoggedActivityDTO laDTO : activities) { if (laDTO.getStartDate() != null && laDTO.getEndDate() != null && laDTO.getDurationInMillisecs() > minProcessingTimeToRender) { if (drActualCount.get(laDTO.getDataResourceName()) == null) { drActualCount.put(laDTO.getDataResourceName(), new Integer(4)); drCount.put(laDTO.getDataResourceName(), new Integer(0)); } else { Integer theCount = drActualCount.get(laDTO.getDataResourceName()); theCount = new Integer(theCount.intValue() + 4); drActualCount.remove(laDTO.getDataResourceName()); drActualCount.put(laDTO.getDataResourceName(), theCount); } } } StringBuffer fileNameBuffer = new StringBuffer(fileNamePrefix); if (dataResource != null) { fileNameBuffer.append("-resource-"); fileNameBuffer.append(dataResource.getKey()); } else if (dataProvider != null) { fileNameBuffer.append("-provider-"); fileNameBuffer.append(dataProvider.getKey()); } fileNameBuffer.append(".png"); String fileName = fileNameBuffer.toString(); String filePath = System.getProperty("java.io.tmpdir") + File.separator + fileName; File fileToCheck = new File(filePath); if (fileToCheck.exists()) { return fileName; } TimeSeriesCollection dataset = new TimeSeriesCollection(); boolean generateChart = false; int count = 1; int dataResourceCount = 1; Collections.sort(activities, new Comparator<LoggedActivityDTO>() { public int compare(LoggedActivityDTO o1, LoggedActivityDTO o2) { if (o1 == null || o2 == null || o1.getDataResourceKey() != null || o2.getDataResourceKey() != null) return -1; return o1.getDataResourceKey().compareTo(o2.getDataResourceKey()); } }); String currentDataResourceKey = activities.get(0).getDataResourceKey(); for (LoggedActivityDTO laDTO : activities) { if (laDTO.getStartDate() != null && laDTO.getEndDate() != null && laDTO.getDurationInMillisecs() > minProcessingTimeToRender) { if (currentDataResourceKey != null && !currentDataResourceKey.equals(laDTO.getDataResourceKey())) { dataResourceCount++; count = count + 1; currentDataResourceKey = laDTO.getDataResourceKey(); } TimeSeries s1 = new TimeSeries(laDTO.getDataResourceName(), "Process time period", laDTO.getEventName(), Hour.class); s1.add(new Hour(laDTO.getStartDate()), count); s1.add(new Hour(laDTO.getEndDate()), count); dataset.addSeries(s1); generateChart = true; } } if (!generateChart) return null; // create a pie chart... final JFreeChart chart = ChartFactory.createTimeSeriesChart(null, null, null, dataset, false, false, false); XYPlot plot = chart.getXYPlot(); plot.setWeight(10); plot.getRangeAxis().setAutoRange(false); plot.getRangeAxis().setRange(0, drCount.size() + 1); plot.getRangeAxis().setAxisLineVisible(false); plot.getRangeAxis().setAxisLinePaint(Color.WHITE); plot.setDomainCrosshairValue(1); plot.setRangeGridlinesVisible(false); plot.getRangeAxis().setVisible(false); plot.getRangeAxis().setLabel("datasets"); XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer(); renderer.setItemLabelsVisible(true); MyXYItemLabelGenerator labelGenerator = new MyXYItemLabelGenerator(); labelGenerator.setDataResourceActualCount(drActualCount); labelGenerator.setDataResourceCount(drCount); renderer.setItemLabelGenerator(labelGenerator); List<TimeSeries> seriesList = dataset.getSeries(); for (TimeSeries series : seriesList) { if (((String) series.getRangeDescription()).startsWith("extraction")) { renderer.setSeriesPaint(seriesList.indexOf(series), Color.RED); } else { renderer.setSeriesPaint(seriesList.indexOf(series), Color.BLUE); } renderer.setSeriesStroke(seriesList.indexOf(series), new BasicStroke(7f)); } int imageHeight = 30 * dataResourceCount; if (imageHeight < 100) { imageHeight = 100; } else { imageHeight = imageHeight + 100; } final BufferedImage image = new BufferedImage(900, imageHeight, BufferedImage.TYPE_INT_RGB); KeypointPNGEncoderAdapter adapter = new KeypointPNGEncoderAdapter(); adapter.setQuality(1); try { adapter.encode(image); } catch (IOException e) { logger.error(e.getMessage(), e); } final Graphics2D g2 = image.createGraphics(); g2.setFont(new Font("Arial", Font.PLAIN, 11)); final Rectangle2D chartArea = new Rectangle2D.Double(0, 0, 900, imageHeight); // draw chart.draw(g2, chartArea, null, null); //styling chart.setPadding(new RectangleInsets(0, 0, 0, 0)); chart.setBorderVisible(false); chart.setBackgroundImageAlpha(0); chart.setBackgroundPaint(Color.WHITE); chart.setBorderPaint(Color.LIGHT_GRAY); try { FileOutputStream fOut = new FileOutputStream(filePath); ChartUtilities.writeChartAsPNG(fOut, chart, 900, imageHeight); return fileName; } catch (IOException e) { logger.error(e.getMessage(), e); } return null; }