3 Replies - 230 Views - Last Post: 12 February 2013 - 03:21 PM Rate Topic: -----

#1 JBudd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-September 12

Using variable in place of ChartName

Posted 12 February 2013 - 09:11 AM

Hi all,

Hoping for some pointers / help.

I have a C# WinForms application that has four chart controls used to graphically show some analysis results.
I have the code working for each graph, however in an attempt to be more efficient & re-use code I've defined a code block to :-
- create the required series,
- extracts the data from a database & assigns the results to the appropriate series
- add the series to the chart
- customise the charts appearance.

All of the above is done dynamically as the data dies not exist at design time.

The working code I am looking to re-use is :-

 
          // Add both series to the chart.
          ChartName.Series.AddRange(new Series[] { series1, series2 });

          // Cast the chart's diagram to the XYDiagram type, to access its axes.
          XYDiagram diagram = (XYDiagram)ChartName.Diagram;



I'd like to change the ChartName to a variable that I can pass each of the names to in order to re-use the code. Something like (note this does not work):-

 
          var VChart = this.Controls.Find(ChartName, true);
          
          // Add both series to the chart.
          VChart.Series.AddRange(new Series[] { series1, series2 });

          // Cast the chart's diagram to the XYDiagram type, to access its axes.
          XYDiagram diagram = (XYDiagram)VChart.Diagram;



Any ideas, hints, tips, etc on how-to pass a variable into the ChartName would be appreciated.

Cheers

J

Is This A Good Question/Topic? 0
  • +

Replies To: Using variable in place of ChartName

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,658
  • Joined: 02-June 10

Re: Using variable in place of ChartName

Posted 12 February 2013 - 09:34 AM

Your form has a .Controls property that all your controls are part of.

Loop through the .Controls comparing the name until you find it.
Was This Post Helpful? 1
  • +
  • -

#3 JBudd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-September 12

Re: Using variable in place of ChartName

Posted 12 February 2013 - 10:14 AM

Not sure how to loop through the .Controls property within my code....

Full code function included below :-
void Generate_Chart()
    {
        // Create two stacked bar series.
        Series series1 = new Series("Data", ViewType.Bar);
        Series series2 = new Series("Ben", ViewType.Line);

        try
        {                    
            using (var cmd = new SQLiteCommand(m_dbConnection))
            for (int i = LoopMin; i < LoopMax; i++)
            {
                // Retrieve the actual calculated values from the database
                cmd.CommandText = "SELECT " + Chart_SourceActualValue + " FROM " + Chart_SourceTable + " WHERE Value = " + i + "";
                Chart_SeriesA_Value = Convert.ToInt32(cmd.ExecuteScalar());

                // Retrieve the expected values from the database
                cmd.CommandText = "SELECT " + Chart_BenExpValue + " FROM " + Chart_SourceTable + " WHERE Value = " + i + "";
                Chart_SeriesB_Value = Convert.ToInt32(cmd.ExecuteScalar());

                // Add the dynamically created values to a series point for the chart
                series1.Points.Add(new SeriesPoint(i, Chart_SeriesA_Value));
                series2.Points.Add(new SeriesPoint(i, Chart_SeriesB_Value));
            }
        }
        catch (Exception)
        {                
            throw;
        }

        // Add both series to the chart.
        //this.Controls.Find(varChart, true)
        ChartName.Series.AddRange(new Series[] { series1, series2 });

        // Remove the GridLines from the chart for better UI
        // Cast the chart's diagram to the XYDiagram type, to access its axes.
        XYDiagram diagram = (XYDiagram)ChartName.Diagram;
        // Customize the appearance of the axes' grid lines.
        diagram.AxisX.GridLines.Visible = false;
        }        
}


Was This Post Helpful? 0
  • +
  • -

#4 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1181
  • View blog
  • Posts: 1,673
  • Joined: 24-August 11

Re: Using variable in place of ChartName

Posted 12 February 2013 - 03:21 PM

Use a simple foreach loop:

foreach (Control control in myForm.Controls)
{
    // compare control's name to whatever you're looking for
}


Was This Post Helpful? 2
  • +
  • -

Page 1 of 1