📄 grapher.java
字号:
}
// Draw Y axis value grid and labels
valueFormat.setScaling( true, false ); // always scale the label values
if ( graphDef.showGridY() )
{
ValueMarker[] valueList = vGrid.getValueMarkers();
for (int i = 0; i < valueList.length; i++)
{
int valRel = chartGraph.getY( valueList[i].getValue() );
valueFormat.setFormat( valueList[i].getValue(), 2, 0 );
String label = (valueFormat.getScaledValue() + " " + valueFormat.getPrefix()).trim();
if ( majorY && valueList[i].isMajor() )
{
g.setColor( majColor );
g.setStroke( dStroke );
g.drawLine( graphOriginX, graphOriginY - valRel, graphOriginX + chartWidth, graphOriginY - valRel );
g.setStroke( defaultStroke );
g.drawLine( graphOriginX - 2, graphOriginY - valRel, graphOriginX + 2, graphOriginY - valRel );
g.drawLine( graphOriginX + chartWidth - 2, graphOriginY - valRel, graphOriginX + chartWidth + 2, graphOriginY - valRel );
graphString( g, label, graphOriginX - (label.length() * nfont_width) - 7, graphOriginY - valRel + nfont_height/2 - 1 );
}
else if ( minorY )
{
g.setColor( minColor );
g.setStroke( dStroke );
g.drawLine( graphOriginX, graphOriginY - valRel, graphOriginX + chartWidth, graphOriginY - valRel );
g.setStroke( defaultStroke );
g.drawLine( graphOriginX - 1, graphOriginY - valRel, graphOriginX + 1, graphOriginY - valRel );
g.drawLine( graphOriginX + chartWidth - 1, graphOriginY - valRel, graphOriginX + chartWidth + 1, graphOriginY - valRel );
}
}
}
}
/**
* Plots all comments and legends on graph.
* @param g Handle of a Graphics2D context to draw on.
* @throws RrdException Thrown in case of a JRobin specific error.
*/
private void plotComments( Graphics2D g ) throws RrdException
{
if ( !graphDef.showLegend() ) return;
LinkedList markerList = new LinkedList();
// Position the cursor just below the chart area
int posy = y_offset + chartHeight + CHART_UPADDING + CHART_BPADDING + ( graphDef.showMajorGridX() ? nfont_height : 0 );
int posx = LBORDER_SPACE;
g.setColor( normalFontColor );
g.setFont( normal_font );
Comment[] clist = graphDef.getComments();
StringBuffer tmpStr = new StringBuffer("");
boolean newLine = false;
boolean drawText = false;
for (int i = 0; i < clist.length; i++)
{
if ( clist[i].commentType == Comment.CMT_LEGEND )
{
markerList.addLast( new LegendMarker( tmpStr.length() * nfont_width, ((Legend) clist[i]).getColor() ) );
tmpStr.append( " " ); // Add 3 spaces where the mark will be
}
else if ( clist[i].commentType == Comment.CMT_GPRINT )
((Gprint) clist[i]).setValue( sources, sourceIndex, valueFormat );
ArrayList tknpairs = clist[i].getTokens();
for (int j = 0; j < tknpairs.size(); j++)
{
String str = (String) tknpairs.get(j++);
Byte tkn = (Byte) tknpairs.get(j);
if ( clist[i].trimString() )
tmpStr.append( str.trim() );
else
tmpStr.append( str );
if ( tkn != Comment.TKN_NULL )
{
drawText = true;
if ( tkn == Comment.TKN_ALF ) {
newLine = true;
posx = LBORDER_SPACE;
}
else if ( tkn == Comment.TKN_ARF ) {
newLine = true;
posx = imgWidth - RBORDER_SPACE - (tmpStr.length() * nfont_width);
}
else if ( tkn == Comment.TKN_ACF ) {
newLine = true;
posx = imgWidth / 2 - (tmpStr.length() * nfont_width) / 2;
}
else if ( tkn == Comment.TKN_AL )
posx = LBORDER_SPACE;
else if ( tkn == Comment.TKN_AR )
posx = imgWidth - RBORDER_SPACE - (tmpStr.length() * nfont_width);
else if ( tkn == Comment.TKN_AC )
posx = imgWidth / 2 - (tmpStr.length() * nfont_width) / 2;
}
if ( !newLine && clist[i].addSpacer() )
tmpStr.append( SPACER );
// Plot the string
if ( drawText ) {
graphString( g, tmpStr.toString(), posx, posy );
tmpStr = new StringBuffer("");
drawText = false;
// Plot the markers
while ( !markerList.isEmpty() ) {
LegendMarker lm = (LegendMarker) markerList.removeFirst();
g.setColor( lm.getColor() );
g.fillRect( posx + lm.getXPosition(), posy - 9, 10, 10 );
g.setColor( normalFontColor );
g.drawRect( posx + lm.getXPosition(), posy - 9, 10, 10 );
}
}
if ( newLine ) {
posy += nfont_height + LINE_PADDING;
newLine = false;
}
}
}
if ( tmpStr.length() > 0)
{
posx = LBORDER_SPACE;
graphString( g, tmpStr.toString(), posx, posy );
tmpStr = new StringBuffer("");
drawText = false;
// Plot the markers
while ( !markerList.isEmpty() ) {
LegendMarker lm = (LegendMarker) markerList.removeFirst();
g.setColor( lm.getColor() );
g.fillRect( posx + lm.getXPosition(), posy - 9, 10, 10 );
g.setColor( normalFontColor );
g.drawRect( posx + lm.getXPosition(), posy - 9, 10, 10 );
}
}
}
/**
* Plots a possible overlay image over the current graph. All white pixels
* are ignored and treated as 100% transparent.
* @param g Handle of a Graphics2D context to draw on.
*/
private void plotOverlay( Graphics2D g )
{
// If overlay drawing fails, just ignore it
try
{
File overlayImg = graphDef.getOverlay();
if ( overlayImg != null )
{
BufferedImage img = ImageIO.read(overlayImg);
int w = img.getWidth();
int h = img.getHeight();
int rgbWhite = Color.WHITE.getRGB();
int pcolor, red, green, blue;
// For better performance we might want to load all color
// ints of the overlay in one go
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
pcolor = img.getRGB(i, j);
if ( pcolor != rgbWhite )
{
red = (pcolor >> 16) & 0xff;
green = (pcolor >> 8) & 0xff;
blue = pcolor & 0xff;
g.setColor( new Color(red, green, blue) );
g.drawLine( i, j, i, j );
}
}
}
}
} catch (IOException e) {}
}
/**
* Plots the graph title in the corresponding title font.
* @param g Handle of a Graphics2D context to draw on.
*/
private void plotImageTitle( Graphics2D g )
{
Title graphTitle = graphDef.getTitle();
// No title to draw
if ( graphTitle == null )
return;
// Position the cursor just above the chart area
int posy = tfont_height - 1 + UBORDER_SPACE;
int posx = LBORDER_SPACE;
// Set drawing specifics
g.setColor( graphDef.getTitleFontColor() );
g.setFont( title_font );
// Parse and align the title text
StringBuffer tmpStr = new StringBuffer("");
boolean newLine = false;
ArrayList tknpairs = graphTitle.getTokens();
for (int j = 0; j < tknpairs.size(); j++)
{
String str = (String) tknpairs.get(j++);
Byte tkn = (Byte) tknpairs.get(j);
tmpStr.append( str );
if ( tkn != Comment.TKN_NULL )
{
if ( tkn == Comment.TKN_ALF ) {
newLine = true;
posx = LBORDER_SPACE;
}
else if ( tkn == Comment.TKN_ARF ) {
newLine = true;
posx = imgWidth - RBORDER_SPACE - (tmpStr.length() * tfont_width) - tfont_width;
}
else if ( tkn == Comment.TKN_ACF ) {
newLine = true;
posx = imgWidth / 2 - (tmpStr.length() * tfont_width) / 2;
}
else if ( tkn == Comment.TKN_AL )
posx = LBORDER_SPACE;
else if ( tkn == Comment.TKN_AR )
posx = imgWidth - RBORDER_SPACE - (tmpStr.length() * tfont_width) - tfont_width;
else if ( tkn == Comment.TKN_AC )
posx = imgWidth / 2 - (tmpStr.length() * tfont_width) / 2;
}
else { // default is a center alignment for title
posx = imgWidth / 2 - (tmpStr.length() * tfont_width) / 2;
}
// Plot the string
g.drawString( tmpStr.toString(), posx, posy );
tmpStr = new StringBuffer("");
// Go to next line
if ( newLine )
{
posy += tfont_height + LINE_PADDING;
newLine = false;
}
}
}
/**
* Plots the vertical label on the left hand side of the chart area.
* @param g Handle of a Graphics2D context to draw on.
*/
private void plotVerticalLabel( Graphics2D g )
{
String valueAxisLabel = graphDef.getVerticalLabel();
if ( valueAxisLabel == null )
return;
g.setColor( normalFontColor );
int labelWidth = valueAxisLabel.length() * nfont_width;
// draw a rotated label text as vertical label
g.setFont( normal_font );
g.rotate( -Math.PI/2.0 );
graphString( g, valueAxisLabel, - y_offset - CHART_UPADDING
- chartHeight / 2
- labelWidth / 2,
LBORDER_SPACE + nfont_height
);
g.rotate( Math.PI/2.0 );
}
/**
* Draws the standard JRobin signature on the image.
* @param g Handle of a Graphics2D context to draw on.
*/
private void plotSignature( Graphics2D g )
{
if ( !graphDef.showSignature() )
return;
String sig = "www.jrobin.org";
g.setColor( Color.GRAY );
g.setFont( new Font("Courier", Font.PLAIN, 10) );
g.rotate( Math.PI/2.0 );
g.drawString( sig, 5, - imgWidth + 9 );
g.rotate( -Math.PI/2.0 );
}
/**
* Graphs a text string onto a graphics2d context, using the specified default font color.
* @param g Handle of a Graphics2D context to draw on.
* @param str String to draw.
* @param x X start position of the string.
* @param y Y start position of the string.
*/
private void graphString( Graphics2D g, String str, int x, int y )
{
Color oc = g.getColor();
g.setColor( normalFontColor );
g.drawString( str, x, y );
g.setColor( oc );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -