home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 12.2 KB | 406 lines |
- /*---------------------------------------------------------------------------
-
- Written by the Personal Journal developers of Dow Jones & Company, Inc.
-
- Dow Jones makes no representations or warranties about
- the suitability of this software, either express or
- implied, including but not limited to the implied warranties
- of merchantability, fitness for a particular purpose,
- or non-infringement. Dow Jones will not be liable for
- any damages suffered by a user as a result of using,
- modifying or distributing this software or its derivatives.
-
-
- @(#)DJChart.java 0.00 01-Jan-96
-
- A simple PageView that views charts in one PaperSection.
-
- Authors:
-
- alkarao Alka Rao
- Nimesh Nimesh Shah
- jlee James Lee
-
- Version Ident:
-
- $Header: /PjJavaClient/src/pj/awt/DJChart.java 8 3/22/96 11:13p Jlee $
-
- History:
-
- 0.00 05-Feb-96 alkarao Initial Creation
- 0.01 15-Feb-96 Nimesh Revised
- 23-Feb-96 Nimesh & jlee Fixed label alignment and proper griding, etc.
- 23-Feb-96 jlee Added dotted lines.
- 27-Feb-96 jlee Modified date conversion routine for the case of 5 digit date.
- 25-Mar-96 jlee Changed DJChart'super class from Panel to Canvas for
- consistent use of Canvas component for drawing.
- ---------------------------------------------------------------------------*/
-
- package pj.awt;
-
- import pj.awt.PjFinals;
-
- import java.awt.Canvas;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Container;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Graphics;
- import java.awt.GridBagConstraints;
- import java.awt.GridBagLayout;
- import java.awt.Insets;
- import java.awt.Panel;
-
- import java.lang.Integer;
- import java.lang.String;
-
-
- public class DJChart extends Canvas
- {
- // --- Class variables
-
- private static int outerwt;
- private static int outerht;
- private static int chartWidth;
- private static int chartHeight;
-
- // --- Instance variables
-
- private final int dotUnit = 3;
-
- private int djDates[];
- private int djia[];
- private int djValues;
- private String title;
-
- private Font titleFont;
- private int titleHeight;
- private int rowLabelWidth;
- private int rowLabelHeight;
- private int colLabelHeight;
- private int maxLabelWidth;
- private int valueSeparation;
- private int minval;
- private int maxval;
- private int spread;
- private int x;
- private int y;
-
-
- // --- Public constructors
-
- public DJChart(int xVal[], int yVal[], int numVal, String intitle )
- {
- djValues = numVal - 1 ;
- djDates = new int[numVal];
- djia = new int[numVal];
-
- rowLabelWidth = 50;
- colLabelHeight = 15;
- maxLabelWidth = 50;
-
- for (int i=0; i<numVal - 1 ; i++)
- {
- djia[i] = yVal[i];
- djDates[i] = xVal[i];
- }
-
- spread = calculateSpread();
- valueSeparation = chartWidth/(djValues-1);
- setBackground( Color.white );
- System.out.println("Debug-DJChart:constructed");
- }
-
- public DJChart()
- {
- titleFont = PjFinals.fntDJChartTitle;
-
- chartWidth = 300;
- chartHeight = 90;
- titleHeight = 15;
- rowLabelWidth = 50;
- colLabelHeight = 15;
- maxLabelWidth = 50;
-
- setBackground( Color.white );
- }
-
- // --- Public operations
-
- public void resizeDjChart( int width, int height )
- {
- outerwt = width;
- outerht = height;
-
- chartWidth = width - 60 ;
- chartHeight = height - 10;
-
- repaint();
- }
-
-
- public void paint(Graphics g)
- {
- int i, j;
- int deltay;
- int chartOffsetH;
- int yTop, yBottom;
- int xptValue[];
- int yptValue[];
- int nChartLeftX;
- String tempStr;
- String acdest;
-
- valueSeparation = ( outerwt - 60 ) / (djValues - 1) + 1;
- chartWidth = valueSeparation * (djValues-1);
-
- FontMetrics fm = g.getFontMetrics();
- rowLabelWidth = fm.stringWidth( Integer.toString(maxval) );
- rowLabelHeight = getFont().getSize();
-
- chartOffsetH = (outerwt - chartWidth - rowLabelWidth) / 2;
- nChartLeftX = rowLabelWidth + chartOffsetH;
-
- yTop = 5;
- yBottom = chartHeight - 10;
-
- // draw white frame for graph display
- g.setColor(Color.black);
- g.draw3DRect(0, 0, outerwt, outerht, true);
- /*
- g.setColor(Color.white);
- if ( outerwt > 2 && outerht > 2 )
- g.fill3DRect(1, 1, outerwt - 3, outerht - 3, true);
- */
-
- // claculate x and y coordinates for given djia values
- xptValue = new int[djValues+2];
- yptValue = new int[djValues+2];
-
- spread = calculateSpread();
-
- if(minval == maxval)
- return;
-
- for (i = 0; i <djValues ; i++)
- {
- yptValue[i] = (yBottom + 10 ) - (djia[i] - minval) * chartHeight / (maxval - minval);
- xptValue[i] = nChartLeftX + (i * valueSeparation);
- }
-
- xptValue[djValues] = nChartLeftX + chartWidth - 1;
- xptValue[djValues+1] = nChartLeftX;
- yptValue[djValues] = yBottom;
- yptValue[djValues+1] = yBottom;
-
- g.setColor(Color.lightGray);
- g.fillPolygon(xptValue, yptValue, djValues + 2);
-
- //draw once
- g.setColor(Color.black);
- g.drawPolygon(xptValue, yptValue, djValues);
-
- for (i = 0; i <djValues ; i++)
- yptValue[i] += 1;
- //draw twice to make it thick line
- g.setColor(Color.black);
- g.drawPolygon(xptValue, yptValue, djValues);
-
- // draw y axis labels
- String rowLabel[] = new String[5];
-
- g.setColor(Color.darkGray);
- g.setFont(titleFont);
-
- for (i = 0; i < 5; i++)
- rowLabel[i] = Integer.toString( minval + i * (maxval-minval) / 4 );
-
- rowLabel[4] = Integer.toString(maxval);
-
- // draw horizontal grid lines and row labels
- g.setColor(Color.darkGray);
- deltay = yBottom / 4;
-
- drawDottedLine(nChartLeftX, yTop + deltay, nChartLeftX + chartWidth, yTop+deltay);
- g.drawString(rowLabel[3], 5, yTop + deltay + rowLabelHeight / 2);
-
- drawDottedLine(nChartLeftX, yTop+(2*deltay), nChartLeftX + chartWidth, yTop+( 2 * deltay));
- g.drawString(rowLabel[2], 5, yTop + 2 * deltay + rowLabelHeight / 2);
-
- drawDottedLine(nChartLeftX, yTop+(3*deltay), nChartLeftX + chartWidth, yTop+( 3 * deltay));
- g.drawString(rowLabel[1], 5, yTop + 3 * deltay + rowLabelHeight / 2);
-
- //Bottom row label
- g.drawString(rowLabel[0], 5, yBottom + 3 );
- //Top row label
- g.drawString(rowLabel[4], 5, yTop + rowLabelHeight - 3);
-
-
- // draw vertical grid line stubs
- int cy = yBottom + 2;
-
- g.setColor(Color.darkGray);
- for (i= 0; i <djValues; i++)
- g.drawLine( nChartLeftX + i * valueSeparation, yBottom, nChartLeftX + i * valueSeparation, cy) ;
-
- // draw x axis lables
- tempStr = Integer.toString(djDates[0]);
-
- int xlblwt = (g.getFontMetrics().stringWidth(tempStr)) / 2;
- int cx = nChartLeftX - xlblwt;
- int nDateInterval = djValues / 3;
-
- g.setColor(Color.darkGray);
-
- acdest = convertDates(Integer.toString(djDates[0]), djValues);
- g.drawString(acdest,cx, yBottom + 14);
-
- cx = nChartLeftX +( nDateInterval * valueSeparation) - xlblwt;
- acdest = convertDates(Integer.toString(djDates[nDateInterval]), djValues);
- g.drawString(acdest,cx, yBottom + 14);
- drawDottedLine( cx + xlblwt, yTop + 2 * deltay, cx + xlblwt, yBottom );
-
- cx = (nChartLeftX + (nDateInterval * 2 * valueSeparation)) - xlblwt;
- acdest = convertDates(Integer.toString(djDates[nDateInterval * 2]), djValues);
- g.drawString(acdest,cx, yBottom + 14);
- drawDottedLine( cx + xlblwt, yTop + deltay, cx + xlblwt, yBottom );
-
- tempStr = Integer.toString(djDates[djValues-1]);
- cx = (nChartLeftX + ((djValues -1)*valueSeparation)) - xlblwt * 2 + xlblwt / 4;
- acdest = convertDates(Integer.toString(djDates[djValues-1]), djValues-1);
- g.drawString(acdest, cx, yBottom + 14);
-
- g.setColor(Color.black);
- g.drawRect( nChartLeftX, yTop, chartWidth, yBottom - yTop );
- }
-
- public void drawDottedLine( int x1, int y1, int x2, int y2 )
- {
- Graphics g = getGraphics();
-
- int nDotNumber = 0;
- int i;
-
- if ( y1 == y2 )// horizontal
- {
- nDotNumber = ( x2 - x1 ) / dotUnit;
-
- for ( i = 0; i < nDotNumber; i++)
- if ( (i % 2) == 0 )
- g.drawLine( x1 + dotUnit * i, y1, x1 + dotUnit * (i + 1), y2 );
-
- if ( x2 > x1 + dotUnit * i )
- if ( (i % 2) == 0 )
- g.drawLine( x1 + dotUnit * i, y1, x2, y2 );
- }
- else
- if ( x1 == x2 )//Vertical
- {
- nDotNumber = ( y2 - y1 ) / dotUnit;
-
- for ( i = 0; i < nDotNumber; i++)
- if ( (i % 2) == 0 )
- g.drawLine( x1, y1 + dotUnit * i, x2, y1 + dotUnit * (i + 1) );
-
- if ( y2 > y1 + dotUnit * i )
- if ( (i % 2) == 0 )
- g.drawLine( x1, y1 + dotUnit * i, x2, y2 );
- }
-
- g.finalize();
- }
-
-
- public String convertDates(String dummyDates, int djValues)
- {
- String tmpMM;
- String tmpDD;
- String tmpYY;
- String djDates;
- int nMonth;
- int nOffset = 0;
-
-
- String[] months =
- {
- "", "Jan.", "Feb.", "Mar.", "Apr.", "May.", "Jun.",
- "Jul.", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."
- };
-
- //There is case where date is like 042395 rather than 122395 so that one less cahr in the string.
- if ( dummyDates.length() < 6 )
- nOffset = 1;
-
- tmpMM = dummyDates.substring(0, 2 - nOffset); //get month
- tmpDD = dummyDates.substring(2 - nOffset, 4 - nOffset); // get day
- tmpYY = dummyDates.substring(4 - nOffset, 6 - nOffset); // get year
- nMonth = Integer.parseInt(tmpMM);
- tmpMM = months[nMonth];
- djDates = tmpMM.concat(tmpDD);
-
- return djDates;
- }
-
-
- // --- Private operations
-
- /** Sort Operation **/
- private void SortArray(int yvalues[], int xvalues[], int numpoints)
- {
- int gap, i, j;
- int ytemp, xtemp;
-
- for(gap = numpoints /2 ; gap > 0; gap /= 2)
- {
- for(i = gap; i <numpoints; i++)
- {
- for(j = i - gap; j >= 0; j -= gap)
- {
- if(yvalues[j] <= yvalues[j+gap])
- break;
-
- ytemp = yvalues[j];
- yvalues[j] = yvalues[j+gap];
- yvalues[j+gap] = ytemp;
-
- xtemp = xvalues[j];
- xvalues[j] = xvalues[j+gap];
- xvalues[j+gap] = xtemp;
-
- }
- }
- }
- }
-
-
- private int calculateSpread()
- {
- int i;
- int max, min;
-
- min = max = djia[0];
-
- for (i = 1; i < djValues; i++)
- {
- if (djia[i] < min)
- min = djia[i];
- }
-
- for (i = 1; i < djValues; i++)
- {
- if (djia[i] > max)
- max = djia[i];
- }
-
- if (max != 0)
- maxval = max + 100;
-
- if (min != 0)
- minval = min - 100;
-
- return (maxval - minval);
- }
- }
-