leif 02/03/04 19:59:51
Modified:
src/scratchpad/org/apache/avalon/excalibur/altprofile/profiler/gui
LineChart.java
Log:
Make the chart paint correctly when the component size is shrunk down to
very small widths or heights.
Revision Changes Path
1.5 +240 -227
jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/altprofile/profiler/gui/LineChart.java
Index: LineChart.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/altprofile/profiler/gui/LineChart.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- LineChart.java 5 Mar 2002 03:52:56 -0000 1.4
+++ LineChart.java 5 Mar 2002 03:59:51 -0000 1.5
@@ -25,7 +25,7 @@
* Draws a nice pretty chart given a set of data.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Leif Mortenson</a>
- * @version CVS $Revision: 1.4 $ $Date: 2002/03/05 03:52:56 $
+ * @version CVS $Revision: 1.5 $ $Date: 2002/03/05 03:59:51 $
* @since 4.1
*/
@@ -313,163 +313,149 @@
int chartTop = insets.top + 5;
int chartWidth = size.width - chartLeft - insets.right - 1 - 5;
int chartHeight = size.height - chartTop - insets.bottom - 1 -
fontHeight;
-
+
// Draw the horizontal grid (Left to Right lines)
- int horizonalLineLabeledInterval = (int)Math.ceil(
- (float)fontHeight / ( yLabelInterval * chartHeight / ( m_max -
m_min ) ) );
- int horizontalLineNumber = 0;
- for ( int i = ( (int)Math.ceil( (float)m_min / yLabelInterval ) ) *
yLabelInterval;
- i < m_max; i += yLabelInterval )
- {
- // Calculate the location of the line on the y-axis. Be careful
of very large numbers
- int y = chartTop + chartHeight -
- (int)( (long)chartHeight * ( i - m_min ) / ( m_max - m_min )
);
-
- if ( horizontalLineNumber >= horizonalLineLabeledInterval )
- {
- horizontalLineNumber = 0;
- }
- if ( horizontalLineNumber == 0 )
- {
- String lbl = m_intFormat.format( i );
- g.setColor( m_frameColor );
- g.drawString( lbl, chartLeft - 5 - fontMetrics.stringWidth(
lbl ),
- y + fontHalfHeight );
-
- g.setColor( m_darkGridColor );
- }
- else
+ if ( chartHeight > 0 )
+ {
+ int horizonalLineLabeledInterval = (int)Math.ceil(
+ (float)fontHeight / ( yLabelInterval * chartHeight / ( m_max
- m_min ) ) );
+ int horizontalLineNumber = 0;
+ for ( int i = ( (int)Math.ceil( (float)m_min / yLabelInterval )
) * yLabelInterval;
+ i < m_max; i += yLabelInterval )
{
- g.setColor( m_lightGridColor );
+ // Calculate the location of the line on the y-axis. Be
careful of very large numbers
+ int y = chartTop + chartHeight -
+ (int)( (long)chartHeight * ( i - m_min ) / ( m_max -
m_min ) );
+
+ if ( horizontalLineNumber >= horizonalLineLabeledInterval )
+ {
+ horizontalLineNumber = 0;
+ }
+ if ( horizontalLineNumber == 0 )
+ {
+ String lbl = m_intFormat.format( i );
+ g.setColor( m_frameColor );
+ g.drawString( lbl, chartLeft - 5 -
fontMetrics.stringWidth( lbl ),
+ y + fontHalfHeight );
+
+ g.setColor( m_darkGridColor );
+ }
+ else
+ {
+ g.setColor( m_lightGridColor );
+ }
+ horizontalLineNumber++;
+
+ if ( chartWidth > 0 )
+ {
+ g.drawLine( chartLeft, y, chartLeft + chartWidth, y );
+ }
}
- horizontalLineNumber++;
-
- g.drawLine( chartLeft, y, chartLeft + chartWidth, y );
}
// Draw the vertical grid (Top to Bottom lines)
-
- // Figure out how wide a label is for formatting.
- String format = MessageFormat.format( m_format, new String[] { "00",
"00","00", "00" } );
- int fw = fontMetrics.stringWidth( format ) + 10;
-
- // Figure out what internal of lines we can place labels under.
- int verticalLineLabeledInterval;
- if ( ( m_values.length > 0 ) && ( chartWidth > 0 ) )
+ if ( chartWidth > 0 )
{
- verticalLineLabeledInterval = (int)Math.ceil( (float)fw /
- ( m_lineSampleInterval * chartWidth / ( m_values.length ) )
);
- }
- else
- {
- verticalLineLabeledInterval = 1;
- }
-
- // Calculate a base time for drawing the vertical lines.
- long baseTime = ( ( m_time - m_values.length * m_sampleInterval ) /
- ( m_sampleInterval * m_lineSampleInterval ) ) *
- ( m_sampleInterval * m_lineSampleInterval );
-
- // Draw each of the lines.
- int verticalLineNumber = 0;
- for ( int i = 0; i < m_values.length; i++ )
- {
- long time = m_time - ( m_values.length - i - 1 ) *
m_sampleInterval;
- if ( ( ( ( time - baseTime ) / m_sampleInterval ) %
m_lineSampleInterval ) == 0 )
+ // Figure out how wide a label is for formatting.
+ String format = MessageFormat.format( m_format, new String[] {
"00", "00","00", "00" } );
+ int fw = fontMetrics.stringWidth( format ) + 10;
+
+ // Figure out what internal of lines we can place labels under.
+ int verticalLineLabeledInterval;
+ if ( ( m_values.length > 0 ) && ( chartWidth > 0 ) )
{
- int x = chartLeft + i * chartWidth / ( m_values.length - 1 );
-
- // Draw a label under the line if line should have a label.
- if ( ( verticalLineNumber >= verticalLineLabeledInterval ) ||
- ( verticalLineNumber == 0 ) )
+ verticalLineLabeledInterval = (int)Math.ceil( (float)fw /
+ ( m_lineSampleInterval * chartWidth / ( m_values.length
) ) );
+ }
+ else
+ {
+ verticalLineLabeledInterval = 1;
+ }
+
+ // Calculate a base time for drawing the vertical lines.
+ long baseTime = ( ( m_time - m_values.length * m_sampleInterval
) /
+ ( m_sampleInterval * m_lineSampleInterval ) ) *
+ ( m_sampleInterval * m_lineSampleInterval );
+
+ // Draw each of the lines.
+ int verticalLineNumber = 0;
+ for ( int i = 0; i < m_values.length; i++ )
+ {
+ long time = m_time - ( m_values.length - i - 1 ) *
m_sampleInterval;
+ if ( ( ( ( time - baseTime ) / m_sampleInterval ) %
m_lineSampleInterval ) == 0 )
{
- format = getFormattedTime( new Date( time ), false );
+ int x = chartLeft + i * chartWidth / ( m_values.length -
1 );
- if ( x - fontMetrics.stringWidth( format ) / 2 >=
chartLeft )
+ // Draw a label under the line if line should have a
label.
+ if ( ( verticalLineNumber >= verticalLineLabeledInterval
) ||
+ ( verticalLineNumber == 0 ) )
{
- g.setColor( m_frameColor );
- g.drawString( format, x - fontMetrics.stringWidth(
format ) / 2,
- chartTop + chartHeight + fontHeight );
-
- g.setColor( m_darkGridColor );
- verticalLineNumber = 1;
+ format = getFormattedTime( new Date( time ), false );
+
+ if ( x - fontMetrics.stringWidth( format ) / 2 >=
chartLeft )
+ {
+ g.setColor( m_frameColor );
+ g.drawString( format, x -
fontMetrics.stringWidth( format ) / 2,
+ chartTop + chartHeight + fontHeight );
+
+ g.setColor( m_darkGridColor );
+ verticalLineNumber = 1;
+ }
+ else
+ {
+ g.setColor( m_lightGridColor );
+ }
}
else
{
g.setColor( m_lightGridColor );
+ verticalLineNumber++;
+ }
+
+ // Draw the vertical line
+ if ( chartHeight > 0 )
+ {
+ g.drawLine( x, chartTop, x, chartTop + chartHeight );
}
}
- else
- {
- g.setColor( m_lightGridColor );
- verticalLineNumber++;
- }
-
- // Draw the vertical line
- g.drawLine( x, chartTop, x, chartTop + chartHeight );
}
}
-
- // Draw the frame
- g.setColor( m_frameColor );
- g.drawLine( chartLeft, chartTop, chartLeft, chartTop + chartHeight );
- g.drawLine( chartLeft, chartTop + chartHeight, chartLeft +
chartWidth,
- chartTop + chartHeight );
-
-
-
- // Draw the the values that make up the data of the chart.
- if ( ( m_averageWindow > 0 ) && ( m_mousePressed ) )
+ if ( ( chartWidth > 0 ) && ( chartHeight > 0 ) )
{
- g.setColor( m_lightLineColor );
- }
- else
- {
- g.setColor( m_lineColor );
- }
+ // Draw the frame
+ g.setColor( m_frameColor );
+ g.drawLine( chartLeft, chartTop, chartLeft, chartTop +
chartHeight );
+ g.drawLine( chartLeft, chartTop + chartHeight, chartLeft +
chartWidth,
+ chartTop + chartHeight );
- int lastX = 0;
- int lastY = 0;
- for ( int i = 0; i < m_values.length; i++ )
- {
- // Calculate the location of the point on the x-axis.
- int x = chartLeft + i * chartWidth / ( m_values.length - 1 );
- // Calculate the location of the line on the y-axis. Be careful
of very large numbers
- int y = chartTop + chartHeight -
- (int)( (long)chartHeight * ( m_values[i] - m_min ) / ( m_max
- m_min ) );
- if ( i > 0 )
+ // Draw the the values that make up the data of the chart.
+ if ( ( m_averageWindow > 0 ) && ( m_mousePressed ) )
{
- g.drawLine( lastX, lastY, x, y );
+ g.setColor( m_lightLineColor );
+ }
+ else
+ {
+ g.setColor( m_lineColor );
}
- lastX = x;
- lastY = y;
- }
-
- // Draw the averaged values of the chart
- if ( ( m_averageWindow > 0 ) && ( m_mousePressed ) )
- {
- g.setColor( m_lineColor );
- lastX = 0;
- lastY = 0;
- for ( int i = m_averageWindow; i < m_averageWindowValues.length;
i++ )
+ int lastX = 0;
+ int lastY = 0;
+ for ( int i = 0; i < m_values.length; i++ )
{
// Calculate the location of the point on the x-axis.
- int x = chartLeft + i * chartWidth / (
m_averageWindowValues.length - 1 );
+ int x = chartLeft + i * chartWidth / ( m_values.length - 1 );
- // Calculate the location of the line on the y-axis. Be
careful of very large
- // numbers. The float value average valus makes this easy
here.
+ // Calculate the location of the line on the y-axis. Be
careful of very large numbers
int y = chartTop + chartHeight -
- (int)( chartHeight * ( m_averageWindowValues[i] - m_min
) /
- ( m_max - m_min ) );
+ (int)( (long)chartHeight * ( m_values[i] - m_min ) / (
m_max - m_min ) );
- if ( i > m_averageWindow )
+ if ( i > 0 )
{
g.drawLine( lastX, lastY, x, y );
}
@@ -477,138 +463,165 @@
lastX = x;
lastY = y;
}
- }
-
- // Make the label visible if the user tracks over any part of the
chart.
- if ( ( m_mouseOver ) && ( m_mouseX >= chartLeft ) &&
- ( m_mouseX <= chartLeft + chartWidth ) )
- {
- // Figure out the index of the data point where the mouse is.
- int index = (int)Math.round(
- (float)( m_values.length - 1 ) * ( m_mouseX - chartLeft ) /
chartWidth );
-
- // Draw the label
- int mouseDataPointX = 0;
- int mouseDataPointY = 0;
- String mouseDataPointValue = null;
- long mouseDataPointTime = 0;
- boolean showLabel = false;
+ // Draw the averaged values of the chart
if ( ( m_averageWindow > 0 ) && ( m_mousePressed ) )
{
- if ( ( index >= m_averageWindow ) && ( index <
m_averageWindowValues.length ) )
+ g.setColor( m_lineColor );
+ lastX = 0;
+ lastY = 0;
+ for ( int i = m_averageWindow; i <
m_averageWindowValues.length; i++ )
{
- // Draw the label for the average data
-
// Calculate the location of the point on the x-axis.
- mouseDataPointX = chartLeft + index * chartWidth /
- ( m_averageWindowValues.length - 1 );
+ int x = chartLeft + i * chartWidth / (
m_averageWindowValues.length - 1 );
// Calculate the location of the line on the y-axis. Be
careful of very large
// numbers. The float value average valus makes this
easy here.
- mouseDataPointY = chartTop + chartHeight -
- (int)( chartHeight * ( m_averageWindowValues[index]
- m_min ) /
- ( m_max - m_min ) );
-
- // Round the average value to 2 decimal places.
- mouseDataPointValue = m_floatFormat.format(
m_averageWindowValues[index] );
-
- // Get the time at the mouse data point
- mouseDataPointTime = m_time -
- ( m_averageWindowValues.length - index - 1 ) *
m_sampleInterval;
-
- showLabel = true;
- }
- }
- else
- {
- if ( ( index >= 0 ) && ( index < m_values.length ) )
- {
- // Draw the label for the regular data.
-
- // Calculate the location of the point on the x-axis.
- mouseDataPointX = chartLeft + index * chartWidth / (
m_values.length - 1 );
-
- // Calculate the location of the line on the y-axis. Be
careful of very large
- // numbers.
- mouseDataPointY = chartTop + chartHeight -
- (int)( (long)chartHeight * ( m_values[index] - m_min
) /
+ int y = chartTop + chartHeight -
+ (int)( chartHeight * ( m_averageWindowValues[i] -
m_min ) /
( m_max - m_min ) );
- // Get the average value.
- mouseDataPointValue = m_intFormat.format(
m_values[index] );
-
- // Get the time at the mouse data point
- mouseDataPointTime = m_time - ( m_values.length - index
- 1 ) *
- m_sampleInterval;
+ if ( i > m_averageWindow )
+ {
+ g.drawLine( lastX, lastY, x, y );
+ }
- showLabel = true;
+ lastX = x;
+ lastY = y;
}
}
- if ( showLabel )
+ // Make the label visible if the user tracks over any part of
the chart.
+ if ( ( m_mouseOver ) && ( m_mouseX >= chartLeft ) &&
+ ( m_mouseX <= chartLeft + chartWidth ) )
{
- // Draw a cross at the point being to be labeled.
- g.setColor( m_crossColor );
- g.drawLine( mouseDataPointX, chartTop, mouseDataPointX,
chartTop + chartHeight );
- g.drawLine( chartLeft, mouseDataPointY, chartLeft +
chartWidth, mouseDataPointY );
+ // Figure out the index of the data point where the mouse is.
+ int index = (int)Math.round(
+ (float)( m_values.length - 1 ) * ( m_mouseX - chartLeft
) / chartWidth );
+
+ // Draw the label
+ int mouseDataPointX = 0;
+ int mouseDataPointY = 0;
+ String mouseDataPointValue = null;
+ long mouseDataPointTime = 0;
+ boolean showLabel = false;
- // Get the text of the label
- String mouseDataPointLabel = mouseDataPointValue + " : " +
- getFormattedTime( new Date( mouseDataPointTime ), true );
- int mouseDataPointLabelWidth = fontMetrics.stringWidth(
mouseDataPointLabel );
- int mouseDataPointLabelLeft;
- int mouseDataPointLabelTop;
-
- // If the point is near the edges of the chart, then it
would run off the chart.
- // To avoid this, the label is moved around relative to the
location of the cross.
- // Decide where it should go.
- if ( mouseDataPointX + 5 + mouseDataPointLabelWidth <
chartLeft + chartWidth )
- {
- // Ok on the right
- mouseDataPointLabelLeft = mouseDataPointX + 4;
- if ( mouseDataPointY + 5 + fontHeight < chartTop +
chartHeight )
+ if ( ( m_averageWindow > 0 ) && ( m_mousePressed ) )
+ {
+ if ( ( index >= m_averageWindow ) && ( index <
m_averageWindowValues.length ) )
{
- // Ok on the bottom
- mouseDataPointLabelTop = mouseDataPointY + 4;
+ // Draw the label for the average data
+
+ // Calculate the location of the point on the x-axis.
+ mouseDataPointX = chartLeft + index * chartWidth /
+ ( m_averageWindowValues.length - 1 );
+
+ // Calculate the location of the line on the y-axis.
Be careful of very large
+ // numbers. The float value average valus makes
this easy here.
+ mouseDataPointY = chartTop + chartHeight -
+ (int)( chartHeight * (
m_averageWindowValues[index] - m_min ) /
+ ( m_max - m_min ) );
+
+ // Round the average value to 2 decimal places.
+ mouseDataPointValue = m_floatFormat.format(
m_averageWindowValues[index] );
+
+ // Get the time at the mouse data point
+ mouseDataPointTime = m_time -
+ ( m_averageWindowValues.length - index - 1 ) *
m_sampleInterval;
+
+ showLabel = true;
}
- else
+ }
+ else
+ {
+ if ( ( index >= 0 ) && ( index < m_values.length ) )
{
- // Must go on the top
- mouseDataPointLabelTop = mouseDataPointY - 4 -
fontHeight;
+ // Draw the label for the regular data.
+
+ // Calculate the location of the point on the x-axis.
+ mouseDataPointX = chartLeft + index * chartWidth / (
m_values.length - 1 );
+
+ // Calculate the location of the line on the y-axis.
Be careful of very large
+ // numbers.
+ mouseDataPointY = chartTop + chartHeight -
+ (int)( (long)chartHeight * ( m_values[index] -
m_min ) /
+ ( m_max - m_min ) );
+
+ // Get the average value.
+ mouseDataPointValue = m_intFormat.format(
m_values[index] );
+
+ // Get the time at the mouse data point
+ mouseDataPointTime = m_time - ( m_values.length -
index - 1 ) *
+ m_sampleInterval;
+
+ showLabel = true;
}
}
- else
+
+ if ( showLabel )
{
- // Must go on the left
- mouseDataPointLabelLeft = mouseDataPointX - 4 -
mouseDataPointLabelWidth;
- if ( mouseDataPointY + 5 + fontHeight < chartTop +
chartHeight )
+ // Draw a cross at the point being to be labeled.
+ g.setColor( m_crossColor );
+ g.drawLine( mouseDataPointX, chartTop, mouseDataPointX,
chartTop + chartHeight );
+ g.drawLine( chartLeft, mouseDataPointY, chartLeft +
chartWidth, mouseDataPointY );
+
+ // Get the text of the label
+ String mouseDataPointLabel = mouseDataPointValue + " : "
+
+ getFormattedTime( new Date( mouseDataPointTime ),
true );
+ int mouseDataPointLabelWidth = fontMetrics.stringWidth(
mouseDataPointLabel );
+ int mouseDataPointLabelLeft;
+ int mouseDataPointLabelTop;
+
+ // If the point is near the edges of the chart, then it
would run off the chart.
+ // To avoid this, the label is moved around relative to
the location of the cross.
+ // Decide where it should go.
+ if ( mouseDataPointX + 5 + mouseDataPointLabelWidth <
chartLeft + chartWidth )
{
- // Ok on the bottom
- mouseDataPointLabelTop = mouseDataPointY + 4;
+ // Ok on the right
+ mouseDataPointLabelLeft = mouseDataPointX + 4;
+ if ( mouseDataPointY + 5 + fontHeight < chartTop +
chartHeight )
+ {
+ // Ok on the bottom
+ mouseDataPointLabelTop = mouseDataPointY + 4;
+ }
+ else
+ {
+ // Must go on the top
+ mouseDataPointLabelTop = mouseDataPointY - 4 -
fontHeight;
+ }
}
else
{
- // Must go on the top
- mouseDataPointLabelTop = mouseDataPointY - 4 -
fontHeight;
+ // Must go on the left
+ mouseDataPointLabelLeft = mouseDataPointX - 4 -
mouseDataPointLabelWidth;
+ if ( mouseDataPointY + 5 + fontHeight < chartTop +
chartHeight )
+ {
+ // Ok on the bottom
+ mouseDataPointLabelTop = mouseDataPointY + 4;
+ }
+ else
+ {
+ // Must go on the top
+ mouseDataPointLabelTop = mouseDataPointY - 4 -
fontHeight;
+ }
}
+
+ // Draw an outline around the location of the label.
+ g.setColor( m_maskFrameColor );
+ g.drawRect( mouseDataPointLabelLeft - 1,
mouseDataPointLabelTop - 1,
+ mouseDataPointLabelWidth + 2, fontHeight + 2 );
+
+ // Draw the background of the label. By default this is
partly transparent and
+ // looks better on top of the outline.
+ g.setColor( m_maskColor );
+ g.fillRect( mouseDataPointLabelLeft - 1,
mouseDataPointLabelTop - 1,
+ mouseDataPointLabelWidth + 2, fontHeight + 2 );
+
+ // Draw the text of the label.
+ g.setColor( m_crossColor );
+ g.drawString( mouseDataPointLabel,
mouseDataPointLabelLeft,
+ mouseDataPointLabelTop + fontHeight );
}
-
- // Draw an outline around the location of the label.
- g.setColor( m_maskFrameColor );
- g.drawRect( mouseDataPointLabelLeft - 1,
mouseDataPointLabelTop - 1,
- mouseDataPointLabelWidth + 2, fontHeight + 2 );
-
- // Draw the background of the label. By default this is
partly transparent and
- // looks better on top of the outline.
- g.setColor( m_maskColor );
- g.fillRect( mouseDataPointLabelLeft - 1,
mouseDataPointLabelTop - 1,
- mouseDataPointLabelWidth + 2, fontHeight + 2 );
-
- // Draw the text of the label.
- g.setColor( m_crossColor );
- g.drawString( mouseDataPointLabel, mouseDataPointLabelLeft,
- mouseDataPointLabelTop + fontHeight );
}
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>