"Untitled Post" - Views: 80 · Hits: 80 - Type: Public

import java.awt.Color;
import java.awt.Font;
import java.awt.Paint;
import java.io.File;
import java.io.IOException;
import java.text.NumberFormat;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.block.GridArrangement;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RectangleInsets;

public class StackedAreaChart {

	public StackedAreaChart() {
		CategoryDataset dataset = createDataset();
		createChart(dataset);
	}

	public CategoryDataset createDataset() {
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		dataset.addValue(1.0, "Series1", "2001");
		dataset.addValue(2.0, "Series1", "2002");
		dataset.addValue(3.0, "Series1", "2003");
		dataset.addValue(4.0, "Series1", "2004");
		dataset.addValue(5.0, "Series1", "2005");
		dataset.addValue(0, "Series1", "2006");
		dataset.addValue(0, "Series1", "2007");
		dataset.addValue(0, "Series1", "2008");

		dataset.addValue(6.0, "Series2", "2001");
		dataset.addValue(3.0, "Series2", "2002");
		dataset.addValue(4.0, "Series2", "2003");
		dataset.addValue(3.0, "Series2", "2004");
		dataset.addValue(9.0, "Series2", "2005");
		dataset.addValue(0, "Series2", "2006");
		dataset.addValue(0, "Series2", "2007");
		dataset.addValue(0, "Series2", "2008");

		dataset.addValue(0, "Series3", "2001");
		dataset.addValue(0, "Series3", "2002");
		dataset.addValue(0, "Series3", "2003");
		dataset.addValue(0, "Series3", "2004");
		dataset.addValue(0, "Series3", "2005");
		dataset.addValue(-2, "Series3", "2006");
		dataset.addValue(-3, "Series3", "2007");
		dataset.addValue(-4, "Series3", "2008");
		return dataset;
	}

	/**
	 * Creates a sample chart.
	 * 
	 * @param dataset
	 *            the dataset.
	 * 
	 * @return A sample chart.
	 */
	public JFreeChart createChart(CategoryDataset dataset) {

		final JFreeChart chart = ChartFactory.createStackedAreaChart("", // chart
																			// title
				"", // domain axis label
				"", // range axis label
				dataset, // data
				PlotOrientation.VERTICAL, // orientation
				true, // include legend
				true, false);

		chart.setBackgroundPaint(Color.white);

		final CategoryPlot plot = (CategoryPlot) chart.getPlot();
		plot.setBackgroundPaint(Color.WHITE);

		CategoryItemRenderer renderer = plot.getRenderer();
		Paint p1 = Color.blue;
		renderer.setSeriesPaint(0, p1);

		Paint p2 = Color.green;
		renderer.setSeriesPaint(1, p2);

		Paint p3 = Color.RED;
		renderer.setSeriesPaint(2, p3);

		RectangleInsets axisOffset = null;
		axisOffset = new RectangleInsets(-1.0, -0.6, 0, 0);
		System.out.println(renderer.getLegendItems().getItemCount());

		CategoryAxis domainAxis = plot.getDomainAxis();
		domainAxis.setTickMarksVisible(false);
		domainAxis.setLowerMargin(0.02);
		domainAxis.setUpperMargin(0.01);
		domainAxis.setCategoryMargin(0);

		NumberAxis rangeAxis = new NumberAxis();
		rangeAxis.setTickLabelInsets(new RectangleInsets(12, 10, 3, 2));
		rangeAxis.setTickMarkInsideLength(0);
		rangeAxis.setTickMarkOutsideLength(7);
		rangeAxis.setTickLabelPaint(Color.GRAY);
		rangeAxis.setAutoRange(true);
		rangeAxis.setAutoRangeIncludesZero(false);
		rangeAxis.setAutoRangeStickyZero(false);
		NumberFormat currency = NumberFormat.getCurrencyInstance();
		currency.setMaximumFractionDigits(0);
		rangeAxis.setNumberFormatOverride(currency);

		LegendTitle legend = chart.getLegend();
		chart.getLegend().setVisible(false);
		legend = new LegendTitle(plot, new GridArrangement(renderer
				.getLegendItems().getItemCount(), 1), new GridArrangement(
				renderer.getLegendItems().getItemCount(), 1));
		legend.setPosition(RectangleEdge.TOP);
		legend.setHorizontalAlignment(HorizontalAlignment.RIGHT);
		legend.setItemFont(new Font("Arial", Font.BOLD, 13));
		chart.getLegend().setFrame(BlockBorder.NONE);
		chart.addLegend(legend);
		plot.setOutlineVisible(false);
		plot.setRangeAxis(rangeAxis);
		plot.setAxisOffset(axisOffset);
		plot.setRangeGridlinesVisible(false);
		plot.setDomainGridlinesVisible(false);
		chart.setBorderPaint(Color.WHITE);
		chart.setAntiAlias(false);
		return chart;

	}

	public void saveChart(JFreeChart chart, String fileLocation) {
		String fileName = fileLocation;
		try {

			ChartUtilities.saveChartAsPNG(new File(fileName), chart, 1000, 600);
		} catch (IOException e) {
			e.printStackTrace();
			System.err.println("Problem occurred creating chart.");
		}
	}

	/**
	 * Starting point for the demonstration application.
	 * 
	 * @param args
	 *            ignored.
	 */
	public static void main(final String[] args) {

		final StackedAreaChart demo = new StackedAreaChart();

		CategoryDataset categoryDataset = demo.createDataset();
		/** Create 3D Pie Chart based on this dataset* */
		JFreeChart chart = demo.createChart(categoryDataset);

		/** Define a file location to save this chart */
		String fileLocation = "C:/charts/StackAreaChart.png";

		/** Save the chart to the file system* */
		demo.saveChart(chart, fileLocation);

		System.out.println("Pie Chart has been created successfully");
		System.out.println("Chart has been saved to: " + fileLocation);

	}

}