Using the Chart class to create scatter plot with trendline

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

import com.pdfjet.*;


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

    private Exception e = null;


    public Example_09() throws Exception {

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

        PDF pdf = new PDF(fos);

        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);

        Page page = new Page(pdf, Letter.PORTRAIT);
        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, 50);
        chart.setSize(500, 300);
        chart.drawOn(page);

        addTableToChart(page, chart, f3, f4);

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


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

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

        List trendline = new ArrayList();
        double x = 0.0;
        double y = m * x + b;
        Point p1 = new Point(x, y);
        p1.setDrawLineTo(true);
        p1.setColor(RGB.BLUE);
        p1.setShape(Point.INVISIBLE);

        x = 1.5;
        y = m * x + b;
        Point p2 = new Point(x, y);
        p2.setDrawLineTo(true);
        p2.setColor(RGB.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(f3, f4);
        List> tableData = new ArrayList>();
        List points = chart.getData().get(0);
        for (int i = 0; i < points.size(); i++) {
            Point point = points.get(i);
            if (point.getShape() != Point.CIRCLE) {
                List tableRow = new ArrayList();

                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.2);
        table.setPosition(70.0, 360.0);
        table.setColumnWidth(0, 9.0);
        table.drawOn(page);
    }


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

        BufferedReader reader =
                new BufferedReader(new FileReader(fileName));
        List points = new ArrayList();
        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(RGB.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

© 2011 Innovatics Inc.