package com.ruoyi.common.utils;
import org.apache.poi.util.Units;
import org.apache.poi.xddf.usermodel.chart.*;
import org.apache.poi.xwpf.usermodel.*;
import java.io.FileInputStream;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.List;

import org.apache.poi.xwpf.usermodel.XWPFChart;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSpacing;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STLineSpacingRule;



/**
 * @Description: poi工具类
 * @Date: 2022/10/11
 * @Author shuaihua zang
 */
public class BarChart
{
    public static void drawTable(XWPFDocument document, String[] xAxisData,Double[] yAxisData) throws Exception {
        /*int numOfPoints = categories.length;
        String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 0, 0));
        String valuesDataRangeA = chart.formatRange(new CellRangeAddress(1, numOfPoints, 1, 1));
        String valuesDataRangeB = chart.formatRange(new CellRangeAddress(1, numOfPoints, 2, 2));
        XDDFDataSource<String> categoriesData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange, 0);
        XDDFNumericalDataSource<Double> valuesDataA = XDDFDataSourcesFactory.fromArray(valuesA, valuesDataRangeA, 1);
        XDDFNumericalDataSource<Double> valuesDataB = XDDFDataSourcesFactory.fromArray(valuesB, valuesDataRangeB, 2);*/
        // 创建chart图表对象,抛出异常
        XWPFChart chart = document.createChart(15 * Units.EMU_PER_CENTIMETER, 10 * Units.EMU_PER_CENTIMETER);
        // 图表相关设置
        chart.setTitleText(""); // 图表标题

        chart.setTitleOverlay(false); // 图例是否覆盖标题
        //XDDFChartLegend legend = chart.getOrAddLegend();
        //legend.setPosition(LegendPosition.TOP); // 图例位置:上下左右
        // X轴(分类轴)相关设置
        XDDFCategoryAxis xAxis =chart.createCategoryAxis(AxisPosition.BOTTOM); // 创建X轴,并且指定位置
        xAxis.setTitle(""); // x轴标题
        XDDFCategoryDataSource xAxisSource = XDDFDataSourcesFactory.fromArray(xAxisData); // 设置X轴数据
        // Y轴(值轴)相关设置
        //chart.createValueAxis(AxisPosition.LEFT);
        XDDFValueAxis yAxis = chart.createValueAxis(AxisPosition.LEFT); // 创建Y轴,指定位置
        yAxis.setTitle("Area(hm²)"); // Y轴标题
        yAxis.setCrossBetween(AxisCrossBetween.BETWEEN); // 设置图柱的位置:BETWEEN居中
        XDDFNumericalDataSource<Double> yAxisSource = XDDFDataSourcesFactory.fromArray(yAxisData); // 设置Y轴数据
        ChartTypes chartTypes = ChartTypes.BAR;
        // 创建柱状图对象
        XDDFBarChartData barChart = (XDDFBarChartData) chart.createData(chartTypes, xAxis, yAxis);
        barChart.setBarDirection(BarDirection.COL); // 设置柱状图的方向:BAR横向,COL竖向,默认是BAR
        barChart.setVaryColors(false);
        // 加载柱状图数据集
        XDDFBarChartData.Series barSeries = (XDDFBarChartData.Series) barChart.addSeries(xAxisSource, yAxisSource);
        barSeries.setTitle("                                                                                                                                                                                                                                                                                             ", null); // 图例标题
        // 绘制柱状图
        chart.plot(barChart);
    }

    //插入图片
    public static void insertPic(XWPFDocument document,String fileAddress)
    {
        XWPFParagraph Paragraph = document.createParagraph();
        Paragraph.setAlignment(ParagraphAlignment.CENTER);//对齐方式
        XWPFRun run = Paragraph.createRun();
        try (FileInputStream is = new FileInputStream
                (fileAddress)) {
            run.addPicture(is, XWPFDocument.PICTURE_TYPE_PNG,
                    fileAddress,
                    Units.toEMU(185), Units.toEMU(223)); // 200x200 pixels
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //创建段落并插入文字
    public static void createParagraphAndInsertWord(XWPFDocument document,String date) {
        XWPFParagraph Paragraph = document.createParagraph();
        //设置行距固定值20磅
        //setLineSpace(Paragraph,30);
        Paragraph.setAlignment(ParagraphAlignment.LEFT);//对齐方式
        //paragraph_2.setFirstLineIndent(400);//首行缩进
        XWPFRun run = Paragraph.createRun();
        run.setText(date);
        run.setBold(false);//加粗
        run.setFontSize(12);
        run.setFontFamily("Times New Roman");
    }
    //创建一级标题的内容
    public static void createFirstLevelTopic(XWPFDocument document,String date)
    {
        XWPFParagraph Paragraph = document.createParagraph();
        Paragraph.setAlignment(ParagraphAlignment.LEFT);//对齐方式
        //BarChart.setLineSpace(Paragraph,30);
        XWPFRun run = Paragraph.createRun();
        run.setText(date);
        run.setBold(true);//加粗
        run.setFontSize(16);
        run.setFontFamily("Times New Roman");
    }
    //创建二级标题的内容
    public static void createSecondLevelTopic(XWPFDocument document,String date)
    {
        XWPFParagraph Paragraph = document.createParagraph();
        Paragraph.setAlignment(ParagraphAlignment.LEFT);//对齐方式
        //BarChart.setLineSpace(Paragraph,30);
        XWPFRun run = Paragraph.createRun();
        run.setText(date);
        run.setBold(true);//加粗
        run.setFontSize(12);
        run.setFontFamily("Times New Roman");
    }
    //创建图片标题的内容
    public static void setPicTitle(XWPFDocument document,String date)
    {
        XWPFParagraph Paragraph = document.createParagraph();
        Paragraph.setAlignment(ParagraphAlignment.CENTER);//对齐方式
        //BarChart.setLineSpace(Paragraph,30);
        XWPFRun run = Paragraph.createRun();
        run.setText(date);
        run.setBold(true);//加粗
        run.setFontSize(10);
        run.setFontFamily("Times New Roman");
    }
    //设置题目
    public static void setTitle(XWPFDocument document,String date)
    {
        XWPFParagraph title = document.createParagraph();
        title.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun runTitle = title.createRun();
        runTitle.setText(date);
        runTitle.setBold(false);
        runTitle.setFontSize(16);
        runTitle.setFontFamily("Times New Roman");
    }
    //设置行间距
    public static void setLineSpace(XWPFParagraph titleParagraph,int size) {
        CTP ctp = titleParagraph.getCTP();
        CTPPr ppr = ctp.isSetPPr() ? ctp.getPPr() : ctp.addNewPPr();
        CTSpacing spacing = ppr.isSetSpacing()? ppr.getSpacing() : ppr.addNewSpacing();
        spacing.setAfter(BigInteger.valueOf(0));
        spacing.setBefore(BigInteger.valueOf(0));
        //设置行距类型为 EXACT
        spacing.setLineRule(STLineSpacingRule.EXACT);
        //1磅数是20
        spacing.setLine(BigInteger.valueOf(size*20));
    }
    public static String getDoubleNumber(Double d)
    {
        DecimalFormat df = new DecimalFormat("#.00");
       return df.format(d);
    }
    public static String changeDate(String date)
    {
        String s1 = date.substring(0, 4);
        String s2 = date.substring(4, 6);
        String s3 = date.substring(6, date.length());
        return  s1+"-"+s2+"-"+s3;
    }



}