PDF jet

Using the Chart class to create scatter plot with trendline


Example_09.pdf
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;

using PDFjet.NET;


/**
 *  Example_09.cs
 *
 */
public class Example_09 {

    private Exception e = null;

    public Example_09() {

        FileStream fos = new FileStream("Example_09.pdf", FileMode.Create);
        BufferedStream bos = new BufferedStream(fos);

        PDF pdf = new PDF(bos);
        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, 50);
        chart.SetSize(500, 300);
        chart.DrawOn(page);

        addTableToChart(page, chart, f3, f4);

        pdf.Flush();
        bos.Close();
    }


    public void addTrendLine(Chart chart) {
        List<Point> points = chart.GetData()[0];

        double m = chart.Slope(points);
        double b = chart.Intercept(points, m);

        List<Point> trendline = new List<Point>();
        double x = 0.0;
        double y = m * x + b;
        Point p1 = new Point(x, y);
        p1.SetDrawLineTo(true);
        p1.SetColor(Color.blue);
        p1.SetShape(Point.INVISIBLE);

        x = 1.5;
        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) {
        Table table = new Table();
        List<List<Cell>> tableData = new List<List<Cell>>();
        List<Point> points = chart.GetData()[0];
        for (int i = 0; i < points.Count; i++) {
            Point point = points[i];
            if (point.GetShape() != Point.CIRCLE) {
                List<Cell> tableRow = new List<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.2);
        table.SetPosition(70.0, 360.0);
        table.SetColumnWidth(0, 9.0);
        table.DrawOn(page);
    }


    public List<List<Point>> GetData(
            String fileName,
            String delimiter) {
        List<List<Point>> chartData = new List<List<Point>>();

        StreamReader reader =
                new StreamReader(fileName);
        List<Point> points = new List<Point>();
        String line = null;
        while ((line = reader.ReadLine()) != null) {
            String[] cols = null;
            if (delimiter.Equals("|")) {
                cols = line.Split(new Char[] {'|'});
            } else if (delimiter.Equals("\t")) {
                cols = line.Split(new Char[] {'\t'});
            } else {
                throw new Exception(
                    "Only pipes and tabs can be used as delimiters");
            }

            Point point = new Point();
            try {
                double population =
                        Double.Parse(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.Parse(
                        cols[5].Replace(",", "")) / population);
                point.SetY(Double.Parse(
                        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) {
                // Don't print or log the exceptions and
                // prevent csc from generating warnings
                this.e = e;
            }
        }
        reader.Close();
        chartData.Add(points);

        return chartData;
    }

    public static void Main(String[] args) {
        try {
            new Example_09();
        } catch (Exception e) {
            Console.WriteLine(e.StackTrace);
        }
    }

}   // End of Example_09.cs

© 2017 Innovatics Inc.