JFreeChart is a JAVA project on the open source site SourceForge.net. It is mainly used for a variety of charts, including: pie charts, bar charts (normal bar charts and stack bar charts), line charts, area charts, distribution charts, mixed charts, Gantt charts, and some dashboards.
To apply jfreechart to draw pictures, you need two jar packages: jfreechart.jar and jcommon.jar, download address.
Here is an example of drawing a line chart:
package yuth.jfree.demo; /** * Create a line chart using categoryDataset dataset* When there is a lot of data, the value of the horizontal coordinate cannot be fully seen in the JPanel, and it is displayed as an ellipsis. * Solution: * Method 1. When saving the report as a picture, set the width of the picture to be large enough (2000 or 3000), so that the picture can display the horizontal coordinate value. * This method treats the symptoms but not the root cause, so there is a second method* Method 2: Set the Lable on the X-axis to tilt it 45 degrees. */ import java.awt.BasicStroke; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.geom.Ellipse2D; import java.io.File; import java.io.IOException; import java.net.URL; import java.text.DecimalFormat; import java.util.Random; import javax.swing.ImageIcon; import javax.swing.JPanel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.CategoryAxis; import org.jfree.chart.axis.CategoryLabelPositions; import org.jfree.chart.axis.NumberAxis; 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.chart.title.TextTitle; import org.jfree.data.category.CategoryDataset; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.ui.ApplicationFrame; import org.jfree.ui.HorizontalAlignment; import org.jfree.ui.RectangleEdge; import org.jfree.ui.RectangleInsets; import org.jfree.ui.RefineryUtilities; public class LineChartDemo1 extends ApplicationFrame { private static final long serialVersionUID = -6354350604313079793L; /* synthetic */static Class class$demo$LineChartDemo1; public LineChartDemo1(String string) { super(string); JPanel jpanel = createDemoPanel(); jpanel.setPreferredSize(new Dimension(500, 270)); setContentPane(jpanel); } /** * How to distinguish different legends: distinguish based on whether the second parameter of DefaultCategoryDataset.addValue() is the same. * The order of data added in the same legend affects the final display; the order of data added in the different legends does not affect the final display. * @return */ private static CategoryDataset createDataset() { DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset(); //Defaultcategorydataset.addValue() parameter analysis: (numerical, legend name, horizontal coordinate value) /* //Add data method 1 //Legend 1 defaultcategorydataset.addValue(212.0, "First", "1001.0"); defaultcategorydataset.addValue(504.0, "First", "1001.1"); defaultcategorydataset.addValue(1520.0, "First", "1001.2"); // Legend 2 defaultcategorydataset.addValue(712.0, "Second", "1001.0"); defaultcategorydataset.addValue(1204.0, "Second", "1001.1"); defaultcategorydataset.addValue(1820.0, "Second", "1001.2"); /*/ //* // Add data method 2 // Experiment random number to generate test results Random random = new Random(12345); // Legend 1, 40 data for(int i=0;i<40;i++){ defaultcategorydataset.addValue(random.nextInt(100000), "First",String.valueOf(1000+i) ); } // Legend 2, 40 data for (int i = 0; i < 40; i++) { defaultcategorydataset.addValue(random.nextInt(100000), "Second", String.valueOf(1000 + i)); } //*/ return defaultcategorydataset; } private static JFreeChart createChart(CategoryDataset categorydataset) { JFreeChart jfreechart = ChartFactory.createLineChart( "jfreechart test",// Chart title "X", // Spindle label (x-axis) "Y",// Range axis label (y-axis) categorydataset, // Dataset PlotOrientation.VERTICAL,// Orientation false, // Whether to include legend true, // Prompt message whether false);// Whether to use urls // Change the background color of the chart jfreechart.setBackgroundPaint(Color.white); CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot(); categoryplot.setBackgroundPaint(Color.lightGray); categoryplot.setRangeGridlinePaint(Color.white); categoryplot.setRangeGridlinesVisible(false); //Modify the range axis. We change the default scale value (allowed to display decimals) to only display integer scale values. NumberAxis numberraxis = (NumberAxis) categoryplot.getRangeAxis(); numberraxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); // Set the Lable on the X axis to tilt it 45 degrees CategoryAxis domainAxis = categoryplot.getDomainAxis(); domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); // Set the Lable on the X axis to tilt it 45 degrees domainAxis.setLowerMargin(0.0); // Set the distance from the left end of the picture domainAxis.setUpperMargin(0.0); // Set the distance from the right end of the picture LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot .getRenderer(); lineandshaperenderer.setShapesVisible(true); lineandshaperenderer.setDrawOutlines(true); lineandshaperenderer.setUseFillPaint(true); lineandshaperenderer.setBaseFillPaint(Color.white); lineandshaperenderer.setSeriesStroke(0, new BasicStroke(3.0F)); lineandshaperenderer.setSeriesOutlineStroke(0, new BasicStroke(2.0F)); lineandshaperenderer.setSeriesShape(0, new Ellipse2D.Double(-5.0, -5.0, 10.0, 10.0)); lineandshaperenderer.setItemMargin(0.4); //Set the spacing of each value of the x-axis (not working??) // Show data value DecimalFormat decimalformat1 = new DecimalFormat("##.##");// The format of data point displaying data values lineandshaperenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator( "{2}", decimalformat1));// Set the generator of data item label lineandshaperenderer.setBaseItemLabelsVisible(true);// The basic item label display lineandshaperenderer.setBaseShapesFilled(true);// Show solid small icons at the data point return jfreechart; } public static JPanel createDemoPanel() { JFreeChart jfreechart = createChart(createDataset()); try { ChartUtilities.saveChartAsJPEG( new File("D:/LineChartDemo1.png"), //The physical path to save the file includes the path and file name// 1.0f, //Image quality, 0.0f~1.0f jfreechart, //Chart object 1024, //Image width, this will determine whether the horizontal axis value of the chart can be fully displayed or ellipsis 768); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //Image height return new ChartPanel(jfreechart); } public static void main(String[] strings) { LineChartDemo1 linechartdemo1 = new LineChartDemo1( "JFreeChart - Line Chart Demo 1"); linechartdemo1.pack(); RefineryUtilities.centerFrameOnScreen(linechartdemo1); linechartdemo1.setVisible(true); } /* synthetic */ static Class class$(String string) { Class var_class; try { var_class = Class.forName(string); } catch (ClassNotFoundException classnotfoundexception) { throw new NoClassDefFoundError(classnotfoundexception.getMessage()); } return var_class; } }Running results:
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.