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

智能推荐

Android之设计模式DataSetObserver_android datasetobserver_每天涨姿势的博客-程序员宅基地

1.Receives call backs when a data set has been changed, or made invalid. The typically data sets that are observed areCursors orAdapters. DataSetObserver must be implemented by objects which are a_android datasetobserver

【开发教程5】AI语音人脸识别(会议记录仪/人脸打卡机)-定时器_efans_Mike的博客-程序员宅基地

【开发教程5】AI语音人脸识别(会议记录仪/人脸打卡机)-定时器

vue开发--生成token并保存到本地存储中_Rank92的博客-程序员宅基地

首先回顾一下token:token认证是RESTFUL.api的一个很重要的部分,通过token认证和token设置,后端会有一个接口传给前台:http://localhost/yiiserver/web/index.php/token?client_appid=aaa&client_appkey=bbb 其实就是向用户表里去..._到临时 code再调用后端登录接口得到 token 并保存本地和 globaldata中

Java CountDownLatch –出色的同步助手-程序员宅基地

Java CountDownLatch class is part of Concurrency API. It allows us to create a synchronization, where a thread waits until the count down is 0. It’s useful when we want a thread to wait on a finite nu...

随便推点

Nginx - 具体应用(部署静态资源-反向代理-负载均衡)_nginx代理其他服务器静态资源_鬼鬼骑士的博客-程序员宅基地

Nginx - 具体应用(部署静态资源-反向代理-负载均衡)_nginx代理其他服务器静态资源

CPU漏洞详解_宋宝华的博客-程序员宅基地

1. 导言性能测试对于 Linux 发行版来说至关重要,Alibaba Cloud Linux 2 也是如此。(Alibaba Cloud Linux 2 是阿里巴巴操作系统团队推出的一款..._cpu漏洞

Navicat premium 连接oracle数据库导入dmp文件过程_navicat premium 15 oracle 数据导入 需要sysdba角色_念。的博客-程序员宅基地

问题:解决办法:(需要进入到远程oracle服务器上赋值账号sysdba权限,操作如下)1)切换到oracle用户模式下: su - oracle  2)登录sqlplus:sqlplus xx/xx as sysdba (xx/xx :账号/密码)3)赋值权限:grant sysdba to xx; (xx:账号)Navicat premium 设置如下选择角色:sysdba..._navicat premium 15 oracle 数据导入 需要sysdba角色

自定义博客园皮肤 黑色主题_weixin_30535167的博客-程序员宅基地

自定义博客园皮肤 黑色主题我的css主要是参考这位博主的自定义博客园皮肤的博客。对他的代码做了一些简化,有一些我认为没有什么需要的就删去了。我选择的主题也是lessismore这个主题,记得在设置自定义的CSS的时候不要勾选 禁用模板默认CSS自定义CSS设置顶部导航栏的大小,并设置为横向一行设置home居中 即整体居中设置sideBar的宽度为15% 而不是固定的#navigator...

python字典的创建 访问 添加 修改 删除 序列解包_嘎嘎_哈的博客-程序员宅基地

字典的创建1,可以通过{ } 或者 dict() 函数创建字典对象>>> a = {"name": "pipi", "age": 17, "job": "teacher"}b = dict(name="pipi", age=17, job="teacher")a = dict([("name", "pipi"), ("age", 17), ("job", "te...

推荐文章

热门文章

相关标签