DuPont analysis chart-杜邦分析

杜邦分析图例

 

 

 

java代码

 

package cn.com.fasoft.bank.util;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;

import cn.com.fasoft.bank.form.zhibiao.DbfxfForm;
import cn.com.fasoft.core.util.writer.BaseCell;
import cn.com.fasoft.core.util.writer.BaseParagraph;
import cn.com.fasoft.core.util.writer.BaseWriter;
import cn.com.fasoft.core.util.writer.ChineseFont;

import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Table;
import com.lowagie.text.rtf.field.RtfPageNumber;
import com.lowagie.text.rtf.field.RtfTotalPageNumber;
import com.lowagie.text.rtf.headerfooter.RtfHeaderFooter;
import com.lowagie.text.rtf.style.RtfParagraphStyle;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class DbfxfToWord extends BaseWriter {
    private String imgPath;

    public String getImgPath() {
        return imgPath;
    }

    public void setImgPath(String imgPath) {
        this.imgPath = imgPath;
    }

    public DbfxfToWord(String filepath, String filename, int type, boolean bol) {
        super(filepath + filename, type, bol);
        this.setImgPath(filepath);
    }

    public DbfxfToWord(String title) {
        super(title);
    }

    // 设置页眉
    public void setHeader(String title) {
        HeaderFooter header = new HeaderFooter(new Phrase(title, ChineseFont
                .create(12, BOLD)), false);
        header.setBorder(2);
        header.setAlignment(RIGHT);
        document.setHeader(header);
    }

    // 设置页脚
    public void setFooder() {
        Font fontSong = ChineseFont.create(9, NORMAL);
        HeaderFooter footer = new HeaderFooter(new Phrase("第 ", fontSong),
                new Phrase(" 页", fontSong));
        footer.setAlignment(RIGHT);
        footer.setBorder(1);
        document.setFooter(footer);
    }

    // 设置页脚
    public void setFooter() {
        try {
            table = new Table(2);
            table.setBorder(0);
            table.setWidth(100);

            Paragraph pageNotify = new BaseParagraph("内部资料,注意保密", LEFT, 9,
                    NORMAL);
            table.addCell(new Cell(pageNotify));

            Paragraph pageNumber = new BaseParagraph("第 ", RIGHT, 9, NORMAL);
            pageNumber.add(new RtfPageNumber());
            pageNumber.add(" 页 共 ");
            pageNumber.add(new RtfTotalPageNumber());
            pageNumber.add("页");
            table.addCell(new Cell(pageNumber));

            document.setFooter(new RtfHeaderFooter(table));
        } catch (BadElementException e) {
            e.printStackTrace();
        }
    }

    // 输出一级标题
    public void setTitle1(String value) {
        try {
            Paragraph parag = new Paragraph(value,
                    RtfParagraphStyle.STYLE_HEADING_1);
            parag.setAlignment(LEFT);
            parag.setSpacingBefore(0);
            parag.setSpacingAfter(0);
            // RtfParagraphStyle.STYLE_HEADING_1.setColor(Color.BLUE);
            RtfParagraphStyle.STYLE_HEADING_1.setSize(18);
            document.add(parag);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    // 输出二级标题
    public void setTitle2(String value) {
        try {
            Paragraph parag = new Paragraph(value,
                    RtfParagraphStyle.STYLE_HEADING_2);
            parag.setAlignment(LEFT);
            parag.setSpacingBefore(0);
            parag.setSpacingAfter(0);
            RtfParagraphStyle.STYLE_HEADING_2.setSize(16);
            document.add(parag);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    // 输出三级标题
    public void setTitle3(String value) {
        try {
            Paragraph parag = new Paragraph(value,
                    RtfParagraphStyle.STYLE_HEADING_3);
            parag.setAlignment(LEFT);
            parag.setSpacingBefore(0);
            parag.setSpacingAfter(0);
            RtfParagraphStyle.STYLE_HEADING_3.setSize(14);
            document.add(parag);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    // 输出一段文字
    public void SetConclusion(String value) {
        try {
            document.add(new Paragraph("    " + value, ChineseFont.create(14,
                    NORMAL)));
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    // 输出当前字符串
    public void AddChar(String value) {
        try {
            document.add(new Paragraph(value, ChineseFont.create(14, NORMAL)));
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    public void setTitle(String value) {
        try {
            document.add(new BaseParagraph(value, CENTER, 22, BOLD));
            document.add(new Paragraph("\t"));
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    // 添加图片
    public void AddImage(String imgPath) {
        try {
            Image img = Image.getInstance(imgPath);
            img.setAlignment(Image.MIDDLE);
            document.add(img);
            document.add(new Paragraph("\t"));
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 结束
    public void End() {
        try {
            close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void simpleTable(String dateStr, String tableName, List dateContent,
            int colNumber,List suitecodeList) {

        Map map = new HashMap();// 验证数据用
        Map map1 = new HashMap();// 验证数据

        Font fontChinese = ChineseFont.create(9, NORMAL);
        try {
            // 设置表头
            document.add(new BaseParagraph(tableName, CENTER, 15, BOLD));

            // 创建表格
            table = new Table(colNumber);
            table.setBorderWidth(0);
            // table.getDefaultCell().setBackgroundColor(Color.ORANGE);
            table.getDefaultCell().setHorizontalAlignment(CENTER);

            // 添加表头元素
            table.addCell(new BaseCell("杜邦", 1, 1, 14, NORMAL));

            table.addCell(new BaseCell("净资产收益率", 1, 1, 14, NORMAL));
            table.addCell(new BaseCell("资产利润率", 1, 1, 14, NORMAL));
            table.addCell(new BaseCell("权益乘数", 1, 1, 14, NORMAL));
            table.addCell(new BaseCell("销售净利率", 1, 1, 14, NORMAL));
            table.addCell(new BaseCell("资产周转率", 1, 1, 14, NORMAL));
            table.addCell(new BaseCell("少数权益率", 1, 1, 14, NORMAL));
            table.addCell(new BaseCell("资产负债率", 1, 1, 14, NORMAL));
            table.addCell(new BaseCell("净利润", 1, 1, 14, NORMAL));
            table.addCell(new BaseCell("销售收入", 1, 1, 14, NORMAL));
            table.addCell(new BaseCell("资产总额", 1, 1, 14, NORMAL));
            table.addCell(new BaseCell("少数股东权益", 1, 1, 14, NORMAL));
            table.addCell(new BaseCell("负债总额", 1, 1, 14, NORMAL));

            for (int k = 0; k < dateContent.size(); k++) {
                DbfxfForm dbfxfform = (DbfxfForm) dateContent.get(k);
                table.addCell(dbfxfform.getDateId());
                table.addCell(dbfxfform.getValue_A());
                table.addCell(dbfxfform.getValue_AA());
                table.addCell(dbfxfform.getValue_AB());
                table.addCell(dbfxfform.getValue_AAA());
                table.addCell(dbfxfform.getValue_AAB());
                table.addCell(dbfxfform.getValue_AAC());
                table.addCell(dbfxfform.getValue_AAD());
                table.addCell(dbfxfform.getValue_AAAA());
                table.addCell(dbfxfform.getValue_AAAB());
                table.addCell(dbfxfform.getValue_AABB());
                table.addCell(dbfxfform.getValue_AACA());
                table.addCell(dbfxfform.getValue_AADA());
            }
            document.add(getsuitecodeList(suitecodeList));//增加数据来源
            document.add(new Paragraph("\t"));
            document.add(new Paragraph("\t"));
            document.add(table);
            document.add(new Paragraph("\t"));
            document.add(new Paragraph("\t"));
            document.add(new Paragraph("\t"));
            try {
                String fileUrl=this.getImgPath()+ "\\dbfxf\\bg.jpg";
                InputStream imageIn = new FileInputStream(new File(fileUrl));
                JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(imageIn);
                BufferedImage image = decoder.decodeAsBufferedImage();
                Graphics g = image.getGraphics();

                // 设置颜色
                g.setColor(Color.black);

                // 设置字体
                java.awt.Font mFont = new java.awt.Font("Arial",
                        java.awt.Font.PLAIN, 11);// 默认字体
                // if (fontStyle.equalsIgnoreCase("italic"))

                // if (fontStyle.equalsIgnoreCase("bold"))
                // mFont = new Font(fontName, Font.ITALIC, 12);
                // if (fontStyle.equalsIgnoreCase("plain"))
                // mFont = new Font(fontName, Font.ITALIC, 12);
                g.setFont(mFont);

                // 输出文字
                String[] dataArr = dateStr.split(",");
                g.drawString(dataArr[0], 377, 19);
                g.drawString(dataArr[1], 181, 99);
                g.drawString(dataArr[2], 554, 99);
                g.drawString(dataArr[3], 88, 176);
                g.drawString(dataArr[4], 280, 176);
                g.drawString(dataArr[5], 493, 176);
                g.drawString(dataArr[6], 657, 176);
                g.drawString(dataArr[7], 3, 271);
                g.drawString(dataArr[8], 92, 271);
                g.drawString(dataArr[9], 189, 271);
                g.drawString(dataArr[10], 286, 271);
                g.drawString(dataArr[11], 376, 271);
                g.drawString(dataArr[12], 482, 271);
                g.drawString(dataArr[13], 579, 271);
                g.drawString(dataArr[14], 669, 271);
                // 输出数据流
                Image img = Image.getInstance(ChartUtilities.encodeAsPNG( image));
                img.setAlignment(Element.ALIGN_CENTER);
                img.scaleAbsolute(600,200);
                // img.scalePercent(62, 57);
                document.add(img);
                document.add(new Paragraph("        "));
            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    private String getDateTime() {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String ret = fmt.format(new java.util.Date());
        return ret;
    }

    public void processorDateContent(Map reportMap, String dateStr)
            throws IOException {
        List dateContent = (List) reportMap.get("dataList");
        List suitecodeList=(List)reportMap.get("suitecodeList");
        int titleNum = 13;

        setFooter();
        simpleTable(dateStr, "杜邦分析", dateContent, titleNum,suitecodeList);
        End();
    }

    public void saveWord() {
        try {
            close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void writeImage(Document document, JFreeChart chart, int aWidth,
            int aHeight) {
        try {
            Image img = Image.getInstance(ChartUtilities.encodeAsPNG(chart
                    .createBufferedImage(769, 240, 740D, 200D, null)));
            img.setAlignment(Element.ALIGN_CENTER);
            img.scaleAbsolute(aWidth, aHeight);
            // img.scalePercent(62, 57);
            document.add(img);
            document.add(new Paragraph("        "));
            // document.add(new Paragraph("\t"));
        } catch (DocumentException docEx) {
            docEx.printStackTrace();
        } catch (IOException ioEx) {
            ioEx.printStackTrace();
        }
    }

}

 

---恢复内容结束---

posted @ 2013-11-10 15:52  霍哥  阅读(1361)  评论(0编辑  收藏  举报