⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cewolfchartfactory.java

📁 cewolf是个一个自动报表程序
💻 JAVA
字号:
/* ================================================================
 * Cewolf : Chart enabling Web Objects Framework
 * ================================================================
 *
 * Project Info:  http://cewolf.sourceforge.net
 * Project Lead:  Guido Laures (guido@laures.de);
 *
 * (C) Copyright 2002, by Guido Laures
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 */

package de.laures.cewolf.taglib;

import java.util.List;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CombinedDomainXYPlot;
import org.jfree.chart.plot.CombinedRangeXYPlot;
import org.jfree.chart.plot.MeterPlot;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.IntervalCategoryDataset;
import org.jfree.data.general.Dataset;
import org.jfree.data.general.PieDataset;
import org.jfree.data.general.ValueDataset;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.OHLCDataset;
import org.jfree.data.xy.SignalsDataset;
import org.jfree.data.xy.WindDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYZDataset;

import de.laures.cewolf.ChartValidationException;
import de.laures.cewolf.DatasetProduceException;
import de.laures.cewolf.util.Assert;

/**
 *
 * @author  Guido Laures
 */
public class CewolfChartFactory implements ChartConstants, AxisConstants, LayoutConstants {

	/** Creates a new instance of ChartFactory */
	private CewolfChartFactory() {
	}

	private static final int getChartTypeConstant(String type) {
		final int res = ChartTypes.typeList.indexOf(type.toLowerCase());
		if (res < 0) {
			throw new RuntimeException("unsupported axis type " + type);
		}
		return res;
	}

	private static final int getLayoutConstant(String layout) {
		return LayoutTypes.typeList.indexOf(layout.toLowerCase());
	}

	public static JFreeChart getChartInstance(String chartType, String title, String xAxisLabel, String yAxisLabel, Dataset data) throws ChartValidationException {
		switch (getChartTypeConstant(chartType)) {
			case XY :
				check(data, XYDataset.class, XY);
				return ChartFactory.createXYLineChart(title, xAxisLabel, yAxisLabel, (XYDataset) data, PlotOrientation.VERTICAL, true, true, true);
			case PIE :
				check(data, PieDataset.class, PIE);
				return ChartFactory.createPieChart(title, (PieDataset) data, true, true, true);
			case AREA_XY :
				check(data, XYDataset.class, AREA_XY);
				return ChartFactory.createXYAreaChart(title, xAxisLabel, yAxisLabel, (XYDataset) data, PlotOrientation.VERTICAL, true, false, false);
			case SCATTER :
				check(data, XYDataset.class, SCATTER);
				return ChartFactory.createScatterPlot(title, xAxisLabel, yAxisLabel, (XYDataset) data, PlotOrientation.VERTICAL, true, false, false);
			case AREA :
				check(data, CategoryDataset.class, AREA);
				return ChartFactory.createAreaChart(title, xAxisLabel, yAxisLabel, (CategoryDataset) data, PlotOrientation.VERTICAL, true, false, false);
			case HORIZONTAL_BAR :
				check(data, CategoryDataset.class, HORIZONTAL_BAR);
				return ChartFactory.createBarChart(title, xAxisLabel, yAxisLabel, (CategoryDataset) data, PlotOrientation.HORIZONTAL, true, false, false);
			case HORIZONTAL_BAR_3D :
				check(data, CategoryDataset.class, HORIZONTAL_BAR_3D);
				return ChartFactory.createBarChart3D(title, xAxisLabel, yAxisLabel, (CategoryDataset) data, PlotOrientation.HORIZONTAL, true, false, false);
			case LINE :
				check(data, CategoryDataset.class, LINE);
				return ChartFactory.createLineChart(title, xAxisLabel, yAxisLabel, (CategoryDataset) data, PlotOrientation.VERTICAL, true, false, false);
			case STACKED_HORIZONTAL_BAR :
				check(data, CategoryDataset.class, STACKED_HORIZONTAL_BAR);
				return ChartFactory.createStackedBarChart(title, xAxisLabel, yAxisLabel, (CategoryDataset) data, PlotOrientation.HORIZONTAL, true, false, false);
			case STACKED_VERTICAL_BAR :
				check(data, CategoryDataset.class, STACKED_VERTICAL_BAR);
				return ChartFactory.createStackedBarChart(title, xAxisLabel, yAxisLabel, (CategoryDataset) data, PlotOrientation.VERTICAL, true, false, false);
			case STACKED_VERTICAL_BAR_3D :
				check(data, CategoryDataset.class, STACKED_VERTICAL_BAR_3D);
				return ChartFactory.createStackedBarChart3D(title, xAxisLabel, yAxisLabel, (CategoryDataset) data, PlotOrientation.VERTICAL, true, false, false);
			case VERTICAL_BAR :
				check(data, CategoryDataset.class, VERTICAL_BAR);
				return ChartFactory.createBarChart(title, xAxisLabel, yAxisLabel, (CategoryDataset) data, PlotOrientation.VERTICAL, true, false, false);
			case VERTICAL_BAR_3D :
				check(data, CategoryDataset.class, VERTICAL_BAR_3D);
				return ChartFactory.createBarChart3D(title, xAxisLabel, yAxisLabel, (CategoryDataset) data, PlotOrientation.VERTICAL, true, false, false);
			case TIME_SERIES :
				check(data, XYDataset.class, TIME_SERIES);
				return ChartFactory.createTimeSeriesChart(title, xAxisLabel, yAxisLabel, (XYDataset) data, true, false, false);
			case CANDLE_STICK :
				check(data, OHLCDataset.class, CANDLE_STICK);
				return ChartFactory.createCandlestickChart(title, xAxisLabel, yAxisLabel, (OHLCDataset) data, true);
			case HIGH_LOW :
				check(data, OHLCDataset.class, HIGH_LOW);
				return ChartFactory.createHighLowChart(title, xAxisLabel, yAxisLabel, (OHLCDataset) data, true);
			case GANTT :
				check(data, IntervalCategoryDataset.class, GANTT);
				return ChartFactory.createGanttChart(title, xAxisLabel, yAxisLabel, (IntervalCategoryDataset) data, true, false, false);
			case WIND :
				check(data, WindDataset.class, WIND);
				return ChartFactory.createWindPlot(title, xAxisLabel, yAxisLabel, (WindDataset) data, true, false, false);
			case SIGNAL :
				check(data, SignalsDataset.class, SIGNAL);
				return ChartFactory.createSignalChart(title, xAxisLabel, yAxisLabel, (SignalsDataset) data, true);
			case VERRTICAL_XY_BAR :
				check(data, IntervalXYDataset.class, VERRTICAL_XY_BAR);
				return ChartFactory.createXYBarChart(title, xAxisLabel, true,yAxisLabel, (IntervalXYDataset) data, PlotOrientation.VERTICAL, true, false, false);
			case PIE_3D :
				check(data, PieDataset.class, PIE_3D);
				return ChartFactory.createPieChart3D(title, (PieDataset) data, true, false, false);
			case METER :
				check(data, ValueDataset.class, METER);
				MeterPlot plot = new MeterPlot((ValueDataset) data);
				JFreeChart chart = new JFreeChart(title, plot);
				return chart;
			case STACKED_AREA :
				check(data, CategoryDataset.class, STACKED_AREA);
				return ChartFactory.createStackedAreaChart(title, xAxisLabel, yAxisLabel, (CategoryDataset) data, PlotOrientation.VERTICAL, true, false, false);
			case BUBBLE :
				check(data, XYZDataset.class, BUBBLE);
				return ChartFactory.createBubbleChart(title, xAxisLabel, yAxisLabel, (XYZDataset) data, PlotOrientation.VERTICAL, true, false, false);
			default :
				throw new UnsupportedChartTypeException(chartType + " is not supported.");
		}
	}

	public static JFreeChart getOverlaidChartInstance(String chartType, String title, String xAxisLabel, String yAxisLabel, int xAxisType, int yAxisType, List plotDefinitions)
		throws ChartValidationException, DatasetProduceException {
		final int chartTypeConst = getChartTypeConstant(chartType);
		switch (chartTypeConst) {
			case OVERLAY_XY :
				final AxisFactory axisFactory = AxisFactory.getInstance();
				ValueAxis domainAxis = axisFactory.createAxis(ORIENTATION_HORIZONTAL, xAxisType, xAxisLabel);
				// get main plot
				PlotDefinition mainPlotDef = (PlotDefinition) plotDefinitions.get(0);
				check((Dataset) mainPlotDef.getDataset(), XYDataset.class, OVERLAY_XY);
				XYPlot plot = (XYPlot) mainPlotDef.getPlot(chartTypeConst);
				plot.setDomainAxis(domainAxis);
				plot.setRangeAxis(axisFactory.createAxis(ORIENTATION_VERTICAL, yAxisType, yAxisLabel));
				//plot.setRenderer(new StandardXYItemRenderer());
				// add second and later datasets to main plot
				for (int plotidx = 1;plotidx<plotDefinitions.size();plotidx++) {
					PlotDefinition subPlotDef = (PlotDefinition) plotDefinitions.get(plotidx);
					check((Dataset) subPlotDef.getDataset(), XYDataset.class, OVERLAY_XY);
					plot.setDataset(plotidx, (XYDataset)subPlotDef.getDataset());
					
					String subPlotType = subPlotDef.getType();
					int rendererIndex = PlotTypes.typeList.indexOf(subPlotType.toLowerCase());
					if (rendererIndex < 0) {
						throw new AttributeValidationException("plot.type, plotidx=" + plotidx, subPlotType);
					}
					XYItemRenderer rend = (XYItemRenderer) PlotTypes.getRenderer(rendererIndex);
					plot.setRenderer(plotidx, rend);
				}
				return new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, true);
			default :
				throw new UnsupportedChartTypeException(chartType + " is not supported.");
		}
	}

	// [tb]
	public static JFreeChart getCombinedChartInstance(String chartType, String title, String xAxisLabel, String yAxisLabel, List plotDefinitions, String layout)
		throws ChartValidationException, DatasetProduceException {
		final int chartTypeConst = getChartTypeConstant(chartType);
		switch (chartTypeConst) {
			case COMBINED_XY :
				final int layoutConst = getLayoutConstant(layout);
				Plot plot = null;
				switch (layoutConst) {
					case DOMAIN :
						ValueAxis domainAxis = new DateAxis(xAxisLabel);
						plot = new CombinedDomainXYPlot(domainAxis);
						for (int i = 0; i < plotDefinitions.size(); i++) {
							PlotDefinition pd = (PlotDefinition) plotDefinitions.get(i);
							check((Dataset) pd.getDataset(), XYDataset.class, COMBINED_XY);
							XYPlot temp = (XYPlot) pd.getPlot(chartTypeConst);
							temp.setRangeAxis(new NumberAxis(pd.getYaxislabel()));
							((CombinedDomainXYPlot) plot).add(temp);
						}
						return new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, true);
					case RANGE :
						ValueAxis rangeAxis = new NumberAxis(yAxisLabel);
						plot = new CombinedRangeXYPlot(rangeAxis);
						for (int i = 0; i < plotDefinitions.size(); i++) {
							PlotDefinition pd = (PlotDefinition) plotDefinitions.get(i);
							check((Dataset) pd.getDataset(), XYDataset.class, COMBINED_XY);
							XYPlot temp = (XYPlot) pd.getPlot(chartTypeConst);
							temp.setDomainAxis(new DateAxis(pd.getXaxislabel()));
							((CombinedRangeXYPlot) plot).add(temp);
						}
						return new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, true);
					default :
						throw new AttributeValidationException(layout, " any value");
				}
			default :
				throw new UnsupportedChartTypeException(chartType);
		}
	}

	private static void check(Dataset data, Class clazz, int chartType) throws IncompatibleDatasetException {
		if (!clazz.isInstance(data)) {
			throw new IncompatibleDatasetException("Charts of type " + ChartTypes.typeNames[chartType] + " " + "need datasets of type " + clazz.getName());
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -