📄 legend.java
字号:
{
return this.legendProperties;
}
/************************************************************************************************
* Calculates the width and height needed to display the Legend. Use the getWidth() and
* getHeight() methods to extract this information.
*
* @param iData can pass either the IPieChartDataSet or the IChartDataSeries to this.
************************************************************************************************/
public void calculateDrawingValues( IData iData )
{
int numberOfLabels;
this.labels = new ArrayList();
this.paints = new ArrayList();
if( iData instanceof IAxisDataSeries )
{
IAxisDataSeries iAxisDataSeries = (IAxisDataSeries) iData;
this.processData( iAxisDataSeries );
numberOfLabels = iAxisDataSeries.getTotalNumberOfDataSets();
}
else
{
IPieChartDataSet iPieChartDataSet = (IPieChartDataSet) iData;
this.processData( iPieChartDataSet );
numberOfLabels = iPieChartDataSet.getNumberOfLegendLabels();
}
//---make the icon proportional to the Font being used.
this.iconSide = (float) .50 * this.textProcessor.getTallestLabel();
this.determineWidthAndHeight( numberOfLabels );
}
/********************************************************************************************
*
********************************************************************************************/
public float getWidth()
{
return this.width;
}
/********************************************************************************************
*
********************************************************************************************/
public int getHeight()
{
//why not return a float here?
return ((int) Math.ceil( this.height ));
}
/**********************************************************************************************
* Determines the dimensions needed for the Legend and creates the image for it.
*
**********************************************************************************************/
private void determineWidthAndHeight( int numberOfLabels )
{
//---start with the padding no matter how many columns or specified width
width = this.legendProperties.getEdgePadding() * 2;
height = width;
//---if don't care how many columns or the number of labels is less than num columns specified, all in one row.
if( this.legendProperties.getNumColumns() == LegendAreaProperties.COLUMNS_AS_MANY_AS_NEEDED
|| this.legendProperties.getNumColumns() >= numberOfLabels )
{
this.numColumns = numberOfLabels;
width += this.textProcessor.getTotalLabelWidths();
this.numRows = 1;
}
//---else, more than one row
else
{
//---one less addition to do when looping.
this.widestLabelAndColumnPadding = this.textProcessor.getWidestLabel() + this.legendProperties.getColumnPadding();
if( legendProperties.getNumColumns() == LegendAreaProperties.COLUMNS_FIT_TO_IMAGE )
{
// calculate that the columns match exactly
float actualWidth = legendProperties.getSize().width;
float widestLabelColumnAndIcon =
widestLabelAndColumnPadding +
iconSide +
legendProperties.getIconPadding() +
legendProperties.getColumnPadding();
numColumns = (int) (actualWidth / widestLabelColumnAndIcon);
numColumns = Math.min( numColumns, numberOfLabels );
}
else
{
numColumns = this.legendProperties.getNumColumns();
}
width += this.textProcessor.getWidestLabel() * this.numColumns;
this.numRows = (int) Math.ceil( (double) numberOfLabels / (double) this.numColumns );
}
//---account for icons
width += (this.iconSide + this.legendProperties.getIconPadding()) * this.numColumns;
//---account for space between each column
width += this.legendProperties.getColumnPadding() * (this.numColumns - 1);
//---account for each row
height += (this.textProcessor.getTallestLabel() * this.numRows);
//---account for each row padding
height += (this.legendProperties.getRowPadding() * (this.numRows - 1));
}
/**********************************************************************************************
* Renders the legend.
*
**********************************************************************************************/
public void render()
{
Graphics2D g2d = this.chart.getGraphics2D();
//---get the bounds of the image
Rectangle2D.Float rectangle = new Rectangle2D.Float( this.x, this.y, this.width - 1, this.height - 1 );
//---fill the background of the Legend with the specified Paint
if( this.legendProperties.getBackgroundPaint() != null )
{
g2d.setPaint( this.legendProperties.getBackgroundPaint() );
g2d.fill( rectangle );
}
//---draw Legend border
if( this.legendProperties.getBorderStroke() != null )
{
this.legendProperties.getBorderStroke().draw( g2d, rectangle );
}
//---dont think we want this so text will be clean but leave commented out.
//g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF );
//---set the font and text color.
g2d.setFont( this.legendProperties.getFont() );
//---icon coordinates
rectangle.y += this.legendProperties.getEdgePadding() + (this.textProcessor.getTallestLabel() / 2) - (this.iconSide / 2);
rectangle.width = this.iconSide;
rectangle.height = this.iconSide;
float posX = this.x + this.legendProperties.getEdgePadding();
float fontY = rectangle.y + rectangle.height;
//---pre calculate utility values
float yIncrement = this.textProcessor.getTallestLabel() + this.legendProperties.getRowPadding();
float iconAndPaddingWidth = this.iconSide + this.legendProperties.getIconPadding();
int labelIndex = 0;
//LOOP
for( int j = 0; j < this.numRows; j++ )
{
//LOOP
for( int i = 0; i < this.numColumns; i++ )
{
rectangle.x = posX;
//---display icon
g2d.setPaint( (Paint) this.paints.get( labelIndex ) );
//---get the original transform so can reset it.
AffineTransform affineTransform = g2d.getTransform();
if( this.shapes.size() > 0 )
{
//---translate the Shape into position.
g2d.translate( rectangle.x, rectangle.y );
if( ((Boolean) this.fillPointsFlags.get( labelIndex )).booleanValue() )
{
g2d.fill( (Shape) this.shapes.get( labelIndex ) );
//---if we are filling the points, see if we should outline the Shape
if( this.pointOutlinePaints.get( labelIndex ) != null )
{
g2d.setPaint( (Paint) this.pointOutlinePaints.get( labelIndex ) );
g2d.draw( (Shape) this.shapes.get( labelIndex ) );
}
}
else
{
g2d.draw( (Shape) this.shapes.get( labelIndex ) );
}
g2d.setTransform( affineTransform );
}
else
{
g2d.fill( rectangle );
//---border around icon
if( this.legendProperties.getIconBorderStroke() != null )
{
g2d.setStroke( this.legendProperties.getIconBorderStroke() );
g2d.setPaint( this.legendProperties.getIconBorderPaint() );
g2d.draw( rectangle );
}
}
//---draw the label
g2d.setPaint( this.legendProperties.getFontPaint() );
posX += iconAndPaddingWidth;
g2d.drawString( (String) this.labels.get( labelIndex ), posX, fontY );
if( this.legendProperties.getNumColumns() == LegendAreaProperties.COLUMNS_AS_MANY_AS_NEEDED
|| this.legendProperties.getNumColumns() >= this.labels.size() )
{
//---each column is as wide as it needs to be
posX += this.textProcessor.getTextTag( labelIndex ).getWidth() + this.legendProperties.getColumnPadding();
}
else
{
//---all columns have the same width
posX += this.widestLabelAndColumnPadding;
}
labelIndex++;
//---if no more labels, we are done.
if( labelIndex == this.labels.size() ) break;
}
posX = this.x + this.legendProperties.getEdgePadding();
fontY += yIncrement;
rectangle.y += yIncrement;
}
}
/*********************************************************************************************
* Enables the testing routines to display the contents of this Object.
*
* @param htmlGenerator
**********************************************************************************************/
public void toHTML( HTMLGenerator htmlGenerator )
{
htmlGenerator.legendTableStart();
htmlGenerator.addTableRow( "Width", Float.toString( this.width ) );
htmlGenerator.addTableRow( "Height", Float.toString( this.height ) );
htmlGenerator.addTableRow( "Icon Side", Float.toString( this.iconSide ) );
htmlGenerator.innerTableRowStart();
this.legendProperties.toHTML( htmlGenerator );
htmlGenerator.innerTableRowEnd();
htmlGenerator.legendTableEnd();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -