com.anevis.jfreechartsamplespring.chart.ChartServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.anevis.jfreechartsamplespring.chart.ChartServiceImpl.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.anevis.jfreechartsamplespring.chart;

import com.anevis.jfreechartsamplespring.boundary.PieChartDataRepository;
import com.anevis.jfreechartsamplespring.boundary.RingChartDataRepository;
import com.anevis.jfreechartsamplespring.model.PieChartData;
import com.anevis.jfreechartsamplespring.model.RingChartData;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;
import org.springframework.stereotype.Component;

/**
 *
 * @author chot
 */
@Component
public class ChartServiceImpl implements ChartService {

    private final PieChartDataRepository pieChartDataRepository;
    private final RingChartDataRepository ringChartDataRepository;

    public ChartServiceImpl(PieChartDataRepository pieChartDataRepository,
            RingChartDataRepository ringChartDataRepository) {
        this.pieChartDataRepository = pieChartDataRepository;
        this.ringChartDataRepository = ringChartDataRepository;
    }

    @Override
    public byte[] getChartPDF() {
        return writeChartsToDocument(createPieChart(), createRingChart());
    }

    private JFreeChart createPieChart() {
        List<PieChartData> dataList = pieChartDataRepository.findAll();
        DefaultPieDataset dataSet = new DefaultPieDataset();

        dataList.forEach(data -> dataSet.setValue(data.getCountry(), data.getWeight()));

        JFreeChart chart = ChartFactory.createPieChart("Anteil am Fondsvermgen", dataSet, true, false,
                Locale.GERMANY);

        return chart;
    }

    private JFreeChart createRingChart() {
        List<RingChartData> dataList = ringChartDataRepository.findAll();
        DefaultPieDataset dataSet = new DefaultPieDataset();

        dataList.forEach(data -> dataSet.setValue(data.getSecurity(), data.getWeighting()));

        JFreeChart chart = ChartFactory.createRingChart("", dataSet, true, false, Locale.GERMANY);

        return chart;
    }

    private byte[] writeChartsToDocument(JFreeChart... charts) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            float width = PageSize.A4.getWidth();
            float height = PageSize.A4.getHeight() / 2;
            int index = 0;

            Document document = new Document();
            PdfWriter writer = PdfWriter.getInstance(document, baos);
            document.open();
            PdfContentByte contentByte = writer.getDirectContent();

            for (JFreeChart chart : charts) {

                PdfTemplate template = contentByte.createTemplate(width, height);
                Graphics2D graphics2D = template.createGraphics(width, height);
                Rectangle2D rectangle2D = new Rectangle2D.Double(0, 0, width, height);

                chart.draw(graphics2D, rectangle2D);

                graphics2D.dispose();
                contentByte.addTemplate(template, 0, height - (height * index));
                index++;
            }

            writer.flush();
            document.close();

            return baos.toByteArray();
        } catch (DocumentException ex) {
            Logger.getLogger(ChartService.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

}