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

📄 chartcomponent.java

📁 OLAP 的客户端代码
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/*
 * ====================================================================
 * This software is subject to the terms of the Common Public License
 * Agreement, available at the following URL:
 *   http://www.opensource.org/licenses/cpl.html .
 * Copyright (C) 2003-2004 TONBELLER AG.
 * All Rights Reserved.
 * You must accept the terms of that agreement to use this software.
 * ====================================================================
 */
package com.tonbeller.jpivot.chart;

import java.awt.Color;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpSession;
import javax.xml.parsers.DocumentBuilder;

import org.apache.log4j.Logger;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryLabelPosition;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.CategoryLabelWidthType;
import org.jfree.chart.entity.ChartEntity;
import org.jfree.chart.entity.EntityCollection;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.chart.title.Title;
import org.jfree.chart.urls.CategoryURLGenerator;
import org.jfree.chart.urls.StandardCategoryURLGenerator;
import org.jfree.chart.urls.StandardPieURLGenerator;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.text.TextBlockAnchor;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.RectangleAnchor;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.TextAnchor;
import org.jfree.ui.VerticalAlignment;
import org.jfree.util.TableOrder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.tonbeller.jpivot.core.ModelChangeEvent;
import com.tonbeller.jpivot.core.ModelChangeListener;
import com.tonbeller.jpivot.olap.model.Cell;
import com.tonbeller.jpivot.olap.model.Member;
import com.tonbeller.jpivot.olap.model.OlapModel;
import com.tonbeller.jpivot.olap.model.Position;
import com.tonbeller.jpivot.olap.model.Result;
import com.tonbeller.jpivot.olap.navi.DrillThrough;
import com.tonbeller.jpivot.olap.navi.MemberTree;
import com.tonbeller.wcf.component.Component;
import com.tonbeller.wcf.component.ComponentSupport;
import com.tonbeller.wcf.controller.Dispatcher;
import com.tonbeller.wcf.controller.DispatcherSupport;
import com.tonbeller.wcf.controller.RequestContext;
import com.tonbeller.wcf.controller.RequestListener;
import com.tonbeller.wcf.table.ITableComponent;
import com.tonbeller.wcf.table.TableModel;
import com.tonbeller.wcf.utils.DomUtils;
import com.tonbeller.wcf.utils.XmlUtils;

/**
 * Contains a reference to an OlapModel.
 * Produces a chart image.
 *
 */
public class ChartComponent extends ComponentSupport implements ModelChangeListener {
	private static Logger logger = Logger.getLogger(ChartComponent.class);

	String ref;
	Document document;
	OlapModel olapModel;
	boolean dirty = true;
	Locale locale;

	Result result;
	Iterator cellIterator;
	int dimCount;
	Element rootElement;
	int colCount;

	// servlet mapping - optionally can be set as a servlet context parameter
	// with name "chartServlet"
	// for example:
	/*
	<context-param>
	  <param-name>chartServlet</param-name>
	  <param-value>/path/to/chartServlet</param-value>
	</context-param>
	*/
	String CHART_SERVLET = "/DisplayChart";
	final String CHART_SERVLET_KEY = "chartServlet";
    boolean baseDisplayURLSet = false;
    String webControllerURL = "";
    
	String filename = null;
	// chart properties
	final int DEFAULT_CHART_WIDTH = 500;
	final int DEFAULT_CHART_HEIGHT = 300;
	String chartTitle = "";
	java.awt.Font titleFont = JFreeChart.DEFAULT_TITLE_FONT;
	String fontName = "SansSerif";
	int fontStyle = java.awt.Font.BOLD;
	int fontSize = 18;
	String slicerFontName = "SansSerif";
	int slicerFontStyle = java.awt.Font.PLAIN;
	int slicerFontSize = 12;
	String axisFontName = "SansSerif";
	int axisFontStyle = java.awt.Font.PLAIN;
	int axisFontSize = 12;
	String axisTickFontName = "SansSerif";
	int axisTickFontStyle = java.awt.Font.PLAIN;
	int axisTickFontSize = 12;
	String legendFontName = "SansSerif";
	int legendFontStyle = java.awt.Font.PLAIN;
	int legendFontSize = 10;
	int legendPosition = 3; //RectangleEdge.BOTTOM;
	int slicerPosition = 1; // BOTTOM - see chartpropertiesform.xsl
	int slicerAlignment = 3; // LEFT
	//java.awt.Color bgColor = java.awt.Color.white;
	int bgColorR = 255;
	int bgColorG = 255;
	int bgColorB = 255;
	int chartType = 1;
	int chartHeight = DEFAULT_CHART_HEIGHT;
	int chartWidth = DEFAULT_CHART_WIDTH;
	String horizAxisLabel = "";
	String vertAxisLabel = "";
	boolean showLegend = true;
	boolean showSlicer = true;
	boolean showTooltips = true;
	boolean drillThroughEnabled = false;
        int tickLabelRotate = 30;   //default 30 degree rotation

	ChartRenderingInfo info = null;

	Dispatcher dispatcher = new DispatcherSupport();

	/**
	 * Constructor
	 * @param id the id of this component
	 * @param parent parent component
	 * @param ref a reference to an olap model
	 * @param baseDisplayURL URL for chart servlet. Overrides default
	 * @param controllerURL URL for web application where JPivot/WCF is running. Overrides default
	 * @param context RequestContext
	 */
	public ChartComponent(String id, Component parent, String ref, String baseDisplayURL, String controllerURL, RequestContext context) {
		super(id, parent);
		this.ref = ref;

		this.olapModel = (OlapModel) context.getModelReference(ref);
		this.olapModel.addModelChangeListener(this);
		this.locale = context.getLocale();
		// extend the controller
		getDispatcher().addRequestListener(null, null, dispatcher);
		// optional servlet context parameter for chart servlet location
        String chartServlet = baseDisplayURL;
		if ( chartServlet == null ) {
            chartServlet = context.getServletContext().getInitParameter(CHART_SERVLET_KEY);
		} else {
            baseDisplayURLSet = true;
        }
        if ( chartServlet != null ) {
            this.CHART_SERVLET = chartServlet;
        }
        if (controllerURL != null) {
        	this.webControllerURL = controllerURL;
        }
	}
    
    public ChartComponent(String id, Component parent, String ref, RequestContext context) {
        this(id, parent, ref, null, null, context);
    }
    
	/**
	 * called once by the creating tag
	 */
	public void initialize(RequestContext context) throws Exception {
		super.initialize(context);
	}

	/**
	 * Entry point for producing charts, called by wcf render tag.
	 * Produces a jfreechart dataset from olap model, then creates a chart and
	 * writes it to the servlet container temp directory.
	 * Returns a DOM document for Renderer to transform into html.
	 * Requires that jfreechart servlet is installed in this application context.
	 */
	public Document render(RequestContext context) throws Exception {
		// check if we need to produce a new chart
		if (dirty) {
			// clear old listeners
			dispatcher.clear();
			this.result = olapModel.getResult();
			this.cellIterator = result.getCells().iterator();
			this.dimCount = result.getAxes().length;
			DefaultCategoryDataset dataset = null;
                        switch (dimCount) {
			  case 1 :
				logger.info("1-dim data");
				dataset = build1dimDataset();
				break;
			  case 2 :
				logger.info("2-dim data");
				dataset = build2dimDataset();
				break;
			  default :
				logger.error("less than 1 or more than 2 dimensions");
				throw new IllegalArgumentException("ChartRenderer requires a 1 or 2 dimensional result");
			}
			// re-set dirty flag
			dirty = false;

			// create font
			titleFont = new java.awt.Font(fontName, fontStyle, fontSize);

			CategoryURLGenerator urlGenerator = new jpivotCategoryURLGenerator(webControllerURL);

			// Create the chart object
			JFreeChart chart=null;
			switch (chartType) {
				case 1 :
					chart = ChartFactory.createBarChart(
						chartTitle,
						titleFont,
						horizAxisLabel,
						vertAxisLabel,
						dataset,
						PlotOrientation.VERTICAL,
						showLegend,
						showTooltips,
						drillThroughEnabled,
						urlGenerator
						);
					break;
				case 2 :
					chart = ChartFactory.createBarChart3D(
						chartTitle,
						titleFont,
						horizAxisLabel,
						vertAxisLabel,
						dataset,
						PlotOrientation.VERTICAL,
						showLegend,
						showTooltips,
						drillThroughEnabled,
						urlGenerator
						);
					break;
				case 3 :
					chart = ChartFactory.createBarChart(
						chartTitle,
						titleFont,
						horizAxisLabel,
						vertAxisLabel,
						dataset,
						PlotOrientation.HORIZONTAL,
						showLegend,
						showTooltips,
						drillThroughEnabled,
						urlGenerator
						);
					break;
				case 4 :
					chart = ChartFactory.createBarChart3D(
						chartTitle,
						titleFont,
						horizAxisLabel,
						vertAxisLabel,
						dataset,
						PlotOrientation.HORIZONTAL,
						showLegend,
						showTooltips,
						drillThroughEnabled,
						urlGenerator
						);
					break;
				case 5 :
					chart = ChartFactory.createStackedBarChart(
						chartTitle,
						titleFont,
						horizAxisLabel,
						vertAxisLabel,
						dataset,
						PlotOrientation.VERTICAL,
						showLegend,
						showTooltips,
						drillThroughEnabled,
						urlGenerator
						);
					break;
				case 6 :
					chart = ChartFactory.createStackedBarChart3D(
						chartTitle,
						titleFont,
						horizAxisLabel,
						vertAxisLabel,
						dataset,
						PlotOrientation.VERTICAL,
						showLegend,
						showTooltips,
						drillThroughEnabled,
						urlGenerator
						);
					break;
				case 7 :
					chart = ChartFactory.createStackedBarChart(
						chartTitle,
						titleFont,
						horizAxisLabel,
						vertAxisLabel,
						dataset,
						PlotOrientation.HORIZONTAL,
						showLegend,
						showTooltips,
						drillThroughEnabled,
						urlGenerator
						);
					break;
				case 8 :
					chart = ChartFactory.createStackedBarChart3D(
						chartTitle,
						titleFont,
						horizAxisLabel,
						vertAxisLabel,
						dataset,
						PlotOrientation.HORIZONTAL,
						showLegend,
						showTooltips,
						drillThroughEnabled,
						urlGenerator
						);
					break;
				case 9 :
					chart = ChartFactory.createLineChart(
						chartTitle,
						titleFont,
						horizAxisLabel,
						vertAxisLabel,
						dataset,
						PlotOrientation.VERTICAL,
						showLegend,
						showTooltips,
						drillThroughEnabled,
						urlGenerator
						);
					break;
				case 10 :
					chart = ChartFactory.createLineChart(
						chartTitle,
						titleFont,
						horizAxisLabel,
						vertAxisLabel,
						dataset,
						PlotOrientation.HORIZONTAL,
						showLegend,
						showTooltips,
						drillThroughEnabled,
						urlGenerator
						);
					break;
				case 11 :
					chart = ChartFactory.createAreaChart(
						chartTitle,
						titleFont,
						horizAxisLabel,
						vertAxisLabel,
						dataset,
						PlotOrientation.VERTICAL,
						showLegend,
						showTooltips,
						drillThroughEnabled,
						urlGenerator

⌨️ 快捷键说明

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