1. Line chart demo
package com.pdf; import java.awt.Font; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.StandardChartTheme; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.category.CategoryDataset; import org.jfree.data.general.DatasetUtilities; //JFreeChart Line Chart (line chart) public class CreateJFreeChartLine { public static void main(String[] args) { // Step 1: Create CategoryDataset object (prepare data) CategoryDataset dataset = createDataset(); // Step 2: Generate JFreeChart object according to Dataset, and make corresponding settings JFreeChart freeChart = createChart(dataset); // Step 3: Output the JFreeChart object to a file, Servlet output stream, etc. saveAsFile(freeChart, "c:jfreechartline.jpg", 600, 400); } // save as file public static void saveAsFile(JFreeChart chart, String outputPath,int weight, int height) { FileOutputStream out = null; try { File outFile = new File(outputPath); if (!outFile.getParentFile().exists()) { outFile.getParentFile().mkdirs(); } out = new FileOutputStream(outputPath); // save as PNG // ChartUtilities.writeChartAsPNG(out, chart, 600, 400); // save as JPEG ChartUtilities.writeChartAsJPEG(out, chart, 600, 400); out.flush(); } catch (FileNotFoundException e) { e.printStackTrace (); } catch (IOException e) { e.printStackTrace (); } finally { if (out != null) { try { out.close(); } catch (IOException e) { // do nothing } } } } // Create a JFreeChart object based on CategoryDataset public static JFreeChart createChart(CategoryDataset categoryDataset) { //The "name" parameter here; I don't know what it means, but it can be used anyway StandardChartTheme standardChartTheme = new StandardChartTheme("name"); //You can change the font of the axis standardChartTheme.setLargeFont(new Font("楷体",Font.BOLD, 12)); // You can change the font of the legend standardChartTheme.setRegularFont(new Font("宋体",Font.BOLD, 8)); // You can change the title font of the icon standardChartTheme.setExtraLargeFont(new Font("隶书",Font.BOLD, 20)); ChartFactory.setChartTheme(standardChartTheme);//Set the theme // Create a JFreeChart object: ChartFactory.createLineChart JFreeChart jfreechart = ChartFactory.createLineChart3D("Different categories calculate line chart by hour", "Year", "Quantity", categoryDataset, PlotOrientation.VERTICAL, true, true, false); // Use CategoryPlot to set various parameters. The following settings can be omitted. CategoryPlot plot = (CategoryPlot) jfreechart.getPlot(); // background color transparency plot.setBackgroundAlpha(0.5f); // Foreground color transparency plot.setForegroundAlpha(0.5f); // Other settings refer to CategoryPlot class return jfreechart; } /** * Create dataset * @return */ public static CategoryDataset createDataset() { String[] rowKeys = {"Platform A","Your sister"}; String[] colKeys = {"0:00", "1:00", "2:00", "7:00", "8:00", "9:00","10:00", "11:00", "12:00", "13:00", "16:00", "20:00", "21:00","23:00" }; double[][] data = {{4, 8, 1, 1, 1, 1, 2, 2, 2, 1, 8, 2, 1, 1 },{9, 3, 5, 8, 1, 7, 2, 2, 5, 1, 8, 2, 3, 1}}; return DatasetUtilities.createCategoryDataset(rowKeys, colKeys, data); } }
2. Line chart custom x-axis demo
package com.pdf; import java.awt.Color; import java.awt.Dimension; import javax.swing.JPanel; 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.RectangleInsets; import org.jfree.ui.RefineryUtilities; public class LineChartDemo2 extends ApplicationFrame { public LineChartDemo2(String paramString) { super(paramString); XYDataset localXYDataset = createDataset(); JFreeChart localJFreeChart = createChart(localXYDataset); ChartPanel localChartPanel = new ChartPanel(localJFreeChart); localChartPanel.setPreferredSize(new Dimension(500, 270)); setContentPane(localChartPanel); } private static XYDataset createDataset() { XYSeries localXYSeries1 = new XYSeries("First"); localXYSeries1.add(1, 1); localXYSeries1.add(2, 4); localXYSeries1.add(3, 3); localXYSeries1.add(4, 5); localXYSeries1.add(5, 5); localXYSeries1.add(6, 7); localXYSeries1.add(7, 7); localXYSeries1.add(21, 8); XYSeries localXYSeries2 = new XYSeries("Second"); localXYSeries2.add(1, 5); localXYSeries2.add(2, 7); localXYSeries2.add(3, 6); localXYSeries2.add(4, 8); localXYSeries2.add(5, 4); localXYSeries2.add(6, 4); localXYSeries2.add(7, 2); localXYSeries2.add(8, 1); XYSeries localXYSeries3 = new XYSeries("Third"); localXYSeries3.add(3, 4); localXYSeries3.add(4, 3); localXYSeries3.add(5, 2); localXYSeries3.add(6, 3); localXYSeries3.add(7, 6); localXYSeries3.add(8, 3); localXYSeries3.add(9, 4); localXYSeries3.add(10, 3); XYSeriesCollection localXYSeriesCollection = new XYSeriesCollection(); localXYSeriesCollection.addSeries(localXYSeries1); localXYSeriesCollection.addSeries(localXYSeries2); localXYSeriesCollection.addSeries(localXYSeries3); return localXYSeriesCollection; } private static JFreeChart createChart(XYDataset paramXYDataset) { JFreeChart localJFreeChart = ChartFactory.createXYLineChart("Line Chart Demo 2", "X", "Y", paramXYDataset, PlotOrientation.VERTICAL, true, true, false); localJFreeChart.setBackgroundPaint(Color.white); XYPlot localXYPlot = (XYPlot)localJFreeChart.getPlot(); localXYPlot.setBackgroundPaint(Color.lightGray); localXYPlot.setAxisOffset(new RectangleInsets(10, 10, 10, 10)); localXYPlot.setDomainGridlinePaint(Color.white); localXYPlot.setRangeGridlinePaint(Color.white); XYLineAndShapeRenderer localXYLineAndShapeRenderer = (XYLineAndShapeRenderer) localXYPlot.getRenderer (); localXYLineAndShapeRenderer.setBaseShapesVisible(true); localXYLineAndShapeRenderer.setBaseShapesFilled(true); NumberAxis localNumberAxis = (NumberAxis)localXYPlot.getRangeAxis(); localNumberAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); return localJFreeChart; } public static JPanel createDemoPanel() { JFreeChart localJFreeChart = createChart(createDataset()); return new ChartPanel(localJFreeChart); } public static void main(String[] paramArrayOfString) { LineChartDemo2 localLineChartDemo2 = new LineChartDemo2("Line Chart Demo 2"); localLineChartDemo2.pack(); RefineryUtilities.centerFrameOnScreen(localLineChartDemo2); localLineChartDemo2.setVisible(true); } }
3. Display the Y-axis of the histogram as a demo of the percentage
package com.pdf; import java.awt.Color; import java.awt.Font; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.text.DecimalFormat; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.StandardChartTheme; import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.renderer.category.BarRenderer3D; import org.jfree.data.category.CategoryDataset; import org.jfree.data.general.DatasetUtilities; //JFreeChart Line Chart (bar chart) public class CreateJFreeChartColumn { public static void main(String[] args) { // Step 1: Create CategoryDataset object (prepare data) CategoryDataset dataset = createDataset(); // Step 2: Generate JFreeChart object according to Dataset, and make corresponding settings JFreeChart freeChart = createChart(dataset); // Step 3: Output the JFreeChart object to a file, Servlet output stream, etc. saveAsFile(freeChart, "c:jfreechartline.jpg", 600, 400); } // save as file public static void saveAsFile(JFreeChart chart, String outputPath,int weight, int height) { FileOutputStream out = null; try { File outFile = new File(outputPath); if (!outFile.getParentFile().exists()) { outFile.getParentFile().mkdirs(); } out = new FileOutputStream(outputPath); // save as PNG // ChartUtilities.writeChartAsPNG(out, chart, 600, 400); // save as JPEG ChartUtilities.writeChartAsJPEG(out, chart, 600, 400); out.flush(); } catch (FileNotFoundException e) { e.printStackTrace (); } catch (IOException e) { e.printStackTrace (); } finally { if (out != null) { try { out.close(); } catch (IOException e) { // do nothing } } } } // Create a JFreeChart object based on CategoryDataset public static JFreeChart createChart(CategoryDataset categoryDataset) { //The "name" parameter here; I don't know what it means, but it can be used anyway StandardChartTheme standardChartTheme = new StandardChartTheme("name"); //You can change the font of the axis standardChartTheme.setLargeFont(new Font("楷体",Font.BOLD, 12)); // You can change the font of the legend standardChartTheme.setRegularFont(new Font("宋体",Font.BOLD, 8)); // You can change the title font of the icon standardChartTheme.setExtraLargeFont(new Font("隶书",Font.BOLD, 20)); ChartFactory.setChartTheme(standardChartTheme);//Set the theme // Create a JFreeChart object: ChartFactory.createLineChart JFreeChart jfreechart = ChartFactory.createBarChart("Different categories are calculated by hourly pie chart", "year", "quantity", categoryDataset, PlotOrientation.VERTICAL, true, true, false); // Use CategoryPlot to set various parameters. The following settings can be omitted. CategoryPlot plot = (CategoryPlot) jfreechart.getPlot(); //Set the histogram display as a percentage BarRenderer3D renderer = new BarRenderer3D(); renderer.setBaseOutlinePaint(Color.BLACK); //Display the value of each bar and modify the font property of the value renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}",DecimalFormat.getPercentInstance())); renderer.setItemLabelsVisible(true); plot.setRenderer (renderer); // background color transparency plot.setBackgroundAlpha(0.5f); // Foreground color transparency plot.setForegroundAlpha(0.5f); // Other settings refer to CategoryPlot class return jfreechart; } /** * Create dataset * @return */ public static CategoryDataset createDataset() { String[] rowKeys = {"A platform"}; String[] colKeys = {"0:00", "1:00", "2:00", "7:00", "8:00", "9:00","10:00", "11:00", "12:00", "13:00", "16:00", "20:00", "21:00","23:00" }; double[][] data = {{0.8, 0.5, 0.124, 0.58, 0.25, 0.23, 0.12, 0.47, 0.29, 0.86, 0.75, 0.64, 0.31, 0.123 }}; return DatasetUtilities.createCategoryDataset(rowKeys, colKeys, data); } }