barvaluegroup.java

来自「jCharts是一个100%基于Java的制图工具」· Java 代码 · 共 600 行 · 第 1/2 页

JAVA
600
字号
/***********************************************************************************************
 * File Info: $Id: BarValueGroup.java,v 1.1 2002/12/05 22:07:28 nathaniel_auvil Exp $
 * Copyright (C) 2000
 * Author: John Thomsen
 * 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.chartText;


import org.jCharts.chartData.interfaces.IAxisChartDataSet;
import org.jCharts.properties.AxisProperties;
import org.jCharts.properties.BarChartProperties;
import org.jCharts.chartText.TextTag;
import org.jCharts.axisChart.AxisChart;

import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.text.NumberFormat;
import java.util.ArrayList;


/**
 */
public class BarValueGroup
{

	/** Helper class to handle a barValue - wraps TextTag
	 * Meant to be called only by BarValueGroup class.
	 */
	protected class BarValue extends TextTag
	{
		private Rectangle2D.Float barRect;
		//private Rectangle2D.Float bounds;
		private int startPosition = 0;
		private boolean isNegative = false;
		private String text;


		public BarValue( String text,
		                 Font font,
		                 FontRenderContext fontRenderContext,
		                 Rectangle2D.Float barRect,
		                 int startPosition,
		                 boolean isNegative )
		{
			super( text, font, fontRenderContext );
			this.text = text;
			this.barRect = barRect;
			this.startPosition = startPosition;
			this.isNegative = isNegative;
		}


		public Rectangle2D.Float getBarRect()
		{
			return this.barRect;
		}


		public int getStartPosition()
		{
			return this.startPosition;
		}


		public boolean getIsNegative()
		{
			return this.isNegative;
		}


		public String getText()
		{
			return this.text;
		}

	}
	// -- End of inner class BarValue


	private AxisChart axisChart;
	//private IAxisChartDataSet iAxisChartDataSet;

	// Values to create or get once...
	private FontRenderContext fontRenderContext;
	private boolean isVertical;
	private BarChartProperties barChartProperties;
	private AxisProperties axisProperties;
	private NumberFormat numberFormat;

	private boolean showBarValues;
	private int barValuePosition;
	private int barValueItem;
	private Font barValueFont;
	private Paint barValueFontColor;

	private float horizontalPadding;
	private float verticalPadding;

	private double totalDataValue = 0.0d;

	private float centerChart = 0.0f;

	private ArrayList textTagList;


	public BarValueGroup( AxisChart axisChart, IAxisChartDataSet iAxisChartDataSet )
	{
/*
		this.barChartProperties = ( BarChartProperties ) iAxisChartDataSet.getChartTypeProperties();
		this.showBarValues = barChartProperties.getShowBarValues();

		//---NOTE: Aborting HERE if not needed.
		if( this.showBarValues == false ) return; // <--- Possible return.

		this.axisChart = axisChart;
		//this.iAxisChartDataSet = iAxisChartDataSet;

		this.axisProperties = axisChart.getAxisProperties();
		this.isVertical = ( axisProperties.getOrientation() == AxisProperties.VERTICAL );

		this.barValuePosition = barChartProperties.getShowBarValuesPosition();
		this.barValueItem = barChartProperties.getShowBarValuesItemToDisplay();
		this.barValueFont = barChartProperties.getShowBarValuesFont();
		this.barValueFontColor = barChartProperties.getShowBarValuesFontColor();

		for( int i = 0; i < iAxisChartDataSet.getNumberOfDataItems(); i++ )
		{
			double dataValue = iAxisChartDataSet.getValue( 0, i );
			totalDataValue += ( dataValue > 0 ) ? dataValue : -dataValue;
		}

		int precision = barChartProperties.getShowBarValuesPrecision();

		if( barChartProperties.getShowBarValuesAsMoney() )
		{
			numberFormat = NumberFormat.getCurrencyInstance();
		}

		if( precision > 0 || numberFormat != null )
		{
			if( numberFormat == null )
			{
				numberFormat = NumberFormat.getInstance();
			}

			numberFormat.setMaximumFractionDigits( precision );
			numberFormat.setMinimumFractionDigits( precision );
		}

		if( isVertical )
		{
			this.verticalPadding = barChartProperties.getShowBarValuesVerticalPadding();
		}
		else
		{
			this.horizontalPadding = barChartProperties.getShowBarValuesHorizontalPadding();
		}

		this.fontRenderContext = axisChart.getGraphics2D().getFontRenderContext();

		this.centerChart = ( isVertical ) ? axisChart.getVerticalAxisPixelLength() : axisChart.getHorizontalAxisPixelLength();
		this.centerChart /= 2.0f;

		textTagList = null;
*/
	}


	/*******************************************************************************************************
	 * Gets the text associated (by the user) for the given bar value.
	 * Meant only to be called by BarChart.render()
	 ********************************************************************************************************/
	private String getBarValueAt( int i, double dataValue )
	{
/*
		String str = "";

		if( barValueItem == BarChartProperties.SHOW_CUSTOM_STRINGS )
		{
			return barChartProperties.getBarValuesString( i );
		}

		if( barValueItem == BarChartProperties.SHOW_LABEL ||
		   barValueItem == BarChartProperties.SHOW_LABEL_AND_PERCENTAGE ||
		   barValueItem == BarChartProperties.SHOW_LABEL_AND_VALUE )
		{
			str = axisChart.getIDataSeries().getLabelAxisLabel( i );
		}

		if( barValueItem != BarChartProperties.SHOW_LABEL )
		{
			String pct = "";

			if( barValueItem == BarChartProperties.SHOW_LABEL_AND_PERCENTAGE ||
			   barValueItem == BarChartProperties.SHOW_LABEL_AND_VALUE )
			{
				str += ": ";
			}

			if( barValueItem == BarChartProperties.SHOW_PERCENTAGE ||
			   barValueItem == BarChartProperties.SHOW_LABEL_AND_PERCENTAGE )
			{
				dataValue = ( dataValue / totalDataValue ) * 100;
				pct = "%";
			}

			if( numberFormat == null )
			{
				str += ( int ) dataValue + pct;
			}
			else
			{
				str += numberFormat.format( dataValue ) + pct;
			}
		}

		return str;
*/
		return null;
	}


	/*******************************************************************************************************
	 * Gets the rectangle coords associated (by the user) for the given bar value.
	 * Meant only to be called by BarChart.render()
	 ********************************************************************************************************/
	private Rectangle2D.Float getBarValueRectangleCoordinates( int position, BarValue barValue )
	{
		//---Called by getBarValueRectangle

		//---Will this spacingwork for Vertical Text? Probably not.

/*
		float stringWidth = barValue.getWidth();
		float stringHeight = barValue.getHeight();
		Rectangle2D.Float barRect = barValue.getBarRect();

		Rectangle2D.Float displayPosition = new Rectangle2D.Float(
		   barRect.x,
		   barRect.y,
		   stringWidth + ( 2 * horizontalPadding ),
		   stringHeight + ( 2 * verticalPadding ) );

		float offset = 0.0f;

		if( position == BarChartProperties.BEYOND_BAR )
		{
			//System.out.println("BEYOND");
			offset = ( isVertical ) ? barRect.height : barRect.width;
			offset += ( isVertical ) ? verticalPadding : horizontalPadding;
		}
		else if( position == BarChartProperties.BEFORE_BAR )
		{
			//System.out.println("BEFORE");
			offset -= ( isVertical ) ? stringHeight : stringWidth;
			offset -= ( isVertical ) ? verticalPadding : horizontalPadding;

⌨️ 快捷键说明

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