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

📄 axischart.java

📁 jCharts是一个100%基于Java的制图工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/***********************************************************************************************
 * File Info: $Id: AxisChart.java,v 1.38 2003/04/19 01:40:33 nathaniel_auvil Exp $
 * Copyright (C) 2002
 * Author: Nathaniel G. Auvil
 * Contributor(s):
 *
 * Copyright 2002 (C) Nathaniel G. Auvil. All Rights Reserved.
 *
 * Redistribution and use of this software and associated documentation ("Software"), with or
 * without modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain copyright statements and notices.
 * 	Redistributions must also contain a copy of this document.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
 * 	conditions and the following disclaimer in the documentation and/or other materials
 * 	provided with the distribution.
 *
 * 3. The name "jCharts" or "Nathaniel G. Auvil" must not be used to endorse or promote
 * 	products derived from this Software without prior written permission of Nathaniel G.
 * 	Auvil.  For written permission, please contact nathaniel_auvil@users.sourceforge.net
 *
 * 4. Products derived from this Software may not be called "jCharts" nor may "jCharts" appear
 * 	in their names without prior written permission of Nathaniel G. Auvil. jCharts is a
 * 	registered trademark of Nathaniel G. Auvil.
 *
 * 5. Due credit should be given to the jCharts Project (http://jcharts.sourceforge.net/).
 *
 * THIS SOFTWARE IS PROVIDED BY Nathaniel G. Auvil AND CONTRIBUTORS ``AS IS'' AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
 * jCharts OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
 ************************************************************************************************/


package org.jCharts.axisChart;


import org.jCharts.Chart;
import org.jCharts.axisChart.axis.*;
import org.jCharts.axisChart.axis.scale.*;
import org.jCharts.chartText.NumericTagGroup;
import org.jCharts.chartText.TextTagGroup;
import org.jCharts.chartData.interfaces.*;
import org.jCharts.chartData.processors.AxisChartDataProcessor;
import org.jCharts.chartData.ChartDataException;
import org.jCharts.imageMap.ImageMap;
import org.jCharts.properties.*;
import org.jCharts.test.*;
import org.jCharts.types.ChartType;

import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.util.Iterator;


/****************************************************************************************************
 * This Class is used to create all axis chart types.  This class knows how to render charts based on
 *  the ChartType specified in on the iAxisChartDataSet.
 *
 *****************************************************************************************************/
public class AxisChart extends Chart implements HTMLChartTestable
{
	protected XAxis xAxis;
	protected YAxis yAxis;
	protected AxisProperties axisProperties;

	private IAxisDataSeries iAxisDataSeries;


	/**************************************************************************************************
	 * Constructor
	 *
	 * @param iAxisDataSeries
	 * @param chartProperties
	 * @param axisProperties
	 * @param legendProperties if no legend is desired, pass NULL
	 * @param pixelWidth
	 * @param pixelHeight
	 ***************************************************************************************************/
	public AxisChart( IAxisDataSeries iAxisDataSeries,
							ChartProperties chartProperties,
							AxisProperties axisProperties,
							LegendProperties legendProperties,
							int pixelWidth,
							int pixelHeight )
	{
		super( legendProperties, chartProperties, pixelWidth, pixelHeight );
		this.axisProperties = axisProperties;
		this.iAxisDataSeries = iAxisDataSeries;
	}


	/*************************************************************************************************
	 *
	 * @return IAxisDataSeries
	 *************************************************************************************************/
	public IAxisDataSeries getIAxisDataSeries()
	{
		return this.iAxisDataSeries;
	}


	/********************************************************************************************
	 * ScatterPlots create a subclass of AxisChartDataProcessor so we need this method so we can
	 * 	overload it.
	 *
	 * @return AxisChartDataProcessor
	 ********************************************************************************************/
	public AxisChartDataProcessor createAxisChartDataProcessor()
	{
		return new AxisChartDataProcessor();
	}

   /************************************************************************************************
	 * Once we determine which axis is the data axis, the logic to set it up is the same whether it
	 * 	is a horizontal or vertical plot.
	 *
	 * @param dataAxisProperties
	 * @param axisChartDataProcessor
	 * @param fontRenderContext
	 * @return NumericTagGroup need to set this on the right axis
	 ************************************************************************************************/
	protected NumericTagGroup setupDataAxisProperties( Axis axis,
																	 DataAxisProperties dataAxisProperties,
																	 AxisChartDataProcessor axisChartDataProcessor,
																	 FontRenderContext fontRenderContext )
	{
		if( dataAxisProperties.getScaleCalculator() == null )
		{
			ScaleCalculator s;

			if( dataAxisProperties.hasUserDefinedScale() )
			{
				s = new UserDefinedScaleCalculator( dataAxisProperties.getUserDefinedMinimumValue(), dataAxisProperties.getUserDefinedIncrement() );
			}
			else
			{
				s = new AutomaticScaleCalculator();
				s.setMaxValue( axisChartDataProcessor.getMaxValue() );
				s.setMinValue( axisChartDataProcessor.getMinValue() );
			}

			axis.setScaleCalculator( s );
		}
		else
		{
			axis.setScaleCalculator( dataAxisProperties.getScaleCalculator() );
			axis.getScaleCalculator().setMaxValue( axisChartDataProcessor.getMaxValue() );
			axis.getScaleCalculator().setMinValue( axisChartDataProcessor.getMinValue() );
		}

		axis.getScaleCalculator().setRoundingPowerOfTen( dataAxisProperties.getRoundToNearest() );
		axis.getScaleCalculator().setNumberOfScaleItems( dataAxisProperties.getNumItems() );
		axis.getScaleCalculator().computeScaleValues();


//		if( dataAxisProperties.showAxisLabels() )
		{
//TODO what if they do not want to display axis labels?
//todo we still need to know how to size the axis
			NumericTagGroup numericTagGroup = new NumericTagGroup( dataAxisProperties.getScaleChartFont(),
																					 fontRenderContext,
																					 dataAxisProperties.useDollarSigns(),
																					 dataAxisProperties.usePercentSigns(),
																					 dataAxisProperties.useCommas(),
																					 dataAxisProperties.getRoundToNearest() );

			numericTagGroup.createAxisScaleLabels( axis.getScaleCalculator() );
			return numericTagGroup;
		}
/*
		else
		{
			return null;
		}
*/
	}


	/***************************************************************************************
	 *
	 *
	 * @param axisChartDataProcessor
	 * @param fontRenderContext
	 **************************************************************************************/
	protected void setupAxis( AxisChartDataProcessor axisChartDataProcessor, FontRenderContext fontRenderContext ) throws ChartDataException
	{
      IDataSeries iDataSeries= (IDataSeries) this.getIAxisDataSeries();

		if( this.axisProperties.isPlotHorizontal() )
		{
			//---X AXIS---------------------------------------------------------------------------
			DataAxisProperties dataAxisProperties = (DataAxisProperties) this.getAxisProperties().getXAxisProperties();
			this.xAxis = new XAxis( this, dataAxisProperties.getNumItems() );

			NumericTagGroup numericTagGroup= setupDataAxisProperties( this.xAxis, dataAxisProperties, axisChartDataProcessor, fontRenderContext );
			this.xAxis.setAxisLabelsGroup( numericTagGroup );


			//---Y AXIS---------------------------------------------------------------------------
			AxisTypeProperties axisTypeProperties = this.getAxisProperties().getYAxisProperties();
			this.yAxis = new YAxis( this, axisChartDataProcessor.getNumberOfElementsInADataSet() );
			if( axisTypeProperties.showAxisLabels() )
			{
				TextTagGroup textTagGroup = new TextTagGroup( axisTypeProperties.getScaleChartFont(), fontRenderContext );

				//LOOP
				for( int i = 0; i < iDataSeries.getNumberOfAxisLabels(); i++ )
				{
					if( iDataSeries.getAxisLabel( i ) == null )
					{
						throw new ChartDataException( "None of the axis labels can be NULL." );
					}
					textTagGroup.addLabel( iDataSeries.getAxisLabel( i ) );
				}

				this.yAxis.setAxisLabelsGroup( textTagGroup );
			}
		}
		else
		{
			//---X AXIS---------------------------------------------------------------------------
			AxisTypeProperties axisTypeProperties = this.getAxisProperties().getXAxisProperties();
			this.xAxis = new XAxis( this, axisChartDataProcessor.getNumberOfElementsInADataSet() );
			if( axisTypeProperties.showAxisLabels() )
			{
				TextTagGroup textTagGroup = new TextTagGroup( axisTypeProperties.getScaleChartFont(), fontRenderContext );

				//LOOP
				for( int i = 0; i < iDataSeries.getNumberOfAxisLabels(); i++ )
				{
					if( iDataSeries.getAxisLabel( i ) == null )
					{
						throw new ChartDataException( "None of the axis labels can be NULL." );
					}
					textTagGroup.addLabel( iDataSeries.getAxisLabel( i ) );
				}

				this.xAxis.setAxisLabelsGroup( textTagGroup );
			}

			//---Y AXIS---------------------------------------------------------------------------
			DataAxisProperties dataAxisProperties = ( DataAxisProperties ) this.getAxisProperties().getYAxisProperties();
			this.yAxis = new YAxis( this, dataAxisProperties.getNumItems() );
			NumericTagGroup numericTagGroup= setupDataAxisProperties( this.yAxis, dataAxisProperties, axisChartDataProcessor, fontRenderContext );
			this.yAxis.setAxisLabelsGroup( numericTagGroup );
		}


		//---if yAxisTitle is null, do not show title
		this.yAxis.computeMinimumWidthNeeded( iDataSeries.getYAxisTitle() );
		this.xAxis.computeMinimumHeightNeeded( iDataSeries.getXAxisTitle() );
	}


	/***********************************************************************************************
	 * Finalizes the size of both Axis and sets the origin position
	 *
	 * @param xAxisWidth
	 * @param yAxisHeight
	 * @param chartTitleHeight
	 **********************************************************************************************/
	private void sizeAndPositionAxis( float xAxisWidth, float yAxisHeight, float chartTitleHeight )
	{
		//---SUBTRACT space for axis titles, labels, ticks...
		xAxisWidth -= this.yAxis.getMinimumWidthNeeded();
		yAxisHeight -= this.xAxis.getMinimumHeightNeeded();

		//---SET THE PIXEL LENGTH OF THE AXIS
		this.xAxis.setPixelLength( xAxisWidth );

		if( axisProperties.getYAxisProperties().showAxisLabels() )
		{
			this.yAxis.setPixelLength( yAxisHeight - this.yAxis.getAxisLabelsGroup().getTallestLabel() / 2 );
		}
		else
		{
			this.yAxis.setPixelLength( yAxisHeight );
		}

		if( this.getLegend() != null )
		{
			//---SET THE ORIGIN COORDINATES
			if( ( this.getLegend().getLegendProperties().getPlacement() == LegendAreaProperties.RIGHT )
				|| ( this.getLegend().getLegendProperties().getPlacement() == LegendAreaProperties.BOTTOM ) )
			{
				this.xAxis.setOrigin( this.yAxis.getMinimumWidthNeeded() + super.getChartProperties().getEdgePadding() );
				this.yAxis.setOrigin( yAxisHeight + super.getChartProperties().getEdgePadding() + chartTitleHeight );
			}
			else if( this.getLegend().getLegendProperties().getPlacement() == LegendAreaProperties.LEFT )
			//---else, LegendAreaProperties.LEFT, OR LegendAreaProperties.TOP
			{
				this.xAxis.setOrigin( super.getImageWidth() - xAxisWidth - super.getChartProperties().getEdgePadding() );
				this.yAxis.setOrigin( yAxisHeight + super.getChartProperties().getEdgePadding() + chartTitleHeight );
			}
			else if( this.getLegend().getLegendProperties().getPlacement() == LegendAreaProperties.TOP )
			{
				this.xAxis.setOrigin( this.yAxis.getMinimumWidthNeeded() + super.getChartProperties().getEdgePadding() );
				this.yAxis.setOrigin( super.getImageHeight() - super.getChartProperties().getEdgePadding() - this.xAxis.getMinimumHeightNeeded() );
			}
		}
		else
		{
         this.xAxis.setOrigin( this.yAxis.getMinimumWidthNeeded() + super.getChartProperties().getEdgePadding() );
			this.yAxis.setOrigin( yAxisHeight + super.getChartProperties().getEdgePadding() + chartTitleHeight );
		}
	}


	/******************************************************************************************
	 *
	 *****************************************************************************************/
	protected void deriveAxisValues()
	{
		this.xAxis.computeLabelFilter();
		this.xAxis.computeShouldTickStartAtYAxis( this.iAxisDataSeries, this.axisProperties.getXAxisProperties() );

		if( this.axisProperties.isPlotHorizontal() )
		{
			//DataAxisProperties dataAxisProperties = (DataAxisProperties) this.axisProperties.getXAxisProperties();
			//LabelAxisProperties labelAxisProperties= (LabelAxisProperties) this.axisProperties.getYAxisProperties();

⌨️ 快捷键说明

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