Using a variable to access the chart

I have a C # WinForms application that has four chart controls used to graphically display some of the analysis results.

I have code that works for each graph, however, in an attempt to be more efficient and reuse the code, I defined a block of code:

  • create the required series,
  • retrieves data from the database and assigns the results to the appropriate series
  • add series to table
  • customize the appearance of charts.

All of the above is done dynamically because the data does not exist at design time.

The working code that I am looking for reuse is the following:

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

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

      

I would like to change the object ChartName

to a variable that I can pass to each of the diagrams in order to reuse the code. Something like (please note this doesn't work): -

var VChart = this.Controls.Find(ChartName, true);  

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

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

      

Any ideas, tips, hints, etc. on how to pass the variable to the ChartName will be appreciated.

Complete code:

    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 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;
        }        
}

      

+3


source to share


1 answer


It looks like you are asking to replace hardcoded ChartName

with a variable so that you can call your routine four times, each time with a different diagram. I took your code and replaced part of the global variable of your chart control and settings and made them the parameters that you passed to the function:

void Generate_Chart(DevExpress.XtraCharts.ChartControl chartCtrl,
                   string chart_sourceActualValue,
                    string chart_sourceTable,
                   string chart_benExpValue
                   )
    {
        // 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 " + 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.
        chartCtrl.Series.AddRange(new Series[] { series1, series2 });

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

      



Then you call this method as follows, using the original values ​​as arguments:

void Generate_Chart(ChartName, Chart_SourceActualValue, Chart_SourceTable, 
                    Chart_BenExpValue);

// call it three other times passing in the different specifics for that chart. e.g.
void Generate_Chart(SomeOtherChartName, SomeOtherChart_SourceActualValue, 
                    SomeOhterChart_SourceTable, SomeOtherChart_BenExpValue);

.....

      

+1


source







All Articles