|
From: <cob...@us...> - 2003-10-14 22:11:39
|
Update of /cvsroot/jrobin/src/jrobin/graph In directory sc8-pr-cvs1:/tmp/cvs-serv10598/src/jrobin/graph Modified Files: RrdGraph.java ChartGraphics.java Grapher.java Log Message: GRAPH LIB UPDATES Repeatedly saving fixed (only with the same graph dimensions) Fixed graph generating for > 1000 pixel wide graphs Fixed graph generating for extremely small time ranges (smaller than or equal to step size) Index: RrdGraph.java =================================================================== RCS file: /cvsroot/jrobin/src/jrobin/graph/RrdGraph.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** RrdGraph.java 11 Oct 2003 20:32:35 -0000 1.4 --- RrdGraph.java 14 Oct 2003 22:11:34 -0000 1.5 *************** *** 1,230 **** ! /* ============================================================ ! * JRobin : Pure java implementation of RRDTool's functionality ! * ============================================================ ! * ! * Project Info: http://www.sourceforge.net/projects/jrobin ! * Project Lead: Sasa Markovic (sa...@eu...); ! * ! * (C) Copyright 2003, by Sasa Markovic. ! * ! * This library is free software; you can redistribute it and/or modify it under the terms ! * of the GNU Lesser General Public License as published by the Free Software Foundation; ! * either version 2.1 of the License, or (at your option) any later version. ! * ! * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; ! * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ! * See the GNU Lesser General Public License for more details. ! * ! * You should have received a copy of the GNU Lesser General Public License along with this ! * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, ! * Boston, MA 02111-1307, USA. ! */ ! ! package jrobin.graph; ! ! import jrobin.core.RrdException; ! ! import javax.imageio.*; ! import javax.imageio.stream.*; ! import java.awt.image.*; ! ! import java.util.*; ! import java.io.ByteArrayOutputStream; ! import java.io.File; ! import java.io.IOException; ! import java.io.Serializable; ! ! /** ! * <p>Class to represent JRobin graphs. This class is a light wrapper around ! * <a href="http://www.jfree.org/jfreechart/javadoc/org/jfree/chart/JFreeChart.html">JFreeChart ! * class</a>. ! * <a href="http://www.jfree.org/jfreechart/index.html">JFreeChart</a> ! * is an excellent free Java library for generating charts and graphs.</p> ! */ ! public class RrdGraph implements Serializable ! { ! private Grapher grapher; ! ! /** ! * Constructs new JRobin graph from the supplied definition. ! * @param graphDef Graph definition. ! * @throws IOException Thrown in case of I/O error. ! * @throws RrdException Thrown in case of JRobin specific error. ! */ ! public RrdGraph(RrdGraphDef graphDef) throws IOException, RrdException ! { ! grapher = new Grapher( graphDef ); ! } ! ! /** ! * Creates buffered image object from the graph. ! * @param width Image width. ! * @param height Image height. ! * @return Graph as a buffered image. ! */ ! public BufferedImage getBufferedImage(int width, int height) ! { ! try ! { ! return grapher.createImage( width, height ); ! } ! catch (RrdException e) { ! e.printStackTrace(); ! } ! ! return null; ! } ! ! /** ! * Saves graph in PNG format. ! * @param path Path to PNG file. ! * @param width Image width ! * @param height Image height ! * @throws IOException Thrown in case of I/O error. ! */ ! public void saveAsPNG(String path, int width, int height) throws IOException ! { ! try ! { ! BufferedImage gImage = grapher.createImage( width, height ); ! ImageIO.write( (RenderedImage) gImage, "png", new File(path) ); ! } ! catch ( RrdException e ) { ! e.printStackTrace(); ! } ! } ! ! /** ! * Saves graph in JPEG format ! * @param path Path to JPEG file. ! * @param width Image width. ! * @param height Image height. ! * @param quality JPEG qualitty (between 0 and 1). ! * @throws IOException Thrown in case of I/O error. ! */ ! public void saveAsJPEG(String path, int width, int height, float quality) throws IOException ! { ! // Based on http://javaalmanac.com/egs/javax.imageio/JpegWrite.html?l=rel ! try { ! // Retrieve jpg image to be compressed ! BufferedImage gImage = grapher.createImage( width, height ); ! RenderedImage rndImage = (RenderedImage) gImage; ! ! // Find a jpeg writer ! ImageWriter writer = null; ! Iterator iter = ImageIO.getImageWritersByFormatName("jpg"); ! if (iter.hasNext()) { ! writer = (ImageWriter)iter.next(); ! } ! ! // Prepare output file ! ImageOutputStream ios = ImageIO.createImageOutputStream(new File(path)); ! writer.setOutput(ios); ! ! // Set the compression quality ! ImageWriteParam iwparam = new JpegImageWriteParam(); ! iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT) ; ! iwparam.setCompressionQuality(quality); ! ! // Write the image ! writer.write(null, new IIOImage(rndImage, null, null), iwparam); ! ! // Cleanup ! ios.flush(); ! writer.dispose(); ! ios.close(); ! } ! catch (Exception e) { ! e.printStackTrace(); ! } ! ! } ! ! /** ! * Returns panel object so that graph can be easily embedded in swing applications. ! * @return Swing panel object with graph embedded in panel. ! */ ! public ChartPanel getChartPanel() ! { ! ChartPanel p = new ChartPanel(); ! try ! { ! p.setChart( grapher.createImage(0,0) ); ! } ! catch ( RrdException e ) { ! e.printStackTrace(); ! } ! ! return p; ! } ! ! /** ! * Returns graph as an array of PNG bytes ! * @param width Image width. ! * @param height Image height. ! * @return Array of PNG bytes. ! * @throws IOException Thrown in case of I/O error. ! */ ! public byte[] getPNGBytes(int width, int height) throws IOException ! { ! ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ! try ! { ! BufferedImage gImage = grapher.createImage( width, height ); ! ! ImageIO.write( (RenderedImage) gImage, "png", outputStream ); ! } ! catch ( RrdException e ) { ! e.printStackTrace(); ! } ! ! return outputStream.toByteArray(); ! } ! ! /** ! * Returns graph as an array of JPEG bytes ! * @param width Image width. ! * @param height Image height. ! * @param quality JPEG quality between 0 and 1. ! * @return Array of PNG bytes. ! * @throws IOException Thrown in case of I/O error. ! */ ! public byte[] getJPEGBytes(int width, int height, float quality) throws IOException ! { ! ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ! try { ! // Retrieve jpg image to be compressed ! BufferedImage gImage = grapher.createImage( width, height ); ! RenderedImage rndImage = (RenderedImage) gImage; ! ! // Find a jpeg writer ! ImageWriter writer = null; ! Iterator iter = ImageIO.getImageWritersByFormatName("jpg"); ! if (iter.hasNext()) { ! writer = (ImageWriter)iter.next(); ! } ! ! // Prepare output file ! ImageOutputStream ios = ImageIO.createImageOutputStream(outputStream); ! writer.setOutput(ios); ! ! // Set the compression quality ! ImageWriteParam iwparam = new JpegImageWriteParam(); ! iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT) ; ! iwparam.setCompressionQuality(quality); ! ! // Write the image ! writer.write(null, new IIOImage(rndImage, null, null), iwparam); ! ! // Cleanup ! ios.flush(); ! writer.dispose(); ! ios.close(); ! } ! catch (Exception e) { ! e.printStackTrace(); ! } ! return outputStream.toByteArray(); ! } ! ! } --- 1,219 ---- ! /* ============================================================ ! * JRobin : Pure java implementation of RRDTool's functionality ! * ============================================================ ! * ! * Project Info: http://www.sourceforge.net/projects/jrobin ! * Project Lead: Sasa Markovic (sa...@eu...); ! * ! * (C) Copyright 2003, by Sasa Markovic. ! * ! * This library is free software; you can redistribute it and/or modify it under the terms ! * of the GNU Lesser General Public License as published by the Free Software Foundation; ! * either version 2.1 of the License, or (at your option) any later version. ! * ! * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; ! * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ! * See the GNU Lesser General Public License for more details. ! * ! * You should have received a copy of the GNU Lesser General Public License along with this ! * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, ! * Boston, MA 02111-1307, USA. ! */ ! ! package jrobin.graph; ! ! import jrobin.core.RrdException; ! ! import javax.imageio.*; ! import javax.imageio.stream.*; ! import java.awt.image.*; ! ! import java.util.*; ! import java.io.ByteArrayOutputStream; ! import java.io.File; ! import java.io.IOException; ! import java.io.Serializable; ! ! /** ! * <p>Class to represent JRobin graphs. This class is a light wrapper around ! * <a href="http://www.jfree.org/jfreechart/javadoc/org/jfree/chart/JFreeChart.html">JFreeChart ! * class</a>. ! * <a href="http://www.jfree.org/jfreechart/index.html">JFreeChart</a> ! * is an excellent free Java library for generating charts and graphs.</p> ! */ ! public class RrdGraph implements Serializable ! { ! private Grapher grapher; ! private BufferedImage img = null; ! ! /** ! * Constructs new JRobin graph from the supplied definition. ! * @param graphDef Graph definition. ! * @throws IOException Thrown in case of I/O error. ! * @throws RrdException Thrown in case of JRobin specific error. ! */ ! public RrdGraph(RrdGraphDef graphDef) throws IOException, RrdException ! { ! grapher = new Grapher( graphDef ); ! } ! ! /** ! * Creates buffered image object from the graph. ! * @param width Image width. ! * @param height Image height. ! * @return Graph as a buffered image. ! */ ! // Check, if width/height has changed since last generation ! // Regenerate the graph ! public BufferedImage getBufferedImage(int width, int height) ! { ! try ! { ! if ( img != null ) ! return img; ! else ! { ! img = grapher.createImage( width, height ); ! return img; ! } ! } ! catch (RrdException e) { ! e.printStackTrace(); ! } ! ! return null; ! } ! ! /** ! * Saves graph in PNG format. ! * @param path Path to PNG file. ! * @param width Image width ! * @param height Image height ! * @throws IOException Thrown in case of I/O error. ! */ ! public void saveAsPNG(String path, int width, int height) throws IOException ! { ! ImageIO.write( (RenderedImage) getBufferedImage(width, height), "png", new File(path) ); ! } ! ! /** ! * Saves graph in JPEG format ! * @param path Path to JPEG file. ! * @param width Image width. ! * @param height Image height. ! * @param quality JPEG qualitty (between 0 and 1). ! * @throws IOException Thrown in case of I/O error. ! */ ! public void saveAsJPEG(String path, int width, int height, float quality) throws IOException ! { ! // Based on http://javaalmanac.com/egs/javax.imageio/JpegWrite.html?l=rel ! try { ! // Retrieve jpg image to be compressed ! BufferedImage gImage = getBufferedImage(width, height); ! RenderedImage rndImage = (RenderedImage) gImage; ! ! // Find a jpeg writer ! ImageWriter writer = null; ! Iterator iter = ImageIO.getImageWritersByFormatName("jpg"); ! if (iter.hasNext()) { ! writer = (ImageWriter)iter.next(); ! } ! ! // Prepare output file ! ImageOutputStream ios = ImageIO.createImageOutputStream(new File(path)); ! writer.setOutput(ios); ! ! // Set the compression quality ! ImageWriteParam iwparam = new JpegImageWriteParam(); ! iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT) ; ! iwparam.setCompressionQuality(quality); ! ! // Write the image ! writer.write(null, new IIOImage(rndImage, null, null), iwparam); ! ! // Cleanup ! ios.flush(); ! writer.dispose(); ! ios.close(); ! } ! catch (Exception e) { ! e.printStackTrace(); ! } ! ! } ! ! /** ! * Returns panel object so that graph can be easily embedded in swing applications. ! * @return Swing panel object with graph embedded in panel. ! */ ! public ChartPanel getChartPanel() ! { ! ChartPanel p = new ChartPanel(); ! p.setChart( getBufferedImage(0, 0) ); ! ! return p; ! } ! ! /** ! * Returns graph as an array of PNG bytes ! * @param width Image width. ! * @param height Image height. ! * @return Array of PNG bytes. ! * @throws IOException Thrown in case of I/O error. ! */ ! public byte[] getPNGBytes(int width, int height) throws IOException ! { ! ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ! ! ImageIO.write( (RenderedImage) getBufferedImage(width, height), "png", outputStream ); ! ! return outputStream.toByteArray(); ! } ! ! /** ! * Returns graph as an array of JPEG bytes ! * @param width Image width. ! * @param height Image height. ! * @param quality JPEG quality between 0 and 1. ! * @return Array of PNG bytes. ! * @throws IOException Thrown in case of I/O error. ! */ ! public byte[] getJPEGBytes(int width, int height, float quality) throws IOException ! { ! ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ! try { ! // Retrieve jpg image to be compressed ! BufferedImage gImage = getBufferedImage(width, height); ! RenderedImage rndImage = (RenderedImage) gImage; ! ! // Find a jpeg writer ! ImageWriter writer = null; ! Iterator iter = ImageIO.getImageWritersByFormatName("jpg"); ! if (iter.hasNext()) { ! writer = (ImageWriter)iter.next(); ! } ! ! // Prepare output file ! ImageOutputStream ios = ImageIO.createImageOutputStream(outputStream); ! writer.setOutput(ios); ! ! // Set the compression quality ! ImageWriteParam iwparam = new JpegImageWriteParam(); ! iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT) ; ! iwparam.setCompressionQuality(quality); ! ! // Write the image ! writer.write(null, new IIOImage(rndImage, null, null), iwparam); ! ! // Cleanup ! ios.flush(); ! writer.dispose(); ! ios.close(); ! } ! catch (Exception e) { ! e.printStackTrace(); ! } ! return outputStream.toByteArray(); ! } ! ! } Index: ChartGraphics.java =================================================================== RCS file: /cvsroot/jrobin/src/jrobin/graph/ChartGraphics.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** ChartGraphics.java 13 Oct 2003 22:53:33 -0000 1.4 --- ChartGraphics.java 14 Oct 2003 22:11:34 -0000 1.5 *************** *** 52,58 **** } void fillRect(int x1, int y1, int x2, int y2) { ! g.fillRect( x1, -y1, x2 - x1, y2 - y1 ); } --- 52,59 ---- } + // Contrary to Graphics2D fillRect, this method uses boundary points void fillRect(int x1, int y1, int x2, int y2) { ! g.fillRect( x1, -y2, x2 - x1, y2 - y1 ); } Index: Grapher.java =================================================================== RCS file: /cvsroot/jrobin/src/jrobin/graph/Grapher.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** Grapher.java 13 Oct 2003 22:53:33 -0000 1.7 --- Grapher.java 14 Oct 2003 22:11:34 -0000 1.8 *************** *** 517,521 **** RrdSecond[] times = (RrdSecond[]) s.getSeries().times.toArray( new RrdSecond[0] ); Double[] values = (Double[]) s.getSeries().values.toArray( new Double[0] ); ! for (int i = 0; i < times.length; i++) { --- 517,521 ---- RrdSecond[] times = (RrdSecond[]) s.getSeries().times.toArray( new RrdSecond[0] ); Double[] values = (Double[]) s.getSeries().values.toArray( new Double[0] ); ! for (int i = 0; i < times.length; i++) { *************** *** 525,529 **** ny += p[i]; ! if ( ax != 0 && ay != Integer.MIN_VALUE && ny != Integer.MIN_VALUE ) g.drawLine( ax, ay, nx, ny ); --- 525,529 ---- ny += p[i]; ! if ( nx != 0 && ay != Integer.MIN_VALUE && ny != Integer.MIN_VALUE ) g.drawLine( ax, ay, nx, ny ); *************** *** 547,551 **** RrdSecond[] times = (RrdSecond[]) s.getSeries().times.toArray( new RrdSecond[0] ); Double[] values = (Double[]) s.getSeries().values.toArray( new Double[0] ); ! for (int i = 0; i < times.length; i++) { --- 547,551 ---- RrdSecond[] times = (RrdSecond[]) s.getSeries().times.toArray( new RrdSecond[0] ); Double[] values = (Double[]) s.getSeries().values.toArray( new Double[0] ); ! for (int i = 0; i < times.length; i++) { *************** *** 560,566 **** } ! if ( ax != 0 && py != Integer.MIN_VALUE && ny != Integer.MIN_VALUE ) g.drawLine( nx, py, nx, ny ); ! p[i] = ny; ax = nx; --- 560,575 ---- } ! if (nx > ax + 1) // More than one pixel hop, draw intermediate pixels too ! { ! // For each pixel between nx and ax, calculate the y, plot the line ! int co = (ny - ay) / (nx - ax); ! int j = (ax > 0 ? ax : 1 ); // Skip 0 ! ! for (j = ax; j <= nx; j++) ! g.drawLine( j, py, j, ( co * (j - ax) + ay) ); ! } ! else if ( nx != 0 && py != Integer.MIN_VALUE && ny != Integer.MIN_VALUE ) g.drawLine( nx, py, nx, ny ); ! p[i] = ny; ax = nx; *************** *** 589,593 **** numPoints = (int)(endTime - startTime + 1); - /* // ------------------------------------------------------- // Experimental test code, faster fetching research --- 598,601 ---- *************** *** 632,642 **** } rrd.close(); - - - } // ------------------------------------------------------- - */ // ------------------------------------------------------- // Old fetching code --- 640,647 ---- } rrd.close(); } // ------------------------------------------------------- + /* // ------------------------------------------------------- // Old fetching code *************** *** 644,648 **** sources[i].setIntervalInternal(startTime, endTime); // ------------------------------------------------------- ! s3 = Calendar.getInstance().getTimeInMillis(); --- 649,653 ---- sources[i].setIntervalInternal(startTime, endTime); // ------------------------------------------------------- ! */ s3 = Calendar.getInstance().getTimeInMillis(); |