文章目录

  • itextpdf 表格跨行跨列与可视化图表
    • 效果图
      • 普通表格一(表头背景色)
      • 普通表格二 (隔列变色)
      • 表格跨行跨列
      • 可视化图表
    • 使用示例
      • 普通表格一(表头背景色)
      • 普通表格二 (隔列变色)
      • 表格跨行跨列
      • 可视化图表
    • 工具类源码
      • 依赖包
      • PdfUtil
      • GetData
      • RowData
      • TableDataUtil
      • ChartUtil

itextpdf 表格跨行跨列与可视化图表

效果图

普通表格一(表头背景色)

普通表格二 (隔列变色)

表格跨行跨列

可视化图表

饼图

柱状图

折线图

使用示例

普通表格一(表头背景色)

PdfUtil.addLevel2Title(doc, "对外投资信息");
PdfUtil.Table(7, textFont, new TableDataUtil().add(RowData.add("序号", "名称", "法定代表人", "注册资金", "注册号", "成立日期", "地址")).addList(GetData.get(desensitization, result, Compoent.INVESTMENTLIST, "NO", "InvestName", "Legal", "RegCapital", "EntRegNo", "EstDate", "RegAddress")).toList(), doc);

普通表格二 (隔列变色)

PdfUtil.addLevel2Title(doc, "社保基本信息");
PdfUtil.TableBuleColspan(4, textFont, new TableDataUtil().add(RowData.add("社保单位编号", GetData.get(result, Compoent.INHFSOCIALSECURITY, "SocialSecurityNo", desensitization), "投保起始年", GetData.get(result, Compoent.INHFSOCIALSECURITY, "InsuredYear"))).add(RowData.add("投保起始月", GetData.get(result, Compoent.INHFSOCIALSECURITY, "InsuredMonth"), "当前状态", GetData.get(result, Compoent.INHFSOCIALSECURITY, "CurrentState"))).add(RowData.add("参保总人数", GetData.get(result, Compoent.INHFSOCIALSECURITY, "TotalCNT"), "养老参保人数", GetData.get(result, Compoent.INHFSOCIALSECURITY, "EICNT"))).add(RowData.add("医疗参保人数", GetData.get(result, Compoent.INHFSOCIALSECURITY, "MICNT"), "工伤参保人数", GetData.get(result, Compoent.INHFSOCIALSECURITY, "IICNT"))).add(RowData.add("失业参保人数", GetData.get(result, Compoent.INHFSOCIALSECURITY, "UICNT"))).toList(), doc, new int[]{5}, new int[]{2}, new int[]{3});

表格跨行跨列

PdfUtil.TableBlueColRowSpan(4, textFont, new TableDataUtil().add(RowData.add("科目", "报表年份")).add(balanceSheetKeys).addList(RowData.add(balanceSheetDataGroup, (data) -> {List<List<String>> res = new ArrayList<>();data.forEach((k, v) -> {List<String> row = new ArrayList<>();row.add(k);Map<String, String> kv = v.stream().collect(Collectors.toMap(key -> String.valueOf(key.get("PeriodYear")), value -> String.valueOf(value.get("ClosingBalanceAsset"))));balanceSheetKeysGroup.keySet().forEach(i -> {row.add(Optional.ofNullable(kv.get(i)).orElse(""));});if (row.size() < 4) {for (int i = row.size() - 1; i < 3; i++) {row.add("");}}res.add(row);});return res;})).toList(), doc, new int[][]{{1}, {1}}, new int[][]{{2}, {1}}, new int[][]{{3}, {2}});

可视化图表

饼图

PdfPTable table = new PdfPTable(2);
table.setTotalWidth(530);
table.setLockedWidth(true);PdfPCell leftImage = new PdfPCell();bos = new ByteArrayOutputStream();
ChartUtils.writeChartAsJPEG(bos, ChartUtil.pieChart(String.join("", balanceSheetChartDataGroup.keySet()) + "资产结构", balanceSheetPieData), 400, 400);
image = Image.getInstance(bos.toByteArray());
image.scalePercent(60);
leftImage.addElement(image);
leftImage.disableBorderSide(-1);
table.addCell(leftImage);

柱状图

imageBaos = new ByteArrayOutputStream();
ChartUtils.writeChartAsJPEG(imageBaos, ChartUtil.barChart("", "", "", balanceSheetBarChartDataset, "{2}%"), 850, 330);
chartImage = Image.getInstance(imageBaos.toByteArray());
chartImage.scalePercent(60);
doc.add(chartImage);

折线图

ChartUtils.writeChartAsJPEG(imageBaos, ChartUtil.lineChart("用水量(立方)", "", "", waterLineDataset, CategoryLabelPositions.UP_90), 850, 430);
chartImage = Image.getInstance(imageBaos.toByteArray());
chartImage.scalePercent(60);
doc.add(chartImage);

工具类源码

依赖包

  • itextpdf 5.5.6
  • jfreechart 1.5.3

大家根据实际使用场景删除代码

PdfUtil

static class PdfUtil {public static void addLevel1Title(Document doc, String title) throws DocumentException {Paragraph p1 = new Paragraph(title, secondTitleFont);p1.setAlignment(Element.ALIGN_CENTER);doc.add(p1);}public static void addLevel2Title(Document doc, String title) throws DocumentException {Paragraph p1 = new Paragraph(title, threeTitleFont);p1.setAlignment(Element.ALIGN_LEFT);p1.setSpacingAfter(8);doc.add(p1);}public static void addOtherContent(Document doc, String content, int alignment, Font fontSize, float spacingAfter) throws DocumentException {Paragraph p1 = new Paragraph(content, fontSize);p1.setAlignment(alignment);p1.setSpacingAfter(spacingAfter);doc.add(p1);}public static void addLevel2Title(Document doc, String title, boolean newPage) throws DocumentException {doc.newPage();Paragraph p1 = new Paragraph(title, threeTitleFont);p1.setAlignment(Element.ALIGN_LEFT);p1.setSpacingAfter(8);doc.add(p1);}/*** 生成一个表格** @param total    总列数* @param textFont 字体* @param data     表格数据     X行    Y列* @param doc      PDF文档对象* @throws DocumentException* @author hou_fx*/public static void Table(int total, Font textFont, List<List<String>> data, Document doc) throws DocumentException {// 创建一个有N列的表格PdfPTable table = new PdfPTable(total);table.setPaddingTop(20);table.setSpacingAfter(20);table.setTotalWidth(530); //设置列宽table.setLockedWidth(true); //锁定列宽PdfPCell cell;for (int i = 0; i < data.size(); i++) {  //遍历数据行   每个数据行都是一个listIterator<String> it = data.get(i).iterator();while (it.hasNext()) {               //遍历每行数据,每个数据都是一个单元格String text = it.next();cell = new PdfPCell(new Phrase(text, textFont));//表头背景色if (i == 0) {cell = new PdfPCell(new Phrase(text, textFontBold));cell.setBackgroundColor(new BaseColor(199, 217, 241));}cell.setMinimumHeight(17); //设置单元格高度cell.setUseAscender(true); //设置可以居中cell.setHorizontalAlignment(i == 0 ? Element.ALIGN_CENTER : Element.ALIGN_LEFT); //表头居中,其余左对齐cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //设置垂直居中table.addCell(cell);}}doc.add(table);}public static void TableNoAfter(int total, Font textFont, List<List<String>> data, Document doc) throws DocumentException {// 创建一个有N列的表格PdfPTable table = new PdfPTable(total);table.setPaddingTop(20);table.setTotalWidth(530); //设置列宽table.setLockedWidth(true); //锁定列宽PdfPCell cell;for (int i = 0; i < data.size(); i++) {  //遍历数据行   每个数据行都是一个listIterator<String> it = data.get(i).iterator();while (it.hasNext()) {               //遍历每行数据,每个数据都是一个单元格String text = it.next();cell = new PdfPCell(new Phrase(text, textFont));//表头背景色if (i == 0) {cell = new PdfPCell(new Phrase(text, textFontBold));cell.setBackgroundColor(new BaseColor(199, 217, 241));}cell.setMinimumHeight(17); //设置单元格高度cell.setUseAscender(true); //设置可以居中cell.setHorizontalAlignment(i == 0 ? Element.ALIGN_CENTER : Element.ALIGN_LEFT); //表头居中,其余左对齐cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //设置垂直居中table.addCell(cell);}}doc.add(table);}public static void TableNoPadding(int total, Font textFont, List<List<String>> data, Document doc) throws DocumentException {// 创建一个有N列的表格PdfPTable table = new PdfPTable(total);table.setTotalWidth(530); //设置列宽table.setLockedWidth(true); //锁定列宽PdfPCell cell;for (int i = 0; i < data.size(); i++) {  //遍历数据行   每个数据行都是一个listIterator<String> it = data.get(i).iterator();while (it.hasNext()) {               //遍历每行数据,每个数据都是一个单元格String text = it.next();cell = new PdfPCell(new Phrase(text, textFont));cell.setMinimumHeight(17); //设置单元格高度cell.setUseAscender(true); //设置可以居中cell.setHorizontalAlignment(Element.ALIGN_LEFT); //表头居中,其余左对齐cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //设置垂直居中table.addCell(cell);}}doc.add(table);}public static void TableNoPaddingTop(int total, Font textFont, List<List<String>> data, Document doc) throws DocumentException {// 创建一个有N列的表格PdfPTable table = new PdfPTable(total);table.setPaddingTop(0.0f);table.setSpacingAfter(0.0f);table.setTotalWidth(530); //设置列宽table.setLockedWidth(true); //锁定列宽PdfPCell cell;for (int i = 0; i < data.size(); i++) {  //遍历数据行   每个数据行都是一个listIterator<String> it = data.get(i).iterator();while (it.hasNext()) {               //遍历每行数据,每个数据都是一个单元格String text = it.next();cell = new PdfPCell(new Phrase(text, textFont));if (i == 0) {cell = new PdfPCell(new Phrase(text, textFontBold));cell.setBackgroundColor(new BaseColor(199, 217, 241));}cell.setMinimumHeight(17); //设置单元格高度cell.setUseAscender(true); //设置可以居中//表头背景色cell.setHorizontalAlignment(i == 0 ? Element.ALIGN_CENTER : Element.ALIGN_LEFT); //表头居中,其余左对齐cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //设置垂直居中table.addCell(cell);}}doc.add(table);}public static int appearNumber(String srcText, String findText) {int count = 0;Pattern p = Pattern.compile(findText);Matcher m = p.matcher(srcText);while (m.find()) {count++;}return count;}/*** 生成一个表格** @param total    总列数* @param textFont 字体* @param data     表格数据     X行    Y列* @param doc      PDF文档对象* @throws DocumentException* @author hou_fx*/public static void TableBule(int total, Font textFont, List<List<String>> data, Document doc) throws DocumentException {// 创建一个有N列的表格PdfPTable table = new PdfPTable(total);table.setPaddingTop(20);table.setSpacingAfter(20);table.setTotalWidth(530); //设置列宽table.setLockedWidth(true); //锁定列宽PdfPCell cell;for (int i = 0; i < data.size(); i++) {  //遍历数据行   每个数据行都是一个listint count = 0;for (int j = 0; j < data.get(i).size(); j++) {cell = new PdfPCell(new Phrase(data.get(i).get(j), textFont));//第一个单元格背景色if (count % 2 == 0) {cell = new PdfPCell(new Phrase(data.get(i).get(j), textFontBold));cell.setBackgroundColor(new BaseColor(199, 217, 241));}cell.setMinimumHeight(17); //设置单元格高度cell.setUseAscender(true); //设置可以居中cell.setHorizontalAlignment(Element.ALIGN_LEFT); //左对齐cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //设置垂直居中table.addCell(cell);count++;}}doc.add(table);}/*** 生成一个表格** @param total    总列数* @param textFont 字体* @param data     表格数据     X行    Y列* @param doc      PDF文档对象* @param colspan  第几列* @param rowspan  第几行* @param number   跨几列* @throws DocumentException* @author hou_fx*/public static void TableBuleColspan(int total, Font textFont, List<List<String>> data, Document doc, int[] rowspan, int[] colspan, int[] number) throws DocumentException {// 创建一个有N列的表格PdfPTable table = new PdfPTable(total);table.setPaddingTop(20);table.setSpacingAfter(20);table.setTotalWidth(530); //设置列宽table.setLockedWidth(true); //锁定列宽PdfPCell cell;//数组下标int cos = 0;for (int i = 0; i < data.size(); i++) {  //遍历数据行   每个数据行都是一个listint count = 0;for (int j = 0; j < data.get(i).size(); j++) {cell = new PdfPCell(new Phrase(data.get(i).get(j), textFont));//第一个单元格背景色if (j % 2 == 0) {cell = new PdfPCell(new Phrase(data.get(i).get(j), textFontBold));cell.setBackgroundColor(new BaseColor(199, 217, 241)); //173,216,230}cell.setMinimumHeight(17); //设置单元格高度cell.setUseAscender(true); //设置可以居中if (cos < rowspan.length && i == rowspan[cos] - 1 && count == colspan[cos] - 1) {cell.setColspan(number[cos]);//跨单元格cos++;}cell.setHorizontalAlignment(Element.ALIGN_LEFT); //左对齐cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //设置垂直居中table.addCell(cell);count++;}}doc.add(table);}/*** 生成一个表格** @param total    总列数* @param textFont 字体* @param data     表格数据     X行    Y列* @param doc      PDF文档对象* @param colspan  第几列 [][] 0 跨列 1 跨行* @param rowspan  第几行 [][] 0 跨列 1 跨行* @param number   跨几列 [][] 0 跨列 1 跨行* @throws DocumentException* @author hou_fx*/public static void TableBlueColRowSpan(int total, Font textFont, List<List<String>> data, Document doc, int[][] rowspan, int[][] colspan, int[][] number) throws DocumentException {// 创建一个有N列的表格PdfPTable table = new PdfPTable(total);table.setPaddingTop(20);table.setSpacingAfter(20);table.setTotalWidth(530); //设置列宽table.setLockedWidth(true); //锁定列宽PdfPCell cell;for (int i = 0, cos = 0, ros = 0; i < data.size(); i++) {for (int j = 0; j < data.get(i).size(); j++) {cell = new PdfPCell(new Phrase(data.get(i).get(j), textFont));if (total != data.get(i).size()) {cell = new PdfPCell(new Phrase(data.get(i).get(j), textFontBold));cell.setBackgroundColor(new BaseColor(199, 217, 241)); //173,216,230}if (data.get(i).get(j).contains("合计") || data.get(i).get(j).contains("总计")) {cell.setBackgroundColor(new BaseColor(79, 129, 189)); //173,216,230}cell.setMinimumHeight(17); //设置单元格高度cell.setUseAscender(true); //设置可以居中cell.setHorizontalAlignment(Element.ALIGN_CENTER); //左对齐cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //设置垂直居中if (cos < rowspan[0].length && i == rowspan[0][cos] - 1 && j == colspan[0][cos] - 1) {cell.setColspan(number[0][cos]);cos++;}if (ros < rowspan[1].length && i == rowspan[1][ros] - 1 && j == colspan[1][ros] - 1) {cell.setRowspan(number[1][ros]);ros++;}table.addCell(cell);}}doc.add(table);}/*** 生成一个表格** @param total    总列数* @param textFont 字体* @param data     表格数据     X行    Y列* @param doc      PDF文档对象* @param colspan  第几列* @param rowspan  第几行* @param number   跨几列* @throws DocumentException* @author hou_fx*/public static void TableColspan(int total, Font textFont, List<List<String>> data, Document doc, int[] rowspan, int[] colspan, int[] number) throws DocumentException {// 创建一个有N列的表格PdfPTable table = new PdfPTable(total);table.setPaddingTop(20);table.setSpacingAfter(20);table.setTotalWidth(530); //设置列宽table.setLockedWidth(true); //锁定列宽PdfPCell cell;//数组下标int cos = 0;for (int i = 0; i < data.size(); i++) {  //遍历数据行   每个数据行都是一个listIterator<String> it = data.get(i).iterator();int count = 0;while (it.hasNext()) {               //遍历每行数据,每个数据都是一个单元格cell = new PdfPCell(new Phrase(it.next(), textFont));cell.setMinimumHeight(17); //设置单元格高度cell.setUseAscender(true); //设置可以居中if (cos < rowspan.length && i == rowspan[cos] - 1 && count == colspan[cos] - 1) {cell.setColspan(number[cos]);//跨单元格cos++;}cell.setHorizontalAlignment(Element.ALIGN_LEFT);cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //设置垂直居中table.addCell(cell);count++;}}doc.add(table);}public static void TableColspanNo(int total, Font textFont, List<List<String>> data, Document doc, int[] rowspan, int[] colspan, int[] number) throws DocumentException {// 创建一个有N列的表格PdfPTable table = new PdfPTable(total);//设置列宽table.setTotalWidth(530);//锁定列宽table.setLockedWidth(true);PdfPCell cell;//数组下标int cos = 0;for (int i = 0; i < data.size(); i++) {  //遍历数据行   每个数据行都是一个listIterator<String> it = data.get(i).iterator();int count = 0;while (it.hasNext()) {               //遍历每行数据,每个数据都是一个单元格cell = new PdfPCell(new Phrase(it.next(), textFont));cell.setMinimumHeight(17); //设置单元格高度cell.setUseAscender(true); //设置可以居中if (cos < rowspan.length && i == rowspan[cos] - 1 && count == colspan[cos] - 1) {cell.setColspan(number[cos]);//跨单元格cos++;}cell.setHorizontalAlignment(Element.ALIGN_LEFT);cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //设置垂直居中table.addCell(cell);count++;}}doc.add(table);}
}

GetData

static class GetData {private final static Map<String, String> desensitizationData = new LinkedHashMap<>();static {desensitizationData.put("EntName", "深圳******有限公司");desensitizationData.put("RegAddress", "深圳市**区******");desensitizationData.put("SocialCreditCode", "9***************1");desensitizationData.put("ESocialCreditCode", "9***************1");desensitizationData.put("EntRegNo", "1*******0");desensitizationData.put("Legal", "***");desensitizationData.put("EntOrgCode", "1********0");desensitizationData.put("ShareholderName", "***");desensitizationData.put("FDDBRXM", "***");desensitizationData.put("FDDBRSFZJHM", "440**************2");desensitizationData.put("FDDBRGDDH", "0755-*******");desensitizationData.put("FDDBRYDDH", "13********0");desensitizationData.put("MemberName", "***");desensitizationData.put("InvestName", "深圳******有限公司");desensitizationData.put("ChangeItem", "***变更");desensitizationData.put("ChangeBefore", "*****");desensitizationData.put("ChangeAfter", "********");desensitizationData.put("AccFundNo", "10*******3");desensitizationData.put("SocialSecurityNo", "1******0");desensitizationData.put("RestrictDesc", "******");desensitizationData.put("PatentName", "******");desensitizationData.put("PubNo", "******");desensitizationData.put("WorkName", "******");desensitizationData.put("RegNum", "******");desensitizationData.put("TrademarkName", "******");desensitizationData.put("RregCode", "******");desensitizationData.put("LicenseNo", "******");desensitizationData.put("CaseReason", "******");desensitizationData.put("CaseNo", "******");desensitizationData.put("CaseName", "******");desensitizationData.put("PunishContent", "******");desensitizationData.put("ViolationCaseNo", "*********");desensitizationData.put("projectName", "*********");desensitizationData.put("WebSite", "*********");desensitizationData.put("Tel", "*********");desensitizationData.put("Phone", "*********");desensitizationData.put("IDNo", "*********");desensitizationData.put("CaseCode", "*********");}public static String get(Map map, Compoent compoent, String key) {List data = Optional.ofNullable((List) map.get(compoent.getValue())).orElse(new ArrayList<>());IMap imap = new IMap();if (data.size() > 0) {if (data.get(0) instanceof JSONObject) {((JSONObject) data.get(0)).forEach(imap::put);}}return Optional.ofNullable(imap.get(key)).orElse("-");}public static String get(Map map, Compoent compoent, String key, boolean desensitization) {List data = Optional.ofNullable((List) map.get(compoent.getValue())).orElse(new ArrayList<>());IMap imap = new IMap();if (data.size() > 0) {if (data.get(0) instanceof JSONObject) {if (desensitization) {((JSONObject) data.get(0)).forEach((k, v) -> {String s = String.valueOf(v);imap.put(k, desensitizationData.getOrDefault(k, s));});} else {((JSONObject) data.get(0)).forEach(imap::put);}}}return Optional.ofNullable(imap.get(key)).orElse("-");}public static List<List<String>> get(Map result, Compoent compoent, String... keys) {List<List<String>> res = new ArrayList<>();Optional.ofNullable(result.get(compoent.getValue())).ifPresent(v -> {if (v instanceof ArrayList) {((ArrayList) v).forEach(o -> {List<String> data = new ArrayList<>();JSONObject item = ((JSONObject) o);for (String key : keys) {data.add(StringUtils.isEmpty(item.getString(key)) ? "-" : item.getString(key));}res.add(data);});}});if (res.size() == 0) {List<String> data = new ArrayList<>();for (int i = 0; i < keys.length; i++) {data.add("");}res.add(data);}return res;}public static List<List<String>> get(Boolean desensitization, Map result, Compoent compoent, String... keys) {List<List<String>> res = new ArrayList<>();Optional.ofNullable(result.get(compoent.getValue())).ifPresent(v -> {if (v instanceof ArrayList) {((ArrayList) v).forEach(o -> {List<String> data = new ArrayList<>();JSONObject item = ((JSONObject) o);for (String key : keys) {if (desensitization) {data.add(StringUtils.isEmpty(item.getString(key)) ? "-" : desensitizationData.get(key) == null ? item.getString(key) : desensitizationData.get(key));} else {data.add(StringUtils.isEmpty(item.getString(key)) ? "-" : item.getString(key));}}res.add(data);});}});if (res.size() == 0) {List<String> data = new ArrayList<>();for (int i = 0; i < keys.length; i++) {data.add("");}res.add(data);}return res;}public static List<List<String>> get(Map result, Predicate<JSONObject> predicate, Compoent compoent, String... keys) {List<List<String>> res = new ArrayList<>();Optional.ofNullable(result.get(compoent.getValue())).ifPresent(v -> {if (v instanceof ArrayList) {((ArrayList) v).stream().filter(predicate).forEach(o -> {List<String> data = new ArrayList<>();JSONObject item = ((JSONObject) o);for (String key : keys) {data.add(StringUtils.isEmpty(item.getString(key)) ? "-" : item.getString(key));}if (data.size() == 0) {List<String> row = new ArrayList<>();for (int i = 0; i < keys.length; i++) {row.add("");}data.addAll(row);}res.add(data);});}});return res;}public static List<Map> getListMap(Map result, Compoent compoent, String... keys) {List<Map> data = new ArrayList<>();Optional.ofNullable(result.get(compoent.getValue())).ifPresent(v -> {if (v instanceof ArrayList) {((ArrayList) v).forEach(o -> {JSONObject item = ((JSONObject) o);Map map = new LinkedHashMap(16);for (String key : keys) {map.put(key, StringUtils.isEmpty(item.getString(key)) ? "-" : item.getString(key));}data.add(map);});}});return data;}
}

RowData

static class RowData {public static List<String> add(String... cells) {return Arrays.asList(cells);}public static List<String> add(String cells, List<String> datas) {datas.add(0, cells);return datas;}public static List<String> add(String cells, Map<String, String> data, Function<Map<String, String>, List<String>> func) {List<String> res = func.apply(data);res.add(0, cells);return res;}public static List<List<String>> add(Map<String, List<Map>> data, Function<Map<String, List<Map>>, List<List<String>>> func) {List<List<String>> res = func.apply(data);return res;}
}

TableDataUtil

static class TableDataUtil {public List<List<String>> data = new ArrayList<>();public TableDataUtil add(List<String> rowData) {if (rowData != null && rowData.size() > 0) {data.add(rowData);}return this;}public TableDataUtil addList(List<List<String>> rowData) {data.addAll(rowData);return this;}public List<List<String>> toList() {return data;}public TableDataUtil add(Map<String, List<Map>> data, Function<Map<String, List<Map>>, List<List<String>>> res) {this.data.addAll(res.apply(data));return this;}
}

ChartUtil

static 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),new Color(128, 100, 162),new Color(75, 172, 198),new Color(247, 150, 70),new Color(119, 44, 42),new Color(77, 59, 98),new Color(182, 87, 8),};private static final Color[] LINE_COLORS = new Color[]{new Color(90, 154, 213),new Color(237, 123, 46),new Color(155, 187, 89),};private static final Color[] PIE_COLORS = new Color[]{new Color(79, 129, 189),new Color(192, 80, 77),new Color(155, 187, 89),new Color(128, 100, 162),new Color(75, 172, 198),new Color(247, 150, 70),new Color(119, 44, 42),new Color(77, 59, 98),new Color(182, 87, 8),};private static StandardChartTheme initChartTheme() {StandardChartTheme currentTheme = new StandardChartTheme("JFree");currentTheme.setLargeFont(new java.awt.Font("宋体", java.awt.Font.BOLD, 15));currentTheme.setRegularFont(new java.awt.Font("宋体", java.awt.Font.PLAIN, 13));currentTheme.setExtraLargeFont(new java.awt.Font("宋体", java.awt.Font.BOLD, 20));currentTheme.setPlotBackgroundPaint(new Color(255, 255, 204, 0));currentTheme.setPlotOutlinePaint(new Color(0, 0, 0, 0));currentTheme.setRangeGridlinePaint(new Color(78, 74, 74));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,valueAxisLabel,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++) {if (i > LINE_COLORS.length - 1) {renderer.setSeriesPaint(i, new Color(RandomUtils.nextInt(110, 200), RandomUtils.nextInt(110, 200), RandomUtils.nextInt(110, 200)));} else {renderer.setSeriesPaint(i, LINE_COLORS[i]);}}return chart;}public static JFreeChart lineChart(String title, String categoryAxisLabel, String valueAxisLabel, DefaultCategoryDataset dataset, CategoryLabelPositions position) {ChartFactory.setChartTheme(initChartTheme());JFreeChart chart = ChartFactory.createLineChart(title,categoryAxisLabel,valueAxisLabel,dataset,PlotOrientation.VERTICAL,true,true,false);CategoryPlot plot = chart.getCategoryPlot();plot.getDomainAxis().setCategoryLabelPositions(position);LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();renderer.setDefaultItemLabelsVisible(true);renderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());for (int i = 0; i < dataset.getRowKeys().size(); i++) {if (i > LINE_COLORS.length - 1) {renderer.setSeriesPaint(i, new Color(RandomUtils.nextInt(110, 200), RandomUtils.nextInt(110, 200), RandomUtils.nextInt(110, 200)));} else {renderer.setSeriesPaint(i, LINE_COLORS[i]);}}return chart;}/*** 柱状图** @param title* @param categoryAxisLabel* @param valueAxisLabel* @param dataset           数据集* @return org.jfree.chart.JFreeChart* @author Hou_fx* @date 2021.8.4 14:03*/public static JFreeChart barChart(String title, String categoryAxisLabel, String valueAxisLabel, DefaultCategoryDataset dataset) {ChartFactory.setChartTheme(initChartTheme());JFreeChart chart = ChartFactory.createBarChart(title,categoryAxisLabel,valueAxisLabel,dataset,PlotOrientation.VERTICAL,true,true,false);CategoryPlot plot = chart.getCategoryPlot();BarRenderer renderer = (BarRenderer) plot.getRenderer();renderer.setBarPainter(new StandardBarPainter());renderer.setDefaultItemLabelsVisible(true);renderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());renderer.setItemMargin(0.0);for (int i = 0; i < dataset.getRowKeys().size(); i++) {if (i > BAR_COLORS.length - 1) {renderer.setSeriesPaint(i, new Color(RandomUtils.nextInt(110, 200), RandomUtils.nextInt(110, 200), RandomUtils.nextInt(110, 200)));} else {renderer.setSeriesPaint(i, BAR_COLORS[i]);}}return chart;}public static JFreeChart barChart2(String title, String categoryAxisLabel, String valueAxisLabel, DefaultCategoryDataset dataset, DefaultCategoryDataset dataset2, String format2) {ChartFactory.setChartTheme(initChartTheme());JFreeChart chart = ChartFactory.createBarChart(title,categoryAxisLabel,valueAxisLabel,dataset,PlotOrientation.VERTICAL,true,true,false);CategoryPlot plot = chart.getCategoryPlot();NumberAxis axis = new NumberAxis();axis.setNumberFormatOverride(new DecimalFormat("#,##%"));plot.setRangeAxis(1, axis);plot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_RIGHT);plot.setDataset(1, dataset2);plot.mapDatasetToRangeAxis(1, 1);BarRenderer renderer2 = new BarRenderer();renderer2.setSeriesPaint(0, new Color(238, 8, 8, 0));renderer2.setDefaultItemLabelsVisible(true);renderer2.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator(format2, NumberFormat.getInstance()));ItemLabelPosition position1 = new ItemLabelPosition(ItemLabelAnchor.INSIDE12, TextAnchor.TOP_RIGHT);renderer2.setDefaultPositiveItemLabelPosition(position1);plot.setRenderer(1, renderer2);BarRenderer renderer = (BarRenderer) plot.getRenderer();renderer.setBarPainter(new StandardBarPainter());renderer.setDefaultItemLabelsVisible(true);renderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());renderer.setItemMargin(0.0);for (int i = 0; i < dataset.getRowKeys().size(); i++) {if (i > BAR_COLORS.length - 1) {renderer.setSeriesPaint(i, new Color(RandomUtils.nextInt(110, 200), RandomUtils.nextInt(110, 200), RandomUtils.nextInt(110, 200)));} else {renderer.setSeriesPaint(i, BAR_COLORS[i]);}}return chart;}public static JFreeChart barChart(String title, String categoryAxisLabel, String valueAxisLabel, DefaultCategoryDataset dataset, String format) {ChartFactory.setChartTheme(initChartTheme());JFreeChart chart = ChartFactory.createBarChart(title,categoryAxisLabel,valueAxisLabel,dataset,PlotOrientation.VERTICAL,true,true,false);CategoryPlot plot = chart.getCategoryPlot();BarRenderer renderer = (BarRenderer) plot.getRenderer();renderer.setBarPainter(new StandardBarPainter());renderer.setDefaultItemLabelsVisible(true);renderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator(format, NumberFormat.getInstance()));renderer.setItemMargin(0.0);for (int i = 0; i < dataset.getRowKeys().size(); i++) {if (i > BAR_COLORS.length - 1) {renderer.setSeriesPaint(i, new Color(RandomUtils.nextInt(110, 200), RandomUtils.nextInt(110, 200), RandomUtils.nextInt(110, 200)));} else {renderer.setSeriesPaint(i, BAR_COLORS[i]);}}return chart;}/*** 饼图** @param title* @param dataset* @return org.jfree.chart.JFreeChart* @author Hou_fx* @date 2021.8.4 14:04*/public static JFreeChart pieChart(String title, DefaultPieDataset<String> dataset) {ChartFactory.setChartTheme(initChartTheme());JFreeChart chart = ChartFactory.createPieChart(title,dataset,true,true,false);PiePlot plot = (PiePlot) chart.getPlot();for (int i = 0; i < dataset.getKeys().size(); i++) {if (i > PIE_COLORS.length - 1) {plot.setSectionPaint(dataset.getKey(i), new Color(RandomUtils.nextInt(110, 200), RandomUtils.nextInt(110, 200), RandomUtils.nextInt(110, 200)));} else {plot.setSectionPaint(dataset.getKey(i), PIE_COLORS[i]);}}plot.setDefaultSectionOutlinePaint(new Color(255, 255, 255));plot.setDefaultSectionOutlineStroke(new BasicStroke(3));//plot.setLabelLinkPaint(new Color(255, 255, 255, 0));plot.setLabelBackgroundPaint(new Color(255, 255, 255, 0));plot.setLabelOutlinePaint(new Color(255, 255, 255, 0));plot.setLabelShadowPaint(new Color(255, 255, 255, 0));plot.setShadowPaint(new Color(255, 255, 255, 0));//plot.setLabelGenerator(new StandardPieSectionLabelGenerator(("{0}{2}"), NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));plot.setLabelGenerator(new StandardPieSectionLabelGenerator(("{2}"), NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));return chart;}
}

itextpdf 表格跨行跨列与可视化图表相关推荐

  1. php表格最后一行跨列合并,php动态实现表格跨行跨列实现代码

    php动态实现表格跨行跨列实现代码 发布于 2015-01-28 14:56:14 | 203 次阅读 | 评论: 0 | 来源: 网友投递 PHP开源脚本语言PHP(外文名: Hypertext P ...

  2. HTML 表格跨行跨列

    HTML和CSS第一天 8.8跨行跨列表格(次重点,必须掌握) <!DOCTYPE html> <html lang="en"> <head>& ...

  3. JavaWeb 表格跨行跨列

    <!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8" ...

  4. itextpdf 表格生成与跨行跨列

    itextpdf 表格跨行跨列操作 由于前段时间需要做需求接触pdf表格,表格需要跨行跨列操作,写了个工具类,代码如下: static class PdfUtil {/*** 生成一个表格* @aut ...

  5. html跨行跨列学生报名表,HTML表格跨行、跨列操作(rowspan、colspan)

    一般使用 元素的colspan属性来实现单元格跨列操作,使用元素的rowspan属性来实现单元格的跨行操作. colspan属性规定单元格可横跨的列数,所有浏览器都支持colspan属性.其取值为nu ...

  6. 小程序跨行跨列多列复杂表格实现

    今天来实现个跨行跨列多列表格. 如图,这是个列数不确定,有的单元格还要跨行跨列的复杂表格. 这里暂时最多支持4列,列数再多就放不下了. 实现原理 实现原理比较简单,通过多个嵌套的循环将数据取出. 上面 ...

  7. 【尚硅谷 Java Web 笔记】表格的跨行跨列

    colspan 属性设置跨列 rowspan 属性设置跨行 <!DOCTYPE html><html lang="en"><head><m ...

  8. php 中如何设置单元格跨行,HTML表格中单元格跨行跨列

    HTML表格中单元格跨行跨列 对于标准的表格,每一行的单元格 数量是一样的.但在实际使用中,经常会遇到跨行跨列的表格,这个时候,每一行的数量就不一样了. 一.定义 所谓"跨行",是 ...

  9. latex表格 分行 跨行跨列

    某个表格的单元格中 文字太长 需要分行显示的方法 如分成3行显示 添加命令 --------  \newcommand{\tabincell}[2]{\begin{tabular}{@{}#1@{}} ...

最新文章

  1. matlab 迭代 混沌与分形实验报告,实验四 函数的迭代混沌与分形.doc
  2. LeetCode week252
  3. 彩虹背光:芝奇推出KM570 RGB机械游戏键盘
  4. vue拖动添加模块展示_vue-quill-editor的增强模块,提供图片上传,复制插入,拖拽插入...
  5. 情人节脱单秘诀,程序员表白的情话大盘点!| CSDN 博文精选
  6. php 页面异步刷新,php+jQuery+Ajax简单实现页面异步刷新
  7. HALCON:Optical Flow(光流)
  8. 北科大计算机顺德,北京科技大学顺德研究生院2020考研预调剂信息
  9. 微信 塞班版 服务器繁忙,再见了塞班!微信、QQ从此也不能登录了
  10. m3u8 ts文件利用系统工具COPY合并序列TS文件
  11. 记录在处理SIF数据中,遇到的一些问题及解决过程
  12. 一个Unity3D制作的坦克游戏——《燃烧的地平线》
  13. 今日头条,企鹅号,大鱼号,百家号,做搬运视频还赚钱吗?
  14. 怎样在电脑上录制ppt课件?如何录制课件讲解视频
  15. 第6-1课:A* 算法
  16. Vue + ElementUI 实现后台管理系统模板 -- 前端篇(一):搭建基本环境 配置环境
  17. 双网卡电脑如何设置同时上两个不同的网络
  18. SECTION 1 python核心编程 快速入门
  19. 【Shell案例】【awk匹配、grep查找文件内的字符串】6、去掉空行(删除空行)
  20. 短波爱好者如何增强短波收音机接收能力

热门文章

  1. 用Python画出好看的词云图(详解)
  2. 跟健康有关计算机的英语作文,电脑游戏与我们的健康英语作文
  3. java-几何图形计算器
  4. 社会网络分析之中心性
  5. ColorFinale Pro for mac(FCPX插件:专业分级调色插件)
  6. 京东校招编程题——小熊分苹果
  7. ESP32-cam网络开发(3)------传输图像数据(micropython)
  8. MATLAB裂缝检测识别
  9. [附源码]Python计算机毕业设计高校教材管理系统
  10. java_SSM高校教材征订管理系统