Saturday, 27 July 2013

32 - C# dynamic drawing

New form added which shows how C# could be used for drawing graphs and diagrams. This form uses panels, labels and lineshapes to represent stack market data and analysing tools: Japanese candlesticks, 2 and 13 days Force indexes plus MACD histogram. I hope form is not too crowdy with stock information and easy readable (small rectangles are hidden, p.visible=false, in production version. See below.). During daily trading form is updated with latest stock information and graph is after computation updated accordingly.


In production, analyzing Dow Jones Industrial Average for 120 days.




Form looks like this. Black rectangle remains from testing phase. All other elements (controls) are handled dynamically. Change of labels positions (top property) define new drawing area(s). During run-time labels are set to visible=false.



How to add Label control in C#?

First, this is method to add one panel.

 private void addHistPanel(double[] panelParametri) // dodaj hist panel
        {
            Panel p = new Panel();              // new panel
            p.BackColor = Color.Black;          // by default it is black
            p.Width = sirinaHistPanel;          // sted width
            p.Height = Convert.ToInt16(Math.Abs(panelParametri[1])*koefHistVisine());   // set height - po visini do maksimuma
            p.Left = lijevaGranicaHist + Convert.ToInt16(panelParametri[0]) * (sirinaHistPanel+razmakHist); // the most left panel
            p.BorderStyle = BorderStyle.FixedSingle;            // add some fancy line arrond panel
            if (panelParametri[1] > 0) { p.Top = middlePodrucjaHist - p.Height; p.BackColor = Color.Green;  // set its color if positive
            if (p.Top > topGranicaHist) {   // za iznad crte // above zero line
                if ( iznadGranice < p.Top - topGranicaHist){    // also, we are looking for heighest top
                    iznadGranice = p.Top - topGranicaHist;  // nadji naj iznad
                    }
            }  
            } // za ispod crte // bellow line - it means negative value
            else { p.Top = middlePodrucjaHist; p.BackColor = Color.Red;
                if (p.Top + p.Height > downGranicaHist)
                {
                    if (ispodGranice < Math.Abs(p.Top + p.Height - downGranicaHist)) {
                        ispodGranice = p.Top + p.Height - downGranicaHist;
                    }    // traži najispod also, we are looking for lowest top
                    }   
            }         
            Controls.Add(p);    // add panel
        }


This is method which adds all panels in the loop:

 private void dodajHistPanele(double[,] radni)       // two dimensional array has all panel information
         {
             double[] parametar = new double[2];
             for (int i = brStupaca; i >= 0; i--)       // for all columns
             {
                 parametar[0] = brStupaca - i;   // 0 positive or negative;
1 -panel.height                 
                 parametar[1] = radni[i, columnHistogram];  // input for add panel method is one dimensional array with two items
                 addHistPanel(parametar);                   // add panel passing array
             }
         }


Discrete values of Force indexes are shown with small panels. Panel.top(s) are connected with lines called LineShape.

How to add LineShape control in C#?

fi13Lines is two dimensional array which holds about points which should be connected. Actually, these are panel's tops of small rectangles saved in array during p.tops calculation.

void addLineFi13()
        {   //ShapeContainer canvaS;   // LineShape needs ShapeContainer which we'll call canvaS
            canvaS.Parent = this;                       // add canvaS to form
            for (int i = brStupaca; i > 0; i--)
            {
                LineShape ls = new Microsoft.VisualBasic.PowerPacks.LineShape();    // new LineShape
                ls.Parent = canvaS;         // belongs to canvaS
                ls.BorderWidth = 2; // set how much line is thick
                ls.BorderColor = Color.Purple;  // set color
                ls.StartPoint = new System.Drawing.Point(fi13Lines[i, 0], fi13Lines[i, 1]);     // line Star point
                ls.EndPoint = new System.Drawing.Point(fi13Lines[i - 1, 0], fi13Lines[i - 1, 1]);   // line End point
                ls.BringToFront();  // bring it to front - not needed basically
                canvaS.BringToFront(); // bring it to front - not needed basically 
            }
        } 

No comments:

Post a Comment