📄 legend.java
字号:
/***********************************************************************************************
* 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;
import org.jCharts.chartData.interfaces.*;
import org.jCharts.chartData.processors.TextProcessor;
import org.jCharts.properties.*;
import org.jCharts.test.HTMLGenerator;
import org.jCharts.test.HTMLTestable;
import org.jCharts.types.ChartType;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
/*************************************************************************************
*
* @author Nathaniel Auvil, Sandor Dornbush, Sundar Balasubramanian
* @version $Id: Legend.java,v 1.17 2003/04/19 01:39:17 nathaniel_auvil Exp $
************************************************************************************/
final public class Legend implements HTMLTestable, Serializable
{
private Chart chart;
private LegendProperties legendProperties;
private float iconSide;
//---derived values
private float widestLabelAndColumnPadding;
private int numColumns;
private int numRows;
private TextProcessor textProcessor;
private float x;
private float y;
private float width = 0;
private float height = 0;
//---used to extract the legendLabels and paints from the data set and make them easy to loop through
private ArrayList labels;
private ArrayList paints;
private ArrayList shapes = new ArrayList();
private ArrayList fillPointsFlags = new ArrayList();
private ArrayList pointOutlinePaints = new ArrayList();
/*********************************************************************************************
*
* @param chart
* @deprecated
**********************************************************************************************/
public Legend( Chart chart )
{
this.chart = chart;
}
/*********************************************************************************************
*
* @param chart
* @param legendProperties
**********************************************************************************************/
public Legend( Chart chart, LegendProperties legendProperties )
{
this.chart = chart;
this.legendProperties = legendProperties;
}
public void setX( float x )
{
this.x = x;
}
public void setY( float y )
{
this.y = y;
}
/*****************************************************************************************
*
* @param iAxisDataSeries
* @param chartTitleHeight
****************************************************************************************/
public void computeLegendXY( IAxisDataSeries iAxisDataSeries, float chartTitleHeight )
{
//---PROCESS the size needed for drawing the legend.
this.calculateDrawingValues( iAxisDataSeries );
if( (this.getLegendProperties().getPlacement() == LegendAreaProperties.RIGHT)
|| (this.getLegendProperties().getPlacement() == LegendAreaProperties.LEFT) )
{
if( this.getHeight() > this.chart.getImageHeight() - this.chart.getChartProperties().getEdgePadding() * 2 )
{
this.setY( this.chart.getChartProperties().getEdgePadding() );
}
else
{
this.setY( (this.chart.getImageHeight() / 2) - (this.getHeight() / 2) );
}
if( this.getLegendProperties().getPlacement() == LegendAreaProperties.RIGHT )
{
this.setX( this.chart.getImageWidth() - this.getWidth() - this.chart.getChartProperties().getEdgePadding() );
}
else //---else, LegendAreaProperties.LEFT
{
this.setX( this.chart.getChartProperties().getEdgePadding() );
}
}
else //---LegendAreaProperties.BOTTOM, OR LegendAreaProperties.TOP
{
if( this.getWidth() + this.chart.getChartProperties().getEdgePadding() * 2 > this.chart.getImageWidth() )
{
this.setX( this.chart.getChartProperties().getEdgePadding() );
}
else
{
this.setX( (this.chart.getImageWidth() / 2) - (this.getWidth() / 2) );
}
if( this.getLegendProperties().getPlacement() == LegendAreaProperties.BOTTOM )
{
this.setY( this.chart.getImageHeight() - this.getHeight() - this.chart.getChartProperties().getEdgePadding() );
}
else //---else, LegendAreaProperties.TOP
{
this.setY( this.chart.getChartProperties().getEdgePadding() + chartTitleHeight );
}
}
}
/**********************************************************************************************
* Central method for processing data; try to minimize looping.
* 1) calculate the maximum height of labels
* 2) find the maximum label width
*
* @param iAxisDataSeries
**********************************************************************************************/
private void processData( IAxisDataSeries iAxisDataSeries )
{
this.textProcessor = new TextProcessor();
Iterator iterator = iAxisDataSeries.getIAxisPlotDataSetIterator();
//LOOP
while( iterator.hasNext() )
{
this.processLegendLabels( (IAxisPlotDataSet) iterator.next() );
}
}
/**********************************************************************************************
* Central method for processing data; try to minimize looping.
* 1) calculate the maximum height of labels
* 2) find the maximum label width
*
* @param iPieChartDataSet
**********************************************************************************************/
private void processData( IPieChartDataSet iPieChartDataSet )
{
this.textProcessor = new TextProcessor();
this.processLegendLabels( iPieChartDataSet );
}
/**********************************************************************************************
* Method for processing data for AxisPlot datasets; try to minimize
* looping.
* 1) calculate the maximum height of labels
* 2) find the maximum label width
*
* @param iAxisPlotDataSet
* *********************************************************************************************/
private void processLegendLabels( IAxisPlotDataSet iAxisPlotDataSet )
{
for( int i = 0; i < iAxisPlotDataSet.getNumberOfLegendLabels(); i++ )
{
//---StockChartDataSets could have NULLs depending on the data
if( iAxisPlotDataSet.getLegendLabel( i ) != null )
{
this.textProcessor.addLabel( iAxisPlotDataSet.getLegendLabel( i ), this.legendProperties.getFont(), this.chart.getGraphics2D().getFontRenderContext() );
//---pair labels with paints to get around ugly piechart vs axischart data structure mess
this.labels.add( iAxisPlotDataSet.getLegendLabel( i ) );
this.paints.add( iAxisPlotDataSet.getPaint( i ) );
if( iAxisPlotDataSet.getChartType().equals( ChartType.POINT ) )
{
PointChartProperties pointChartProperties = (PointChartProperties) iAxisPlotDataSet.getChartTypeProperties();
this.shapes.add( pointChartProperties.getShape( i ) );
this.fillPointsFlags.add( new Boolean( pointChartProperties.getFillPointsFlag( i ) ) );
this.pointOutlinePaints.add( pointChartProperties.getPointOutlinePaints( i ) );
}
}
}
}
/**********************************************************************************************
* Method for processing data for PieCharts; try to minimize looping.
* 1) calculate the maximum height of labels
* 2) find the maximum label width
* @param iPieChartDataSet
* ********************************************************************************************/
private void processLegendLabels( IPieChartDataSet iPieChartDataSet )
{
for( int i = 0; i < iPieChartDataSet.getNumberOfLegendLabels(); i++ )
{
//---StockChartDataSets could have NULLs depending on the data
if( iPieChartDataSet.getLegendLabel( i ) != null )
{
this.textProcessor.addLabel( iPieChartDataSet.getLegendLabel( i ), this.legendProperties.getFont(), this.chart.getGraphics2D().getFontRenderContext() );
//---pair labels with paints to get around ugly piechart vs axischart data structure mess
this.labels.add( iPieChartDataSet.getLegendLabel( i ) );
this.paints.add( iPieChartDataSet.getPaint( i ) );
}
}
}
/************************************************************************************************
*
*************************************************************************************************/
public LegendProperties getLegendProperties()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -