MySQL绘制POI的实体图_poi 画图工具类_夜看满天繁星的博客-程序员宅基地

技术标签: MySQL绘制POI的实体图  

package com.kehua.framework.utils;

import java.io.FileOutputStream;

import org.apache.poi.ss.usermodel.Cell;

import org.apache.poi.ss.usermodel.Row;

import org.apache.poi.ss.util.CellRangeAddress;

import org.apache.poi.xddf.usermodel.PresetColor;

import org.apache.poi.xddf.usermodel.XDDFColor;

import org.apache.poi.xddf.usermodel.XDDFLineProperties;

import org.apache.poi.xddf.usermodel.XDDFShapeProperties;

import org.apache.poi.xddf.usermodel.XDDFSolidFillProperties;

import org.apache.poi.xddf.usermodel.chart.*;

import org.apache.poi.xssf.usermodel.XSSFChart;

import org.apache.poi.xssf.usermodel.XSSFClientAnchor;

import org.apache.poi.xssf.usermodel.XSSFDrawing;

import org.apache.poi.xssf.usermodel.XSSFSheet;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class DrawXlsxUtil {

/**

* 根据sheet数据画图(单序列)

* caution:文件打开关闭由外部操作

* @param sourceSheet 数据源sheet表(获取数据的表)

* @param distSheet 目标sheet表(画图的表)

* @param anchorPosition 画板位置

* @param chartTitle 图名称

* @param xAxisTitle x轴标识

* @param yAxisTitle y轴标识

* @param xAxisDataRangeAddress x轴数据位置(根据sheet获取)

* @param yAxisDataRangeAddress y轴数据位置

* @param chartType 图样式:暂支持折线、条形

* @param color 颜色

*/

public static void createDataChart(XSSFSheet sourceSheet, XSSFSheet distSheet, AnchorPosition anchorPosition,

String chartTitle, String xAxisTitle, String yAxisTitle,

CellRangeAddress xAxisDataRangeAddress, CellRangeAddress yAxisDataRangeAddress,

ChartTypes chartType, PresetColor color) {

if (!(ChartTypes.BAR.equals(chartType) || ChartTypes.LINE.equals(chartType))) return;

//Create a panel to draw

XSSFDrawing drawing = distSheet.createDrawingPatriarch();

XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0,

anchorPosition.getCol1Index(), anchorPosition.getRow1Index(),

anchorPosition.getCol2Index(), anchorPosition.getRow2Index());

XSSFChart chart = drawing.createChart(anchor);

// Use a category axis for the bottom axis.

XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);

chart.setTitleText(chartTitle);

bottomAxis.setTitle(xAxisTitle);

XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);

leftAxis.setTitle(yAxisTitle);

leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);

leftAxis.setCrossBetween(AxisCrossBetween.BETWEEN);

XDDFCategoryDataSource xs = XDDFDataSourcesFactory.fromStringCellRange(sourceSheet, xAxisDataRangeAddress);

XDDFNumericalDataSource ys1 = XDDFDataSourcesFactory.fromNumericCellRange(sourceSheet, yAxisDataRangeAddress);

XDDFChartData data = chart.createData(chartType, bottomAxis, leftAxis);

XDDFChartData.Series series1 = data.addSeries(xs, ys1);

//line

if (data instanceof XDDFLineChartData) {

XDDFLineChartData.Series series = (XDDFLineChartData.Series)series1;

series.setSmooth(false);

series.setMarkerStyle(MarkerStyle.NONE);

}

//bar

if (data instanceof XDDFBarChartData) {

// in order to transform a bar chart into a column chart, you just need to change the bar direction

XDDFBarChartData bar = (XDDFBarChartData) data;

bar.setBarDirection(BarDirection.COL);

}

//don't delete the following line

series1.setTitle("picture", null);

chart.plot(data);

solidFillSeries(data, 0, color);

}

/**

* 设置图属性

* @param data 待画的数据

* @param index 数据序列编号 从0开始

* @param color 颜色

*/

private static void solidFillSeries(XDDFChartData data, int index, PresetColor color){

XDDFSolidFillProperties fill = new XDDFSolidFillProperties(XDDFColor.from(color));

XDDFChartData.Series series = data.getSeries().get(index);

XDDFShapeProperties properties = series.getShapeProperties();

if (properties == null) properties = new XDDFShapeProperties();

//line

if (data instanceof XDDFLineChartData) {

XDDFLineProperties line = new XDDFLineProperties();

line.setFillProperties(fill);

properties.setLineProperties(line);

}

//bar

if (data instanceof XDDFBarChartData) properties.setFillProperties(fill);

series.setShapeProperties(properties);

}

/**

* 画折线图调用参考

* @throws Exception

*/

private static void createLineChartDemo() throws Exception{

XSSFWorkbook wb = new XSSFWorkbook();

XSSFSheet sheet = wb.createSheet("linechart");

final int NUM_OF_ROWS = 3;

final int NUM_OF_COLUMNS = 10;

Row row;

Cell cell;

for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++) {

row = sheet.createRow((short) rowIndex);

for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {

cell = row.createCell((short) colIndex);

if (rowIndex == 0) cell.setCellValue(String.valueOf(colIndex * (rowIndex + 1.0)));

else cell.setCellValue(colIndex * (rowIndex + 1.0));

}

}

CellRangeAddress xAxisDataRangeAddress = new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1);

CellRangeAddress yAxisDataRangeAddress = new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1);

AnchorPosition anchorPosition = new AnchorPosition(5, 5, 15, 15);

createDataChart(sheet, sheet, anchorPosition, "Picture","Time(min)", "Power(kW)",

xAxisDataRangeAddress, yAxisDataRangeAddress, ChartTypes.LINE, PresetColor.YELLOW);

FileOutputStream fileOut = new FileOutputStream("D:/ooxml-chart.xlsx");

wb.write(fileOut);

}

/**

* 设置画板在sheet中的位置

*/

public static class AnchorPosition {

private int col1Index;//起始位置

private int row1Index;

private int col2Index;//结束位置

private int row2Index;

public AnchorPosition(int col1Index, int row1Index, int col2Index, int row2Index) {

this.col1Index = col1Index;

this.row1Index = row1Index;

this.col2Index = col2Index;

this.row2Index = row2Index;

}

public int getCol1Index() {

return col1Index;

}

public void setCol1Index(int col1Index) {

this.col1Index = col1Index;

}

public int getRow1Index() {

return row1Index;

}

public void setRow1Index(int row1Index) {

this.row1Index = row1Index;

}

public int getCol2Index() {

return col2Index;

}

public void setCol2Index(int col2Index) {

this.col2Index = col2Index;

}

public int getRow2Index() {

return row2Index;

}

public void setRow2Index(int row2Index) {

this.row2Index = row2Index;

}

}

}

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_36057373/article/details/113342966

智能推荐

TensorBoard可视化demo--summary/scalar/histogram/FileWriter_summary可视化_忧郁一休的博客-程序员宅基地

本程序基于tensorflow下对MNIST数据集进行识别的程序代码 修改得到. 主要就是为了实现TensorBoard的可视化,加入了summary data到event file中去,有summary.scalar和summary.histogram还有name_scope的应用,这些都是为了可视化程序运行完成后,在命令行执行tensorboard --logdir=/tmp/tensorfl_summary可视化

codeforces Vacations 动态规划 划水 (`- -)_u_codeforce vacation_Glasier的博客-程序员宅基地

http://codeforces.com/problemset/problem/698/A题目大意:瓦西亚同学每天将面对四种情况:0:健身房关门,没有编程比赛1:健身房关门,有编程比赛2:健身房开门,没有编程比赛3:健身房开门,有编程比赛求这n天当中,最少能休息几天。瓦西亚唯一的要求是:不能连续两天健身,或连续两天比赛。思路:瓦西亚同学每天有三种状..._codeforce vacation

中小型企业的上网行为管理设备推荐_weixin_33935777的博客-程序员宅基地

笔者所在公司使用的上网行为管理设备为深信服AC-M5100,此款设备功能强大,操作简便,并可做×××隧道,丰富的报表,笔者对此款上网行为管理设备非常满意,唯一认为不足的地方是此款设备价格不低,动辄几万块钱的购机款,再加上后续的设备维护和升级费用,不是一般的小企业能够负担的。笔者认为选择上网设备,还是根据公司的实际需要来选择设备,只有适合自己的才是最好的。 以..._小企业上网行为管理

信息系统项目管理师考试经验和心得_信项可以重点学习十大管理吗_闽江小张的博客-程序员宅基地

信息系统项目管理师考试经验和心得_信项可以重点学习十大管理吗

重磅!GitHub 全部源代码被泄露?_程序猿DD_的博客-程序员宅基地

点击上方蓝色“程序猿DD”,选择“设为星标”回复“资源”获取独家整理的学习资料!前言由于内部分析基础设施的容量已达到极限,总部设于俄亥俄州克利夫兰市的美国KeyBank银行于今年开始转向...

SHELL 三剑客之一AWK的使用_shell argind_酱g的博客-程序员宅基地

awk介绍awk是一个处理文本的编程语言工具,能用简短的程序处理输入文件、数据排序、计算以及生产报表等等。在Linux系统下默认awk是gawk,它是awk的GNU版本。可以通过命令查看应用的版本:ls -l /bin/awk基本的命令语法:awk option 'pattern {action}' filepattern表示AWK在数据中查找的内容,pattern参数可以是egrep正则表达式中..._shell argind

随便推点

WinForm开发浏览器,WebBrowser获取页面内容,如何解决中文乱码_weixin_30737363的博客-程序员宅基地

WinForm开发浏览器,WebBrowser获取页面内容,如何解决中文乱码 原文:WinForm开发浏览器,WebBrowser获取页面内容,如何解决中文乱码最近用C#写一个简单的浏览器,遇到一个小小的问题,即:在通过WebBrowser.DocumentText获取的页面内容是乱码?百度了N久,也没看到一个较好的解决办法。通过反复的实验,并..._winform chromiumwebbrowser获取内容

stm32使用usart1串口通信以及truestudio重定向printf踩的坑_stm32里ustra1接口掉不出来_码农不是农的博客-程序员宅基地

刚接触stm32,这些天一直被stm32的串口通信困扰,先是收不到数据,后来收到数据了但printf函数不能通过串口发送到上位机。问题来自于多方面,有硬件上的也有软件上的,我在这里总结一下,希望能对大家有帮助避免踩坑。我使用的是stm32f103rct6的板子,使用hal库在turestudio9.3环境下开发,主机是ubuntu18系统。硬件问题:1、有一个usb转ttl线有问题,短..._stm32里ustra1接口掉不出来

element开发 - 多个upload组件绑定同一函数的解决方法_多个el-upload上传如何共用一个成功回调函数_Shawyu_的博客-程序员宅基地

参考网上大神,终于可以少写几个函数了。涉及到闭包。html: <el-form :model="form" label-width="160px"> <el-form-item label="商品封面" style="height: 90px;"> <el-upload :action="uploadUrl"..._多个el-upload上传如何共用一个成功回调函数

fMRIPrep介绍_clancy_wu的博客-程序员宅基地

fMRIPrep是融合了FSL, AFNI, freesurfer,ants的pipeline。DPABISurf就是调用了fMRIPrep的workflow,基本可以这么说,DPABISurf的预处理都是fMRIPrep工作的。fMRIPrep的安装,可以看官网,https://fmriprep.org/en/stable/index.html也可以看看下面博主写的内容:https://zhuanlan.zhihu.com/p/148115966作者:QKmeansfMRIPrep的好处在于

Spring Data JPA - 如何创建查询(8)SpEL表达式_jpa spel表达式_Gavin 陈的博客-程序员宅基地

作者简介陈喆,现就职于中科院某研究所担任副研究员,专注于工业云平台、MES系统的设计与研发。内容来源:https://docs.spring.io/spring-data/jpa/docs/2.0.9.RELEASE/reference/html/#jpa.query.spel-expressions从Spring Data JPA 1.4版本开始,支持在@Query定义的查询中使..._jpa spel表达式

R语言使用RMySQL连接及读写Mysql数据库_r语言 dbname_weixin_41855225的博客-程序员宅基地

install.packages("RMySQL")library(RMySQL)help(package="RMySQL") #查看说明文档#创建数据库连接con &lt;- dbConnect(MySQL(), host="", dbname="", user="", password="")summary(con) #获取连接信息dbGetInfo(con) #获取_r语言 dbname

推荐文章

热门文章

相关标签