Commit 8b0ff038 by wangweidong

霍煤宏骏

parent 83d221b3
package com.patzn.cloud.service.lims.test;
import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import com.patzn.cloud.service.lims.common.HSSFWorkbookUtil;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
public class Line {
public static void main(String[] args) {
File file = new File("D://en//ccc.xlsx");
DefaultCategoryDataset mDataset = new DefaultCategoryDataset();
try {
InputStream io = new FileInputStream(file);
XSSFWorkbook xssfWorkbook = HSSFWorkbookUtil.getWorkbookByIO(io);
XSSFSheet sheet = xssfWorkbook.getSheetAt(2);
int xcol = 0;
int xrowBegin = 1;
int xrowEnd = sheet.getLastRowNum();
int ycol = 3;
int yrowBegin = 1;
int yrowEnd = sheet.getLastRowNum();
List<String> xList = new ArrayList<>();
List<String> yList = new ArrayList<>();
for (int i = xrowBegin; i < xrowEnd; i++) {
XSSFRow row = sheet.getRow(i);
if (row == null){
continue;
}
XSSFCell cell = row.getCell(xcol);
if (cell == null){
continue;
}
String xcalue = HSSFWorkbookUtil.getJavaValue(cell).toString();
xList.add(xcalue);
}
for (int i = yrowBegin; i < yrowEnd; i++) {
XSSFRow row = sheet.getRow(i);
if (row == null){
continue;
}
XSSFCell cell = row.getCell(ycol);
if (cell == null){
continue;
}
String ycalue = HSSFWorkbookUtil.getJavaValue(cell).toString();
yList.add(ycalue);
}
int size = xList.size()<yList.size()?xList.size():yList.size();
String[] a =new String[size];
String[] b =new String[size];
for (int i = 0; i < size; i++) {
a[i] = xList.get(i);
b[i] = yList.get(i);
mDataset.addValue(Double.parseDouble(xList.get(i)),"数据",yList.get(i));
}
}catch (Exception e){
}
StandardChartTheme mChartTheme = new StandardChartTheme("CN");
mChartTheme.setLargeFont(new Font("黑体", Font.BOLD, 20));
mChartTheme.setExtraLargeFont(new Font("宋体", Font.PLAIN, 15));
mChartTheme.setRegularFont(new Font("宋体", Font.PLAIN, 15));
ChartFactory.setChartTheme(mChartTheme);
JFreeChart mChart = ChartFactory.createLineChart(
"折线图",//图名字
"年份",//横坐标
"数量",//纵坐标
mDataset,//数据集
PlotOrientation.VERTICAL,
false, // 显示图例
true, // 采用标准生成器
false);// 是否生成超链接
CategoryPlot mPlot = (CategoryPlot)mChart.getPlot();
mPlot.setBackgroundPaint(Color.LIGHT_GRAY);
mPlot.setRangeGridlinePaint(Color.BLUE);//背景底部横虚线
mPlot.setOutlinePaint(Color.RED);//边界线
ChartFrame mChartFrame = new ChartFrame("折线图", mChart);
mChartFrame.pack();
mChartFrame.setVisible(true);
}
public static CategoryDataset GetDataset()
{
DefaultCategoryDataset mDataset = new DefaultCategoryDataset();
// mDataset.addValue(1, "First", "2013");
// mDataset.addValue(3, "First", "2014");
// mDataset.addValue(2, "First", "2015");
// mDataset.addValue(6, "First", "2016");
// mDataset.addValue(5, "First", "2017");
// mDataset.addValue(12, "First", "2018");
return mDataset;
}
}
package com.patzn.cloud.service.lims.test;
import org.apache.commons.lang.math.RandomUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFChart;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.drawingml.x2006.chart.*;
import org.openxmlformats.schemas.drawingml.x2006.main.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
public class TestDian {
public static void main(String[] args) throws Exception{
createScatterChart();
}
public static void createScatterChart() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet("散点图");
Row row;
Cell cell;
for (int r = 0; r < 10; r++) {
row = sheet.createRow(r);
cell = row.createCell(0);
cell.setCellValue("S" + r);
cell = row.createCell(1);
cell.setCellValue(RandomUtils.nextInt(2));
}
XSSFSheet sheet333 = wb.createSheet("哈哈哈");
XSSFDrawing drawing = sheet333.createDrawingPatriarch();
ClientAnchor anchor = drawing.createAnchor(13, 3, 25, 9, 0, 0, 6, 12);
XSSFChart chart = drawing.createChart(anchor);
chart.setTitleText("预选赛项目得分分布图");
chart.setAutoTitleDeleted(false);
CTChart ctChart = chart.getCTChart();
ctChart.addNewPlotVisOnly().setVal(true);
ctChart.addNewDispBlanksAs().setVal(STDispBlanksAs.Enum.forInt(2));
ctChart.addNewShowDLblsOverMax().setVal(false);
CTPlotArea ctPlotArea = ctChart.getPlotArea();
CTScatterChart scatterChart = ctPlotArea.addNewScatterChart();
scatterChart.addNewScatterStyle().setVal(STScatterStyle.LINE_MARKER);
scatterChart.addNewVaryColors().setVal(false);
scatterChart.addNewAxId().setVal(123456);
scatterChart.addNewAxId().setVal(123457);
CTCatAx ctCatAx = ctPlotArea.addNewCatAx();
ctCatAx.addNewAxId().setVal(123456);
CTScaling ctScaling = ctCatAx.addNewScaling();
ctScaling.addNewOrientation().setVal(STOrientation.MIN_MAX);
ctCatAx.addNewDelete().setVal(false);
ctCatAx.addNewAxPos().setVal(STAxPos.B);
ctCatAx.addNewCrossAx().setVal(123457);
ctCatAx.addNewTickLblPos().setVal(STTickLblPos.NEXT_TO);
CTTitle title =ctCatAx.addNewTitle();
CTTx tx = title.addNewTx();
CTTextBody rich = tx.addNewRich();
rich.addNewBodyPr(); // body properties must exist, but can be empty
CTTextParagraph para = rich.addNewP();
CTRegularTextRun r = para.addNewR();
r.setT("xaqaewqwq");
ctCatAx.setTitle(title);
CTValAx ctValAx = ctPlotArea.addNewValAx();
ctValAx.addNewAxId().setVal(123457);
CTScaling ctScaling1 = ctValAx.addNewScaling();
ctScaling1.addNewOrientation().setVal(STOrientation.MIN_MAX);
ctValAx.addNewDelete().setVal(false);
ctValAx.addNewAxPos().setVal(STAxPos.B);
ctValAx.addNewCrossAx().setVal(123456);
CTShapeProperties ctShapeProperties = ctValAx.addNewMajorGridlines().addNewSpPr();
CTLineProperties ctLineProperties = ctShapeProperties.addNewLn();
ctLineProperties.setW(9525);
ctLineProperties.setCap(STLineCap.Enum.forInt(3));
ctLineProperties.setCmpd(STCompoundLine.Enum.forInt(1));
ctLineProperties.setAlgn(STPenAlignment.Enum.forInt(1));
// 不显示Y轴上的坐标刻度线
ctValAx.addNewMajorTickMark().setVal(STTickMark.NONE);
ctValAx.addNewMinorTickMark().setVal(STTickMark.NONE);
ctValAx.addNewTickLblPos().setVal(STTickLblPos.NEXT_TO);
// 设置散点图内的信息
CTScatterSer ctScatterSer = scatterChart.addNewSer();
ctScatterSer.addNewIdx().setVal(0);
ctScatterSer.addNewOrder().setVal(0);
// 去掉连接线
ctPlotArea.getScatterChartArray(0).getSerArray(0).addNewSpPr().addNewLn().addNewNoFill();
// 设置散点图各图例的显示
CTDLbls ctdLbls = scatterChart.addNewDLbls();
ctdLbls.addNewShowVal().setVal(true);
ctdLbls.addNewShowLegendKey().setVal(false);
ctdLbls.addNewShowSerName().setVal(false);
ctdLbls.addNewShowCatName().setVal(false);
ctdLbls.addNewShowPercent().setVal(false);
ctdLbls.addNewShowBubbleSize().setVal(false);
// 设置标记的样式
CTMarker ctMarker = ctScatterSer.addNewMarker();
ctMarker.addNewSymbol().setVal(STMarkerStyle.Enum.forInt(3));
ctMarker.addNewSize().setVal((short) 5);
CTShapeProperties ctShapeProperties1 = ctMarker.addNewSpPr();
ctShapeProperties1.addNewSolidFill().addNewSchemeClr().setVal(STSchemeColorVal.Enum.forInt(5));
CTLineProperties ctLineProperties1 = ctShapeProperties1.addNewLn();
ctLineProperties1.setW(9525);
ctLineProperties1.addNewSolidFill().addNewSchemeClr().setVal(STSchemeColorVal.Enum.forInt(5));
CTAxDataSource ctAxDataSource = ctScatterSer.addNewXVal();
CTStrRef ctStrRef = ctAxDataSource.addNewStrRef();
ctStrRef.setF("散点图!$A$1:$A$100");
CTNumDataSource ctNumDataSource = ctScatterSer.addNewYVal();
CTNumRef ctNumRef = ctNumDataSource.addNewNumRef();
ctNumRef.setF("散点图!$B$1:$B$100");
System.out.println(ctChart);
FileOutputStream fileOut = new FileOutputStream("D:\\out.xlsx");
wb.write(fileOut);
fileOut.close();
wb.close();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment