Java tutorial
/* * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ /* * JFreeChartFileWriter.java * Copyright (C) 2016-2019 University of Waikato, Hamilton, NZ */ package adams.flow.sink; import adams.core.QuickInfoHelper; import adams.data.image.BufferedImageContainer; import adams.data.io.output.AbstractImageWriter; import adams.data.io.output.JAIImageWriter; import adams.data.spreadsheet.SpreadSheet; import adams.gui.visualization.jfreechart.chart.AbstractChartGenerator; import adams.gui.visualization.jfreechart.chart.XYLineChart; import adams.gui.visualization.jfreechart.dataset.AbstractDatasetGenerator; import adams.gui.visualization.jfreechart.dataset.DefaultXY; import adams.gui.visualization.jfreechart.shape.AbstractShapeGenerator; import adams.gui.visualization.jfreechart.shape.Default; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.XYPlot; import org.jfree.data.general.Dataset; import java.awt.Color; import java.awt.Shape; import java.awt.image.BufferedImage; /** <!-- globalinfo-start --> * Generates a JFreeChart plot and writes the bitmap to a file. * <br><br> <!-- globalinfo-end --> * <!-- flow-summary-start --> * Input/output:<br> * - accepts:<br> * adams.data.spreadsheet.SpreadSheet<br> * <br><br> <!-- flow-summary-end --> * <!-- options-start --> * <pre>-logging-level <OFF|SEVERE|WARNING|INFO|CONFIG|FINE|FINER|FINEST> (property: loggingLevel) * The logging level for outputting errors and debugging output. * default: WARNING * </pre> * * <pre>-name <java.lang.String> (property: name) * The name of the actor. * default: JFreeChartFileWriter * </pre> * * <pre>-annotation <adams.core.base.BaseAnnotation> (property: annotations) * The annotations to attach to this actor. * default: * </pre> * * <pre>-skip <boolean> (property: skip) * If set to true, transformation is skipped and the input token is just forwarded * as it is. * default: false * </pre> * * <pre>-stop-flow-on-error <boolean> (property: stopFlowOnError) * If set to true, the flow execution at this level gets stopped in case this * actor encounters an error; the error gets propagated; useful for critical * actors. * default: false * </pre> * * <pre>-silent <boolean> (property: silent) * If enabled, then no errors are output in the console; Note: the enclosing * actor handler must have this enabled as well. * default: false * </pre> * * <pre>-output <adams.core.io.PlaceholderFile> (property: outputFile) * The file to write the plot to * default: ${CWD} * </pre> * * <pre>-dataset <adams.gui.visualization.jfreechart.dataset.AbstractDatasetGenerator> (property: dataset) * The dataset generator to use. * default: adams.gui.visualization.jfreechart.dataset.DefaultXY * </pre> * * <pre>-chart <adams.gui.visualization.jfreechart.chart.AbstractChartGenerator> (property: chart) * The chart generator to use. * default: adams.gui.visualization.jfreechart.chart.XYLineChart * </pre> * * <pre>-shape <adams.gui.visualization.jfreechart.shape.AbstractShapeGenerator> (property: shape) * The shape generator to use for the data point markers. * default: adams.gui.visualization.jfreechart.shape.Default * </pre> * * <pre>-plot-color <java.awt.Color> (property: plotColor) * The color for the plot. * default: #0000ff * </pre> * * <pre>-diagonal-color <java.awt.Color> (property: diagonalColor) * The color for the diagonal (ie second data series if present). * default: #000000 * </pre> * * <pre>-width <int> (property: width) * The width of the plot. * default: 800 * minimum: -1 * </pre> * * <pre>-height <int> (property: height) * The height of the plot. * default: 600 * minimum: -1 * </pre> * * <pre>-writer <adams.data.io.output.AbstractImageWriter> (property: writer) * The image writer to use. * default: adams.data.io.output.JAIImageWriter * </pre> * <!-- options-end --> * * @author FracPete (fracpete at waikato dot ac dot nz) */ public class JFreeChartFileWriter extends AbstractFileWriter { private static final long serialVersionUID = -2648121220428217287L; /** the dataset generator. */ protected AbstractDatasetGenerator m_Dataset; /** the chart generator. */ protected AbstractChartGenerator m_Chart; /** the shape generator. */ protected AbstractShapeGenerator m_Shape; /** the color for the plot. */ protected Color m_PlotColor; /** the color for the diagonal plot. */ protected Color m_DiagonalColor; /** the width of the plot. */ protected int m_Width; /** the height of the plot. */ protected int m_Height; /** the image writer to use. */ protected AbstractImageWriter m_Writer; /** * Returns a string describing the object. * * @return a description suitable for displaying in the gui */ @Override public String globalInfo() { return "Generates a JFreeChart plot and writes the bitmap to a file."; } /** * Adds options to the internal list of options. */ @Override public void defineOptions() { super.defineOptions(); m_OptionManager.add("dataset", "dataset", new DefaultXY()); m_OptionManager.add("chart", "chart", new XYLineChart()); m_OptionManager.add("shape", "shape", new Default()); m_OptionManager.add("plot-color", "plotColor", Color.BLUE); m_OptionManager.add("diagonal-color", "diagonalColor", Color.BLACK); m_OptionManager.add("width", "width", 800, -1, null); m_OptionManager.add("height", "height", 600, -1, null); m_OptionManager.add("writer", "writer", new JAIImageWriter()); } /** * Returns the tip text for this property. * * @return tip text for this property suitable for * displaying in the GUI or for listing the options. */ public String outputFileTipText() { return "The file to write the plot to"; } /** * Sets the dataset generator. * * @param value the generator */ public void setDataset(AbstractDatasetGenerator value) { m_Dataset = value; reset(); } /** * Returns the dataset generator. * * @return the generator */ public AbstractDatasetGenerator getDataset() { return m_Dataset; } /** * Returns the tip text for this property. * * @return tip text for this property suitable for * displaying in the GUI or for listing the options. */ public String datasetTipText() { return "The dataset generator to use."; } /** * Sets the chart generator. * * @param value the generator */ public void setChart(AbstractChartGenerator value) { m_Chart = value; reset(); } /** * Returns the chart generator. * * @return the generator */ public AbstractChartGenerator getChart() { return m_Chart; } /** * Returns the tip text for this property. * * @return tip text for this property suitable for * displaying in the GUI or for listing the options. */ public String chartTipText() { return "The chart generator to use."; } /** * Sets the shape generator. * * @param value the generator */ public void setShape(AbstractShapeGenerator value) { m_Shape = value; reset(); } /** * Returns the shape generator. * * @return the generator */ public AbstractShapeGenerator getShape() { return m_Shape; } /** * Returns the tip text for this property. * * @return tip text for this property suitable for * displaying in the GUI or for listing the options. */ public String shapeTipText() { return "The shape generator to use for the data point markers."; } /** * Sets the color for the plot. * * @param value the color */ public void setPlotColor(Color value) { m_PlotColor = value; reset(); } /** * Returns the color for the plot. * * @return the color */ public Color getPlotColor() { return m_PlotColor; } /** * Returns the tip text for this property. * * @return tip text for this property suitable for * displaying in the GUI or for listing the options. */ public String plotColorTipText() { return "The color for the plot."; } /** * Sets the color for the diagonal (ie second data series if present). * * @param value the color */ public void setDiagonalColor(Color value) { m_DiagonalColor = value; reset(); } /** * Returns the color for the diagonal (ie second data series if present). * * @return the color */ public Color getDiagonalColor() { return m_DiagonalColor; } /** * Returns the tip text for this property. * * @return tip text for this property suitable for * displaying in the GUI or for listing the options. */ public String diagonalColorTipText() { return "The color for the diagonal (ie second data series if present)."; } /** * Sets the width of the plot. * * @param value the width */ public void setWidth(int value) { m_Width = value; reset(); } /** * Returns the currently set width of the plot. * * @return the width */ public int getWidth() { return m_Width; } /** * Returns the tip text for this property. * * @return tip text for this property suitable for * displaying in the GUI or for listing the options. */ public String widthTipText() { return "The width of the plot."; } /** * Sets the height of the plot. * * @param value the height */ public void setHeight(int value) { m_Height = value; reset(); } /** * Returns the currently set height of the plot. * * @return the height */ public int getHeight() { return m_Height; } /** * Returns the tip text for this property. * * @return tip text for this property suitable for * displaying in the GUI or for listing the options. */ public String heightTipText() { return "The height of the plot."; } /** * Sets the image writer. * * @param value the writer */ public void setWriter(AbstractImageWriter value) { m_Writer = value; reset(); } /** * Returns the image writer. * * @return the writer */ public AbstractImageWriter getWriter() { return m_Writer; } /** * Returns the tip text for this property. * * @return tip text for this property suitable for * displaying in the GUI or for listing the options. */ public String writerTipText() { return "The image writer to use."; } /** * Returns the class that the consumer accepts. * * @return the Class of objects that can be processed */ @Override public Class[] accepts() { return new Class[] { SpreadSheet.class }; } /** * Returns a quick info about the actor, which will be displayed in the GUI. * * @return null if no info available, otherwise short string */ @Override public String getQuickInfo() { String result; result = super.getQuickInfo(); result += QuickInfoHelper.toString(this, "dataset", m_Dataset, ", dataset: "); result += QuickInfoHelper.toString(this, "chart", m_Chart, ", chart: "); result += QuickInfoHelper.toString(this, "shape", m_Shape, ", shape: "); result += QuickInfoHelper.toString(this, "width", m_Width, ", width:"); result += QuickInfoHelper.toString(this, "height", m_Height, ", height:"); result += QuickInfoHelper.toString(this, "writer", m_Writer, ", writer: "); return result; } /** * Executes the flow item. * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { String result; SpreadSheet sheet; Dataset dataset; JFreeChart jfreechart; BufferedImage image; BufferedImageContainer cont; Shape shape; XYPlot plot; result = null; try { sheet = (SpreadSheet) m_InputToken.getPayload(); dataset = m_Dataset.generate(sheet); jfreechart = m_Chart.generate(dataset); shape = m_Shape.generate(); jfreechart.getPlot().setBackgroundPaint(Color.WHITE); if (jfreechart.getPlot() instanceof XYPlot) { plot = (XYPlot) jfreechart.getPlot(); plot.setDomainGridlinesVisible(true); plot.setDomainGridlinePaint(Color.GRAY); plot.setRangeGridlinesVisible(true); plot.setRangeGridlinePaint(Color.GRAY); plot.getRenderer().setSeriesPaint(0, m_PlotColor); if (plot.getSeriesCount() > 1) plot.getRenderer().setSeriesPaint(1, m_DiagonalColor); if (shape != null) plot.getRenderer().setSeriesShape(0, shape); } image = jfreechart.createBufferedImage(m_Width, m_Height); cont = new BufferedImageContainer(); cont.setImage(image); m_Writer.write(m_OutputFile, cont); } catch (Exception e) { result = handleException("Failed to generate plot!", e); } return result; } }