Plunging into .NET Development

Weblog Pieter Gheysens
Microsoft .NET Development - C# - Enterprise Library - Visual Studio 2005 Team System - Compuware DevPartner - ...
 


Sunday, March 6

Line Chart Component [GDI+] - part 2

In part 2 (read part 1) I will try to highlight some other features of the chart component.
  • Colors



    The comboboxes are filled with all non-system colors and the labels next to the comboboxes display dynamically the selected color. Below you can find the code for filling the main area combo box. The other comboboxes are filled in the same manner.

                Array allColors = Enum.GetValues(typeof(KnownColor));

                ArrayList listOfColors = new ArrayList();

                string tmpColor;

     

                KnownColor tmpKnownColor;

                foreach (object color in allColors)

                {

                    tmpKnownColor = (KnownColor) color;

                    if (!Color.FromKnownColor(tmpKnownColor).IsSystemColor)

                    {

                        //Add only non-system colors to arraylist

                        listOfColors.Add(tmpKnownColor.ToString());

                    }

                }

     

                this.cmbMainArea.DataSource = listOfColors;

                this.cmbChartArea.DataSource = listOfColors.Clone();

                this.cmbCurve.DataSource = listOfColors.Clone();

                this.cmbGrid.DataSource = listOfColors.Clone();

     

                //Main Area

                tmpColor = ConfigurationSettings.AppSettings["BrushMainArea"].ToString();

     

                for (int i=0; i < cmbMainArea.Items.Count; i++)

                {

                    if (tmpColor == cmbMainArea.Items[i].ToString())

                    {

                        cmbMainArea.SelectedIndex = i;

                        break;

                    }

                }


    KnownColor is an enumeration of all available colors. The array allColors will consist of all "KnownColors" and the arrayList listOfColors will contain all non-system colornames that will eventually be shown in the combobox. The dataSource of the comboBoxes are then set to the listOfColors or a clone of it. Eventhandling is enabled for setting the backColor of the labels.

            private void cmb_SelectedIndexChanged(object sender, System.EventArgs e)

            {

                ComboBox cmb = (ComboBox) sender;

     

                switch (cmb.Name)

                {

                    case "cmbMainArea" :

                        this.lblColorMain.BackColor = Color.FromName(cmb.Text);

                        break;

                    case "cmbChartArea" :

                        this.lblColorChart.BackColor = Color.FromName(cmb.Text);

                        break;

                    case "cmbCurve" :

                        this.lblColorCurve.BackColor = Color.FromName(cmb.Text);

                        break;

                    case "cmbGrid" :

                        this.lblColorGrid.BackColor = Color.FromName(cmb.Text);

                        break;

                }

            }



  • Refresh of chart
    When the button is clicked all changes in the settings are pushed to the chart component.

            private void buttonUpdate_Click(object sender, System.EventArgs e)

            {

                //Update marges

                this.graph.UpdateMarges(Convert.ToInt32(this.txtTop.Text),

                        Convert.ToInt32(this.txtBottom.Text),

                        Convert.ToInt32(this.txtLeft.Text),

                        Convert.ToInt32(this.txtRight.Text));

     

                //Update colors

                this.graph.UpdateColors(cmbMainArea.Text, cmbChartArea.Text, cmbCurve.Text, cmbGrid.Text);

     

                //Invalidate

                this.groupBoxGraph.Visible = false;

                this.groupBoxGraph.Visible = true;

            }


            public void UpdateMarges(int top, int bottom, int left, int right)

            {

                this.marginTop = top;

                this.marginBottom = bottom;

                this.marginLeft = left;

                this.marginRight = right;

            }

     

            public void UpdateColors(string mainArea, string chartArea, string lineColor, string gridColor)

            {

                this.brushMainArea = new SolidBrush(Color.FromName(mainArea));

                this.brushChartArea = new SolidBrush(Color.FromName(chartArea));

                this.currentCurve.Color = Color.FromName(lineColor);

                this.brushGrid = new SolidBrush(Color.FromName(gridColor));

            }


    To repaint the usercontrol I first used the Invalidate-method of the graph component, but this didn't do the trick [I should further investigate why this didn't work]. So I used the visibility-property of the groupBox to trigger the paint-method.
Other features, like tooltips and scaling will be highlighted in another post ...

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home