PDF jet

Using the Chart class to create scatter plot with trendline


Example_09.pdf
import java.io.*;
import java.util.*;

import com.pdfjet.*;


/**
 *  Example_09.java
 *
 */
public class Example_09 {

    public Example_09() throws Exception {

        FileOutputStream fos = new FileOutputStream("Example_09.pdf");

        PDF pdf = new PDF(fos);
        Page page = new Page(pdf, Letter.PORTRAIT);

        Font f1 = new Font(pdf, CoreFont.HELVETICA_BOLD);
        Font f2 = new Font(pdf, CoreFont.HELVETICA_BOLD);
        Font f3 = new Font(pdf, CoreFont.HELVETICA_BOLD);
        Font f4 = new Font(pdf, CoreFont.HELVETICA);

        f1.setSize(10);
        f2.setSize(8);
        f3.setSize(7);
        f4.setSize(7);

        Chart chart = new Chart(f1, f2);
        chart.setTitle("World View - Communications");
        chart.setXAxisTitle("Cell phones per capita");
        chart.setYAxisTitle("Internet users % of the population");

        chart.setData(getData("data/world-communications.txt", "|"));
        addTrendLine(chart);

        chart.setPosition(70.0f, 50.0f);
        chart.setSize(500.0f, 300.0f);
        chart.drawOn(page);

        addTableToChart(page, chart, f3, f4);

        pdf.flush();
        fos.close();
    }


    public void addTrendLine(Chart chart) {
        List<Point> points = chart.getData().get(0);

        float m = chart.slope(points);
        float b = chart.intercept(points, m);

        List<Point> trendline = new ArrayList<Point>();
        float x = 0.0f;
        float y = m * x + b;
        Point p1 = new Point(x, y);
        p1.setDrawLineTo(true);
        p1.setColor(Color.blue);
        p1.setShape(Point.INVISIBLE);

        x = 1.5f;
        y = m * x + b;
        Point p2 = new Point(x, y);
        p2.setDrawLineTo(true);
        p2.setColor(Color.blue);
        p2.setShape(Point.INVISIBLE);
        trendline.add(p1);
        trendline.add(p2);

        chart.getData().add(trendline);
    }


    public void addTableToChart(
            Page page, Chart chart, Font f3, Font f4) throws Exception {
        Table table = new Table();
        List<List<Cell>> tableData = new ArrayList<List<Cell>>();
        List<Point> points = chart.getData().get(0);
        for (int i = 0; i < points.size(); i++) {
            Point point = points.get(i);
            if (point.getShape() != Point.CIRCLE) {
                List<Cell> tableRow = new ArrayList<Cell>();

                Cell cell = new Cell(f4);
                cell.setPoint(point);
                tableRow.add(cell);

                cell = new Cell(f4);
                cell.setText(point.getText());
                tableRow.add(cell);

                cell = new Cell(f4);
                cell.setText(point.getURIAction());
                tableRow.add(cell);

                tableData.add(tableRow);
            }
        }
        table.setData(tableData);
        table.autoAdjustColumnWidths();
        table.setLineWidth(0.2f);
        table.setPosition(70.0f, 360.0f);
        table.setColumnWidth(0, 9.0f);
        table.drawOn(page);
    }


    public List<List<Point>> getData(
            String fileName,
            String delimiter) throws Exception {
        List<List<Point>> chartData = new ArrayList<List<Point>>();

        BufferedReader reader =
                new BufferedReader(new FileReader(fileName));
        List<Point> points = new ArrayList<Point>();
        String line = null;
        while ((line = reader.readLine()) != null) {
            String[] cols = null;
            if (delimiter.equals("|")) {
                cols = line.split("\\|", -1);
            }
            else if (delimiter.equals("\t")) {
                cols = line.split("\t", -1);
            }
            else {
                throw new Exception(
                    "Only pipes and tabs can be used as delimiters");
            }

            Point point = new Point();
            try {
                double population =
                        Double.valueOf(cols[1].replace(",", ""));
                point.setText(cols[0].trim());
                String country_name = point.getText();
                country_name = country_name.replace(" ", "_");
                country_name = country_name.replace("'", "_");
                country_name = country_name.replace(",", "_");
                country_name = country_name.replace("(", "_");
                country_name = country_name.replace(")", "_");
                point.setURIAction(
                        "http://pdfjet.com/country/" + country_name + ".txt");
                point.setX(Double.valueOf(
                        cols[5].replace(",", "")) / population);
                point.setY(Double.valueOf(
                        cols[7].replace(",", "")) / population * 100);
                point.setRadius(2.0);

                if (point.getX() > 1.25) {
                    point.setShape(Point.RIGHT_ARROW);
                }
                if (point.getY() > 80) {
                    point.setShape(Point.UP_ARROW);
                    // point.setFillShape(true);
                    point.setColor(Color.blue);
                }
                if (point.getText().equals("France")) {
                    point.setShape(Point.MULTIPLY);
                    // point.setDrawLineTo(true);
                }
                if (point.getText().equals("Canada")) {
                    point.setShape(Point.BOX);
                    // point.setDrawLineTo(true);
                }
                if (point.getText().equals("United States")) {
                    point.setShape(Point.STAR);
                    // point.setDrawLineTo(true);
                }

                points.add(point);
            } catch (Exception e) {
            }
        }
        reader.close();
        chartData.add(points);

        return chartData;
    }

    public static void main(String[] args) {
        try {
            new Example_09();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}   // End of Example_09.java

© 2017 Innovatics Inc.