For reference: https://blog.csdn.net/qq263229365/article/details/9089975
I refer to the author's, maybe because I want to do it with springboot + thmmeleaf, so some codes are different, so I wrote this text, hoping to be useful to you.
Step 1: Guide Package
In springboot, you only need to add the
<dependency> <groupId>org.jfree</groupId> <artifactId>jfreechart</artifactId> <version>1.0.19</version> </dependency> <dependency> <groupId>org.jfree</groupId> <artifactId>jfreechart</artifactId> <version>1.0.19</version> </dependency>
Import thymeleaf and freechart's package
Step 2: add a method to the class with main method in the project
//DisplayChart image generation object and set the URL to be accessed @Bean public ServletRegistrationBean MyServlet() { return new ServletRegistrationBean<>(new DisplayChart(),"/chart"); } @RequestMapping("/") public String index() { return "index"; }
Note that @ Controller annotation should be added before the class.
Step 3: create the controller class
This is a class of line graphs
import java.awt.Font; import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.jfree.chart.ChartFactory; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.CategoryAxis; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.servlet.ServletUtilities; import org.jfree.chart.title.LegendTitle; import org.jfree.data.category.DefaultCategoryDataset; @Controller public class makeLineAndShapeChartController { @RequestMapping("makeLineAndShapeChart") public String makeLineAndShapeChart(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException { // Define chart object data, data DefaultCategoryDataset dataset =createDataset(); JFreeChart chart = ChartFactory.createLineChart( "Broken line diagram", // chart title "time", // domain axis label "Sales volume(Million)", // range axis label dataset, // data PlotOrientation.VERTICAL, // orientation true, // include legend true, // tooltips false // urls ); CategoryPlot plot = chart.getCategoryPlot(); // Set icon font chart.getTitle().setFont(new Font("Song style", Font.BOLD, 22)); //Set font for horizontal axis CategoryAxis categoryAxis = plot.getDomainAxis(); categoryAxis.setLabelFont(new Font("Song style", Font.BOLD, 22));//x-axis title font categoryAxis.setTickLabelFont(new Font("Song style", Font.BOLD, 18));//x-axis scale font //The following two lines set the font for the legend LegendTitle legend = chart.getLegend(0); legend.setItemFont(new Font("Song style", Font.BOLD, 14)); //Set font for vertical axis NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setLabelFont(new Font("Song style" , Font.BOLD , 22)); //Set font for axis rangeAxis.setTickLabelFont(new Font("Song style" , Font.BOLD , 22)); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());//Remove the vertical axis font display incomplete rangeAxis.setAutoRangeIncludesZero(true); rangeAxis.setUpperMargin(0.20); rangeAxis.setLabelAngle(Math.PI / 2.0); // 6. Convert graphics to pictures and send them to the front desk String fileName = ServletUtilities.saveChartAsJPEG(chart, 700, 400, null, request.getSession()); String chartURL = request.getContextPath() + "/chart?filename=" + fileName; model.addAttribute("makeLineAndShapeChart", chartURL); return "makeLineAndShapeChart"; } // Generate data public static DefaultCategoryDataset createDataset() { DefaultCategoryDataset linedataset = new DefaultCategoryDataset(); // Name of each curve String [] series= {"Refrigerator","Color TV","Washing machine"}; // Horizontal axis name (column name) String [] month = {"1 month","2 month","3 month","4 month"}; //Specific data double [] num = {4,5,6,10,10,15,20,16,10,18,25,19};, int l1 = num.length/series.length; //4 int l2 = month.length; int j=0; for(int i=0;i<num.length;i++) { linedataset.addValue(num[i], series[i/l1], month[j]); j++; if(j==month.length) j=0; } return linedataset; } }
The following cycle is fixed, but I need to get data from the database. There are three parameter arrays, so I write a cycle, but the output is the same. If a fairy wants to use the database, she can transfer the data to the array.
Output on HTML page
This is the index home page
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <div style="text-align: center"> <p> jfreechart Generate line chart<br/> <a href="makeLineAndShapeChart">Broken line diagram</a> </p> </div> </body> </html>
Line chart HTML page
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <div style="text-align: center"> jfreechart Broken line diagram <br> <br> <img th:src="${makeLineAndShapeChart}" width=600 height=400 border=0 color=gray> </div> </body> </html>
Because I mainly use line chart, so I don't write anything else.
As it is, I sorted out this project that I wrote, and added common histogram, 3D histogram, multi group histogram, pie chart and line chart. I can also go to this link to download all complete codes.
Link address: https://download.csdn.net/download/yjl23332/11931374