barvaluegroup.java

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

JAVA
600
字号
		}
		else if( position == BarChartProperties.END_OF_BAR )
		{
			//System.out.println("END");
			offset = ( isVertical ) ? barRect.height : barRect.width;
			offset -= ( isVertical ) ? verticalPadding : horizontalPadding;
			offset -= ( isVertical ) ? stringHeight : stringWidth;
		}
		else if( position == BarChartProperties.CENTER_OF_BAR )
		{
			//System.out.println("CENTER");
			offset = ( isVertical ) ? barRect.height / 2.0f : barRect.width / 2.0f;
			offset += ( isVertical ) ? verticalPadding : horizontalPadding;
			offset -= ( isVertical ) ? stringHeight / 2.0f : stringWidth / 2.0f;
		}
		else if( position == BarChartProperties.BASE_OF_BAR )
		{
			//System.out.println("BASE");
			offset += ( isVertical ) ? verticalPadding : horizontalPadding;
		}
		else if( position == BarChartProperties.CENTER_OF_CHART )
		{
			//System.out.println("CENTER_CHART");
			offset = centerChart;
			offset -= ( isVertical ) ? stringHeight / 2.0f : stringWidth / 2.0f;
		}
		else
		{
			return null;
		}

		if( isVertical )
		{
			displayPosition.y += barRect.height - offset;
			displayPosition.x += ( barRect.width - stringWidth ) / 2.0f;
		}
		else
		{
			displayPosition.x += offset;
			displayPosition.y += ( barRect.height ) / 2.0f;
			// Why does this need to be 3? 2 should work right.
			displayPosition.y += ( stringHeight ) / 2.0f;
		}

		return displayPosition;
*/
		return null;
	}


	/** Not sure why this was left out of Rectangle2D...
	 */
	private float getRight( Rectangle2D.Float r )
	{
		return r.x + r.width;
	}


	/** Not sure why this was left out of Rectangle2D...
	 */
	private float getBottom( Rectangle2D.Float r )
	{
		return r.y + r.height;
	}


	/*******************************************************************************************************
	 * Gets the rectangle associated (by the user) for the given bar value.
	 * Meant only to be called by BarChart.render()
	 ********************************************************************************************************/
	private void setBarValuePosition( BarValue barValue, Rectangle2D.Float bounds )
	{
/*
		if( barValue == null ) return;

		//boolean moved = false;

		//---Extra space needed above (or below) baseline
		float ySpace = ( barValue.getHeight() / 2 ) + verticalPadding;

		//---Holder to contain the coordinates for text display.
		Rectangle2D.Float displayPosition;

		//---The rectangle for the Bar on (in, around) which we display
		Rectangle2D.Float barRect = barValue.getBarRect();

		//---Position relative to the Bar
		int position = barValue.getStartPosition();

		//---Remap if in the negative area
		if( barValue.getIsNegative() )
		{
			if( barValuePosition == BarChartProperties.END_OF_BAR )
			{
				position = BarChartProperties.BASE_OF_BAR;
			}
			else if( barValuePosition == BarChartProperties.BEYOND_BAR )
			{
				position = BarChartProperties.BEFORE_BAR;
			}
			else if( barValuePosition == BarChartProperties.BASE_OF_BAR )
			{
				position = BarChartProperties.END_OF_BAR;
			}
		}

		//---The string to be displayed
		//String str = barValue.getText();

		//---Debug
		//System.out.println("Position:"+str+" at ("+position+")");

		//---Get default coordinates.
		displayPosition = getBarValueRectangleCoordinates( position, barValue );

		//---Special case, if before, then we move to base if too far left or down
		//---Should only happen on charts with negative values.
		if( position == BarChartProperties.BEFORE_BAR )
		{
			if( ( isVertical == false && displayPosition.x < bounds.x ) ||
			   ( isVertical && getBottom( displayPosition ) > getBottom( bounds ) ) )
			{
				position = BarChartProperties.BASE_OF_BAR;
				//moved = true;
				displayPosition = getBarValueRectangleCoordinates( position, barValue );
				//System.out.println("0:Moved "+str+" to BASE_OF_BAR");
			}
		}

		//---Do we have to shift it over a bit?
		//---Does it run outside the bars rectangle?
		if( position == BarChartProperties.BASE_OF_BAR ||
		   position == BarChartProperties.CENTER_OF_BAR )
		{
			// Ignore ones that are too wide in vertical chart, or tall in horizontal chart
			if( ( isVertical && ( displayPosition.y - ( ySpace ) ) < barRect.y ) ||
			   ( isVertical == false && getRight( displayPosition ) > getRight( barRect ) ) )
			{
				// Move needed
				position = BarChartProperties.END_OF_BAR;

				//moved = true;
				displayPosition = getBarValueRectangleCoordinates( position, barValue );
				//System.out.println("1:Moved "+str+" to END_OF_BAR");
			}
		}

		//---Does it run over left/bottom edge of chart (bounded area)?
		if( position == BarChartProperties.END_OF_BAR ||
		   position == BarChartProperties.CENTER_OF_BAR )
		{
			if( ( isVertical == false && displayPosition.x < bounds.x ) ||
			   ( isVertical && ( getBottom( displayPosition ) + ySpace ) > getBottom( bounds ) ) )
			{
				position = BarChartProperties.BEYOND_BAR;
				//moved = true;
				displayPosition = getBarValueRectangleCoordinates( position, barValue );
				//System.out.println("2:Moved "+str+" to BEYOND_BAR");
			}
		}

		//---Does it run over right/top edge of chart (bounds)?
		if( position == BarChartProperties.BEYOND_BAR )
		{
			boolean mustMove = false;

			if( isVertical == false )
			{
				if( getRight( displayPosition ) > getRight( bounds ) )
				{
					//System.out.println("Fail " + (displayPosition.x + displayPosition.width) + " > " + (bounds.x + bounds.width));
					mustMove = true;
				}
			}
			else
			{
				if( ( displayPosition.y + ySpace ) <= bounds.y )
				{
					mustMove = true;
				}
			}

			if( mustMove )
			{
				position = BarChartProperties.END_OF_BAR;
				//moved = true;
				displayPosition = getBarValueRectangleCoordinates( position, barValue );

				//System.out.println("3:Moved "+str+" to END_OF_BAR");
			}
		}

		barValue.setPosition( displayPosition.x, displayPosition.y );
*/
	}


	/** Adds a bar value for the given data item, if barValues are on
	 */
	public void addBarValue( int i, double dataValue, Rectangle2D.Float barRect )
	{
/*
		if( this.showBarValues == false )
		{
			return;
		}

		BarValue barValue = null;

		String str = getBarValueAt( i, dataValue );

		if( str != null )
		{

			float originX = axisChart.getXAxisOrigin();
			float originY = axisChart.getYAxisOrigin();
			float startingCoordinate = axisChart.getDataAxis().getZeroLineCoordinate();

			Rectangle2D.Float bounds = new Rectangle2D.Float();

			bounds.x = originX;
			bounds.y = originY;

			bounds.width = axisChart.getHorizontalAxis().getPixelLength();
			bounds.height = axisChart.getVerticalAxis().getPixelLength();

			if( dataValue < 0 )
			{
				if( isVertical )
				{
					//-- Bound on entire chart.
					bounds.y = originY - bounds.height;
				}
				else
				{
					//-- Bound on entire chart - already okay.
				}
			}
			else
			{
				if( isVertical )
				{
					//-- Bound from starting to top of chart.
					bounds.y -= bounds.height;
					bounds.height -= ( originY - startingCoordinate );
				}
				else
				{
					//-- Bound from starting to right side of chart
					bounds.x = startingCoordinate;
					bounds.width -= ( startingCoordinate - originX );
				}
			}

			barValue = new BarValue( str,
			   barValueFont,
			   fontRenderContext,
			   barRect,
			   barValuePosition,
			   ( dataValue < 0 ) );

			setBarValuePosition( barValue, bounds );

			if( textTagList == null )
			{
				textTagList = new ArrayList();
			}

			textTagList.add( barValue );
		}
*/
	}


	/** renders any barValues
	 */
	public void render( Graphics2D g2d )
	{
		if( textTagList == null || this.showBarValues == false )
		{
			return;
		}

		TextTag tag;
		Paint p = this.barValueFontColor;

		g2d.setFont( this.barValueFont );

		for( int i = 0; i < textTagList.size(); i++ )
		{
			tag = ( TextTag ) textTagList.get( i );
			tag.render( g2d, p );
			// Text tag sets the Paint every time if not null
			// So, we might save a small bit of time by changing
			// this to null after first time.
			p = null;
		}
	}
}

⌨️ 快捷键说明

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