文章目录
1. 图表报表简介
在大数据时代,人们需要对大量的数据进行分析,帮助用户或公司领导更直观的察觉差异,做出判断,减少时间成本,而在web项目中除了表格显示数据外,还可以通过图表来表现数据,这种图表形式表现数据使人看的清楚明白且更加直观。对于web项目展示图形报表使用最多的技术就是基于js的前端报表框架,目前前端市场使用最多的图形报表框架有:JfreeChart、ECharts、Highcharts、FusionCharts、amCharts等。
2. JFreechart简介
JFreeChart是JAVA平台上的一个开放的图表绘制类库。它完全使用JAVA语言编写,可生成饼图(pie charts)、柱状图(bar charts)、散点图(scatter plots)、时序图(time series)、甘特图(Gantt charts)等等多种图表,并且可以产生PNG和JPEG格式的输出,还可以与PDF或EXCEL关联。
3. 相关依赖
<dependency>
<groupId>org.jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.5.0</version>
</dependency>
4. 饼图&3D饼图
public static void main(String[] args) throws Exception{
//需求: 统计每个部门的人员
//1.准备数据: 技术部 180; 销售部 20; 人事部 10人
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("技术部",180);
dataset.setValue("销售部",20);
dataset.setValue("人事部",10);
//设置语言
StandardChartTheme chartTheme = new StandardChartTheme("CN");
//设置标题字体
chartTheme.setExtraLargeFont(new Font("华文宋体", Font.BOLD,20));
//设置图例字体
chartTheme.setRegularFont(new Font("华文宋体",Font.BOLD,15));
//设置轴向的字体 x,y轴
chartTheme.setLargeFont(new Font("华文宋体", Font.BOLD,20));
//应用主题样式
ChartFactory.setChartTheme(chartTheme);
//String title 大标题
// PieDataset dataset 数据集
// boolean legend 是否显示图例
// boolean tooltips 是否显示提示
// boolean urls 是否跳转
//平面图
//org.jfree.chart.JFreeChart chart = ChartFactory.createPieChart("各部门人数", dataset, true, false, false);
//3D图
org.jfree.chart.JFreeChart chart = ChartFactory.createPieChart3D("各部门人数", dataset, true, false, false);
ChartUtils.saveChartAsPNG(new File("d:\\chart1.png"),chart,400,300);
}
5. 折线图
public static void main(String[] args) throws Exception{
//需求: 每年各部门入职的人数
//1.准备数据: 技术部 180; 销售部 20; 人事部 10人
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(20,"技术部","2019年");
dataset.addValue(25,"技术部","2020年");
dataset.addValue(26,"技术部","2021年");
dataset.addValue(10,"人事部","2019年");
dataset.addValue(17,"人事部","2020年");
dataset.addValue(13,"人事部","2021年");
dataset.addValue(19,"销售部","2019年");
dataset.addValue(30,"销售部","2020年");
dataset.addValue(25,"销售部","2021年");
dataset.addValue(1,"产品部","2019年");
dataset.addValue(5,"产品部","2020年");
dataset.addValue(4,"产品部","2021年");
//设置语言
StandardChartTheme chartTheme = new StandardChartTheme("CN");
//设置标题字体
chartTheme.setExtraLargeFont(new Font("华文宋体", Font.BOLD,20));
//设置图例字体
chartTheme.setRegularFont(new Font("华文宋体",Font.BOLD,15));
//设置轴向的字体 x,y轴
chartTheme.setLargeFont(new Font("华文宋体", Font.BOLD,20));
//应用主题样式
ChartFactory.setChartTheme(chartTheme);
// String title 大标题
// String categoryAxisLabel X轴的说明
// String valueAxisLabel Y轴的说明
// CategoryDataset dataset
org.jfree.chart.JFreeChart chart = ChartFactory.createLineChart("公司人数","各部门","入职人数",dataset);
ChartUtils.saveChartAsPNG(new File("d:\\chart2.png"),chart,400,300);
}
6. 柱状图(条形图)
public static void main(String[] args) throws Exception{
//需求: 每年各部门入职的人数
//1.准备数据: 技术部 180; 销售部 20; 人事部 10人
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(20,"技术部","2019年");
dataset.addValue(25,"技术部","2020年");
dataset.addValue(26,"技术部","2021年");
dataset.addValue(10,"人事部","2019年");
dataset.addValue(17,"人事部","2020年");
dataset.addValue(13,"人事部","2021年");
dataset.addValue(19,"销售部","2019年");
dataset.addValue(30,"销售部","2020年");
dataset.addValue(25,"销售部","2021年");
dataset.addValue(1,"产品部","2019年");
dataset.addValue(5,"产品部","2020年");
dataset.addValue(4,"产品部","2021年");
//设置语言
StandardChartTheme chartTheme = new StandardChartTheme("CN");
//设置标题字体
chartTheme.setExtraLargeFont(new Font("华文宋体", Font.BOLD,20));
//设置图例字体
chartTheme.setRegularFont(new Font("华文宋体",Font.BOLD,15));
//设置轴向的字体 x,y轴
chartTheme.setLargeFont(new Font("华文宋体", Font.BOLD,20));
//应用主题样式
ChartFactory.setChartTheme(chartTheme);
// String title 大标题
// String categoryAxisLabel X轴的说明
// String valueAxisLabel Y轴的说明
// CategoryDataset dataset
org.jfree.chart.JFreeChart chart = ChartFactory.createBarChart("公司人数","各部门","入职人数",dataset);
ChartUtils.saveChartAsPNG(new File("d:\\chart3.png"),chart,400,300);
}
7. 显示到页面上
7.1 Controller代码
//显示图表
@GetMapping("/jfreeChar")
public void jfreeChar(HttpServletResponse response) throws IOException{
userService.jfreeChar(response);
}
7.2 Service代码
public void jfreeChar(HttpServletResponse response) throws IOException {
//需求: 每年各部门入职的人数显示到页面上
//1.准备数据: 技术部 180; 销售部 20; 人事部 10人
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(20,"技术部","2019年");
dataset.addValue(25,"技术部","2020年");
dataset.addValue(26,"技术部","2021年");
dataset.addValue(10,"人事部","2019年");
dataset.addValue(17,"人事部","2020年");
dataset.addValue(13,"人事部","2021年");
dataset.addValue(19,"销售部","2019年");
dataset.addValue(30,"销售部","2020年");
dataset.addValue(25,"销售部","2021年");
dataset.addValue(1,"产品部","2019年");
dataset.addValue(5,"产品部","2020年");
dataset.addValue(4,"产品部","2021年");
//设置语言
StandardChartTheme chartTheme = new StandardChartTheme("CN");
//设置标题字体
chartTheme.setExtraLargeFont(new Font("华文宋体", Font.BOLD,20));
//设置图例字体
chartTheme.setRegularFont(new Font("华文宋体",Font.BOLD,15));
//设置轴向的字体 x,y轴
chartTheme.setLargeFont(new Font("华文宋体", Font.BOLD,20));
//应用主题样式
ChartFactory.setChartTheme(chartTheme);
// String title 大标题
// String categoryAxisLabel X轴的说明
// String valueAxisLabel Y轴的说明
// CategoryDataset dataset
org.jfree.chart.JFreeChart chart = ChartFactory.createBarChart("公司人数","各部门","入职人数",dataset);
ChartUtils.writeChartAsPNG(response.getOutputStream(),chart,500,400);
}
7.3 HTML代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> 显示JFreeChart图片</title>
</head>
<body>
<!--后台地址-->
<img src="/user/jfreeChar" >
</body>
</html>
8. 总结
//设置饼状图
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("技术部",180);
//设置柱状图
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(20,"技术部","2019年");
//创建饼状图类 参数
JFreeChart chart = ChartFactory.createPieChart3D(大标题, 数据源, 是否显示图例, 是否显示提示, 是否跳转);
//创建柱状图类 参数
JFreeChart chart = ChartFactory.createBarChart(大标题,X轴的说明,Y轴的说明,数据源);
//设置语言
StandardChartTheme chartTheme = new StandardChartTheme("CN");
//设置标题字体
chartTheme.setExtraLargeFont(new Font("华文宋体", Font.BOLD,20));
//设置图例字体
chartTheme.setRegularFont(new Font("华文宋体",Font.BOLD,15));
//设置轴向的字体 x,y轴
chartTheme.setLargeFont(new Font("华文宋体", Font.BOLD,20));
//应用主题样式
ChartFactory.setChartTheme(chartTheme);
//输出到本地
ChartUtils.saveChartAsPNG(new File("d:\\chart1.png"),chart,400,300);
//输出到页面
ChartUtils.writeChartAsPNG(response.getOutputStream(),chart,500,400);