1. 添加依赖
<!-- https://mvnrepository.com/artifact/org.jfree/jfreechart -->
<dependency>
<groupId>org.jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.5.0</version>
</dependency>
2. 生成折线图
public File createChartLine(ArrayList<String[]> detail) {
//添加数据集
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for(int i=0; i<detail.size(); i++) {
//col value, line name, row value
dataset.addValue(Integer.valueOf(detail.get(i)[2]), "Detail",detail.get(i)[0]);
}
//创建折线图
JFreeChart chart = ChartFactory.createLineChart("曲线", "项", "数据",
dataset, PlotOrientation.VERTICAL, true, true, false);
//设置主标题字体
chart.getTitle().setFont(new Font("宋体", Font.BOLD, 18));
chart.getTitle().setPaint(ChartColor.RED);
//设置图例字体
chart.getLegend().setItemFont(new Font("宋体", Font.BOLD, 15));
// //设置图例显示位置
// chart.getLegend().setPosition(RectangleEdge.RIGHT);
//设置图例是否可见
chart.getLegend().setVisible(false);
//获取图表区域对象
CategoryPlot categoryPlot = (CategoryPlot) chart.getPlot();
//设置背景色
categoryPlot.setBackgroundPaint(ChartColor.WHITE);
//设置网格线色
categoryPlot.setRangeGridlinePaint(ChartColor.GRAY);
categoryPlot.setDomainGridlinePaint(ChartColor.GRAY);
categoryPlot.setNoDataMessage("没有数据");
// x轴分类轴网格是否可见
categoryPlot.setDomainGridlinesVisible(true);
// y轴数据轴网格是否可见
categoryPlot.setRangeGridlinesVisible(true);
//设置部分区域显示颜色
IntervalMarker inter = new IntervalMarker(0, 15);
// inter.setLabelOffsetType(LengthAdjustmentType.EXPAND);
inter.setPaint(Color.LIGHT_GRAY);
plot.addRangeMarker(inter,Layer.BACKGROUND);
//获取X轴对象
CategoryAxis categoryAxis = (CategoryAxis) categoryPlot.getDomainAxis();
//设置X轴坐标上的文字
categoryAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 13));
//设置X轴坐标标签颜色
categoryAxis.setLabelPaint(ChartColor.DARK_GRAY);
//设置X轴的标题文字
categoryAxis.setLabelFont(new Font("宋体", Font.BOLD, 15));
//设置X轴坐标文件显示方向
categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
//获取Y轴对象
NumberAxis numberAxis = (NumberAxis) categoryPlot.getRangeAxis();
//设置Y轴坐标上的文字
numberAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 15));
//设置Y轴坐标标签颜色
numberAxis.setLabelPaint(ChartColor.DARK_GRAY);
//设置Y轴的标题文字
numberAxis.setLabelFont(new Font("宋体", Font.BOLD, 15));
//处理Y轴上的刻度, 范围为0到100,间隔5
numberAxis.setAutoTickUnitSelection(false);
NumberTickUnit unit = new NumberTickUnit(5);
numberAxis.setTickUnit(unit);
numberAxis.setUpperBound(100D);
numberAxis.setLowerBound(0D);
// numberAxis.drawTickMarksAndLabels()
//获取绘图区域(折线)对象
LineAndShapeRenderer lineAndShapeRenderer = (LineAndShapeRenderer)categoryPlot.getRenderer();
// //设置折线加粗
// lineAndShapeRenderer.setSeriesStroke(0, new BasicStroke(3F));
//设置绘制线条的颜色,包括了外廓线的颜色
lineAndShapeRenderer.setSeriesPaint(0, ChartColor.LIGHT_RED);
lineAndShapeRenderer.setSeriesFillPaint(0, ChartColor.VERY_LIGHT_RED);//
//设置转折点是否可见
lineAndShapeRenderer.setDefaultShapesVisible(true);
// //设置折点的大小
// lineAndShapeRenderer.setSeriesOutlineStroke(0, new BasicStroke(3F));
//设置折点形状,Ellipse2D圆形 Rectangle2D正方形
lineAndShapeRenderer.setSeriesShape(0, new Ellipse2D.Double(-4.0D, -4.0D, 8.0D, 8.0D));
//使用数据点颜色填充
lineAndShapeRenderer.setUseFillPaint(true);
//开启显示外廓线
lineAndShapeRenderer.setDrawOutlines(true);
//让数值显示到页面上
lineAndShapeRenderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());
lineAndShapeRenderer.setDefaultItemLabelsVisible(true);
lineAndShapeRenderer.setDefaultItemLabelFont(new Font("宋体", Font.BOLD, 15));
//用时间作为文件名防止重名的问题
String filename = new SimpleDateFormat("yyyyMMddHHmmss").format((new Date()).getTime()) + ".png";
File file = new File(filename);
try {
//保存文件到工程目录下
ChartUtils.saveChartAsPNG(file,chart,600,400);
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
3. 插入excel
private void imageOut(XSSFWorkbook wb,XSSFSheet sheet,File file) {
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
BufferedImage bufferImg;
try {
bufferImg = ImageIO.read(file);
ImageIO.write(bufferImg, "png", byteArrayOut);
Drawing dp = sheet.createDrawingPatriarch();
//设置图表在excel中位置
XSSFClientAnchor anchor = new XSSFClientAnchor(300000,100000,0,0,5,0,15,25);
anchor.setAnchorType(AnchorType.DONT_MOVE_DO_RESIZE);
dp.createPicture(anchor,wb.addPicture(byteArrayOut.toByteArray(),wb.PICTURE_TYPE_PNG)).resize(0.8);
//插入后删除目录下图片文件
if(file.exists()){
file.delete();
}
} catch (IOException e) {
e.printStackTrace();
}
}
本文介绍了如何在Java项目中利用JFreeChart库生成折线图,并将生成的图表插入到Excel表格中,包括添加相关依赖、创建折线图以及整合到Excel的过程。

370

被折叠的 条评论
为什么被折叠?



