home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / un2maiq4 / pjjava / src / pj / awt / djchart.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  12.2 KB  |  406 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.     Written by the Personal Journal developers of Dow Jones & Company, Inc.
  4.  
  5.     Dow Jones makes no representations or warranties about 
  6.     the suitability of this software, either express or 
  7.     implied, including but not limited to the implied warranties 
  8.     of merchantability, fitness for a particular purpose, 
  9.     or non-infringement.  Dow Jones will not be liable for 
  10.     any damages suffered by a user as a result of using, 
  11.     modifying or distributing this software or its derivatives.
  12.  
  13.  
  14.     @(#)DJChart.java  0.00 01-Jan-96
  15.  
  16.         A simple PageView that views charts in one PaperSection.
  17.  
  18.     Authors:
  19.  
  20.        alkarao   Alka Rao
  21.        Nimesh    Nimesh Shah
  22.        jlee      James Lee
  23.  
  24.     Version Ident:
  25.  
  26.         $Header: /PjJavaClient/src/pj/awt/DJChart.java 8     3/22/96 11:13p Jlee $
  27.  
  28.     History:
  29.  
  30.         0.00 05-Feb-96  alkarao         Initial Creation
  31.         0.01 15-Feb-96  Nimesh          Revised
  32.              23-Feb-96  Nimesh & jlee   Fixed label alignment and proper griding, etc.
  33.              23-Feb-96  jlee            Added dotted lines.
  34.              27-Feb-96  jlee            Modified date conversion routine for the case of 5 digit date.
  35.              25-Mar-96  jlee            Changed DJChart'super class from Panel to Canvas for
  36.                                         consistent use of Canvas component for drawing.
  37. ---------------------------------------------------------------------------*/
  38.  
  39. package pj.awt;
  40.  
  41. import pj.awt.PjFinals;
  42.  
  43. import java.awt.Canvas;
  44. import java.awt.Color;
  45. import java.awt.Component;
  46. import java.awt.Container;
  47. import java.awt.Dimension;
  48. import java.awt.Font;
  49. import java.awt.FontMetrics;
  50. import java.awt.Graphics;
  51. import java.awt.GridBagConstraints;
  52. import java.awt.GridBagLayout;
  53. import java.awt.Insets;
  54. import java.awt.Panel;
  55.  
  56. import java.lang.Integer;
  57. import java.lang.String;
  58.  
  59.  
  60. public class DJChart extends Canvas
  61. {
  62.     // --- Class variables
  63.  
  64.     private static int  outerwt;
  65.     private static int  outerht;
  66.     private static int  chartWidth;
  67.     private static int  chartHeight;
  68.  
  69.     // --- Instance variables
  70.  
  71.     private final int dotUnit = 3;
  72.  
  73.     private int         djDates[];
  74.     private int         djia[];
  75.     private int         djValues;
  76.     private String      title;
  77.  
  78.     private Font        titleFont;
  79.     private int         titleHeight;
  80.     private int         rowLabelWidth;
  81.     private int         rowLabelHeight;
  82.     private int         colLabelHeight;
  83.     private int         maxLabelWidth;
  84.     private int         valueSeparation;
  85.     private int         minval;
  86.     private int         maxval;
  87.     private int         spread;
  88.     private int         x;
  89.     private int         y;
  90.  
  91.  
  92.     // --- Public constructors
  93.  
  94.     public DJChart(int xVal[], int yVal[], int numVal, String intitle )
  95.         {
  96.         djValues    = numVal - 1 ;
  97.         djDates     = new int[numVal];
  98.         djia        = new int[numVal];
  99.  
  100.         rowLabelWidth   = 50;
  101.         colLabelHeight  = 15;
  102.         maxLabelWidth   = 50;
  103.  
  104.         for (int i=0; i<numVal - 1   ; i++)
  105.             {
  106.             djia[i] = yVal[i];
  107.             djDates[i] = xVal[i];
  108.             }
  109.  
  110.         spread = calculateSpread();
  111.         valueSeparation = chartWidth/(djValues-1);
  112.         setBackground( Color.white );
  113.         System.out.println("Debug-DJChart:constructed");
  114.         }
  115.  
  116.     public DJChart()
  117.         {
  118.         titleFont = PjFinals.fntDJChartTitle;
  119.  
  120.         chartWidth = 300;
  121.         chartHeight = 90;
  122.         titleHeight = 15;
  123.         rowLabelWidth = 50;
  124.         colLabelHeight = 15;
  125.         maxLabelWidth = 50;
  126.  
  127.         setBackground( Color.white );
  128.         }
  129.  
  130.     // --- Public operations
  131.  
  132.     public void resizeDjChart( int width, int height )
  133.         {
  134.         outerwt = width;
  135.         outerht = height;
  136.  
  137.         chartWidth = width - 60 ;
  138.         chartHeight = height  - 10;
  139.  
  140.         repaint();
  141.         }
  142.  
  143.  
  144.     public void paint(Graphics g)
  145.         {
  146.         int     i, j;
  147.         int     deltay;
  148.         int     chartOffsetH;
  149.         int     yTop, yBottom;
  150.         int     xptValue[];
  151.         int     yptValue[];
  152.         int     nChartLeftX;
  153.         String  tempStr;
  154.         String  acdest;
  155.  
  156.         valueSeparation = ( outerwt - 60 ) / (djValues - 1) + 1;
  157.         chartWidth      = valueSeparation * (djValues-1);
  158.  
  159.         FontMetrics fm  = g.getFontMetrics();
  160.         rowLabelWidth   = fm.stringWidth( Integer.toString(maxval) );
  161.         rowLabelHeight  = getFont().getSize();
  162.  
  163.         chartOffsetH    = (outerwt - chartWidth - rowLabelWidth) / 2;
  164.         nChartLeftX     = rowLabelWidth + chartOffsetH;
  165.  
  166.         yTop    = 5;
  167.         yBottom = chartHeight - 10;
  168.  
  169.         // draw white frame for graph display
  170.         g.setColor(Color.black);
  171.         g.draw3DRect(0, 0, outerwt, outerht, true);
  172.         /*
  173.         g.setColor(Color.white);
  174.         if ( outerwt > 2 && outerht > 2 )
  175.             g.fill3DRect(1, 1, outerwt - 3, outerht - 3, true);
  176.         */
  177.  
  178.         // claculate x and y coordinates for given djia values
  179.         xptValue = new int[djValues+2];
  180.         yptValue = new int[djValues+2];
  181.  
  182.         spread = calculateSpread();
  183.  
  184.         if(minval == maxval)
  185.             return;
  186.  
  187.         for (i = 0; i <djValues ; i++)
  188.             {
  189.             yptValue[i] = (yBottom + 10 ) - (djia[i] - minval) * chartHeight / (maxval - minval);
  190.             xptValue[i] = nChartLeftX + (i * valueSeparation);
  191.             }
  192.  
  193.         xptValue[djValues]   = nChartLeftX + chartWidth - 1;
  194.         xptValue[djValues+1] = nChartLeftX;
  195.         yptValue[djValues]   = yBottom;
  196.         yptValue[djValues+1] = yBottom;
  197.  
  198.         g.setColor(Color.lightGray);
  199.         g.fillPolygon(xptValue, yptValue, djValues + 2);
  200.  
  201.         //draw once
  202.         g.setColor(Color.black);
  203.         g.drawPolygon(xptValue, yptValue, djValues);
  204.  
  205.         for (i = 0; i <djValues ; i++)
  206.             yptValue[i] += 1;
  207.         //draw twice to make it thick line
  208.         g.setColor(Color.black);
  209.         g.drawPolygon(xptValue, yptValue, djValues);
  210.  
  211.         // draw y axis labels
  212.         String  rowLabel[] = new String[5];
  213.  
  214.         g.setColor(Color.darkGray);
  215.         g.setFont(titleFont);
  216.  
  217.         for (i = 0; i < 5; i++)
  218.             rowLabel[i] = Integer.toString( minval + i * (maxval-minval) / 4 );
  219.  
  220.         rowLabel[4] = Integer.toString(maxval);
  221.  
  222.         // draw horizontal grid lines and row labels
  223.         g.setColor(Color.darkGray);
  224.         deltay = yBottom / 4;
  225.  
  226.         drawDottedLine(nChartLeftX, yTop + deltay, nChartLeftX + chartWidth, yTop+deltay);
  227.         g.drawString(rowLabel[3], 5, yTop + deltay + rowLabelHeight / 2);
  228.  
  229.         drawDottedLine(nChartLeftX, yTop+(2*deltay), nChartLeftX + chartWidth, yTop+( 2 * deltay));
  230.         g.drawString(rowLabel[2], 5, yTop + 2 * deltay + rowLabelHeight / 2);
  231.  
  232.         drawDottedLine(nChartLeftX, yTop+(3*deltay), nChartLeftX + chartWidth, yTop+( 3 * deltay));
  233.         g.drawString(rowLabel[1], 5, yTop + 3 * deltay + rowLabelHeight / 2);
  234.  
  235.         //Bottom row label
  236.         g.drawString(rowLabel[0], 5, yBottom + 3 );
  237.         //Top row label
  238.         g.drawString(rowLabel[4], 5,  yTop + rowLabelHeight - 3);
  239.  
  240.  
  241.         // draw vertical grid line stubs
  242.         int cy = yBottom + 2;
  243.  
  244.         g.setColor(Color.darkGray);
  245.         for (i= 0; i <djValues; i++)
  246.             g.drawLine( nChartLeftX + i * valueSeparation, yBottom, nChartLeftX + i * valueSeparation, cy) ;
  247.  
  248.         // draw x axis lables
  249.         tempStr = Integer.toString(djDates[0]);
  250.  
  251.         int xlblwt = (g.getFontMetrics().stringWidth(tempStr)) / 2;
  252.         int cx = nChartLeftX - xlblwt;
  253.         int nDateInterval = djValues / 3;
  254.  
  255.         g.setColor(Color.darkGray);
  256.  
  257.         acdest = convertDates(Integer.toString(djDates[0]), djValues);
  258.         g.drawString(acdest,cx, yBottom + 14);
  259.  
  260.         cx = nChartLeftX +( nDateInterval * valueSeparation) - xlblwt;
  261.         acdest = convertDates(Integer.toString(djDates[nDateInterval]), djValues);
  262.         g.drawString(acdest,cx, yBottom + 14);
  263.         drawDottedLine( cx + xlblwt, yTop + 2 * deltay, cx + xlblwt, yBottom );
  264.  
  265.         cx = (nChartLeftX + (nDateInterval * 2 * valueSeparation)) - xlblwt;
  266.         acdest = convertDates(Integer.toString(djDates[nDateInterval * 2]), djValues);
  267.         g.drawString(acdest,cx, yBottom + 14);
  268.         drawDottedLine( cx + xlblwt, yTop + deltay, cx + xlblwt, yBottom );
  269.  
  270.         tempStr = Integer.toString(djDates[djValues-1]);
  271.         cx = (nChartLeftX + ((djValues -1)*valueSeparation)) - xlblwt * 2 + xlblwt / 4;
  272.         acdest = convertDates(Integer.toString(djDates[djValues-1]), djValues-1);
  273.         g.drawString(acdest, cx, yBottom + 14);
  274.  
  275.         g.setColor(Color.black);
  276.         g.drawRect( nChartLeftX, yTop, chartWidth, yBottom - yTop );
  277.         }
  278.  
  279.     public void drawDottedLine( int x1, int y1, int x2, int y2 )
  280.         {
  281.         Graphics g = getGraphics();
  282.  
  283.         int nDotNumber = 0;
  284.         int i;
  285.  
  286.         if ( y1 == y2 )// horizontal
  287.             {
  288.             nDotNumber = ( x2 - x1 ) / dotUnit;
  289.  
  290.             for ( i = 0; i < nDotNumber; i++)
  291.                 if ( (i % 2) == 0 )
  292.                     g.drawLine( x1 + dotUnit * i, y1, x1 + dotUnit * (i + 1), y2 );
  293.  
  294.             if ( x2 > x1 + dotUnit * i )
  295.                 if ( (i % 2) == 0 )
  296.                     g.drawLine( x1 + dotUnit * i, y1, x2, y2 );
  297.             }
  298.         else
  299.         if ( x1 == x2 )//Vertical
  300.             {
  301.             nDotNumber = ( y2 - y1 ) / dotUnit;
  302.  
  303.             for ( i = 0; i < nDotNumber; i++)
  304.                 if ( (i % 2) == 0 )
  305.                     g.drawLine( x1, y1 + dotUnit * i, x2, y1 + dotUnit * (i + 1) );
  306.  
  307.             if ( y2 > y1 + dotUnit * i )
  308.                 if ( (i % 2) == 0 )
  309.                     g.drawLine( x1, y1 + dotUnit * i, x2, y2 );
  310.             }
  311.  
  312.         g.finalize();
  313.         }
  314.  
  315.  
  316.     public String convertDates(String dummyDates, int djValues)
  317.         {
  318.         String  tmpMM;
  319.         String  tmpDD;
  320.         String  tmpYY;
  321.         String  djDates;
  322.         int     nMonth;
  323.         int     nOffset = 0;
  324.  
  325.  
  326.         String[] months =
  327.             {
  328.             "", "Jan.", "Feb.", "Mar.", "Apr.", "May.", "Jun.",
  329.             "Jul.", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."
  330.             };
  331.  
  332.         //There is case where date is like 042395 rather than 122395 so that one less cahr in the string.
  333.         if ( dummyDates.length() < 6 )
  334.             nOffset = 1;
  335.  
  336.         tmpMM   = dummyDates.substring(0, 2 - nOffset);              //get month
  337.         tmpDD   = dummyDates.substring(2 - nOffset, 4 - nOffset);    // get day
  338.         tmpYY   = dummyDates.substring(4 - nOffset, 6 - nOffset);    // get year
  339.         nMonth  = Integer.parseInt(tmpMM);
  340.         tmpMM   = months[nMonth];
  341.         djDates = tmpMM.concat(tmpDD);
  342.  
  343.         return djDates;
  344.         }
  345.  
  346.  
  347.     // --- Private operations
  348.  
  349.     /**  Sort Operation  **/
  350.     private void SortArray(int yvalues[], int xvalues[], int numpoints)
  351.         {
  352.         int gap, i, j;
  353.         int ytemp, xtemp;
  354.  
  355.         for(gap = numpoints /2 ; gap > 0; gap /= 2)
  356.             {
  357.             for(i = gap; i <numpoints; i++)
  358.                 {
  359.                 for(j = i - gap; j >= 0; j -= gap)
  360.                     {
  361.                     if(yvalues[j] <=  yvalues[j+gap])
  362.                         break;
  363.  
  364.                     ytemp = yvalues[j];
  365.                     yvalues[j] = yvalues[j+gap];
  366.                     yvalues[j+gap] = ytemp;
  367.  
  368.                     xtemp = xvalues[j];
  369.                     xvalues[j] = xvalues[j+gap];
  370.                     xvalues[j+gap] = xtemp;
  371.  
  372.                     }
  373.                 }
  374.             }
  375.         }
  376.  
  377.  
  378.     private int calculateSpread()
  379.         {
  380.         int     i;
  381.         int     max, min;
  382.  
  383.         min = max = djia[0];
  384.  
  385.         for (i = 1; i < djValues; i++)
  386.             {
  387.             if (djia[i] < min)
  388.                 min = djia[i];
  389.             }
  390.  
  391.         for (i = 1; i < djValues; i++)
  392.             {
  393.             if (djia[i] > max)
  394.                max = djia[i];
  395.             }
  396.  
  397.         if (max != 0)
  398.             maxval = max + 100;
  399.  
  400.         if (min != 0)
  401.             minval = min - 100;
  402.  
  403.         return (maxval - minval);
  404.         }
  405.     }
  406.