My favorites | Sign in
Project Home Downloads Wiki Issues Source
Project Information
Members
Featured
Downloads
Links

A fluent Java API that wraps working with the Google Chart API.

Example of use

There are two modes of operation, the ChartMaker class, which is aimed at using predefined chart layouts:

package com.google.api.chart;

import static com.google.api.chart.ChartMaker.*;

public class ChartDemo {

	public static void main(String[] args) {

		// construct a chart type with layout options
		ChartTypeMaker myLineChart = line()
			.colors(color("ff0000"), color("00ff00"))
			.axes(axisLeftY().labels("Bad", "Good"))
			.gridlines(gridline(20, 20))
			.legend("Me", "You");
		
		// get an url, using data and layout
		ChartMaker maker = new ChartMaker().type(myLineChart)
			.size(dim(300, 150))
			.data(dataSeries(10, 20, 30, 50, 10, 22, 23, 14, 15), 
					dataSeries(10, 40, 100, 10, 90, 74, 10, 55, 65));
			
		System.out.println("url = " + maker.getURL());
		
		try {
			byte[] data = maker.getImageData();
			// write file here..
			System.out.println("received bytes: " + data.length);
		}
		catch (Exception e) {
			// handle it
		}
	}
}

Construct a simple line chart, with two dataseries, some colors, and a single axis.

Or, by using the SimpleChartMaker class, which wraps everything into a single class, which is more fluent than the ChartMaker approach.

package com.google.api.chart;

import static com.google.api.chart.ChartMaker.*;

public class SimpleChartMakerDemo {

		String url = simple(bar().stacked().vertical())
			.size(dim(350, 100))
			.colors(color("ff0000"), color("00ff00"))
			.data(dataSeries(10, 20, 30, 40, 50),
					dataSeries(50, 40, 30, 20, 10))
			.relative(1.0, 1.0)
			.dataEncoder(SIMPLE)
			.getURL();
			
		System.out.println("url = " + url);
}

The static import at the top, imports all the "functions" that makes it look nice to program with.

Specifying data series

You can use the built-in dataSeries(double) wrappers, or, you can make your own by implementing the DataSeriesMaker interface.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import static com.google.api.chart.ChartMaker.*;

public class MyDataSeriesMaker implements DataSeriesMaker {

	public static void main(String[] args) {
		
		String url = simple(line())
			.data(new MyDataSeriesMaker(0),
					new MyDataSeriesMaker(1))
			.getURL();

		System.out.println("url = " + url);
	}

	private int i;
	public MyDataSeriesMaker(int i) {
		this.i = i;
	}
	@Override
	public BigDecimal[] getData() {
		List<BigDecimal> data = new ArrayList<BigDecimal>();
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int j = 0;
		while (true) {
			System.out.print("enter data element " + i + ":" + (j++) + " (ENTER to end)> ");
			try {
				String tmp = br.readLine();
				if (tmp == null || "".equals(tmp))
					break;
				data.add(new BigDecimal(tmp));
			}
			catch (Exception e) {
				break;
			}
		}
		return data.toArray(new BigDecimal[data.size()]);
	}
}

Maven2 repository

The library is available through maven like the following

<dependencies>
  <dependency>
    <groupId>com.google.api.chart</groupId>
    <artifactId>chartmaker</artifactId>
    <version>0.9.1</version>
  </dependency>
</dependencies>
<repositories>
  <repository>
    <id>kokila</id>
    <url>http://mirrors.kokila.it/maven2</url>
  </repository>
</repositories>
Powered by Google Project Hosting