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

📄 cewolfchartfactory.java

📁 jfreechart标签库
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* ================================================================
 * 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.HashMap;
import java.util.List;
import java.util.Map;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
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.category.CategoryItemRenderer;
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.WindDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYZDataset;

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

/**
 * Chart factory creates Jfreechart instances. To add a new factory use the
 * <code>
 * 		CewolfChartFactory.registerFactory(new CewolfChartFactory() {...});
 * </code>
 * method.
 *
 * @author  Guido Laures
 */
public abstract class CewolfChartFactory implements ChartConstants, AxisConstants, LayoutConstants {

    // chart type string
  protected String chartType;
  // map contains registered factories, (String) chartType->CewolfChartFactory mappings
  private static Map factories = new HashMap();

    /** Creates a new instance of ChartFactory */
  protected CewolfChartFactory(String chartType) {
      this.chartType = chartType;
  }

  /**
   * Callback when the chart instance to be created.
     * @param title The title of chart
     * @param xAxisLabel label on x axis
     * @param yAxisLabel label on y axis
     * @param data The dataset to create chart for
     * @return The newly created JFreeChart instance
     *
     * @throws IncompatibleDatasetException If the incoming data is not compatible with this factory
     */
    public abstract JFreeChart getChartInstance(String title, String xAxisLabel, String yAxisLabel, Dataset data) throws IncompatibleDatasetException;

    //////////////// static part ///////////////////////

  /**
   * Register a new chart factory instance.
   * @param factory The factory to register
   */
  public static void registerFactory(CewolfChartFactory factory) {
      factories.put(factory.chartType, factory);
  }

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

  private static final int getLayoutConstant(String layout) {
    return LayoutTypes.typeList.indexOf(layout.toLowerCase());
  }
  
  static {
    // histogram chart type
    registerFactory(new CewolfChartFactory("histogram") {
	    public JFreeChart getChartInstance(String title, String xAxisLabel, String yAxisLabel, Dataset data) throws IncompatibleDatasetException {
	        check(data, IntervalXYDataset.class, this.chartType);
	        return ChartFactory.createHistogram(title, xAxisLabel, yAxisLabel, (IntervalXYDataset) data, PlotOrientation.VERTICAL, true, false, false);
	     }
    });
  }

  public static JFreeChart getChartInstance(String chartType, String title, String xAxisLabel, String yAxisLabel, Dataset data) throws ChartValidationException {
      // first check the dynamically registered chart types
      CewolfChartFactory factory = (CewolfChartFactory) factories.get(chartType);
      if (factory != null) {
          // custom factory found, use it
          return factory.getChartInstance(title, xAxisLabel, yAxisLabel, data);
      }

    switch (getChartTypeConstant(chartType)) {
      case XY :
        check(data, XYDataset.class, chartType);
        return ChartFactory.createXYLineChart(title, xAxisLabel, yAxisLabel, (XYDataset) data, PlotOrientation.VERTICAL, true, true, true);
      case PIE :
        check(data, PieDataset.class, chartType);
        return ChartFactory.createPieChart(title, (PieDataset) data, true, true, true);
      case AREA_XY :
        check(data, XYDataset.class, chartType);
        return ChartFactory.createXYAreaChart(title, xAxisLabel, yAxisLabel, (XYDataset) data, PlotOrientation.VERTICAL, true, false, false);
      case SCATTER :
        check(data, XYDataset.class, chartType);
        return ChartFactory.createScatterPlot(title, xAxisLabel, yAxisLabel, (XYDataset) data, PlotOrientation.VERTICAL, true, false, false);
      case AREA :
        check(data, CategoryDataset.class, chartType);
        return ChartFactory.createAreaChart(title, xAxisLabel, yAxisLabel, (CategoryDataset) data, PlotOrientation.VERTICAL, true, false, false);
      case HORIZONTAL_BAR :
        check(data, CategoryDataset.class, chartType);
        return ChartFactory.createBarChart(title, xAxisLabel, yAxisLabel, (CategoryDataset) data, PlotOrientation.HORIZONTAL, true, false, false);
      case HORIZONTAL_BAR_3D :
        check(data, CategoryDataset.class, chartType);
        return ChartFactory.createBarChart3D(title, xAxisLabel, yAxisLabel, (CategoryDataset) data, PlotOrientation.HORIZONTAL, true, false, false);
      case LINE :
        check(data, CategoryDataset.class, chartType);
        return ChartFactory.createLineChart(title, xAxisLabel, yAxisLabel, (CategoryDataset) data, PlotOrientation.VERTICAL, true, false, false);
      case STACKED_HORIZONTAL_BAR :
        check(data, CategoryDataset.class, chartType);

⌨️ 快捷键说明

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