-3

我正在编辑一个由谁知道,一位老师制作的程序。无论如何,它是显示存储在图表中显示的数组中的股票值。老师已经注意到图形 Y 轴不正确,但我们不必更改它。我们必须更改的是复选框的单选按钮,以便我们可以在图表上显示多个股票值。

因此,要绘制新图表,我们必须获得所选股票的最小和最大 Y 值。我决定去:

if (checkBox1.Checked == true)
{
    GetStockValues(0);
    maxVal[0] = StockArray.Max();
}
int maxValue = Convert.ToInt16(maxVal.Min());
return maxValue;

minValue 也是如此。问题是我没有得到任何正确的数字。这是整个程序:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace VC2008_StockMktGraphics
{
    public partial class frmMaster : Form
    {
        // let's make the stock values array be public so we don't have to pass
        // them back and forth.

        public double[] StockArray = new double[ 30 ] ;
        public double[] minVal = new double [ 4 ];
        public double[] maxVal = new double[4];

        public frmMaster()
        {
            InitializeComponent();
        }

        private void frmMaster_Resize(object sender, EventArgs e)
        {
            this.Invalidate() ;
        }

        protected override void OnPaint(PaintEventArgs e)
            {
            int h = this.Bounds.Height ;
            int w = this.Bounds.Width ;

            Rectangle r = new Rectangle();

            // Create the graphics context to draw into
            Graphics gc = this.CreateGraphics() ;
            Pen pBl = new Pen( Color.Black , 2) ;
            SolidBrush sB = new SolidBrush(Color.Honeydew ) ;

            // if the window is too small, force it to a size
            if ( ( h < 200 ) || ( w < 200 ) )
            {
                this.Width = 200 ;
                this.Height = 200 ;
            }

            // Set dynamic location of graphics box
            r.X=Convert.ToInt32( Convert.ToDouble( w ) * 0.2 ) ;
            r.Y=Convert.ToInt32( Convert.ToDouble( w ) * 0.2 ) ;
            r.Width=Convert.ToInt32( Convert.ToDouble( w ) * 0.8 ) -r.X ;
            r.Height=Convert.ToInt32( Convert.ToDouble( h ) * 0.8 -r.Y );

            // draw the outer rectangle and fill with a light color
            gc.DrawRectangle(pBl, r);
            gc.FillRectangle(sB, r);

            // set the location of the buttons
            btnExit.Left = r.X + r.Width + 10;
            btnExit.Top = 10;
            chkStartAtZero.Left = r.X + r.Width + 10;
            chkStartAtZero.Top = btnExit.Top + 25;
            checkBox1.Left = r.X + r.Width + 10;
            checkBox1.Top = chkStartAtZero.Top + 25;
            checkBox2.Left = checkBox1.Left;
            checkBox2.Top = checkBox1.Top + 25;
            checkBox3.Left = checkBox2.Left;
            checkBox3.Top = checkBox2.Top + 25;
            checkBox4.Left = checkBox3.Left;
            checkBox4.Top = checkBox3.Top + 25;

            if (checkBox1.Checked == true)
                DrawStock(0, gc, r);
            if (checkBox2.Checked == true)
                DrawStock(1, gc, r);
            if (checkBox3.Checked == true)
                DrawStock(2, gc, r);
            if (checkBox4.Checked == true)
                DrawStock(3, gc, r);

            // dispose of object memories we've allocated
            pBl.Dispose() ;
            sB.Dispose();
            gc.Dispose() ;
        }

        public void DrawStock( int iWhichStock , Graphics gc , Rectangle r ) 
        {
            int i ;
            int iLabelX;
            int iMinY = 999999;
            int iMaxY = 0;
            int iCnt;

            double dxIncrement;
            double dyIncrement;

            string strAxisLabel;

            Point pLineStart = new Point();
            Point pLineEnd = new Point();

            Pen pB2 = new Pen(Color.Red, 2);

            Font f = new Font("Ariel", (float)10.0);

            Brush b = new SolidBrush(Color.Blue);

            // Normally, we'd get stock from a database, I'm just going to go 
            // get it from a function with hard-coded values for demonstration
            // purposes.
            iMinY = Convert.ToInt16(GetMin());
            iMaxY = Convert.ToInt16(GetMax());


            // Take the min and max to the next increment of 5 outside them.
            iMinY = Convert.ToInt16( iMinY / 5 ) * 5 ;
            iMaxY = ( Convert.ToInt16(iMaxY / 5 ) + 1 ) * 5 ;


            // Find the number of x-coordinate values
            iCnt = 0;
            foreach (double d in StockArray)
            {
                if (d > 0)
                    iCnt++;
            }

            // so now we know how many x values there are to spread on the x-axis

            dxIncrement = Convert.ToDouble(r.Width / iCnt);

            // if the zero checkbox is checked, then we'll set the minimums to zero
            if (chkStartAtZero.Checked == true)
            {
                iMinY = 0;
                dMinStockValue = 0.0;
            }

            // so each y-axis pixel must contain this many dollars of actual stock price

            dyIncrement = Convert.ToDouble(r.Height / (iMaxY - iMinY));

            // set the starting point of the stock line
            pLineStart.X = r.X;
            pLineStart.Y = r.Top + r.Height - 
                           Convert.ToInt16(dyIncrement * (StockArray[ 0 ] - Convert.ToDouble(iMinY) ) ) ;

            // now graph them
            for( i = 1 ; i < StockArray.Length ; i++ )
            {
                if( StockArray[i] == 0 )
                    continue ;
                pLineEnd.X = pLineStart.X + Convert.ToInt16(dxIncrement);
                pLineEnd.Y = r.Top + r.Height -
                                  Convert.ToInt16(dyIncrement * (StockArray[i] - Convert.ToDouble(iMinY)));
                gc.DrawLine(pB2,pLineStart, pLineEnd ) ;
                pLineStart = pLineEnd ;
            }

            // now draw the y-axis labels
            i = iMinY;
            pLineStart.X = r.X;
            pLineStart.Y = r.Top + r.Height - Convert.ToInt16(0);
            pLineEnd.X = r.X - 8;

            while( i <= iMaxY )
            {
                pLineStart.Y = r.Top + r.Height - 
                               Convert.ToInt16( Convert.ToDouble(i-iMinY ) * dyIncrement ) ;
                pLineEnd.Y = pLineStart.Y;
                gc.DrawLine(pB2, pLineStart, pLineEnd);
                strAxisLabel = Convert.ToString(i);
                iLabelX = pLineEnd.X - 18;
                if (i < 10)
                    iLabelX = pLineEnd.X - 8;
                else
                    if (i < 100)
                        iLabelX = pLineEnd.X - 18;
                    else
                        if (i < 1000)
                            iLabelX = pLineEnd.X - 28;

                gc.DrawString(strAxisLabel, f, b, iLabelX, pLineEnd.Y - 8);
                i += 10;
            }                
        }

        public double GetMin()
        {    
            if (checkBox1.Checked == true)
            {
                GetStockValues(0);
                minVal[0] = StockArray.Min();
            }
            if (checkBox2.Checked == true)
            {
                GetStockValues(1);
                minVal[1] = StockArray.Min();
            }
            if (checkBox3.Checked == true)
            {
                GetStockValues(2);
                minVal[2] = StockArray.Min();
            }
            if (checkBox4.Checked == true)
            {
                GetStockValues(3);
                minVal[3] = StockArray.Min();
            }
            int minValue = Convert.ToInt16(minVal.Min());
            label1.Text = Convert.ToString(minValue);
            return minValue;
        }

        public double GetMax()
        {
            if (checkBox1.Checked == true)
            {
                GetStockValues(0);
                maxVal[0] = StockArray.Max();
            }
            if (checkBox2.Checked == true)
            {
                GetStockValues(1);
                maxVal[1] = StockArray.Max();
            }
            if (checkBox3.Checked == true)
            {
                GetStockValues(2);
                maxVal[2] = StockArray.Max();
            }
            if (checkBox4.Checked == true)
            {
                GetStockValues(3);
                maxVal[3] = StockArray.Max();
            }
            int maxValue = Convert.ToInt16(maxVal.Min());
            label2.Text = Convert.ToString(maxValue);
            return maxValue;
        }

        public void GetStockValues(int iWhichStock)
        {
         if (iWhichStock == 0)
            {
                // IBM
                StockArray[0] = 147.64;  // 1-10-11
                StockArray[1] = 147.28;
                StockArray[2] = 149.10;
                StockArray[3] = 148.82;
                StockArray[4] = 150.00;
                StockArray[5] = 150.65; 
                StockArray[6] = 155.69;
                StockArray[7] = 155.80;
                StockArray[8] = 155.50;
                StockArray[9] = 159.63;
                StockArray[10] = 161.44;
                StockArray[11] = 161.04;
                StockArray[12] = 161.07;
                StockArray[13] = 159.21;
                StockArray[14] = 162.00;
                StockArray[15] = 163.56;
                StockArray[16] = 163.30;
                StockArray[17] = 163.53;
                StockArray[18] = 164.00;
                StockArray[19] = 164.83;
            }
            if (iWhichStock == 1)
            {
                // BA (The Boeing Company)
                StockArray[0] = 69.09;  // 1-10-11
                StockArray[1] = 68.96;
                StockArray[2] = 70.15;
                StockArray[3] = 69.63;
                StockArray[4] = 70.07;
                StockArray[5] = 72.47; 
                StockArray[6] = 71.73;
                StockArray[7] = 71.12;
                StockArray[8] = 71.68;
                StockArray[9] = 72.73; 
                StockArray[10] = 72.24;
                StockArray[11] = 70.02;
                StockArray[12] = 70.56;
                StockArray[13] = 69.23;
                StockArray[14] = 69.48;
                StockArray[15] = 70.29;
                StockArray[16] = 71.00;
                StockArray[17] = 70.98;
                StockArray[18] = 71.38;
                StockArray[19] = 71.93;
            }
            if (iWhichStock == 2)
            {
                // CAP (CAI International
                StockArray[0] = 19.44;  // 1-10-11
                StockArray[1] = 19.66;
                StockArray[2] = 19.68;
                StockArray[3] = 19.45;
                StockArray[4] = 19.79;
                StockArray[5] = 19.90;
                StockArray[6] = 19.53;
                StockArray[7] = 19.06;
                StockArray[8] = 19.05;
                StockArray[9] = 19.14;
                StockArray[10] = 19.28;
                StockArray[11] = 20.11;
                StockArray[12] = 19.74;
                StockArray[13] = 19.06;
                StockArray[14] = 19.04;
                StockArray[15] = 20.03;
                StockArray[16] = 19.69;
                StockArray[17] = 19.56;
                StockArray[18] = 19.61;
                StockArray[19] = 19.63;
            }
            if (iWhichStock == 3)
            {
                // TOT (Total Societe Anonyme)
                StockArray[0] = 53.00;  // 1-10-11
                StockArray[1] = 53.32;
                StockArray[2] = 55.00;
                StockArray[3] = 56.03;
                StockArray[4] = 57.11;
                StockArray[5] = 57.10;
                StockArray[6] = 56.95;
                StockArray[7] = 57.12;
                StockArray[8] = 57.12;
                StockArray[9] = 58.04;
                StockArray[10] = 58.79;
                StockArray[11] = 58.69;
                StockArray[12] = 59.00;
                StockArray[13] = 59.50;
                StockArray[14] = 57.80;
                StockArray[15] = 58.77;
                StockArray[16] = 61.03;
                StockArray[17] = 60.70;
                StockArray[18] = 59.76;
                StockArray[19] = 59.25;
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void chkStartAtZero_CheckedChanged(object sender, EventArgs e)
        {
            this.Invalidate();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            this.Invalidate();
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            this.Invalidate();
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            this.Invalidate();
        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            this.Invalidate();
        }

        private void frmMaster_Load(object sender, EventArgs e)
        {

        }
    }
}
4

1 回答 1

1

几个问题。你的主要问题是你打电话int maxValue = Convert.ToInt16(maxVal.Min());GetMax()我想你想要int maxValue = Convert.ToInt16(maxVal.Max());的。

但是,一旦您解决了这个问题,您就会遇到这样一个事实:您正在比较 maxVal 数组的未初始化/旧值。

假设我只选中了复选框 1 和 3,然后我取消选中 3,第二次运行 GetMax 或 GetMin 时 maxVal[2] 的值是多少,(这里有一个提示,它不是 0)

于 2012-05-02T22:36:13.310 回答