Java tutorial
/* =========================================================== * JFreeChart : a free chart library for the Java(tm) platform * =========================================================== * * (C) Copyright 2000-2004, by Object Refinery Limited and Contributors. * * Project Info: http://www.jfree.org/jfreechart/index.html * * 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. * * [Java is a trademark or registered trademark of Sun Microsystems, Inc. * in the United States and other countries.] * * ------------------- * LineChartDemo6.java * ------------------- * (C) Copyright 2004, by Object Refinery Limited and Contributors. * * Original Author: David Gilbert (for Object Refinery Limited); * Contributor(s): -; * * $Id: LineChartDemo6.java,v 1.5 2004/04/26 19:11:55 taqua Exp $ * * Changes * ------- * 27-Jan-2004 : Version 1 (DG); * */ package required; import java.awt.Color; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInput; import java.io.ObjectInputStream; import java.util.Iterator; import java.util.LinkedHashMap; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; import org.jfree.data.xy.XYDataset; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; import org.jfree.ui.ApplicationFrame; import org.jfree.ui.RefineryUtilities; /** * A simple demonstration application showing how to create a line chart using data from an * {@link XYDataset}. * */ public class ChartPlotter extends ApplicationFrame { /** * */ private static final long serialVersionUID = 1L; /** * Creates a new demo. * * @param title the frame title. * @throws IOException * @throws ClassNotFoundException * @throws FileNotFoundException */ public ChartPlotter(final String title) throws FileNotFoundException, ClassNotFoundException, IOException { super(title); final XYDataset dataset = createDataset(); final JFreeChart chart = createChart(dataset); final ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setPreferredSize(new java.awt.Dimension(500, 270)); setContentPane(chartPanel); } /** * Creates a sample dataset. * * @return a sample dataset. * @throws IOException * @throws FileNotFoundException * @throws ClassNotFoundException */ @SuppressWarnings("unchecked") private XYDataset createDataset() throws FileNotFoundException, IOException, ClassNotFoundException { LinkedHashMap<String, Long> lhash = new LinkedHashMap<>(); try (InputStream file = new FileInputStream("SortedFreq.r"); InputStream buffer = new BufferedInputStream(file); ObjectInput input = new ObjectInputStream(buffer); ) { //deserialize the List lhash = (LinkedHashMap<String, Long>) input.readObject(); input.close(); buffer.close(); file.close(); System.out.println(lhash.size()); } final XYSeries series2 = new XYSeries("Second"); Iterator<String> it = lhash.keySet().iterator(); for (int i = 0; i < lhash.size(); i++) { if (it.hasNext()) series2.add(i, lhash.get(it.next())); } final XYSeriesCollection dataset = new XYSeriesCollection(); dataset.addSeries(series2); return dataset; } /** * Creates a chart. * * @param dataset the data for the chart. * * @return a chart. */ private JFreeChart createChart(final XYDataset dataset) { // create the chart... final JFreeChart chart = ChartFactory.createXYLineChart("Zipf's LAW", // chart title "frequency", // x axis label "words", // y axis label dataset, // data PlotOrientation.VERTICAL, true, // include legend true, // tooltips false // urls ); // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART... chart.setBackgroundPaint(Color.white); // final StandardLegend legend = (StandardLegend) chart.getLegend(); // legend.setDisplaySeriesShapes(true); // get a reference to the plot for further customisation... final XYPlot plot = chart.getXYPlot(); plot.setBackgroundPaint(Color.lightGray); // plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0)); plot.setDomainGridlinePaint(Color.white); plot.setRangeGridlinePaint(Color.white); final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(); renderer.setSeriesLinesVisible(0, false); renderer.setSeriesShapesVisible(1, false); plot.setRenderer(renderer); // change the auto tick unit selection to integer units only... final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); // OPTIONAL CUSTOMISATION COMPLETED. return chart; } // **************************************************************************** // * JFREECHART DEVELOPER GUIDE * // * The JFreeChart Developer Guide, written by David Gilbert, is available * // * to purchase from Object Refinery Limited: * // * * // * http://www.object-refinery.com/jfreechart/guide.html * // * * // * Sales are used to provide funding for the JFreeChart project - please * // * support us so that we can continue developing free software. * // **************************************************************************** /** * Starting point for the demonstration application. * * @param args ignored. * @throws IOException * @throws ClassNotFoundException * @throws FileNotFoundException */ public static void main(final String[] args) throws FileNotFoundException, ClassNotFoundException, IOException { final ChartPlotter demo = new ChartPlotter("ZipF's Law over CACM data"); demo.pack(); RefineryUtilities.centerFrameOnScreen(demo); demo.setVisible(true); } public static void runMe() throws FileNotFoundException, ClassNotFoundException, IOException { final ChartPlotter demo = new ChartPlotter("ZipF's Law over CACM data"); demo.pack(); RefineryUtilities.centerFrameOnScreen(demo); demo.setVisible(true); } }