JFreeChart is an open chart drawing class library on the JAVA platform. It is written completely in JAVA language and is designed for applications, applets, servlets, and JSP. JFreeChart can generate pie charts, bar charts, scatter plots, time series, Gantt charts and other charts, and can generate output in PNG and JPEG formats, and can also be associated with PDF and EXCEL.
Example of line chart
package com.sprite.test; 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.labels.StandardCategoryItemLabelGenerator; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.renderer.category.LineAndShapeRenderer; import org.jfree.data.category.CategoryDataset; import org.jfree.data.general.DatasetUtilities; //JFreeChart Line Chart (line chart) public class TestJFreeChart { /** * Create JFreeChart Line Chart (line chart) */ 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, "E://line.jpg", 600, 400); } // Save as a 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 JFreeChart object based on CategoryDataset public static JFreeChart createChart(CategoryDataset categoryDataset) { // Create JFreeChart object: ChartFactory.createLineChart JFreeChart jfreechart = ChartFactory.createLineChart("Different categories calculated by hour", // Title "Year Division", // categoryAxisLabel (category axis, horizontal axis, X-axis label) "Quantity", // valueAxisLabel (value axis, vertical axis, Y-axis label) categoryDataset, // dataset PlotOrientation.VERTICAL, true, // legend false, // tooltips false); // URLs // 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); // For other settings refer to the CategoryPlot class LineAndShapeRenderer renderer = (LineAndShapeRenderer)plot.getRenderer(); renderer.setBaseShapesVisible(true); // series point (i.e. data point) can be visible renderer.setBaseLinesVisible(true); // series There is a connection between points (i.e. data points) and can be seen renderer.setUseSeriesOffset(true); // Set the offset renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); renderer.setBaseItemLabelsVisible(true); return jfreechart; } /** * Create a CategoryDataset object* */ 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 = {{4, 3, 1, 1, 1, 1, 2, 2, 2, 1, 8, 2, 1, 1},}; // Or use code similar to the following // DefaultCategoryDataset categoryDataset = new // DefaultCategoryDataset(); // categoryDataset.addValue(10, "rowKey", "colKey"); return DatasetUtilities.createCategoryDataset(rowKeys, colKeys, data); } }Generate renderings:
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.