|
@@ -1,17 +1,24 @@
|
|
|
package com.css.simulation.resource.server.infra.util;
|
|
|
|
|
|
+import com.itextpdf.text.*;
|
|
|
+import com.itextpdf.text.Image;
|
|
|
+import com.itextpdf.text.pdf.BaseFont;
|
|
|
+import com.itextpdf.text.pdf.PdfPCell;
|
|
|
+import com.itextpdf.text.pdf.PdfPTable;
|
|
|
import lombok.SneakyThrows;
|
|
|
import org.jfree.chart.ChartFactory;
|
|
|
import org.jfree.chart.JFreeChart;
|
|
|
import org.jfree.chart.StandardChartTheme;
|
|
|
+import org.jfree.chart.labels.ItemLabelAnchor;
|
|
|
+import org.jfree.chart.labels.ItemLabelPosition;
|
|
|
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
|
|
|
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
|
|
|
import org.jfree.chart.plot.CategoryPlot;
|
|
|
import org.jfree.chart.plot.PiePlot;
|
|
|
import org.jfree.chart.plot.PlotOrientation;
|
|
|
import org.jfree.chart.renderer.category.BarRenderer;
|
|
|
-import org.jfree.chart.renderer.category.LineAndShapeRenderer;
|
|
|
import org.jfree.chart.renderer.category.StandardBarPainter;
|
|
|
+import org.jfree.chart.ui.TextAnchor;
|
|
|
import org.jfree.data.category.DefaultCategoryDataset;
|
|
|
import org.jfree.data.general.DefaultPieDataset;
|
|
|
|
|
@@ -19,14 +26,12 @@ import java.awt.*;
|
|
|
import java.text.DecimalFormat;
|
|
|
import java.text.NumberFormat;
|
|
|
|
|
|
-import com.itextpdf.text.Document;
|
|
|
-import com.itextpdf.text.Image;
|
|
|
-import com.itextpdf.text.PageSize;
|
|
|
import com.itextpdf.text.pdf.PdfWriter;
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
import org.jfree.chart.ChartUtils;
|
|
|
|
|
|
import java.io.*;
|
|
|
+import java.util.Arrays;
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -36,8 +41,6 @@ import java.io.*;
|
|
|
public class ChartUtil {
|
|
|
private static final Color[] BAR_COLORS = new Color[]{new Color(79, 129, 189), new Color(192, 80, 77), new Color(155, 187, 89),};
|
|
|
|
|
|
- public static final Color[] LINE_COLORS = new Color[]{new Color(237, 123, 46), new Color(90, 154, 213), new Color(164, 164, 164),};
|
|
|
-
|
|
|
private static final Color[] PIE_COLORS = new Color[]{new Color(75, 172, 198), new Color(128, 100, 162), new Color(155, 187, 89), new Color(192, 80, 77), new Color(79, 129, 189), new Color(44, 77, 117), new Color(247, 150, 70), new Color(165, 165, 165),};
|
|
|
|
|
|
|
|
@@ -57,39 +60,6 @@ public class ChartUtil {
|
|
|
return currentTheme;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 线图
|
|
|
- *
|
|
|
- * @param title 标题
|
|
|
- * @param categoryAxisLabel 分类标签
|
|
|
- * @param valueAxisLabel 数值标签
|
|
|
- * @param dataset 数据集
|
|
|
- * @return org.jfree.chart.JFreeChart
|
|
|
- * @author Hou_fx
|
|
|
- * @date 2021.8.4 10:39
|
|
|
- */
|
|
|
- public static JFreeChart lineChart(String title, String categoryAxisLabel, String valueAxisLabel, DefaultCategoryDataset dataset) {
|
|
|
- ChartFactory.setChartTheme(initChartTheme());
|
|
|
-
|
|
|
- JFreeChart chart = ChartFactory.createLineChart(title,// 图标题
|
|
|
- categoryAxisLabel,// x轴标题
|
|
|
- valueAxisLabel,// y轴标题
|
|
|
- dataset,//数据集
|
|
|
- PlotOrientation.VERTICAL,//图表方向
|
|
|
- true, true, false);
|
|
|
-
|
|
|
- CategoryPlot plot = chart.getCategoryPlot();
|
|
|
- LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
|
|
|
- // 折现点显示数值
|
|
|
- renderer.setDefaultItemLabelsVisible(true);
|
|
|
- renderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());
|
|
|
- // 更改线条颜色
|
|
|
- for (int i = 0; i < dataset.getRowKeys().size(); i++) {
|
|
|
- renderer.setSeriesPaint(i, LINE_COLORS[i]);
|
|
|
- }
|
|
|
- return chart;
|
|
|
- }
|
|
|
-
|
|
|
|
|
|
/**
|
|
|
* 柱状图
|
|
@@ -112,8 +82,20 @@ public class ChartUtil {
|
|
|
// 纯色显示
|
|
|
renderer.setBarPainter(new StandardBarPainter());
|
|
|
// 柱子上显示小数字
|
|
|
- renderer.setDefaultItemLabelsVisible(true);
|
|
|
+ renderer.setDefaultItemLabelsVisible(false);
|
|
|
renderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());
|
|
|
+ //设置不能在柱子上正常显示的那些数值的显示方式,将这些数值显示在柱子外面
|
|
|
+ ItemLabelPosition itemLabelPositionFallback = new ItemLabelPosition(
|
|
|
+ ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT,
|
|
|
+ TextAnchor.HALF_ASCENT_LEFT, -1.57D);
|
|
|
+
|
|
|
+ //设置不能正常显示的柱子label的position
|
|
|
+ renderer.setPositiveItemLabelPositionFallback(itemLabelPositionFallback);
|
|
|
+ renderer.setNegativeItemLabelPositionFallback(itemLabelPositionFallback);
|
|
|
+ // 显示每个柱的数值,并修改该数值的字体属性
|
|
|
+
|
|
|
+
|
|
|
+// renderer.setMaximumBarWidth(0.05);
|
|
|
// 设置柱子间隔
|
|
|
renderer.setItemMargin(0.0);
|
|
|
// 设置柱子颜色
|
|
@@ -144,7 +126,7 @@ public class ChartUtil {
|
|
|
// 设置扇区的线条颜色
|
|
|
plot.setDefaultSectionOutlinePaint(new Color(255, 255, 255));
|
|
|
// 设置扇区的线条大小
|
|
|
- plot.setDefaultSectionOutlineStroke(new BasicStroke(3));
|
|
|
+ plot.setDefaultSectionOutlineStroke(new BasicStroke(2));
|
|
|
// 设置标签颜色
|
|
|
plot.setLabelLinkPaint(new Color(255, 255, 255, 0));
|
|
|
// 设置标签背景色
|
|
@@ -181,16 +163,70 @@ public class ChartUtil {
|
|
|
|
|
|
// 饼图
|
|
|
ByteArrayOutputStream pieBos = new ByteArrayOutputStream();
|
|
|
- ChartUtils.writeChartAsJPEG(pieBos, ChartUtil.pieChart("饼图", getPieDataset()), 650, 240);
|
|
|
+ ChartUtils.writeChartAsJPEG(pieBos, ChartUtil.pieChart("复杂度等级分布图", getPieDataset()), 300, 188);
|
|
|
Image pieImage = Image.getInstance(pieBos.toByteArray());
|
|
|
- pieImage.scalePercent(60);
|
|
|
- doc.add(pieImage);
|
|
|
+// pieImage.scalePercent(80);
|
|
|
+// doc.add(pieImage);
|
|
|
// 饼图
|
|
|
ByteArrayOutputStream pieBos1 = new ByteArrayOutputStream();
|
|
|
- ChartUtils.writeChartAsJPEG(pieBos1, ChartUtil.pieChart("饼图2", getPieDataset()), 650, 240);
|
|
|
+ ChartUtils.writeChartAsJPEG(pieBos1, ChartUtil.pieChart("危险度等级分布图", getPieDataset()), 300, 188);
|
|
|
Image pieImage1 = Image.getInstance(pieBos1.toByteArray());
|
|
|
- pieImage1.scalePercent(60);
|
|
|
- doc.add(pieImage1);
|
|
|
+// pieImage1.scalePercent(80);
|
|
|
+// doc.add(pieImage1);
|
|
|
+
|
|
|
+ float[] floats = new float[2];
|
|
|
+ Arrays.fill(floats, 60);
|
|
|
+ PdfPTable table = new PdfPTable(floats);
|
|
|
+ table.setTotalWidth(600);
|
|
|
+ table.setLockedWidth(true);
|
|
|
+ table.setHorizontalAlignment(Element.ALIGN_CENTER);
|
|
|
+ table.getDefaultCell().setBorder(1);
|
|
|
+ PdfPCell cell = new PdfPCell(pieImage);
|
|
|
+ pieImage.scaleAbsolute(300, 188);
|
|
|
+ cell.setHorizontalAlignment(Element.ALIGN_CENTER);
|
|
|
+ cell.setBorder(com.itextpdf.text.Rectangle.NO_BORDER);
|
|
|
+ table.addCell(cell);
|
|
|
+
|
|
|
+ PdfPCell cell2 = new PdfPCell(pieImage1);
|
|
|
+ pieImage1.scaleAbsolute(300, 188);
|
|
|
+ cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
|
|
|
+ cell2.setBorder(com.itextpdf.text.Rectangle.NO_BORDER);
|
|
|
+ table.addCell(cell2);
|
|
|
+ doc.add(table);
|
|
|
+ BaseFont font = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
|
|
|
+ Paragraph elements2 = new Paragraph(40, " ", new com.itextpdf.text.Font(font, 14));
|
|
|
+
|
|
|
+ doc.add(elements2);
|
|
|
+
|
|
|
+ ByteArrayOutputStream pieBos2 = new ByteArrayOutputStream();
|
|
|
+ ChartUtils.writeChartAsJPEG(pieBos2, ChartUtil.barChart("复杂度分布图", null, "复杂度", getDataSet()), 300, 188);
|
|
|
+ Image pieImage3 = Image.getInstance(pieBos2.toByteArray());
|
|
|
+
|
|
|
+ ByteArrayOutputStream pieBos3 = new ByteArrayOutputStream();
|
|
|
+ ChartUtils.writeChartAsJPEG(pieBos3, ChartUtil.barChart("危险度分布图", null, "危险度", getDataSet()), 300, 188);
|
|
|
+ Image pieImage4 = Image.getInstance(pieBos3.toByteArray());
|
|
|
+
|
|
|
+
|
|
|
+ float[] floats1 = new float[2];
|
|
|
+ Arrays.fill(floats1, 60);
|
|
|
+ PdfPTable table1 = new PdfPTable(floats1);
|
|
|
+ table1.setTotalWidth(600);
|
|
|
+ table1.setLockedWidth(true);
|
|
|
+ table1.setHorizontalAlignment(Element.ALIGN_CENTER);
|
|
|
+ table1.getDefaultCell().setBorder(1);
|
|
|
+ PdfPCell cell1 = new PdfPCell(pieImage3);
|
|
|
+ pieImage3.scaleAbsolute(300, 188);
|
|
|
+ cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
|
|
|
+ cell1.setBorder(com.itextpdf.text.Rectangle.NO_BORDER);
|
|
|
+ table1.addCell(cell1);
|
|
|
+
|
|
|
+ PdfPCell cell3 = new PdfPCell(pieImage4);
|
|
|
+ pieImage4.scaleAbsolute(300, 188);
|
|
|
+ cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
|
|
|
+ cell3.setBorder(com.itextpdf.text.Rectangle.NO_BORDER);
|
|
|
+ table1.addCell(cell3);
|
|
|
+ doc.add(table1);
|
|
|
+
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
@@ -203,6 +239,17 @@ public class ChartUtil {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ public static DefaultCategoryDataset getDataSet() {
|
|
|
+ DefaultCategoryDataset dataset = new DefaultCategoryDataset();
|
|
|
+ dataset.addValue(50, "场景名称", "1");
|
|
|
+ dataset.addValue(40, "场景名称", "2");
|
|
|
+ dataset.addValue(23, "场景名称", "3");
|
|
|
+ dataset.addValue(24, "场景名称", "4");
|
|
|
+ dataset.addValue(11, "场景名称", "5");
|
|
|
+ dataset.addValue(55, "场景名称", "6");
|
|
|
+ return dataset;
|
|
|
+ }
|
|
|
+
|
|
|
public static DefaultPieDataset getPieDataset() {
|
|
|
DefaultPieDataset dataset = new DefaultPieDataset();
|
|
|
dataset.setValue("S ", 2);
|