关于使用java+springboot+jfreeChart制作折线图

本文参考链接:https://blog.csdn.net/qq263229365/article/details/9089975
参考了这个作者的,可能因为我是要拿Springboot+thymeleaf做的,所以有些代码不同,我就写了这篇文字,希望对大家有用。

第一步:导包

在springboot中,只需要在pom.xml文件中加

<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>

将thymeleaf和就freechart的包导入

第二步:在项目中有main方法的类中添加方法

//DisplayChart图片生成对象,并设置访问的URL
	@Bean
	public ServletRegistrationBean MyServlet() {
		return new ServletRegistrationBean<>(new DisplayChart(),"/chart");
	}
	@RequestMapping("/")
	public String index() {
		return "index";
	}

注意,类前面还要加@Controller注解

第三步:创建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<num.length;i++) {
			 linedataset.addValue(num[i], series[i/l1], month[j]);	
			 j++;
			 if(j==month.length)
				 j=0;
		 }			 
		 return linedataset;
		}		
}

下面这个循环本来是定死的,但是我需要用到从数据库中拿数据,又有三个参数数组,所以写了一个循环,但是输出的东西还是一样的,如有小仙女要用到数据库,可以把数据传到数组中。

在HTML页面输出

这是index首页

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<div style="text-align: center">
        <p>
		   jfreechart 生成折线图<br/>                            
           <a href="makeLineAndShapeChart">折线图</a>
        </p>       
	</div>
</body>
</html>

折线图HTML页面

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div style="text-align: center">
		jfreechart 折线图 <br> <br> <img th:src="${makeLineAndShapeChart}"
			width=600 height=400 border=0 color=gray>
	</div>
</body>
</html>

因为我主要要用到的就是折线图,所以其他的就不写了。
如实在有需要,我整理了下我写的这个项目,添加了常用的柱状图,3D柱状图,多组柱状图,饼状图和折线图 也可以去这个链接下载完整的所有代码
链接地址:https://download.csdn.net/download/yjl23332/11931374

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值