jfree生成折线图插入Excel表格

本文介绍了如何在Java项目中利用JFreeChart库生成折线图,并将生成的图表插入到Excel表格中,包括添加相关依赖、创建折线图以及整合到Excel的过程。

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();
		}
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值