本文参考链接:https://blog.csdn.net/qq263229365/article/details/9089975
参考了这个作者的,可能因为我是要拿Springboot+thymeleaf做的,所以有些代码不同,我就写了这篇文字,希望对大家有用。
在springboot中,只需要在pom.xml文件中加
org.jfree
jfreechart
1.0.19
org.jfree
jfreechart
1.0.19
将thymeleaf和就freechart的包导入
//DisplayChart图片生成对象,并设置访问的URL
@Bean
public ServletRegistrationBean MyServlet() {
return new ServletRegistrationBean<>(new DisplayChart(),"/chart");
}
@RequestMapping("/")
public String index() {
return "index";
}
注意,类前面还要加@Controller注解
这是折线图的类
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 {
// 定义图表对象数据,数据
DefaultCategoryDataset dataset =createDataset();
JFreeChart chart = ChartFactory.createLineChart(
"折线图", // chart title
"时间", // domain axis label
"销售额(百万)", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips
false // urls
);
CategoryPlot plot = chart.getCategoryPlot();
// 设置图示字体
chart.getTitle().setFont(new Font("宋体", Font.BOLD, 22));
//设置横轴的字体
CategoryAxis categoryAxis = plot.getDomainAxis();
categoryAxis.setLabelFont(new Font("宋体", Font.BOLD, 22));//x轴标题字体
categoryAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 18));//x轴刻度字体
//以下两行 设置图例的字体
LegendTitle legend = chart.getLegend(0);
legend.setItemFont(new Font("宋体", Font.BOLD, 14));
//设置竖轴的字体
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setLabelFont(new Font("宋体" , Font.BOLD , 22)); //设置数轴的字体
rangeAxis.setTickLabelFont(new Font("宋体" , Font.BOLD , 22));
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());//去掉竖轴字体显示不全
rangeAxis.setAutoRangeIncludesZero(true);
rangeAxis.setUpperMargin(0.20);
rangeAxis.setLabelAngle(Math.PI / 2.0);
// 6. 将图形转换为图片,传到前台
String fileName = ServletUtilities.saveChartAsJPEG(chart, 700, 400, null, request.getSession());
String chartURL = request.getContextPath() + "/chart?filename=" + fileName;
model.addAttribute("makeLineAndShapeChart", chartURL);
return "makeLineAndShapeChart";
}
// 生成数据
public static DefaultCategoryDataset createDataset() {
DefaultCategoryDataset linedataset = new DefaultCategoryDataset();
// 各曲线名称
String [] series= {"冰箱","彩电","洗衣机"};
// 横轴名称(列名称)
String [] month = {"1月","2月","3月","4月"};
//具体数据
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
下面这个循环本来是定死的,但是我需要用到从数据库中拿数据,又有三个参数数组,所以写了一个循环,但是输出的东西还是一样的,如有小仙女要用到数据库,可以把数据传到数组中。
这是index首页
Insert title here
jfreechart 生成折线图
折线图
折线图HTML页面
Insert title here
jfreechart 折线图
因为我主要要用到的就是折线图,所以其他的就不写了。
如实在有需要,我整理了下我写的这个项目,添加了常用的柱状图,3D柱状图,多组柱状图,饼状图和折线图 也可以去这个链接下载完整的所有代码
链接地址:https://download.csdn.net/download/yjl23332/11931374