Exercise: Drawing charts with Google Charts API

Last modified by superadmin on 2018-01-12 20:28

Exercise: Drawing charts with Google Charts API

Goal: Familiarize yourself with parameter passing to Java command-line applications (i.e. "args" for the main() method). Build a Java class, which constructs string URLs for barchart diagrams (command-line parameters include width, height, barWidth, maxValue, data, etc.). First class is GoogleBarChartCreator, which does all the URL building stuff (String URL is built from a StringBuffer object - this is done to avoid creating too much garbage, since StringBuffer is a mutable object.). Another class is the one, which processes command-line parameters - GoogleBarChartCreatorMain

  1. Create another project under the JDeveloper application, e.g. "project2". 
  2. Create a Java class under that project with the following source code: 
package project2;

public class GoogleBarChartCreator {

    private String caption = "Diagramma";
    private int[] data;
    private int maxValue;
    private int width = 300;
    private int height = 200;
    private String type = "bvs";
    private int barWidth;
    private String[] labels;
    private int partitionSize;

    public static final String CODES =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    public GoogleBarChartCreator() {
    }

    public String getChartUrl() {
        StringBuffer sb =
            new StringBuffer("http://chart.apis.google.com/chart?chtt=" +
                             caption);
        sb.append("&chs=" + width + "x" + height);
        sb.append("&chd=s:");


        for (int i = 0; i < data.length; i++) {
            long index =
                Math.round((CODES.length() - 1) * data[i] * 1.0 / maxValue);
            sb.append(CODES.charAt((int)index));
        }
        if (type != null) {
            sb.append("&cht=" + type);
        }
        if (barWidth != 0) {
            sb.append("&chbh=" + barWidth);
        }
        sb.append("&chxt=x,y");
        if (labels != null) {
            sb.append("&chxl=0:");
            for (int i = 0; i < labels.length; i++) {
                sb.append("|" + labels[i]);
            }
            if (partitionSize > 0) {
                sb.append("|1:");
                for (int i = 0; i <= maxValue; i += partitionSize) {
                    sb.append("|" + i);
                }
            }
            else {
                sb.append("|1:|0|" + maxValue);
            }
        }
        if (partitionSize != 0) {
            sb.append("&chg=0," + (100*partitionSize)/maxValue + ",2,5");
        }
        return sb.toString();
    }

    public static int[] buildIntArray(String arg) {
        String[] vals = arg.split(",");
        int[] result = new int[vals.length];
        for (int i = 0; i < vals.length; i++) {
            result[i] = Integer.parseInt(vals[i]);
        }
        return result;
    }

    public void setCaption(String caption) {
        this.caption = caption;
    }

    public String getCaption() {
        return caption;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getWidth() {
        return width;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getHeight() {
        return height;
    }

    public void setData(int[] data) {
        this.data = data;
    }

    public void setSize(String size) {
        String[] pair = size.split("x");
        this.width = Integer.parseInt(pair[0]);
        this.height = Integer.parseInt(pair[1]);
    }

    void setType(String type) {
        this.type = type;
    }

    void setBarWidth(int barWidth) {
        this.barWidth = barWidth;
    }

    void setLabels(String[] labels) {
        this.labels = labels;
    }

    void setMaxValue(int maxValue) {
        this.maxValue = maxValue;
    }

    void setPartitionSize(int partitionSize) {
        this.partitionSize = partitionSize;
    }
}
  1. Create a class calling this GoogleBarChartCreator class: 
package project2;

public class GoogleBarChartCreatorMain {
    public GoogleBarChartCreatorMain() {
    }

    public static void main(String[] args) {
        GoogleBarChartCreator chartsDemo = new GoogleBarChartCreator();
        char flag = ' ';
        for (int i = 0; i < args.length; i++) {
            if (args[i].charAt(0) == '-') {
                flag = args[i].charAt(1);
                continue;
            }
            switch (flag) {
            case 'b':
                chartsDemo.setBarWidth(Integer.parseInt(args[i]));
                break;
            case 'c':
                chartsDemo.setCaption(args[i]);
                break;
            case 'd':
                chartsDemo.setData(GoogleBarChartCreator.buildIntArray(args[i]));
                break;
            case 'l':
                chartsDemo.setLabels(args[i].split(","));
                break;
            case 'm':
                chartsDemo.setMaxValue(Integer.parseInt(args[i]));
                break;
            case 'p':
                chartsDemo.setPartitionSize(Integer.parseInt(args[i]));
                break;
            case 's':
                chartsDemo.setSize(args[i]);
                break;
            case 't':
                chartsDemo.setType(args[i]);
                break;
            default:
                // nonrecognized flag, do nothing
            }
            flag = ' ';
        }
        String url = chartsDemo.getChartUrl();
        System.out.println(url);
    }
}
  1. Pick from the JDeveloper menu Run -> Choose Active Run Configuration -> Manage Run configurations
  2. Create a new run configuration for the class GoogleBarChartCreatorMain
  3. As launch settings copy this string 
-data 110,40,30 -maxValue 120 -size 300x200 -labels Oracle,Java,SAP -partitionSize 40 -barWidth 30

 These become values for the "args" array in the "main()" method of GoogleBarChartCreatorMain

  1. Run the program. It should display the following URL address: http://chart.apis.google.com/chart?chtt=Diagramma&chs=300x200&chd=s:4UP&cht=bvs&chbh=30&chxt=x,y&chxl=0:|Oracle|Java|SAP|1:|0|40|80|120&chg=0,33,2,5
  2. Clicking on that link should display the following filename

Exercise

  1. Read the relevant documentation here http://code.google.com/apis/chart/ and display the same chart with horizontal bars. You may need to change the URL-generation code for GoogleBarChartCreator. The URL you may need to create is something like this: 
http://chart.apis.google.com/chart?chtt=Diagramma&chs=300x200&chd=s:4UP&cht=bhs&chbh=30&chxt=x,y&chxl=1:|Oracle|Java|SAP|0:|0|40|80|120&chg=33,0,2,5
  1. If size is too large, then the image is still the 300x200 pixels (or whatever we have passed as "chs" - the chart-size parameter). Modify the code in the method getChartUrl() so that the size (width in the case of vertical bars or height in case of horizontal bars) is diminished if the bar chart actually fits in a smaller rectangle.
  2. Is it possible to pass arguments to the GoogleBarChartCreator so that the program exits with an exception? 

Bibliography

  1. Google Chart API documentation
  2. A few samples of Google charts may be found on this server http://www.ante.lv/xwiki/bin/view/Main/Visitors?language=lv
Tags:
Created by Kalvis Apsītis on 2008-02-28 13:39
    
This wiki is licensed under a Creative Commons 2.0 license
XWiki Enterprise 6.4 - Documentation